initial commit
This commit is contained in:
41
alterego/build.rs
Normal file
41
alterego/build.rs
Normal file
@ -0,0 +1,41 @@
|
||||
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,
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user