about summary refs log tree commit diff
path: root/compiler/rustc_codegen_gcc/build_system/src/fmt.rs
blob: 91535f217e35153dc04c3904c06388712e912101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::ffi::OsStr;
use std::path::Path;

use crate::utils::{run_command_with_output, walk_dir};

fn show_usage() {
    println!(
        r#"
`fmt` command help:

    --check                : Pass `--check` argument to `cargo fmt` commands
    --help                 : Show this help"#
    );
}

pub fn run() -> Result<(), String> {
    let mut check = false;
    // We skip binary name and the `info` command.
    let args = std::env::args().skip(2);
    for arg in args {
        match arg.as_str() {
            "--help" => {
                show_usage();
                return Ok(());
            }
            "--check" => check = true,
            _ => return Err(format!("Unknown option {arg}")),
        }
    }

    let cmd: &[&dyn AsRef<OsStr>] =
        if check { &[&"cargo", &"fmt", &"--check"] } else { &[&"cargo", &"fmt"] };

    run_command_with_output(cmd, Some(Path::new(".")))?;
    run_command_with_output(cmd, Some(Path::new("build_system")))?;

    run_rustfmt_recursively("tests/run", check)
}

fn run_rustfmt_recursively<P>(dir: P, check: bool) -> Result<(), String>
where
    P: AsRef<Path>,
{
    walk_dir(
        dir,
        &mut |dir| run_rustfmt_recursively(dir, check),
        &mut |file_path| {
            if file_path.extension().filter(|ext| ext == &OsStr::new("rs")).is_some() {
                let rustfmt_cmd: &[&dyn AsRef<OsStr>] = if check {
                    &[&"rustfmt", &"--check", &file_path]
                } else {
                    &[&"rustfmt", &file_path]
                };

                run_command_with_output(rustfmt_cmd, Some(Path::new(".")))
            } else {
                Ok(())
            }
        },
        true,
    )
}