Skip to main content

anodizer_core/config/
snapcraft.rs

1use std::collections::BTreeMap;
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6use super::archives::TemplatedExtraFile;
7use super::{StringOrBool, deserialize_string_or_bool_opt};
8
9// ---------------------------------------------------------------------------
10// SnapcraftConfig
11// ---------------------------------------------------------------------------
12
13#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
14#[serde(default, deny_unknown_fields)]
15pub struct SnapcraftConfig {
16    /// Unique identifier for this snapcraft config.
17    pub id: Option<String>,
18    /// Build IDs to include. Empty means all builds.
19    pub ids: Option<Vec<String>>,
20    /// Snap package name in the store.
21    pub name: Option<String>,
22    /// Canonical application title (user-facing in store).
23    pub title: Option<String>,
24    /// Single-line elevator pitch (max 79 characters).
25    pub summary: Option<String>,
26    /// Extended description (user-facing in store).
27    pub description: Option<String>,
28    /// Path to the snap icon image (`.png` or `.svg`).
29    ///
30    /// When set, anodizer copies the file to `meta/gui/<name>.<ext>` inside
31    /// the staged prime directory before `snapcraft pack` runs. The icon is
32    /// delivered to the Snap Store via snapcraft's GUI metadata channel and
33    /// never appears in `snap.json`, keeping uploads schema-clean. (The Snap
34    /// Store rejects `snap.json` that contains an `icon:` key with
35    /// "Additional properties are not allowed ('icon' was unexpected)".)
36    ///
37    /// The source path may be absolute or relative to the project root.
38    /// Anodizer errors before staging if the file does not exist.
39    pub icon: Option<String>,
40    /// Runtime base snap: core, core18, core20, core22, core24, bare.
41    pub base: Option<String>,
42    /// Release stability level: stable, devel.
43    pub grade: Option<String>,
44    /// License identifier (SPDX format).
45    pub license: Option<String>,
46    /// Whether to publish to the snapcraft store.
47    pub publish: Option<bool>,
48    /// Distribution channels: edge, beta, candidate, stable.
49    pub channel_templates: Option<Vec<String>>,
50    /// Security confinement level: strict, devmode, classic.
51    pub confinement: Option<String>,
52    /// Top-level snap plug definitions (structured map).
53    /// Keys are plug names, values are either `null` (simple plug) or an object
54    /// with `interface` and optional attributes (e.g. `{ interface: "content", target: "$SNAP/shared" }`).
55    /// GoReleaser uses `map[string]any` for this field.
56    pub plugs: Option<BTreeMap<String, serde_json::Value>>,
57    // No top-level `slots:` — Snapcraft itself has no top-level slots
58    // concept; use `apps.<name>.slots` for per-app slots.
59    /// Required snapd features/versions.
60    pub assumes: Option<Vec<String>>,
61    /// Application configurations defining daemons, commands, env vars.
62    pub apps: Option<BTreeMap<String, SnapcraftApp>>,
63    /// Directory mappings for sandbox accessibility.
64    pub layouts: Option<BTreeMap<String, SnapcraftLayout>>,
65    /// Additional static files to bundle (string shorthand or structured form).
66    pub extra_files: Option<Vec<SnapcraftExtraFileSpec>>,
67    /// Extra files whose contents are rendered through the template engine before bundling.
68    /// Unlike `extra_files` which copy as-is, template variables like `{{ .Tag }}` are expanded.
69    /// GoReleaser Pro feature.
70    pub templated_extra_files: Option<Vec<TemplatedExtraFile>>,
71    /// Template for the output snap filename.
72    pub name_template: Option<String>,
73    /// Skip this snapcraft config. Accepts bool or template string
74    /// (e.g. `"{{ if .IsSnapshot }}true{{ endif }}"` for conditional skip).
75    /// Accepts the legacy `disable:` spelling via serde alias for back-compat
76    /// with imported GoReleaser configs (GR's snapcraft config field is
77    /// `pkg/config/config.go:1033` `Disable string`).
78    #[serde(
79        default,
80        alias = "disable",
81        deserialize_with = "deserialize_string_or_bool_opt"
82    )]
83    pub skip: Option<StringOrBool>,
84    /// Remove source archives from artifacts, keeping only snap.
85    pub replace: Option<bool>,
86    /// Output timestamp for reproducible builds.
87    pub mod_timestamp: Option<String>,
88    /// Snap hooks — maps hook name to arbitrary hook config.
89    pub hooks: Option<BTreeMap<String, serde_json::Value>>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
93#[serde(default)]
94pub struct SnapcraftApp {
95    /// Command to run (relative to snap root).
96    pub command: Option<String>,
97    /// Daemon type: simple, forking, oneshot, notify, dbus.
98    pub daemon: Option<String>,
99    /// How to stop the daemon: sigterm, sigkill, etc.
100    #[serde(alias = "stop-mode")]
101    pub stop_mode: Option<String>,
102    /// Interface plugs the app needs.
103    pub plugs: Option<Vec<String>>,
104    /// Environment variables for the app (supports string, integer, and boolean values).
105    pub environment: Option<BTreeMap<String, serde_json::Value>>,
106    /// Additional arguments passed to the command.
107    pub args: Option<String>,
108    /// Restart condition: on-failure, always, on-success, on-abnormal, on-abort, on-watchdog, never.
109    #[serde(alias = "restart-condition")]
110    pub restart_condition: Option<String>,
111    /// Snap adapter type: "none" or "full" (default: "full").
112    pub adapter: Option<String>,
113    /// Services that must start before this app.
114    pub after: Option<Vec<String>>,
115    /// Alternative names for the command.
116    pub aliases: Option<Vec<String>>,
117    /// Desktop file for autostart.
118    pub autostart: Option<String>,
119    /// Services that must start after this app.
120    pub before: Option<Vec<String>>,
121    /// D-Bus well-known bus name.
122    #[serde(alias = "bus-name")]
123    pub bus_name: Option<String>,
124    /// Wrapper commands run before the main command.
125    #[serde(alias = "command-chain")]
126    pub command_chain: Option<Vec<String>>,
127    /// AppStream metadata common ID.
128    #[serde(alias = "common-id")]
129    pub common_id: Option<String>,
130    /// Path to bash completion script relative to snap.
131    pub completer: Option<String>,
132    /// Path to .desktop file relative to snap.
133    pub desktop: Option<String>,
134    /// Snap extensions to apply.
135    pub extensions: Option<Vec<String>>,
136    /// Installation mode: "enable" or "disable".
137    #[serde(alias = "install-mode")]
138    pub install_mode: Option<String>,
139    /// Arbitrary YAML passed through to snap.yaml.
140    pub passthrough: Option<BTreeMap<String, serde_json::Value>>,
141    /// Command to run after daemon stops.
142    #[serde(alias = "post-stop-command")]
143    pub post_stop_command: Option<String>,
144    /// Refresh behavior: "endure" or "restart".
145    #[serde(alias = "refresh-mode")]
146    pub refresh_mode: Option<String>,
147    /// Command to reload daemon config.
148    #[serde(alias = "reload-command")]
149    pub reload_command: Option<String>,
150    /// Delay between restarts (duration string).
151    #[serde(alias = "restart-delay")]
152    pub restart_delay: Option<String>,
153    /// Interface slots this app provides.
154    pub slots: Option<Vec<String>>,
155    /// Socket definitions map.
156    pub sockets: Option<BTreeMap<String, serde_json::Value>>,
157    /// Start timeout duration string.
158    #[serde(alias = "start-timeout")]
159    pub start_timeout: Option<String>,
160    /// Command to gracefully stop the daemon.
161    #[serde(alias = "stop-command")]
162    pub stop_command: Option<String>,
163    /// Stop timeout duration string.
164    #[serde(alias = "stop-timeout")]
165    pub stop_timeout: Option<String>,
166    /// Timer definition (systemd timer syntax).
167    pub timer: Option<String>,
168    /// Watchdog timeout duration string.
169    #[serde(alias = "watchdog-timeout")]
170    pub watchdog_timeout: Option<String>,
171}
172
173#[derive(Debug, Clone, Serialize, Deserialize, Default, JsonSchema)]
174#[serde(default)]
175pub struct SnapcraftLayout {
176    /// Bind-mount a directory to the snap's layout.
177    pub bind: Option<String>,
178    /// Bind-mount a single file to the snap's layout.
179    pub bind_file: Option<String>,
180    /// Symlink a path to a location in the snap.
181    pub symlink: Option<String>,
182    /// Layout entry type.
183    #[serde(rename = "type")]
184    pub type_: Option<String>,
185}
186
187/// Specifies an extra file for snapcraft. Can be a simple source path string or
188/// a structured object with source, destination, and mode fields (matching
189/// GoReleaser's SnapcraftExtraFiles).
190#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
191#[serde(untagged)]
192pub enum SnapcraftExtraFileSpec {
193    /// Simple source path string.
194    Source(String),
195    /// Structured form with source, destination, and mode.
196    Detailed {
197        source: String,
198        #[serde(skip_serializing_if = "Option::is_none")]
199        destination: Option<String>,
200        #[serde(skip_serializing_if = "Option::is_none")]
201        mode: Option<u32>,
202    },
203}
204
205impl SnapcraftExtraFileSpec {
206    /// Return the source path for this spec.
207    pub fn source(&self) -> &str {
208        match self {
209            SnapcraftExtraFileSpec::Source(s) => s,
210            SnapcraftExtraFileSpec::Detailed { source, .. } => source,
211        }
212    }
213
214    /// Return the optional destination path.
215    pub fn destination(&self) -> Option<&str> {
216        match self {
217            SnapcraftExtraFileSpec::Source(_) => None,
218            SnapcraftExtraFileSpec::Detailed { destination, .. } => destination.as_deref(),
219        }
220    }
221
222    /// Return the optional file mode.
223    pub fn mode(&self) -> Option<u32> {
224        match self {
225            SnapcraftExtraFileSpec::Source(_) => None,
226            SnapcraftExtraFileSpec::Detailed { mode, .. } => *mode,
227        }
228    }
229}