aboutsummaryrefslogtreecommitdiffstats
path: root/sl0ppij/xtask/src/format.rs
blob: 88e858cd64b920dc3103fe67a573ef8375a844ae (plain) (blame)
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
//! Handle running `cargo fmt` on the sources.
use crate::{flags, WorkspaceMember};
use anyhow::Context;
use std::path::{Path, PathBuf};
use xshell::{cmd, Shell};

pub fn format(sh: &Shell, flags: flags::Format) -> anyhow::Result<()> {
    let _pd = sh.push_dir(crate::project_root());

    let cargo = check_rustfmt()
        .and_then(|_| crate::cargo())
        .context("failed to run task 'format'")?;

    for WorkspaceMember { crate_name, .. } in crate::workspace_members().iter() {
        let _pd = sh.push_dir(Path::new(crate_name));
        // Tell the user where we are now
        println!();
        let msg = format!(">> Formatting '{crate_name}'");
        crate::status(&msg);
        println!("{}", msg);

        let mut cmd = cmd!(sh, "{cargo} fmt");
        if flags.check {
            cmd = cmd.arg("--check");
        }
        cmd.run()
            .with_context(|| format!("Failed to format '{crate_name}'"))?;
    }
    Ok(())
}

fn check_rustfmt() -> anyhow::Result<PathBuf> {
    which::which("rustfmt").context(
        "Couldn't find 'rustfmt' executable. Please install it with `cargo install rustfmt`",
    )
}