pub const BINARIES: [&str; 5] = [
"gitpoulpi-tui",
"gitpoulpi",
"gitpoulpi-mcp",
"gitpoulpi-launch",
"gitpoulpi-updater",
];
pub const MARKER_FILE: &str = ".gitpoulpi-dist";
pub const DOCKER_IMAGE: &str = "poulpilab/gitpoulpi";
#[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,
];
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",
}
}
pub fn parse(text: &str) -> Option<Self> {
Self::ALL.into_iter().find(|c| c.as_str() == text)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Marker {
pub version: String,
pub target: String,
pub channel: Option<InstallChannel>,
pub feed: Option<String>,
}
impl Marker {
pub fn new(version: impl Into<String>, target: impl Into<String>) -> Self {
Self {
version: version.into(),
target: target.into(),
channel: None,
feed: None,
}
}
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
}
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"),
})
}
}
pub fn is_windows(target: &str) -> bool {
target.contains("windows")
}
pub fn exe_name(binary: &str, target: &str) -> String {
if is_windows(target) {
format!("{binary}.exe")
} else {
binary.to_string()
}
}
pub fn package_stem(version: &str, target: &str) -> String {
format!("gitpoulpi-{version}-{target}")
}
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))
}
pub fn installer_name(version: &str, target: &str) -> String {
format!("{}-setup.exe", package_stem(version, target))
}
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 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));
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");
}
}