xtask/
tidy.rs

1use std::process::{Command, Stdio};
2
3use cargo_metadata::MetadataCommand;
4
5use crate::clippy::ClippyArgs;
6use crate::Backend;
7
8#[derive(Debug, clap::Args)]
9pub struct TidyArgs {
10    /// do not abort running if we encounter an error
11    /// while running the checks
12    #[clap(long = "keep-going")]
13    keep_going: bool,
14}
15
16impl TidyArgs {
17    pub(crate) fn run(&self) {
18        let mut success = true;
19        let metadata = MetadataCommand::default().exec().unwrap();
20        let mut command = Command::new("cargo");
21
22        command
23            .args(["fmt", "--all", "--check"])
24            .current_dir(&metadata.workspace_root);
25
26        println!("Check code formatting with `{command:?}`");
27        let status = command
28            .stdout(Stdio::inherit())
29            .stdin(Stdio::inherit())
30            .status()
31            .unwrap();
32
33        if !status.success() {
34            println!("Code format issues detected!");
35            println!("\t Run `cargo fmt --all` to format the source code");
36            if !self.keep_going {
37                std::process::exit(1);
38            } else {
39                success = false;
40            }
41        }
42
43        let mut command = Command::new("typos");
44
45        command.current_dir(metadata.workspace_root);
46        println!("Check source code for spelling mistakes with `{command:?}`");
47
48        let status = command
49            .stdout(Stdio::inherit())
50            .stdin(Stdio::inherit())
51            .status()
52            .unwrap();
53
54        if !status.success() {
55            println!("Spelling issues detected!");
56            println!("\t Run `typos -w` to address some of the issues");
57            if !self.keep_going {
58                std::process::exit(1);
59            } else {
60                success = false;
61            }
62        }
63
64        println!("Run clippy: ");
65        ClippyArgs {
66            backend: Backend::All,
67            keep_going: self.keep_going,
68            flags: Vec::new(),
69        }
70        .run();
71
72        println!();
73        if success {
74            println!("All checks were successful. Ready to submit the code!");
75        } else {
76            std::process::exit(1);
77        }
78    }
79}