-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.rs
52 lines (48 loc) · 1.84 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
41
42
43
44
45
46
47
48
49
50
51
52
use std::process::Command;
fn main() {
let command = match Command::new("git")
.args(["log", "-1"])
.output() {
Ok(r) => r,
Err(e) => {
println!("Warning: Unable to find git information, version commmand may be mangled: {e}");
println!("cargo:rustc-env=GIT_HASH=Unknown");
println!("cargo:rustc-env=GIT_DATE=Unknown");
println!("cargo:rustc-env=GIT_MESSAGE=Unknown");
return
},
};
if !command.status.success() {
println!("Warning: Unable to find git information, version commmand may be mangled.");
println!("cargo:rustc-env=GIT_HASH=Unknown");
println!("cargo:rustc-env=GIT_DATE=Unknown");
println!("cargo:rustc-env=GIT_MESSAGE=Unknown");
return
}
let output: String = String::from_utf8(command.stdout).expect("Unable to parse git output to a string.");
let output_lines: Vec<&str> = output.split('\n').collect();
let mut message: String = String::new();
let mut in_message: bool = false;
let mut first_line: bool = true;
for line in &output_lines {
if let Some(commit_hash) = line.trim().strip_prefix("commit ") {
println!("cargo:rustc-env=GIT_HASH={commit_hash}");
}
if let Some(commit_date) = line.trim().strip_prefix("Date: ") {
println!("cargo:rustc-env=GIT_DATE={}", &commit_date.trim());
}
if line.is_empty() {
// Next lines are all part of the message
in_message = true;
continue;
}
if in_message {
if !first_line {
message.push_str("<br>");
}
message.push_str(line.trim());
first_line = false;
}
}
println!("cargo:rustc-env=GIT_MESSAGE={message}");
}