use std::{ env, fs, path::{Path, PathBuf}, }; fn collect(root: &Path, folder: &Path, output: &mut String) { let mut files: Vec<_> = fs::read_dir(folder) .expect("frontend assets") .map(|f| f.unwrap().path()) .collect(); files.sort(); for path in files { if path.file_name().unwrap().to_string_lossy().starts_with('.') { continue; } assert!(!path.is_symlink(), "frontend assets must not be symlinks"); if path.is_dir() { collect(root, &path, output); continue; } let relative = path .strip_prefix(root) .unwrap() .to_string_lossy() .replace('\\', "/"); let mime = match path.extension().and_then(|e| e.to_str()).unwrap_or("") { "html" => "text/html; charset=utf-8", "js" | "mjs" => "text/javascript; charset=utf-8", "wasm" => "application/wasm", "css" => "text/css; charset=utf-8", "png" => "image/png", "svg" => "image/svg+xml", "json" => "application/json", _ => continue, }; output.push_str(&format!( "{:?} => Some((include_bytes!({:?}), {:?})),\n", format!("/{relative}"), path.to_string_lossy(), mime )); } } fn main() { let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("dist/web-ui"); println!("cargo:rerun-if-changed={}", root.display()); assert!( root.join("index.html").is_file(), "Build Rust frontend first: cargo xtask web" ); let mut output = r#"pub fn resource(path: &str) -> Option<(&'static [u8], &'static str)> { let path = if path == "/" || path == "/realtime" { "/index.html" } else { path }; match path { "#.to_owned(); collect(&root, &root, &mut output); output.push_str("_ => None } }\n"); fs::write( PathBuf::from(env::var("OUT_DIR").unwrap()).join("web_assets.rs"), output, ) .unwrap(); }