use std::env; use std::fs; use std::path::{Path, PathBuf}; use sha2::{Digest, Sha256}; fn files(root: &Path, dir: &Path, output: &mut Vec) { let mut entries = fs::read_dir(dir) .unwrap_or_else(|error| panic!("reading {}: {error}", dir.display())) .map(|entry| entry.unwrap().path()) .collect::>(); entries.sort(); for path in entries { if path.is_dir() { files(root, &path, output); } else { output.push(path.strip_prefix(root).unwrap().to_owned()); } } } fn hash(bytes: &[u8], length: usize) -> String { format!("{:x}", Sha256::digest(bytes))[..length].to_owned() } fn main() { for path in [ "src/style.css", "assets/fonts", "web/dist", ".glisten/dist/manifest.json", ] { println!("cargo::rerun-if-changed={path}"); } let manifest = fs::read(".glisten/dist/manifest.json") .expect("missing .glisten/dist/manifest.json; run deno task glisten first"); let entries = fs::read_dir("web/dist") .expect("missing web/dist; run deno task build") .map(|entry| entry.expect("reading web/dist entry")) .filter(|entry| entry.file_type().expect("reading web asset type").is_file()) .map(|entry| { entry .file_name() .into_string() .expect("non-UTF-8 web asset") }) .collect::>(); let entry = |prefix: &str| { let matches = entries .iter() .filter(|name| { name.starts_with(prefix) && name.ends_with(".js") && name.len() > prefix.len() + 3 }) .collect::>(); assert!( matches.len() == 1, "expected exactly one {prefix}*.js in web/dist; run deno task build" ); matches[0].as_str() }; let main_js = entry("main-"); let worker_js = entry("highlight-worker-"); let mut digest = Sha256::new(); let root = Path::new("assets/fonts"); let mut paths = Vec::new(); files(root, root, &mut paths); paths.sort(); let fonts = paths .iter() .map(|path| { let source = path.to_str().expect("non-UTF-8 font path"); let bytes = fs::read(root.join(path)).expect("reading font"); let content_hash = Sha256::digest(&bytes); digest.update((source.len() as u64).to_be_bytes()); digest.update(source.as_bytes()); digest.update(content_hash); let staged = if path.extension().is_some_and(|ext| ext == "woff2") { let stem = path.file_stem().unwrap().to_str().unwrap(); let hash = format!("{content_hash:x}"); path.with_file_name(format!("{stem}-{}.woff2", &hash[..12])) .to_str() .unwrap() .to_owned() } else { source.to_owned() }; (source.to_owned(), staged) }) .collect::>(); let css = fs::read_to_string("src/style.css").expect("reading stylesheet"); let root = css .split_once(":root {") .expect("stylesheet has a :root block") .1 .split_once('}') .expect("stylesheet :root block is closed") .0; let mut declarations = root .lines() .filter_map(|line| { let trimmed = line.trim(); if trimmed.is_empty() { Some("") } else if [ "color-scheme:", "--bg-", "--fg-", "--accent-", "--selection-col:", "--hl-", ] .iter() .any(|prefix| trimmed.starts_with(prefix)) { Some(line) } else { None } }) .collect::>(); declarations.dedup_by(|a, b| a.is_empty() && b.is_empty()); let declarations = declarations.join("\n"); let declarations = declarations.trim_matches('\n'); let theme = format!("/*\n:root {{\n{declarations}\n}}\n*/\n"); let mut rewritten = String::new(); let mut rest = css.as_str(); while let Some(index) = rest.find("/fonts/") { rewritten.push_str(&rest[..index]); rest = &rest[index + "/fonts/".len()..]; let end = rest .find(|c: char| matches!(c, '\'' | '"' | ')' | '(') || c.is_whitespace()) .unwrap_or(rest.len()); let source = &rest[..end]; let staged = fonts .iter() .find(|(path, _)| path == source && path.ends_with(".woff2")) .unwrap_or_else(|| panic!("unknown stylesheet font: /fonts/{source}")); rewritten.push_str("/fonts/"); rewritten.push_str(&staged.1); rest = &rest[end..]; } rewritten.push_str(rest); let style = format!("style-{}.css", hash(rewritten.as_bytes(), 12)); for bytes in [ style.as_bytes(), main_js.as_bytes(), worker_js.as_bytes(), manifest.as_slice(), ] { digest.update((bytes.len() as u64).to_be_bytes()); digest.update(bytes); } let root = Path::new("web/dist"); let mut paths = Vec::new(); files(root, root, &mut paths); paths.sort(); for path in paths { let name = path.to_str().expect("non-UTF-8 web asset path"); let bytes = fs::read(root.join(&path)).expect("reading web asset"); digest.update((name.len() as u64).to_be_bytes()); digest.update(name.as_bytes()); digest.update((bytes.len() as u64).to_be_bytes()); digest.update(bytes); } let digest = format!("{:x}", digest.finalize()); let generated = format!( "pub const MAIN_JS_FILE: &str = {main_js:?};\n\ pub const WORKER_JS_FILE: &str = {worker_js:?};\n\ pub const STYLE_CSS_FILE: &str = {style:?};\n\ pub const STYLESHEET: &str = {rewritten:?};\n\ pub const THEME_STYLESHEET: &str = {theme:?};\n\ pub const FONT_FILES: &[(&str, &str)] = &{fonts:?};\n\ pub const ASSET_DIGEST: &str = {:?};\n", &digest[..16] ); fs::write( Path::new(&env::var_os("OUT_DIR").unwrap()).join("assets.rs"), generated, ) .expect("writing asset manifest"); }