Files
rustspace/alterego/build.rs
2023-02-26 17:59:35 +03:00

42 lines
1.2 KiB
Rust

use std::env;
fn main() {
let rev = get_value_from_env("GIT_VERSION")
.or_else(|| get_value_from_command("git", &["rev-parse", "--short", "HEAD"]))
.unwrap_or_else(|| "unknown".to_owned());
let branch = get_value_from_env("GIT_BRANCH")
.or_else(|| get_value_from_command("git", &["rev-parse", "--abbrev-ref", "HEAD"]))
.unwrap_or_else(|| "unknown".to_owned());
println!("cargo:rustc-env=GIT_REVISION={}", rev);
println!("cargo:rustc-env=GIT_BRANCH={}", branch);
println!("cargo:rerun-if-env-changed=GIT_REVISION");
}
fn get_value_from_env(key: &str) -> Option<String> {
env::var(key).map_or_else(|_| None, Some)
}
fn get_value_from_command<I: IntoIterator<Item = S>, S: AsRef<std::ffi::OsStr>>(
cmd: &str,
args: I,
) -> Option<String> {
std::process::Command::new(cmd)
.args(args)
.output()
.map_or_else(
|_| None,
|out| {
if !out.status.success() {
return None;
}
match std::str::from_utf8(&out.stdout) {
Ok(value) => Some(value.to_owned()),
Err(_) => None,
}
},
)
}