|
| 1 | +//! Marker-hybrid example blocks (`<!-- forge:example -->`). |
| 2 | +//! |
| 3 | +//! Each example app under `examples/<name>/` imports a set of `runtime:*` |
| 4 | +//! modules in its `src/`. The matching docs page (`docs/examples/<name>.md`) |
| 5 | +//! can opt in with a `<!-- forge:example -->` block listing those modules; the |
| 6 | +//! `example-block` rule fails CI when the block is stale and `make docs-examples` |
| 7 | +//! regenerates it from the app source. Prose outside the markers is untouched. |
| 8 | +//! |
| 9 | +//! This extends drift protection to a real, easily-missed category: an example |
| 10 | +//! app gains/loses a `runtime:*` dependency but its doc page is not updated. |
| 11 | +
|
| 12 | +use crate::checks::read_optional; |
| 13 | +use crate::discovery::Workspace; |
| 14 | +use crate::{markers, Finding}; |
| 15 | +use regex::Regex; |
| 16 | +use std::path::{Path, PathBuf}; |
| 17 | + |
| 18 | +pub const BLOCK_OPEN: &str = "<!-- forge:example -->"; |
| 19 | +pub const BLOCK_CLOSE: &str = "<!-- /forge:example -->"; |
| 20 | + |
| 21 | +/// Discover example apps: immediate subdirectories of `examples/` that contain a |
| 22 | +/// `manifest.app.toml`. Returns `(name, dir)` sorted by name. |
| 23 | +pub fn example_apps(ws: &Workspace) -> Vec<(String, PathBuf)> { |
| 24 | + let examples_dir = ws.root.join("examples"); |
| 25 | + let mut apps = Vec::new(); |
| 26 | + let entries = match std::fs::read_dir(&examples_dir) { |
| 27 | + Ok(e) => e, |
| 28 | + Err(_) => return apps, |
| 29 | + }; |
| 30 | + for entry in entries.flatten() { |
| 31 | + let dir = entry.path(); |
| 32 | + if dir.is_dir() && dir.join("manifest.app.toml").exists() { |
| 33 | + if let Some(name) = dir.file_name().and_then(|n| n.to_str()) { |
| 34 | + apps.push((name.to_string(), dir.clone())); |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + apps.sort_by(|a, b| a.0.cmp(&b.0)); |
| 39 | + apps |
| 40 | +} |
| 41 | + |
| 42 | +/// The sorted, unique `runtime:*` modules imported anywhere under `<app>/src`. |
| 43 | +pub fn runtime_modules(app_dir: &Path) -> Vec<String> { |
| 44 | + let re = Regex::new(r"runtime:([a-z_]+)").expect("valid runtime-module regex"); |
| 45 | + let mut mods: Vec<String> = Vec::new(); |
| 46 | + for ts in ts_sources(&app_dir.join("src")) { |
| 47 | + if let Some(src) = read_optional(&ts) { |
| 48 | + for cap in re.captures_iter(&src) { |
| 49 | + mods.push(format!("runtime:{}", &cap[1])); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + mods.sort(); |
| 54 | + mods.dedup(); |
| 55 | + mods |
| 56 | +} |
| 57 | + |
| 58 | +/// Render the body between the markers. |
| 59 | +pub fn render_block_body(name: &str, modules: &[String]) -> String { |
| 60 | + let list = if modules.is_empty() { |
| 61 | + "_none_".to_string() |
| 62 | + } else { |
| 63 | + modules |
| 64 | + .iter() |
| 65 | + .map(|m| format!("`{m}`")) |
| 66 | + .collect::<Vec<_>>() |
| 67 | + .join(", ") |
| 68 | + }; |
| 69 | + format!( |
| 70 | + "<!-- generated from examples/{name}/src — run `make docs-examples` to refresh -->\n**Runtime modules used:** {list}", |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +/// The docs page path for an example app. |
| 75 | +fn page_path(ws: &Workspace, name: &str) -> PathBuf { |
| 76 | + ws.docs_dir().join("examples").join(format!("{name}.md")) |
| 77 | +} |
| 78 | + |
| 79 | +/// Rule `example-block`: every opted-in example page's block matches the modules |
| 80 | +/// its app actually imports. |
| 81 | +pub fn check(ws: &Workspace) -> Vec<Finding> { |
| 82 | + let mut findings = Vec::new(); |
| 83 | + for (name, dir) in example_apps(ws) { |
| 84 | + let page = match read_optional(&page_path(ws, &name)) { |
| 85 | + Some(p) => p, |
| 86 | + None => continue, |
| 87 | + }; |
| 88 | + if !page.contains(BLOCK_OPEN) { |
| 89 | + continue; // not opted in |
| 90 | + } |
| 91 | + let expected = render_block_body(&name, &runtime_modules(&dir)); |
| 92 | + match markers::find_region(&page, BLOCK_OPEN, BLOCK_CLOSE) { |
| 93 | + Some((_, _, body)) if body == expected => {} |
| 94 | + Some(_) => findings.push(Finding::new( |
| 95 | + "example-block", |
| 96 | + format!( |
| 97 | + "examples/{name}: the <!-- forge:example --> block in {name}.md is stale; run `make docs-examples` to refresh it" |
| 98 | + ), |
| 99 | + )), |
| 100 | + None => findings.push(Finding::new( |
| 101 | + "example-block", |
| 102 | + format!("examples/{name}: {name}.md has an opening <!-- forge:example --> with no closing marker"), |
| 103 | + )), |
| 104 | + } |
| 105 | + } |
| 106 | + findings |
| 107 | +} |
| 108 | + |
| 109 | +/// Regenerate every opted-in example block in place. Returns rewritten paths. |
| 110 | +pub fn write_all(ws: &Workspace) -> std::io::Result<Vec<PathBuf>> { |
| 111 | + let mut written = Vec::new(); |
| 112 | + for (name, dir) in example_apps(ws) { |
| 113 | + let path = page_path(ws, &name); |
| 114 | + let page = match read_optional(&path) { |
| 115 | + Some(p) => p, |
| 116 | + None => continue, |
| 117 | + }; |
| 118 | + if !page.contains(BLOCK_OPEN) { |
| 119 | + continue; |
| 120 | + } |
| 121 | + let expected = render_block_body(&name, &runtime_modules(&dir)); |
| 122 | + if let Some((_, _, body)) = markers::find_region(&page, BLOCK_OPEN, BLOCK_CLOSE) { |
| 123 | + if body == expected { |
| 124 | + continue; |
| 125 | + } |
| 126 | + if let Some(updated) = |
| 127 | + markers::replace_region(&page, BLOCK_OPEN, BLOCK_CLOSE, &expected) |
| 128 | + { |
| 129 | + std::fs::write(&path, updated)?; |
| 130 | + written.push(path); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + Ok(written) |
| 135 | +} |
| 136 | + |
| 137 | +/// Recursively collect `.ts` files under `dir`. |
| 138 | +fn ts_sources(dir: &Path) -> Vec<PathBuf> { |
| 139 | + let mut out = Vec::new(); |
| 140 | + let mut stack = vec![dir.to_path_buf()]; |
| 141 | + while let Some(d) = stack.pop() { |
| 142 | + let entries = match std::fs::read_dir(&d) { |
| 143 | + Ok(e) => e, |
| 144 | + Err(_) => continue, |
| 145 | + }; |
| 146 | + for entry in entries.flatten() { |
| 147 | + let path = entry.path(); |
| 148 | + if path.is_dir() { |
| 149 | + stack.push(path); |
| 150 | + } else if path.extension().and_then(|e| e.to_str()) == Some("ts") { |
| 151 | + out.push(path); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + out.sort(); |
| 156 | + out |
| 157 | +} |
| 158 | + |
| 159 | +#[cfg(test)] |
| 160 | +mod tests { |
| 161 | + use super::*; |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn render_block_body_lists_modules() { |
| 165 | + let body = render_block_body( |
| 166 | + "react-app", |
| 167 | + &["runtime:ipc".to_string(), "runtime:window".to_string()], |
| 168 | + ); |
| 169 | + assert!(body.contains("**Runtime modules used:** `runtime:ipc`, `runtime:window`")); |
| 170 | + assert!(body.contains("examples/react-app/src")); |
| 171 | + } |
| 172 | + |
| 173 | + #[test] |
| 174 | + fn render_block_body_handles_no_modules() { |
| 175 | + assert!(render_block_body("x", &[]).contains("_none_")); |
| 176 | + } |
| 177 | +} |
0 commit comments