use std::{path::Path, sync::LazyLock}; fn build_from_executable(executable: &Path) -> Option { let macos = executable.parent()?; let contents = macos.parent()?; if macos.file_name()? != "MacOS" || contents.file_name()? != "Contents" { return None; } let output = std::process::Command::new("/usr/bin/plutil") .args(["-extract", "CFBundleVersion", "raw", "-o", "-"]) .arg(contents.join("Info.plist")) .output() .ok()?; if !output.status.success() { return Some("unknown".into()); } let build = String::from_utf8(output.stdout).ok()?; Some(build.trim().to_owned()) } pub fn build_number() -> &'static str { static BUILD: LazyLock = LazyLock::new(|| { std::env::current_exe() .ok() .and_then(|executable| build_from_executable(&executable)) .unwrap_or_else(|| { // Tauri omits macOS bundle settings from its generated runtime config. let config: serde_json::Value = serde_json::from_str(include_str!("../tauri.conf.json")).unwrap(); config["bundle"]["macOS"]["bundleVersion"] .as_str() .unwrap_or("unknown") .to_owned() }) }); &BUILD } #[cfg(test)] mod tests { use super::*; #[test] fn installed_build_comes_from_the_app_plist_instead_of_the_app_version() { let root = std::env::temp_dir().join(format!("kanbangit-build-{}", std::process::id())); let contents = root.join("KanbanGit.app/Contents"); std::fs::create_dir_all(contents.join("MacOS")).unwrap(); let executable = contents.join("MacOS/kanbangit-native"); std::fs::write(contents.join("Info.plist"), r#"CFBundleShortVersionString0.1.3CFBundleVersion42"#).unwrap(); assert_eq!(build_from_executable(&executable).as_deref(), Some("42")); std::fs::remove_file(contents.join("Info.plist")).unwrap(); assert_eq!( build_from_executable(&executable).as_deref(), Some("unknown") ); std::fs::remove_dir_all(root).unwrap(); } #[test] fn unbundled_execution_uses_the_configured_build() { assert!(build_from_executable(Path::new("/tmp/target/debug/kanbangit-native")).is_none()); let config: serde_json::Value = serde_json::from_str(include_str!("../tauri.conf.json")).unwrap(); assert_eq!( build_number(), config["bundle"]["macOS"]["bundleVersion"].as_str().unwrap() ); } }