47 lines
1.1 KiB
Rust
47 lines
1.1 KiB
Rust
use std::env;
|
|
use std::process::Command;
|
|
|
|
fn main() {
|
|
let is_production = env::var("APP_ENV")
|
|
.map(|e| e == "production")
|
|
.unwrap_or(false);
|
|
|
|
let version = if is_production {
|
|
get_latest_tag().unwrap_or_else(|| panic!("Production builds must have a GIT tag."))
|
|
} else {
|
|
get_commit_hash().unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string())
|
|
};
|
|
|
|
println!("cargo:rustc-env=GIT_VERSION={}", version)
|
|
}
|
|
|
|
fn get_commit_hash() -> Option<String> {
|
|
let output = Command::new("git")
|
|
.args(&["rev-parse", "--short=8", "HEAD"])
|
|
.output()
|
|
.ok()?;
|
|
|
|
if !output.status.success() {
|
|
return None;
|
|
}
|
|
|
|
String::from_utf8(output.stdout)
|
|
.ok()
|
|
.map(|s| s.trim().to_string())
|
|
}
|
|
|
|
fn get_latest_tag() -> Option<String> {
|
|
let output = Command::new("git")
|
|
.args(&["describe", "--tags", "--match", "v*"])
|
|
.output()
|
|
.ok()?;
|
|
|
|
if !output.status.success() {
|
|
return None;
|
|
}
|
|
|
|
String::from_utf8(output.stdout)
|
|
.ok()
|
|
.map(|s| s.trim().to_string())
|
|
}
|