//! OxygenFlow's cheap checks as one WASI module (#96): what //! `.flow/jobs/{layout,todos,size,summary}.sh` did in an Alpine microVM, so //! that any runner offering the WASM tier (a phone, plex, windows-1) can take //! them. The job's files are preopened as `/src`, which is also the current //! directory, so every path here is relative to the workspace. //! //! - `layout PATH...`: each path is a file; fails naming the first missing. //! - `todos DIR...`: the lines holding a note marker, counted, the first 20 //! listed; never fails. //! - `size DIR`: lines of Rust in each crate directory under DIR. //! - `summary`: one line naming `$PROJECT`. use std::fs; use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::process::ExitCode; /// The note markers, spelt in pieces so this file does not count itself. const MARKERS: [&str; 3] = [concat!("TO", "DO"), concat!("FIX", "ME"), concat!("X", "XX")]; /// How many noted lines `todos` lists. const LISTED: usize = 20; const USAGE: &str = "usage: flow-checks layout PATH... | todos DIR... | size DIR | summary"; fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); let root = Path::new("."); let mut out = io::stdout().lock(); let done = match args.split_first() { Some((cmd, rest)) => match cmd.as_str() { "layout" => layout(root, rest, &mut out), "todos" => todos(root, rest, &mut out), "size" => match rest { [dir] => size(root, dir, &mut out), _ => Err(USAGE.to_string()), }, "summary" => summary(std::env::var("PROJECT").ok().as_deref(), &mut out), _ => Err(USAGE.to_string()), }, None => Err(USAGE.to_string()), }; match done { Ok(()) => ExitCode::SUCCESS, Err(e) => { eprintln!("{e}"); ExitCode::FAILURE } } } type Done = Result<(), String>; fn emit(out: &mut impl Write, line: &str) -> Done { writeln!(out, "{line}").map_err(|e| format!("write: {e}")) } /// The workspace has the files it should. fn layout(root: &Path, paths: &[String], out: &mut impl Write) -> Done { if paths.is_empty() { return Err(USAGE.to_string()); } for p in paths { if !root.join(p).is_file() { return Err(format!("missing: {p}")); } emit(out, &format!("found {p}"))?; } Ok(()) } /// Notes left in the code, for the log. fn todos(root: &Path, dirs: &[String], out: &mut impl Write) -> Done { if dirs.is_empty() { return Err(USAGE.to_string()); } let mut noted = Vec::new(); for d in dirs { for file in files(&root.join(d))? { // Text only, as grep reports a binary file rather than its lines. let Ok(text) = fs::read_to_string(&file) else { continue }; let name = shown(root, &file); for (i, line) in text.lines().enumerate() { if MARKERS.iter().any(|m| line.contains(m)) { noted.push(format!("{name}:{}:{line}", i + 1)); } } } } emit(out, &format!("{} {} notes in {}", noted.len(), MARKERS.join("/"), shown_dirs(dirs)))?; for line in noted.iter().take(LISTED) { emit(out, line)?; } Ok(()) } /// Lines of Rust per crate: each directory directly under `dir`. fn size(root: &Path, dir: &str, out: &mut impl Write) -> Done { for krate in entries(&root.join(dir))?.into_iter().filter(|p| p.is_dir()) { let mut lines = 0; for file in files(&krate)?.into_iter().filter(|f| f.extension().is_some_and(|e| e == "rs")) { let bytes = fs::read(&file).map_err(|e| format!("{}: {e}", file.display()))?; lines += bytes.iter().filter(|&&b| b == b'\n').count(); } emit(out, &format!("{:<24} {lines:>6} lines", format!("{}/", shown(root, &krate))))?; } Ok(()) } fn summary(project: Option<&str>, out: &mut impl Write) -> Done { emit(out, &format!("{}: every check passed on this state.", project.unwrap_or("This project"))) } /// A directory's entries, sorted by name so every runner prints the same. fn entries(dir: &Path) -> Result, String> { let read = fs::read_dir(dir).map_err(|e| format!("{}: {e}", dir.display()))?; let mut all = read.map(|e| e.map(|e| e.path())).collect::, _>>().map_err(|e| format!("{}: {e}", dir.display()))?; all.sort(); Ok(all) } /// Every file under `dir`, depth first, in name order; links are not followed. fn files(dir: &Path) -> Result, String> { let mut found = Vec::new(); for p in entries(dir)? { let kind = fs::symlink_metadata(&p).map_err(|e| format!("{}: {e}", p.display()))?.file_type(); if kind.is_dir() { found.extend(files(&p)?); } else if kind.is_file() { found.push(p); } } Ok(found) } /// A path as the workspace names it, with `/` between its parts. fn shown(root: &Path, p: &Path) -> String { let rel = p.strip_prefix(root).unwrap_or(p); rel.components().map(|c| c.as_os_str().to_string_lossy()).collect::>().join("/") } fn shown_dirs(dirs: &[String]) -> String { dirs.iter().map(|d| format!("{}/", d.trim_end_matches('/'))).collect::>().join(" ") } #[cfg(test)] mod tests { use super::*; fn tree(files: &[(&str, &str)]) -> tempfile::TempDir { let dir = tempfile::tempdir().unwrap(); for (path, text) in files { let p = dir.path().join(path); fs::create_dir_all(p.parent().unwrap()).unwrap(); fs::write(p, text).unwrap(); } dir } fn run(f: impl FnOnce(&mut Vec) -> Done) -> (Done, String) { let mut out = Vec::new(); let done = f(&mut out); (done, String::from_utf8(out).unwrap()) } fn args(a: &[&str]) -> Vec { a.iter().map(|s| s.to_string()).collect() } #[test] fn layout_lists_what_it_found_and_fails_on_the_first_missing() { let t = tree(&[("Cargo.toml", ""), ("docs/m0.md", "")]); let (done, out) = run(|o| layout(t.path(), &args(&["Cargo.toml", "docs/m0.md"]), o)); assert_eq!(done, Ok(())); assert_eq!(out, "found Cargo.toml\nfound docs/m0.md\n"); let (done, out) = run(|o| layout(t.path(), &args(&["Cargo.toml", "nope.rs", "docs/m0.md"]), o)); assert_eq!(done, Err("missing: nope.rs".to_string())); assert_eq!(out, "found Cargo.toml\n"); // A directory is not the file asked for. assert!(layout(t.path(), &args(&["docs"]), &mut Vec::new()).is_err()); } #[test] fn todos_counts_every_marked_line_and_lists_the_first_twenty() { let marker = MARKERS[0]; let many: String = (0..25).map(|i| format!("// {marker} {i}\n")).collect(); let t = tree(&[ ("crates/a/src/lib.rs", &format!("fn a() {{}}\n// {} later\n", MARKERS[1])), ("crates/b/src/many.rs", &many), ("crates/b/blob.bin", "\u{0}"), ]); // Invalid UTF-8 is skipped, as grep skips a binary file's lines. fs::write(t.path().join("crates/b/blob.bin"), [0xff, 0xfe, b'T']).unwrap(); let (done, out) = run(|o| todos(t.path(), &args(&["crates"]), o)); assert_eq!(done, Ok(())); let lines: Vec<&str> = out.lines().collect(); assert_eq!(lines[0], format!("26 {} notes in crates/", MARKERS.join("/"))); assert_eq!(lines[1], format!("crates/a/src/lib.rs:2:// {} later", MARKERS[1])); assert_eq!(lines[2], format!("crates/b/src/many.rs:1:// {marker} 0")); assert_eq!(lines.len(), 1 + LISTED); } #[test] fn size_counts_rust_lines_per_crate_in_name_order() { let t = tree(&[ ("crates/zeta/src/main.rs", "a\nb\nc\n"), ("crates/alpha/src/lib.rs", "a\n"), ("crates/alpha/src/deep/mod.rs", "a\nb\n"), ("crates/alpha/README.md", "not rust\n"), ("crates/loose.txt", "a file, not a crate\n"), ]); let (done, out) = run(|o| size(t.path(), "crates", o)); assert_eq!(done, Ok(())); assert_eq!(out, format!("{:<24} {:>6} lines\n{:<24} {:>6} lines\n", "crates/alpha/", 3, "crates/zeta/", 3)); } #[test] fn summary_names_the_project() { let (_, out) = run(|o| summary(Some("OxygenFlow"), o)); assert_eq!(out, "OxygenFlow: every check passed on this state.\n"); } }