use std::fs;
use std::path::Path;
fn main() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR set");
let root = Path::new(&manifest_dir);
let web_dist = root.join("../../../web/dist");
let local = root.join("dist");
println!(
"cargo:rerun-if-changed={}",
web_dist.parent().expect("web/dist has a parent").display()
);
if web_dist.join("index.html").exists() {
if local.exists() {
fs::remove_dir_all(&local).expect("clear stale crate-local dist/");
}
copy_dir(&web_dist, &local);
} else if !local.join("index.html").exists() {
fs::create_dir_all(&local).expect("create dist/");
fs::write(
local.join("index.html"),
"<!doctype html><title>placeholder</title>\n",
)
.expect("write placeholder index.html");
}
}
fn copy_dir(src: &Path, dst: &Path) {
fs::create_dir_all(dst).unwrap_or_else(|e| panic!("create {}: {e}", dst.display()));
for entry in fs::read_dir(src).unwrap_or_else(|e| panic!("read {}: {e}", src.display())) {
let entry = entry.unwrap_or_else(|e| panic!("read {} entry: {e}", src.display()));
let from = entry.path();
if entry.file_type().expect("dist entry type").is_dir() {
copy_dir(&from, &dst.join(entry.file_name()));
} else {
let to = dst.join(entry.file_name());
fs::copy(&from, &to).unwrap_or_else(|e| panic!("copy {}: {e}", from.display()));
}
}
}