use std::env;
use std::fs;
use std::path::{Path, PathBuf};
fn main() {
println!("cargo:rerun-if-changed=templates/");
let out_dir = env::var("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir);
println!("cargo:rerun-if-changed=config.json");
copy_templates_dir("templates", out_path);
println!("cargo:rustc-env=FERRISUP_TEMPLATES_DIR={}", out_path.display());
}
fn copy_templates_dir<P: AsRef<Path>>(templates_dir: &str, out_dir: P) {
let source_dir = PathBuf::from(templates_dir);
let target_dir = out_dir.as_ref().join("templates");
fs::create_dir_all(&target_dir).unwrap_or_else(|_| panic!("Failed to create templates directory"));
copy_dir_recursively(&source_dir, &target_dir);
}
fn copy_dir_recursively(source: &Path, target: &Path) {
if !target.exists() {
fs::create_dir_all(target)
.unwrap_or_else(|_| panic!("Failed to create directory: {:?}", target));
}
for entry in fs::read_dir(source)
.unwrap_or_else(|_| panic!("Failed to read directory: {:?}", source)) {
let entry = entry.unwrap_or_else(|_| panic!("Failed to read directory entry"));
let entry_path = entry.path();
let target_path = target.join(entry_path.file_name().unwrap());
if entry_path.is_dir() {
copy_dir_recursively(&entry_path, &target_path);
} else {
fs::copy(&entry_path, &target_path)
.unwrap_or_else(|_| panic!("Failed to copy {:?} to {:?}", entry_path, target_path));
}
}
}