use std::path::PathBuf;
use std::process::Command;
fn main() {
set_rerun_triggers();
if let Some(describe) = git_describe() {
set_env("CAESURA_GIT_DESCRIBE", &describe);
}
}
fn set_rerun_triggers() {
let git_dir = workspace_root().join(".git");
println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
println!("cargo:rerun-if-changed={}", git_dir.join("refs").display());
println!(
"cargo:rerun-if-changed={}",
git_dir.join("packed-refs").display()
);
}
fn workspace_root() -> PathBuf {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
manifest_dir
.ancestors()
.find(|p| p.join("Cargo.lock").exists())
.expect("workspace root should contain Cargo.lock")
.to_path_buf()
}
fn git_describe() -> Option<String> {
let output = Command::new("git")
.args(["describe", "--tags", "--match", "v*", "--dirty", "--always"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
let describe = String::from_utf8_lossy(&output.stdout).trim().to_owned();
if describe.is_empty() {
return None;
}
Some(describe)
}
fn set_env(key: &str, value: &str) {
println!("cargo:rustc-env={key}={value}");
}