agentmux_common/
build_kind.rs1use std::path::Path;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum BuildKind {
19 Release,
22 Local,
24 Dev,
26}
27
28impl BuildKind {
29 pub fn label(self) -> &'static str {
31 match self {
32 BuildKind::Release => "release",
33 BuildKind::Local => "local",
34 BuildKind::Dev => "dev",
35 }
36 }
37
38 pub fn from_label(label: &str) -> Option<Self> {
41 match label {
42 "release" => Some(BuildKind::Release),
43 "local" => Some(BuildKind::Local),
44 "dev" => Some(BuildKind::Dev),
45 _ => None,
46 }
47 }
48}
49
50pub fn build_kind_of(dev: bool, portable_label: Option<&str>, made_for_channel: &str) -> BuildKind {
61 if dev {
62 BuildKind::Dev
63 } else if portable_label.is_some_and(|l| !l.trim().is_empty() && !is_plain_version(l.trim())) || made_for_channel != "stable" {
64 BuildKind::Local
65 } else {
66 BuildKind::Release
67 }
68}
69
70fn is_plain_version(label: &str) -> bool {
72 let mut parts = label.split('.');
73 let three = (0..3).all(|_| parts.next().is_some_and(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())));
74 three && parts.next().is_none()
75}
76
77pub fn build_kind_in(exe_dir: &Path) -> BuildKind {
79 let dev = crate::runtime_mode::exe_dir_is_dev(exe_dir);
80 let label = crate::runtime_mode::portable_build_label(exe_dir);
81 build_kind_of(dev, label.as_deref(), crate::data_paths::build_channel_default())
82}
83
84pub fn build_kind() -> BuildKind {
87 static KIND: std::sync::OnceLock<BuildKind> = std::sync::OnceLock::new();
88 *KIND.get_or_init(|| {
89 let dir = std::env::current_exe().ok().and_then(|p| p.parent().map(Path::to_path_buf));
90 match dir {
91 Some(dir) => build_kind_in(&dir),
92 None => build_kind_of(false, None, crate::data_paths::build_channel_default()),
94 }
95 })
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn a_release_is_a_release_whatever_channel_it_runs_on() {
104 assert_eq!(build_kind_of(false, None, "stable"), BuildKind::Release);
107 }
108
109 #[test]
110 fn a_local_package_build_is_local_by_its_marker_or_its_compiled_channel() {
111 assert_eq!(build_kind_of(false, Some("0.60.22+g2a5424fe2.20261110T070000.1"), "local-main-b28b7a-0000000a"), BuildKind::Local);
112 assert_eq!(build_kind_of(false, None, "local-main-b28b7a-0000000a"), BuildKind::Local);
114 assert_eq!(build_kind_of(false, Some("0.60.22+gabc.1"), "stable"), BuildKind::Local);
116 assert_eq!(build_kind_of(false, Some(" "), "stable"), BuildKind::Release);
117 }
118
119 #[test]
120 fn a_release_portable_carries_a_marker_with_its_plain_version() {
121 assert_eq!(build_kind_of(false, Some("0.60.22"), "stable"), BuildKind::Release);
122 for not_plain in ["0.60", "0.60.22.1", "0.60.x", "v0.60.22", "0.60.22-rc1"] {
123 assert!(!is_plain_version(not_plain), "{not_plain}");
124 }
125 }
126
127 #[test]
128 fn a_source_tree_build_is_dev_first() {
129 assert_eq!(build_kind_of(true, None, "stable"), BuildKind::Dev);
130 assert_eq!(build_kind_of(true, Some("x"), "local-x-000000-00000000"), BuildKind::Dev);
131 }
132
133 #[test]
134 fn labels_round_trip_and_unknown_tokens_are_refused() {
135 for kind in [BuildKind::Release, BuildKind::Local, BuildKind::Dev] {
136 assert_eq!(BuildKind::from_label(kind.label()), Some(kind));
137 }
138 for other in ["", "stable", "Release", "local-main-b28b7a-0000000a"] {
139 assert_eq!(BuildKind::from_label(other), None, "{other:?}");
140 }
141 }
142}