|
| 1 | +use std::error::Error; |
| 2 | +use std::process::Command; |
| 3 | +use std::process::exit; |
| 4 | + |
| 5 | +type Result<T> = std::result::Result<T, Box<dyn Error>>; |
| 6 | + |
| 7 | +fn main() -> Result<()> { |
| 8 | + let mut args = std::env::args().skip(1); |
| 9 | + let cmd = args.next(); |
| 10 | + const OPTIONS: &str = "linkcheck, style-check, test-all"; |
| 11 | + match cmd.as_deref() { |
| 12 | + Some("test-all") => { |
| 13 | + mdbook_test()?; |
| 14 | + style_check()?; |
| 15 | + fmt()?; |
| 16 | + linkcheck(args)?; |
| 17 | + cargo_test()?; |
| 18 | + eprintln!("all tests passed!"); |
| 19 | + } |
| 20 | + Some("linkcheck") => linkcheck(args)?, |
| 21 | + Some("style-check") => style_check()?, |
| 22 | + Some("-h" | "--help") => eprintln!("valid options: {OPTIONS}"), |
| 23 | + Some(x) => { |
| 24 | + eprintln!("error: unknown command `{x}` (valid options: {OPTIONS})"); |
| 25 | + exit(1); |
| 26 | + } |
| 27 | + None => { |
| 28 | + eprintln!("error: specify a command (valid options: {OPTIONS})"); |
| 29 | + exit(1); |
| 30 | + } |
| 31 | + } |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | + |
| 35 | +fn mdbook_test() -> Result<()> { |
| 36 | + eprintln!("Testing inline code tests..."); |
| 37 | + let status = Command::new("mdbook") |
| 38 | + .arg("test") |
| 39 | + .status() |
| 40 | + .expect("mdbook should be installed"); |
| 41 | + if !status.success() { |
| 42 | + return Err("inline code tests failed".into()); |
| 43 | + } |
| 44 | + Ok(()) |
| 45 | +} |
| 46 | + |
| 47 | +fn style_check() -> Result<()> { |
| 48 | + eprintln!("Running style checks..."); |
| 49 | + let status = Command::new("cargo") |
| 50 | + .args(["run", "--manifest-path=style-check/Cargo.toml", "--", "src"]) |
| 51 | + .status() |
| 52 | + .expect("cargo should be installed"); |
| 53 | + if !status.success() { |
| 54 | + return Err("style check failed".into()); |
| 55 | + } |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
| 59 | +fn fmt() -> Result<()> { |
| 60 | + eprintln!("Checking code formatting..."); |
| 61 | + for dir in ["style-check", "mdbook-spec", "xtask"] { |
| 62 | + let status = Command::new("cargo") |
| 63 | + .args(["fmt", "--check"]) |
| 64 | + .current_dir(dir) |
| 65 | + .status() |
| 66 | + .expect("cargo should be installed"); |
| 67 | + if !status.success() { |
| 68 | + return Err(format!("fmt check failed for {dir}").into()); |
| 69 | + } |
| 70 | + } |
| 71 | + Ok(()) |
| 72 | +} |
| 73 | + |
| 74 | +fn cargo_test() -> Result<()> { |
| 75 | + eprintln!("Running cargo tests..."); |
| 76 | + let status = Command::new("cargo") |
| 77 | + .arg("test") |
| 78 | + .current_dir("mdbook-spec") |
| 79 | + .status() |
| 80 | + .expect("cargo should be installed"); |
| 81 | + if !status.success() { |
| 82 | + return Err("mdbook-spec test failed".into()); |
| 83 | + } |
| 84 | + Ok(()) |
| 85 | +} |
| 86 | + |
| 87 | +fn linkcheck(args: impl Iterator<Item = String>) -> Result<()> { |
| 88 | + eprintln!("Running linkcheck..."); |
| 89 | + let status = Command::new("curl") |
| 90 | + .args(["-sSLo", "linkcheck.sh", "https://raw.githubusercontent.com/rust-lang/rust/master/src/tools/linkchecker/linkcheck.sh"]) |
| 91 | + .status() |
| 92 | + .expect("curl should be installed"); |
| 93 | + if !status.success() { |
| 94 | + return Err("failed to fetch script from GitHub".into()); |
| 95 | + } |
| 96 | + |
| 97 | + let status = Command::new("sh") |
| 98 | + .args(["linkcheck.sh", "--all", "reference"]) |
| 99 | + .args(args) |
| 100 | + .status() |
| 101 | + .expect("sh should be installed"); |
| 102 | + if !status.success() { |
| 103 | + return Err("linkcheck failed".into()); |
| 104 | + } |
| 105 | + Ok(()) |
| 106 | +} |
0 commit comments