Skip to main content

agentmux_common/
srv_binary.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where the srv binary may be, for the launcher and the CEF host. Each anchors its own folders; the file names and
5//! the order within a folder are the same.
6
7use std::path::{Path, PathBuf};
8
9/// The release these binaries belong to (the workspace version).
10pub const VERSION: &str = env!("CARGO_PKG_VERSION");
11
12/// `.exe` on Windows, nothing elsewhere.
13pub const EXE_SUFFIX: &str = if cfg!(target_os = "windows") { ".exe" } else { "" };
14
15/// The platform and CPU in a portable build's file names.
16fn os_arch() -> (&'static str, &'static str) {
17    let arch = if cfg!(target_arch = "aarch64") { "arm64" } else { "x64" };
18    if cfg!(target_os = "macos") {
19        ("darwin", arch)
20    } else if cfg!(target_os = "linux") {
21        ("linux", arch)
22    } else {
23        ("windows", arch)
24    }
25}
26
27/// `<name>-<version>-<os>.<arch><suffix>`, the name a portable build gives the binary.
28pub fn versioned_name(name: &str, exe_suffix: &str) -> String {
29    let (os, arch) = os_arch();
30    format!("{name}-{VERSION}-{os}.{arch}{exe_suffix}")
31}
32
33/// Every place the binary may be, in search order: in each of `dirs` the versioned name, then the plain one
34/// (a cargo build's output).
35pub fn candidates(dirs: &[&Path], name: &str, exe_suffix: &str) -> Vec<PathBuf> {
36    dirs.iter()
37        .flat_map(|dir| [dir.join(versioned_name(name, exe_suffix)), dir.join(format!("{name}{exe_suffix}"))])
38        .collect()
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn each_dir_gives_the_versioned_name_then_the_plain_one() {
47        let found = candidates(&[Path::new("a"), Path::new("b")], "agentmux-srv", ".exe");
48        assert_eq!(found.len(), 4);
49        assert_eq!(found[0], Path::new("a").join(versioned_name("agentmux-srv", ".exe")));
50        assert_eq!(found[1], Path::new("a").join("agentmux-srv.exe"));
51        assert_eq!(found[2], Path::new("b").join(versioned_name("agentmux-srv", ".exe")));
52        assert_eq!(found[3], Path::new("b").join("agentmux-srv.exe"));
53    }
54
55    #[test]
56    fn the_versioned_name_carries_version_os_and_arch() {
57        let n = versioned_name("agentmux-srv", "");
58        assert!(n.starts_with(&format!("agentmux-srv-{VERSION}-")), "{n}");
59        assert!(n.contains('.'), "{n}");
60    }
61}