use std::path::Path;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
emit_ui_asset_shim();
emit_git_commit();
}
fn emit_ui_asset_shim() {
if std::env::var_os("CARGO_FEATURE_UI").is_none() {
return;
}
let manifest = std::env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR is always set for a build script");
let assets = Path::new(&manifest).join("../../bridge/dist/bridge/browser");
if let Err(err) = std::fs::create_dir_all(&assets) {
println!(
"cargo::warning=the `ui` feature needs {} , which could not be created ({err}). \
The dashboard assets live outside this package, so `ui` only works from a checkout \
that has run the Bridge build; a crates.io build should leave the feature off.",
assets.display()
);
}
}
fn emit_git_commit() {
let manifest = std::env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR is always set for a build script");
let repo_root = Path::new(&manifest).join("../..");
println!(
"cargo:rerun-if-changed={}",
repo_root.join(".git/HEAD").display()
);
println!(
"cargo:rerun-if-changed={}",
repo_root.join(".git/index").display()
);
let Ok(rev) = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.current_dir(&repo_root)
.output()
else {
return;
};
if !rev.status.success() {
return;
}
let mut commit = String::from_utf8_lossy(&rev.stdout).trim().to_string();
if commit.is_empty() {
return;
}
if let Ok(status) = Command::new("git")
.args(["status", "--porcelain"])
.current_dir(&repo_root)
.output()
&& status.status.success()
&& !status.stdout.is_empty()
{
commit.push_str("-dirty");
}
println!("cargo:rustc-env=SALVOR_SERVER_GIT_COMMIT={commit}");
}