use std::fs;
use std::path::Path;
use std::process::Command;
fn main() {
let themes_dir = Path::new("themes");
let out_path = Path::new("src/ui/_generated_bundled_themes.rs");
let script_path = Path::new("scripts/build_themes.py");
println!("cargo:rerun-if-changed=themes/");
println!("cargo:rerun-if-changed=scripts/build_themes.py");
println!("cargo:rerun-if-changed={}", out_path.display());
if !needs_regeneration(themes_dir, out_path) {
return;
}
let python = std::env::var("PYTHON").unwrap_or_else(|_| "python3".to_string());
let status = Command::new(&python).arg(script_path).status();
match status {
Ok(s) if s.success() => {
println!("cargo:rerun-if-changed=src/ui/_generated_bundled_themes.rs");
}
Ok(s) => {
println!(
"cargo:warning=themebuild exited with status {s}; using committed themes file"
);
}
Err(e) => {
println!("cargo:warning=could not invoke {python} ({e}); using committed themes file");
}
}
}
fn needs_regeneration(themes_dir: &Path, out_path: &Path) -> bool {
if !out_path.exists() {
return true;
}
let Ok(out_meta) = fs::metadata(out_path) else {
return true;
};
let Ok(out_mtime) = out_meta.modified() else {
return true;
};
let Ok(entries) = fs::read_dir(themes_dir) else {
return false; };
for entry in entries.flatten() {
let path = entry.path();
if !path.is_file() || path.extension().and_then(|e| e.to_str()) != Some("toml") {
continue;
}
let Ok(meta) = fs::metadata(&path) else {
continue;
};
let Ok(mtime) = meta.modified() else {
continue;
};
if mtime > out_mtime {
return true;
}
}
false
}