use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs};
const SETTINGS: [&str; 4] = [
"Cargo.lock",
".cargo",
"rust-toolchain",
"rust-toolchain.toml",
];
fn main() {
let package = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap_or_default());
let workspace = package.join("..").join("..");
let commit = from_git(&workspace)
.or_else(|| from_archive(&package.join("commit")))
.unwrap_or_else(|| "unknown commit".to_owned());
let version = env::var("CARGO_PKG_VERSION").unwrap_or_default();
println!("cargo:rustc-env=NIB_VERSION={version} ({commit})");
let mut watched = vec![package.join("commit")];
watched.extend(sources(&workspace));
watched.extend(SETTINGS.iter().map(|path| workspace.join(path)));
for path in ["HEAD", "packed-refs"] {
watched.extend(git(&workspace, &["rev-parse", "--git-path", path]).map(PathBuf::from));
}
if let Some(branch) = git(&workspace, &["symbolic-ref", "-q", "HEAD"]) {
watched.extend(git(&workspace, &["rev-parse", "--git-path", &branch]).map(PathBuf::from));
}
for path in watched {
let path = if path.is_relative() {
workspace.join(path)
} else {
path
};
if path.exists() {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}
fn sources(workspace: &Path) -> Vec<PathBuf> {
let mut sources = vec![workspace.join("Cargo.toml")];
for folder in ["crates", "apps"] {
let Ok(crates) = fs::read_dir(workspace.join(folder)) else {
continue;
};
for crate_folder in crates.flatten() {
let path = crate_folder.path();
sources.extend(
["src", "Cargo.toml", "build.rs"]
.into_iter()
.map(|part| path.join(part)),
);
}
}
sources
}
fn from_git(workspace: &Path) -> Option<String> {
let top = git(workspace, &["rev-parse", "--show-toplevel"])?;
if fs::canonicalize(top).ok()? != fs::canonicalize(workspace).ok()? {
return None;
}
let hash = short(&git(workspace, &["rev-parse", "HEAD"])?)?;
let mut status = vec![
"status".to_owned(),
"--porcelain".to_owned(),
"--untracked-files=no".to_owned(),
"--".to_owned(),
];
for path in sources(workspace).iter().chain(
SETTINGS
.iter()
.map(|path| workspace.join(path))
.collect::<Vec<_>>()
.iter(),
) {
if let Ok(relative) = path.strip_prefix(workspace) {
status.push(relative.to_string_lossy().replace('\\', "/"));
}
}
let status: Vec<&str> = status.iter().map(String::as_str).collect();
let changes = git(workspace, &status)?;
Some(if changes.is_empty() {
hash
} else {
format!("{hash}+changes")
})
}
fn from_archive(file: &Path) -> Option<String> {
short(fs::read_to_string(file).ok()?.trim())
}
fn short(hash: &str) -> Option<String> {
let full = (hash.len() == 40 || hash.len() == 64)
&& hash
.chars()
.all(|digit| digit.is_ascii_digit() || ('a'..='f').contains(&digit));
full.then(|| hash[..12].to_owned())
}
fn git(workspace: &Path, arguments: &[&str]) -> Option<String> {
let output = Command::new("git")
.args(arguments)
.current_dir(workspace)
.env("GIT_OPTIONAL_LOCKS", "0")
.output()
.ok()?;
if !output.status.success() {
return None;
}
Some(String::from_utf8(output.stdout).ok()?.trim().to_owned())
}