-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.rs
More file actions
69 lines (62 loc) · 2.04 KB
/
Copy pathbuild.rs
File metadata and controls
69 lines (62 loc) · 2.04 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
69
use std::path::PathBuf;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=deepmem-version.txt");
println!("cargo:rerun-if-changed=.git/HEAD");
emit_git_head_ref_rerun_hint();
let deepmem_version = std::fs::read_to_string("deepmem-version.txt")
.map(|s| s.trim().to_string())
.unwrap_or_else(|_| "unknown".into());
let git_sha = git_output(&["rev-parse", "--short", "HEAD"]).unwrap_or_else(|| "unknown".into());
let git_dirty = git_output(&["status", "--porcelain"])
.map(|s| if s.trim().is_empty() { "false" } else { "true" })
.unwrap_or("unknown");
println!(
"cargo:rustc-env=WX_CLI_DISPLAY_VERSION=deepmem-{}-{deepmem_version}",
env!("CARGO_PKG_VERSION")
);
println!("cargo:rustc-env=WX_CLI_GIT_SHA={git_sha}");
println!("cargo:rustc-env=WX_CLI_GIT_DIRTY={git_dirty}");
}
fn emit_git_head_ref_rerun_hint() {
let Some(git_dir) = git_dir() else {
return;
};
let head_path = git_dir.join("HEAD");
let Ok(head) = std::fs::read_to_string(&head_path) else {
return;
};
let Some(head_ref) = head.trim().strip_prefix("ref: ") else {
return;
};
println!(
"cargo:rerun-if-changed={}",
git_dir.join(head_ref).display()
);
}
fn git_dir() -> Option<PathBuf> {
let git_path = PathBuf::from(".git");
if git_path.is_dir() {
return Some(git_path);
}
let git_file = std::fs::read_to_string(&git_path).ok()?;
let git_dir = git_file.trim().strip_prefix("gitdir: ")?.trim();
let git_dir = PathBuf::from(git_dir);
if git_dir.is_absolute() {
Some(git_dir)
} else {
Some(
git_path
.parent()
.unwrap_or_else(|| std::path::Path::new("."))
.join(git_dir),
)
}
}
fn git_output(args: &[&str]) -> Option<String> {
let out = Command::new("git").args(args).output().ok()?;
if !out.status.success() {
return None;
}
Some(String::from_utf8_lossy(&out.stdout).trim().to_string())
}