use std::fmt::Write as _;
use std::path::{Path, PathBuf};
fn main() {
let manifest = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let out = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR"));
let bundle = manifest.join("dist/web");
println!("cargo:rerun-if-changed={}", bundle.display());
let mut files = Vec::new();
collect(&bundle, &bundle, &mut files);
files.sort();
let mut table = String::from(
"/// Every file of the built interface, by the path a browser asks for.\n\
static BUNDLED: &[(&str, &[u8])] = &[\n",
);
for name in &files {
let full = bundle.join(name);
writeln!(
table,
" ({:?}, include_bytes!({:?})),",
name.replace('\\', "/"),
full.display().to_string()
)
.expect("the table is written to a string");
}
table.push_str("];\n");
std::fs::write(out.join("interface.rs"), table).expect("the asset table is written");
}
fn collect(root: &Path, directory: &Path, found: &mut Vec<String>) {
let Ok(entries) = std::fs::read_dir(directory) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
collect(root, &path, found);
} else if let Ok(relative) = path.strip_prefix(root) {
found.push(relative.to_string_lossy().into_owned());
}
}
}