use std::path::Path;
fn main() {
if let Ok(projects) = std::fs::read_dir("src/templates/project") {
for project in projects.flatten() {
let name = project.file_name();
let owner = match name.to_str() {
Some("billing-stripe") => "billing-base",
Some("messaging-twilio") => "messaging-base",
Some(name) => name,
None => continue,
};
let source = Path::new("../blueprint").join(owner).join(".gitignore");
let embedded = project.path().join(".gitignore");
if source.is_file() && embedded.exists() {
verify_embedded_source(&source, &embedded);
}
}
}
emit_rerun_for_tree(Path::new("src/templates"));
emit_rerun_for_tree(Path::new("../blueprint"));
}
fn verify_embedded_source(source: &Path, embedded: &Path) {
let matches = if source
.file_name()
.is_some_and(|name| name == ".gitignore" || name == ".gitattributes")
{
!embedded.is_symlink()
&& std::fs::read(source)
.ok()
.zip(std::fs::read(embedded).ok())
.is_some_and(|(source, embedded)| source == embedded)
} else {
embedded.canonicalize().ok() == source.canonicalize().ok()
};
assert!(
matches,
"Rust template inventory is stale at {}. Run `python3 scripts/sync_rust_embedded_sources.py` from the repository root before rebuilding the CLI.",
embedded.display()
);
}
fn emit_rerun_for_tree(dir: &Path) {
let Ok(resolved) = dir.canonicalize() else {
return;
};
println!("cargo:rerun-if-changed={}", resolved.display());
let Ok(entries) = std::fs::read_dir(&resolved) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.file_name().is_some_and(|name| {
matches!(
name.to_str(),
Some("node_modules") | Some("dist") | Some("lib") | Some(".git") | Some("target")
)
}) {
continue;
}
if path.is_dir() {
emit_rerun_for_tree(&path);
} else {
println!("cargo:rerun-if-changed={}", path.display());
}
}
}