-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathbuild.rs
More file actions
68 lines (62 loc) · 1.94 KB
/
Copy pathbuild.rs
File metadata and controls
68 lines (62 loc) · 1.94 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{env, process::Command};
fn main() {
let commit = get_commit();
let commit_date = get_commit_date();
println!(
"cargo:rustc-env=AUDIOSERVE_LONG_VERSION={}",
get_long_version(&commit, &commit_date)
);
println!("cargo:rustc-env=AUDIOSERVE_COMMIT={}", commit);
println!(
"cargo:rustc-env=AUDIOSERVE_COMMIT_WITH_DATE={} ({})",
commit, commit_date
);
println!("cargo:rustc-env=AUDIOSERVE_FEATURES={}", get_features());
}
fn get_long_version(commit: &str, commit_date: &str) -> String {
let ver = env::var("CARGO_PKG_VERSION").expect("cargo version is missing");
format!("{} #{} ({})", ver, commit, commit_date)
}
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()
})
}
fn get_commit_date() -> String {
Command::new("git")
.args([
"log",
"-1",
"--format=%cd",
"--date=format:%d.%m.%Y %H:%M:%S",
"HEAD",
])
.output()
.map(|mut o| {
// trim trailing newline
while o.stdout.last() == Some(&b'\n') || o.stdout.last() == Some(&b'\r') {
o.stdout.pop();
}
String::from_utf8(o.stdout).expect("git output must be utf-8")
})
.unwrap_or_else(|e| {
eprintln!("Error running git: {}", e);
"?".to_string()
})
}