gitpoulpi-config 1.2.0

GitPoulpi config schema, TOML load/save, defaults, and OS-correct path resolution.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! How a release archive is named, shared by `xtask dist` (which writes the
//! files) and the update check (which looks for the one matching the running
//! build). One function, so the two can never disagree about a name.

/// The binaries a release archive ships (without the platform suffix).
///
/// `gitpoulpi-launch` is the console-less program the Windows shortcuts run
/// (`gitpoulpi launch` without a console flash); it ships everywhere so an
/// install is the same set of files on every OS.
pub const BINARIES: [&str; 5] = [
    "gitpoulpi-tui",
    "gitpoulpi",
    "gitpoulpi-mcp",
    "gitpoulpi-launch",
    "gitpoulpi-updater",
];

/// Where an archive file comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuiltFrom {
    /// A binary of this cargo package, copied under its own name.
    Package(&'static str),
    /// A copy of the `gitpoulpi-shim` binary under the archive name: a
    /// stub that starts the `gitpoulpi` next to it as that persona.
    Shim,
}

/// The cargo package `xtask dist` builds the shim from.
pub const SHIM_PACKAGE: &str = "gitpoulpi-shim";
/// The shim's binary name (what cargo writes).
pub const SHIM_BINARY: &str = "gitpoulpi-shim";

/// How each name of [`BINARIES`] is produced. The archive file names stay
/// the old ones (an old updater relies on them); only the source changed.
pub const BUILT_FROM: [(&str, BuiltFrom); 5] = [
    ("gitpoulpi-tui", BuiltFrom::Shim),
    ("gitpoulpi", BuiltFrom::Package("gitpoulpi-cli")),
    ("gitpoulpi-mcp", BuiltFrom::Shim),
    ("gitpoulpi-launch", BuiltFrom::Package("gitpoulpi-cli")),
    ("gitpoulpi-updater", BuiltFrom::Package("gitpoulpi-updater")),
];

/// The package `-p` list a release build needs: every package of
/// [`BUILT_FROM`] once (the shim package for the shim copies).
pub fn build_packages() -> Vec<&'static str> {
    let mut v: Vec<&'static str> = Vec::new();
    for (_, from) in BUILT_FROM {
        let p = match from {
            BuiltFrom::Package(p) => p,
            BuiltFrom::Shim => SHIM_PACKAGE,
        };
        if !v.contains(&p) {
            v.push(p);
        }
    }
    v
}

/// The binary name cargo writes for archive file `name` (no suffix).
pub fn built_binary(name: &str) -> Option<&'static str> {
    BUILT_FROM
        .iter()
        .find(|(n, _)| *n == name)
        .map(|(n, f)| match f {
            BuiltFrom::Shim => SHIM_BINARY,
            BuiltFrom::Package(_) => *n,
        })
}

/// Program names a release no longer ships (without the platform suffix).
/// Empty for now: the archive still carries every name of [`BINARIES`].
/// When a name moves here, the updater deletes its file and its `.old` copy
/// after a successful swap, `gitpoulpi uninstall` removes it, and a restart
/// that was recorded against it (see the updater's relaunch mapping) starts
/// `gitpoulpi` instead. Never add a name here in the same release that
/// removes it from the archive: an old updater would leave a stale copy.
pub const RETIRED: [&str; 0] = [];

/// Whether `stem` (a program name without suffix, any case) is in `retired`.
pub fn is_retired_in(retired: &[&str], stem: &str) -> bool {
    retired.iter().any(|r| r.eq_ignore_ascii_case(stem))
}

/// The file names of `retired` for `target` (with the platform suffix).
pub fn retired_names_in(retired: &[&str], target: &str) -> Vec<String> {
    retired.iter().map(|b| exe_name(b, target)).collect()
}

/// The file names of [`RETIRED`] for `target`.
pub fn retired_names(target: &str) -> Vec<String> {
    retired_names_in(&RETIRED, target)
}

/// The file `xtask dist` writes next to the binaries inside a release
/// archive. A running build may replace itself (the self-updater of
/// the updates-and-install design, slice B) only when this
/// file sits next to its exe: a `cargo` build never has one.
pub const MARKER_FILE: &str = ".gitpoulpi-dist";

/// The Docker Hub repository of the container image. One constant: the update
/// panel, the docs and the release workflow all read this name, so a namespace
/// change is a one-line edit (GHCR mirrors it under `ghcr.io/poulpi-lab/`).
pub const DOCKER_IMAGE: &str = "poulpilab/gitpoulpi";

/// How an install got onto the machine: the marker's `channel=` line. It
/// decides what the app may do about an update (the install-channels design).
/// `cargo` is deliberately not a value: a cargo build has no marker, so it is
/// recognized by cargo's own install record instead.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InstallChannel {
    Archive,
    InnoUser,
    InnoMachine,
    Choco,
    ChocoInternal,
    Winget,
    Scoop,
    Homebrew,
    Apt,
    Msi,
    Aur,
    Docker,
}

impl InstallChannel {
    pub const ALL: [InstallChannel; 12] = [
        Self::Archive,
        Self::InnoUser,
        Self::InnoMachine,
        Self::Choco,
        Self::ChocoInternal,
        Self::Winget,
        Self::Scoop,
        Self::Homebrew,
        Self::Apt,
        Self::Msi,
        Self::Aur,
        Self::Docker,
    ];

    /// The value written after `channel=`.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Archive => "archive",
            Self::InnoUser => "inno-user",
            Self::InnoMachine => "inno-machine",
            Self::Choco => "choco",
            Self::ChocoInternal => "choco-internal",
            Self::Winget => "winget",
            Self::Scoop => "scoop",
            Self::Homebrew => "homebrew",
            Self::Apt => "apt",
            Self::Msi => "msi",
            Self::Aur => "aur",
            Self::Docker => "docker",
        }
    }

    /// Exact spelling only; anything else is `None`.
    pub fn parse(text: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|c| c.as_str() == text)
    }
}

/// What the marker file says: the release the files belong to, and how they
/// were installed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Marker {
    pub version: String,
    pub target: String,
    /// `None` for a marker written before channels existed, and for a
    /// `channel=` value this build does not know (a newer marker read by an
    /// older build): both are "Legacy", never a guess.
    pub channel: Option<InstallChannel>,
    /// An internal Chocolatey feed URL written by an internalised package.
    pub feed: Option<String>,
}

impl Marker {
    /// A marker with neither a channel nor a feed (what releases before the
    /// install-channels design wrote).
    pub fn new(version: impl Into<String>, target: impl Into<String>) -> Self {
        Self {
            version: version.into(),
            target: target.into(),
            channel: None,
            feed: None,
        }
    }

    /// `version=...` and `target=...`, then `channel=` / `feed=` only when
    /// set, one per line. Old builds ignore the extra lines.
    pub fn render(&self) -> String {
        let mut out = format!(
            "version={}
target={}
",
            self.version, self.target
        );
        if let Some(c) = self.channel {
            out.push_str(&format!(
                "channel={}
",
                c.as_str()
            ));
        }
        if let Some(f) = &self.feed {
            out.push_str(&format!(
                "feed={f}
"
            ));
        }
        out
    }

    /// Reads [`Marker::render`]'s text; `None` when `version` or `target` is
    /// missing.
    pub fn parse(text: &str) -> Option<Self> {
        let field = |key: &str| {
            text.lines()
                .find_map(|l| l.trim().strip_prefix(key)?.strip_prefix('='))
                .map(|v| v.trim().to_string())
                .filter(|v| !v.is_empty())
        };
        Some(Self {
            version: field("version")?,
            target: field("target")?,
            channel: field("channel").and_then(|c| InstallChannel::parse(&c)),
            feed: field("feed"),
        })
    }
}

/// Whether `target` (a Rust target triple) is a Windows one: it ships a
/// `.zip` of `.exe` files, everything else a `.tar.gz`.
pub fn is_windows(target: &str) -> bool {
    target.contains("windows")
}

/// `binary` with the platform's executable suffix.
pub fn exe_name(binary: &str, target: &str) -> String {
    if is_windows(target) {
        format!("{binary}.exe")
    } else {
        binary.to_string()
    }
}

/// `gitpoulpi-<version>-<target>`: the archive's stem and its top-level
/// directory, so extracting never spills files into the current directory.
pub fn package_stem(version: &str, target: &str) -> String {
    format!("gitpoulpi-{version}-{target}")
}

/// `gitpoulpi-<version>-<target>.zip` on Windows targets, `.tar.gz` elsewhere.
pub fn archive_name(version: &str, target: &str) -> String {
    let ext = if is_windows(target) { "zip" } else { "tar.gz" };
    format!("{}.{ext}", package_stem(version, target))
}

/// `gitpoulpi-<version>-<target>-setup.exe`: the Windows installer
/// (`packaging/windows/gitpoulpi.iss`) the release workflow builds from the
/// zip, uploaded next to it. Kept here so the script, the workflow and the
/// tests that tie them together agree on one name.
pub fn installer_name(version: &str, target: &str) -> String {
    format!("{}-setup.exe", package_stem(version, target))
}

/// The target triple of the running build, as far as the release workflow
/// names them (`x86_64-unknown-linux-gnu`, `x86_64-pc-windows-msvc`,
/// `aarch64-apple-darwin`, ...). Other combinations still get a plausible
/// triple: no release asset will match it, and the update notice then points
/// at the release page instead of an archive.
pub fn host_target() -> String {
    let arch = std::env::consts::ARCH;
    match std::env::consts::OS {
        "windows" => format!("{arch}-pc-windows-msvc"),
        "macos" => format!("{arch}-apple-darwin"),
        "linux" => format!("{arch}-unknown-linux-gnu"),
        other => format!("{arch}-unknown-{other}"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn built_from_covers_exactly_the_archive_names() {
        let names: Vec<&str> = BUILT_FROM.iter().map(|(n, _)| *n).collect();
        assert_eq!(names, BINARIES);
    }

    #[test]
    fn tui_and_mcp_are_shim_copies_and_the_rest_are_real_binaries() {
        for (name, from) in BUILT_FROM {
            let shim = name == "gitpoulpi-tui" || name == "gitpoulpi-mcp";
            assert_eq!(from == BuiltFrom::Shim, shim, "{name}");
            let expect = if shim { SHIM_BINARY } else { name };
            assert_eq!(built_binary(name), Some(expect));
        }
        assert_eq!(built_binary("nope"), None);
    }

    #[test]
    fn a_release_builds_only_cli_updater_and_shim() {
        assert_eq!(
            build_packages(),
            ["gitpoulpi-shim", "gitpoulpi-cli", "gitpoulpi-updater"]
        );
    }

    #[test]
    fn archive_is_zip_on_windows_and_tar_gz_elsewhere() {
        assert_eq!(
            archive_name("0.1.0", "x86_64-pc-windows-msvc"),
            "gitpoulpi-0.1.0-x86_64-pc-windows-msvc.zip"
        );
        assert_eq!(
            archive_name("0.1.0", "x86_64-unknown-linux-gnu"),
            "gitpoulpi-0.1.0-x86_64-unknown-linux-gnu.tar.gz"
        );
        assert_eq!(
            archive_name("0.2.0", "aarch64-apple-darwin"),
            "gitpoulpi-0.2.0-aarch64-apple-darwin.tar.gz"
        );
    }

    #[test]
    fn the_marker_round_trips_and_needs_both_fields() {
        let m = Marker::new("0.2.0", "x86_64-unknown-linux-gnu");
        assert_eq!(Marker::parse(&m.render()), Some(m));
        assert_eq!(Marker::parse("version=0.2.0\n"), None);
        assert_eq!(Marker::parse(""), None);
    }

    #[test]
    fn windows_binaries_get_exe() {
        assert_eq!(
            exe_name("gitpoulpi", "x86_64-pc-windows-msvc"),
            "gitpoulpi.exe"
        );
        assert_eq!(
            exe_name("gitpoulpi", "x86_64-unknown-linux-gnu"),
            "gitpoulpi"
        );
    }

    #[test]
    fn the_host_target_names_this_build() {
        let t = host_target();
        assert!(t.starts_with(std::env::consts::ARCH), "{t}");
        assert_eq!(is_windows(&t), cfg!(windows));
    }

    #[test]
    fn the_marker_round_trips_with_channel_and_feed() {
        let m = Marker {
            channel: Some(InstallChannel::ChocoInternal),
            feed: Some("https://nexus.example/repository/choco/".into()),
            ..Marker::new("0.2.0", "x86_64-pc-windows-msvc")
        };
        let text = m.render();
        assert!(text.contains("channel=choco-internal\n"), "{text}");
        assert_eq!(Marker::parse(&text), Some(m));
    }

    #[test]
    fn channel_and_feed_lines_are_written_only_when_set() {
        let text = Marker::new("0.2.0", "t").render();
        assert_eq!(text, "version=0.2.0\ntarget=t\n");
    }

    #[test]
    fn an_old_marker_parses_as_legacy_and_old_builds_read_a_new_one() {
        let old = Marker::parse("version=0.1.0\ntarget=t\n").unwrap();
        assert_eq!((old.channel, old.feed), (None, None));
        // The extra lines of a new marker must not break the old parse rule.
        let new = "version=0.3.0\ntarget=t\nchannel=apt\nfuture=1\n";
        assert_eq!(
            Marker::parse(new).unwrap().channel,
            Some(InstallChannel::Apt)
        );
    }

    #[test]
    fn every_channel_spelling_round_trips_and_is_unique() {
        let mut seen = std::collections::HashSet::new();
        for c in InstallChannel::ALL {
            assert_eq!(InstallChannel::parse(c.as_str()), Some(c));
            assert!(seen.insert(c.as_str()), "{c:?} repeats a spelling");
            let m = Marker {
                channel: Some(c),
                ..Marker::new("1.0.0", "t")
            };
            assert_eq!(Marker::parse(&m.render()).unwrap().channel, Some(c));
        }
    }

    #[test]
    fn an_unknown_channel_is_legacy_not_a_parse_failure() {
        assert_eq!(InstallChannel::parse("snap"), None);
        assert_eq!(InstallChannel::parse("Archive"), None);
        assert_eq!(InstallChannel::parse("cargo"), None, "never a marker value");
        let m = Marker::parse("version=1.0.0\ntarget=t\nchannel=snap\n").unwrap();
        assert_eq!(m.channel, None);
    }

    #[test]
    fn the_docker_image_is_the_hub_name() {
        assert_eq!(DOCKER_IMAGE, "poulpilab/gitpoulpi");
    }
}