-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathbuild.rs
More file actions
63 lines (51 loc) · 1.88 KB
/
Copy pathbuild.rs
File metadata and controls
63 lines (51 loc) · 1.88 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
use std::path::PathBuf;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=services/api/proto/scheduler.proto");
emit_git_rerun_inputs();
tonic_prost_build::configure()
.build_server(false)
.build_client(true)
.compile_protos(
&["services/api/proto/scheduler.proto"],
&["services/api/proto"],
)
.expect("failed to compile scheduler proto for Rust gRPC client");
let commit = resolve_git_commit().unwrap_or_else(|| "unknown".to_string());
println!("cargo:rustc-env=AENV_GIT_COMMIT={commit}");
}
fn emit_git_rerun_inputs() {
if let Some(path) = git_path("HEAD") {
println!("cargo:rerun-if-changed={}", path.display());
}
let Some(head_ref) = git_stdout(["symbolic-ref", "-q", "HEAD"]) else {
return;
};
let head_ref = head_ref.trim();
if head_ref.is_empty() {
return;
}
if let Some(path) = git_path(head_ref) {
println!("cargo:rerun-if-changed={}", path.display());
}
// Track packed refs as well as the loose branch ref so changes after
// repacking or worktree-local HEAD updates still refresh the embedded SHA.
if let Some(path) = git_path("packed-refs").filter(|path| path.exists()) {
println!("cargo:rerun-if-changed={}", path.display());
}
}
fn git_path(path: &str) -> Option<PathBuf> {
git_stdout(["rev-parse", "--git-path", path]).map(|path| PathBuf::from(path.trim()))
}
fn resolve_git_commit() -> Option<String> {
let commit = git_stdout(["rev-parse", "--short", "HEAD"])?;
let commit = commit.trim();
(!commit.is_empty()).then(|| commit.to_string())
}
fn git_stdout<const N: usize>(args: [&str; N]) -> Option<String> {
let output = Command::new("git").args(args).output().ok()?;
if !output.status.success() {
return None;
}
String::from_utf8(output.stdout).ok()
}