-
Notifications
You must be signed in to change notification settings - Fork 36
/
build.rs
40 lines (35 loc) · 1.12 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use std::{env, process::Command};
fn main() {
let commit = get_commit();
println!(
"cargo:rustc-env=AUDIOSERVE_LONG_VERSION={}",
get_long_version(&commit)
);
println!("cargo:rustc-env=AUDIOSERVE_COMMIT={}", commit);
println!("cargo:rustc-env=AUDIOSERVE_FEATURES={}", get_features());
}
fn get_long_version(commit: &str) -> String {
let ver = env::var("CARGO_PKG_VERSION").expect("cargo version is missing");
format!("{} #{}", ver, commit)
}
const FEATURE_PREFIX: &str = "CARGO_FEATURE_";
fn get_features() -> String {
env::vars()
.filter_map(|(v, _)| v.strip_prefix(FEATURE_PREFIX).map(|s| s.to_string()))
.map(|f| f.to_ascii_lowercase().replace('_', "-"))
.collect::<Vec<_>>()
.join(", ")
}
fn get_commit() -> String {
Command::new("git")
.args(["rev-parse", "HEAD"])
.output()
.map(|mut o| {
o.stdout.truncate(7);
String::from_utf8(o.stdout).expect("git output must be utf-8")
})
.unwrap_or_else(|e| {
eprintln!("Error running git: {}", e);
"?".to_string()
})
}