-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
50 lines (44 loc) · 1.79 KB
/
Copy pathbuild.rs
File metadata and controls
50 lines (44 loc) · 1.79 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
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
if let Ok(head) = std::fs::read_to_string(".git/HEAD")
&& let Some(ref_path) = head.strip_prefix("ref: ")
{
println!("cargo:rerun-if-changed=.git/{}", ref_path.trim());
}
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-env-changed=GITHUB_ACTIONS");
println!("cargo:rerun-if-env-changed=CI");
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
let commit_hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".to_string());
let build_time = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
let is_ci = std::env::var("CI").is_ok() || std::env::var("GITHUB_ACTIONS").is_ok();
let version = if is_ci {
let tag = Command::new("git")
.args(["describe", "--tags", "--always"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".to_string());
format!("{} (built at: {})", tag, build_time)
} else {
let pkg_version =
std::env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "0.0.0".to_string());
format!(
"{} (commit: {}, built at: {})",
pkg_version, commit_hash, build_time
)
};
println!("cargo:rustc-env=PVM_VERSION={}", version);
}