vsg-bridge-ls/build.rs

30 lines
984 B
Rust

use std::fs;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
if let Ok(head) = fs::read_to_string(".git/HEAD") {
if let Some(reference) = head.trim().strip_prefix("ref: ") {
println!("cargo:rerun-if-changed=.git/{reference}");
}
}
let revision = if std::path::Path::new(".git").exists() {
Command::new("git")
.args(["rev-parse", "--short=8", "HEAD"])
.output()
.ok()
.filter(|output| output.status.success())
.and_then(|output| String::from_utf8(output.stdout).ok())
.map(|revision| revision.trim().to_owned())
.filter(|revision| {
!revision.is_empty() && revision.bytes().all(|byte| byte.is_ascii_hexdigit())
})
.unwrap_or_else(|| "unknown".to_owned())
} else {
"unknown".to_owned()
};
println!("cargo:rustc-env=VSG_BRIDGE_GIT_SHA={revision}");
}