Debian Codegraph @

crates/mdbook-compare/src/main.rs

raw
1 //! Utility to compare the output of two different versions of mdbook.
2
3 use std::path::Path;
4 use std::process::Command;
5
6 macro_rules! error {
7 ($msg:literal $($arg:tt)*) => {
8 eprint!("error: ");
9 eprintln!($msg $($arg)*);
10 std::process::exit(1);
11 };
12 }
13
14 fn main() {
15 let mut args = std::env::args().skip(1);
16 let (Some(mdbook1), Some(book1), Some(mdbook2), Some(book2)) =
17 (args.next(), args.next(), args.next(), args.next())
18 else {
19 eprintln!("error: Expected four arguments: <exe1> <dir1> <exe2> <dir2>");
20 std::process::exit(1);
21 };
22 let mdbook1 = Path::new(&mdbook1);
23 let mdbook2 = Path::new(&mdbook2);
24 let book1 = Path::new(&book1);
25 let book2 = Path::new(&book2);
26 let compare1 = Path::new("compare1");
27 let compare2 = Path::new("compare2");
28 clean(compare1);
29 clean(compare2);
30 clean(&book1.join("book"));
31 clean(&book2.join("book"));
32 build(mdbook1, book1);
33 std::fs::rename(book1.join("book"), compare1).unwrap();
34 build(mdbook2, book2);
35 std::fs::rename(book2.join("book"), compare2).unwrap();
36 diff(compare1, compare2);
37 }
38
39 fn clean(path: &Path) {
40 if path.exists() {
41 println!("removing {path:?}");
42 std::fs::remove_dir_all(path).unwrap();
43 }
44 }
45
46 fn build(mdbook: &Path, book: &Path) {
47 println!("running `{mdbook:?} build` in `{book:?}`");
48 let status = Command::new(mdbook)
49 .arg("build")
50 .current_dir(book)
51 .status()
52 .unwrap_or_else(|e| {
53 error!("expected {mdbook:?} executable to exist: {e}");
54 });
55 if !status.success() {
56 error!("process {mdbook:?} failed");
57 }
58 process(&book.join("book"));
59 }
60
61 fn process(path: &Path) {
62 for entry in std::fs::read_dir(path).unwrap() {
63 let entry = entry.unwrap();
64 let path = entry.path();
65 if path.is_dir() {
66 process(&path);
67 } else {
68 if path.extension().is_some_and(|ext| ext == "html") {
69 tidy(&path);
70 process_html(&path);
71 } else {
72 std::fs::remove_file(path).unwrap();
73 }
74 }
75 }
76 }
77
78 fn process_html(path: &Path) {
79 let content = std::fs::read_to_string(path).unwrap();
80 let Some(start_index) = content.find("<main>") else {
81 return;
82 };
83 let end_index = content.rfind("</main>").unwrap();
84 let new_content = &content[start_index..end_index + 8];
85 std::fs::write(path, new_content).unwrap();
86 }
87
88 fn tidy(path: &Path) {
89 // quiet, no wrap, modify in place
90 let args = "-q -w 0 -m --custom-tags yes --drop-empty-elements no";
91 println!("running `tidy {args}` in `{path:?}`");
92 let status = Command::new("tidy")
93 .args(args.split(' '))
94 .arg(path)
95 .status()
96 .expect("tidy should be installed");
97 if !status.success() {
98 // Exit code 1 is a warning.
99 if status.code() != Some(1) {
100 error!("tidy failed: {status}");
101 }
102 }
103 }
104
105 fn diff(a: &Path, b: &Path) {
106 let args = "diff --no-index";
107 println!("running `git {args} {a:?} {b:?}`");
108 Command::new("git")
109 .args(args.split(' '))
110 .args([a, b])
111 .status()
112 .unwrap();
113 }