baldav/src-tauri/build.rs

50 lines
1.9 KiB
Rust
Raw Normal View History

use std::process::Command;
fn main() {
// Re-run this script whenever the checked-out commit changes, so the embedded
// git hash never goes stale: HEAD changes on checkout; the branch ref file
// changes on commit. Without this, a rebuild after a commit (with no source
// change) could keep a cached, wrong hash.
println!("cargo:rerun-if-changed=../.git/HEAD");
if let Ok(head) = std::fs::read_to_string("../.git/HEAD") {
if let Some(r) = head.strip_prefix("ref: ") {
println!("cargo:rerun-if-changed=../.git/{}", r.trim());
}
}
// Pass git hash and build info to Rust via env vars
let hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.unwrap_or_else(|_| "unknown".into());
// "-dirty" ONLY for uncommitted TRACKED code (frontend + Rust src). Version files
// are bumped on every build, binaries/ churn, and there are many untracked files
// in the tree — none of those mean the built code differs from HEAD, so exclude
// them (-uno + pathspec). This flags "the APK's code isn't fully committed".
let dirty = Command::new("git")
.args([
"-C", "..", "status", "--porcelain", "-uno", "--",
"frontend", "src-tauri/src", "src-tauri/build.rs",
])
.output()
.map(|o| !o.stdout.is_empty())
.unwrap_or(false);
let git_hash = if dirty { format!("{}-dirty", hash) } else { hash };
let build_date = chrono_free_date();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
println!("cargo:rustc-env=BUILD_DATE={}", build_date);
tauri_build::build()
}
fn chrono_free_date() -> String {
Command::new("date")
.args(["+%Y-%m-%d %H:%M"])
.output()
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.unwrap_or_else(|_| "unknown".into())
}