use std::path::{Path, PathBuf};
use std::{env, fs};
fn main() {
let root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").expect("cargo sets this"));
let dir = root.join("art").join("templates");
println!("cargo:rerun-if-changed={}", dir.display());
println!("cargo:rerun-if-changed=build.rs");
let mut found: Vec<(String, PathBuf)> = Vec::new();
if let Ok(entries) = fs::read_dir(&dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) != Some("art") {
continue;
}
let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else {
continue;
};
if !stem
.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '-')
|| stem.is_empty()
{
panic!(
"{}: a template file name must be lowercase letters, digits and dashes — \
it is what you type after --template",
path.display()
);
}
println!("cargo:rerun-if-changed={}", path.display());
found.push((stem.to_string(), path));
}
}
found.sort_by(|a, b| a.0.cmp(&b.0));
let mut out = String::from(
"// Generated by build.rs. Add a template by dropping a .art file in\n\
// art/templates/ — nothing here is edited by hand.\n\
pub(crate) static BUILTIN: &[(&str, &str)] = &[\n",
);
for (stem, path) in &found {
out.push_str(&format!(
" ({stem:?}, include_str!({:?})),\n",
path.to_str().expect("a UTF-8 path")
));
}
out.push_str("];\n");
let dest = Path::new(&env::var_os("OUT_DIR").expect("cargo sets this")).join("templates.rs");
fs::write(&dest, out).expect("write the generated catalogue");
}