//! `cargo xtask assets`: every asset file in the repository must have a row //! in `ASSETS.md`, and font files must not be committed at all. //! //! An asset is any file under `assets/` except placeholders and licence texts, //! plus any file elsewhere whose extension marks it as an image, icon, font, //! sound or video. use std::collections::BTreeSet; use std::fs; use crate::util::{TaskResult, repo_files, repo_root}; /// Extensions of image, icon, sound and video files. const MEDIA_EXTENSIONS: &[&str] = &[ "png", "jpg", "jpeg", "gif", "webp", "bmp", "ico", "icns", "svg", "cur", "ani", "tif", "tiff", "avif", "mp3", "wav", "ogg", "mp4", "webm", "mov", ]; /// Extensions of font files, which are never committed. const FONT_EXTENSIONS: &[&str] = &["ttf", "otf", "woff", "woff2", "eot", "ttc"]; /// Runs the check on the repository. /// /// # Errors /// Fails with one line per unlisted asset or committed font. pub fn check_repo() -> TaskResult { let root = repo_root()?; let table = fs::read_to_string(root.join("ASSETS.md")).map_err(|e| format!("ASSETS.md: {e}"))?; let files = repo_files(&root)?; let problems = check(&files, &table); if problems.is_empty() { let count = files.iter().filter(|f| is_asset(f)).count(); println!("assets: {count} asset files, all attributed"); Ok(()) } else { Err(format!("asset problems:\n {}", problems.join("\n "))) } } /// Checks a file list against the contents of `ASSETS.md`. pub fn check(files: &[String], assets_md: &str) -> Vec { let listed = listed_paths(assets_md); let mut problems = Vec::new(); for file in files { if FONT_EXTENSIONS.contains(&extension(file).as_str()) { problems.push(format!( "{file}: font files are never committed; use an @fontsource package" )); } else if is_asset(file) && !listed.contains(file.as_str()) { problems.push(format!("{file}: no row in ASSETS.md")); } } problems } /// Whether a repository path counts as an asset that needs attribution. pub fn is_asset(path: &str) -> bool { let name = path.rsplit('/').next().unwrap_or(path); if path.starts_with("assets/") { let lower = name.to_ascii_lowercase(); let licence_text = lower.starts_with("license") || lower.starts_with("licence") || lower.starts_with("notice") || lower.starts_with("readme"); return name != ".gitkeep" && !licence_text; } let ext = extension(path); MEDIA_EXTENSIONS.contains(&ext.as_str()) || FONT_EXTENSIONS.contains(&ext.as_str()) } /// Lower-case extension of a path, or an empty string. fn extension(path: &str) -> String { let name = path.rsplit('/').next().unwrap_or(path); name.rsplit_once('.') .map(|(_, e)| e.to_ascii_lowercase()) .unwrap_or_default() } /// Paths in the first column of the table rows of `ASSETS.md`, with /// surrounding backticks removed. pub fn listed_paths(assets_md: &str) -> BTreeSet<&str> { assets_md .lines() .map(str::trim) .filter(|l| l.starts_with('|')) .filter_map(|l| l.trim_start_matches('|').split('|').next()) .map(|cell| cell.trim().trim_matches('`')) .filter(|cell| !cell.is_empty() && *cell != "Asset" && !cell.starts_with("---")) .collect() } #[cfg(test)] mod tests { use super::*; const TABLE: &str = "| Asset | Author | Source | Licence | Notes |\n|---|---|---|---|---|\n| `assets/icons/lt-class.svg` | A | original | MIT OR Apache-2.0 | |\n"; fn files(list: &[&str]) -> Vec { list.iter().map(|s| s.to_string()).collect() } #[test] fn listed_assets_pass() { let f = files(&[ "assets/icons/lt-class.svg", "assets/icons/.gitkeep", "assets/icons/LICENSE.txt", "src/main.rs", ]); assert!(check(&f, TABLE).is_empty()); } #[test] fn unlisted_assets_fail_anywhere() { let f = files(&["assets/icons/lt-trait.svg", "ui/src/logo.png"]); let p = check(&f, TABLE); assert_eq!(p.len(), 2); assert!(p[0].contains("lt-trait.svg")); assert!(p[1].contains("logo.png")); } #[test] fn fonts_always_fail() { let f = files(&["ui/public/Inter.woff2"]); assert!(check(&f, TABLE)[0].contains("font files are never committed")); } #[test] fn header_and_separator_are_not_paths() { let listed = listed_paths(TABLE); assert_eq!( listed.into_iter().collect::>(), vec!["assets/icons/lt-class.svg"] ); } }