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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use anyhow::Result;
use xshell::Shell;

use crate::commands::{actionlint_cmd, cargo_cmd, prettier_cmd, typos_cmd};
use crate::utils::{find_files, project_root, to_relative_paths, verbose_cd};
use crate::Config;

pub fn everything(config: &Config) -> Result<()> {
    spelling(config)?; // affects all file types; run this first
    github_actions(config)?;
    markdown(config)?;
    rust(config)?;
    Ok(())
}

pub fn spelling(config: &Config) -> Result<()> {
    let sh = Shell::new()?;
    verbose_cd(&sh, project_root());

    let cmd_option = typos_cmd(config, &sh);
    if let Some(cmd) = cmd_option {
        let args = vec!["--write-changes"];
        cmd.args(args).run()?;
    }

    Ok(())
}

pub fn github_actions(config: &Config) -> Result<()> {
    lint_workflows(config)?;
    Ok(())
}

pub fn markdown(config: &Config) -> Result<()> {
    let sh = Shell::new()?;
    verbose_cd(&sh, project_root());

    let cmd_option = prettier_cmd(config, &sh);
    if let Some(cmd) = cmd_option {
        let args = vec!["--prose-wrap", "always", "--write"];
        let markdown_files = find_files(sh.current_dir(), "md")?;
        let relative_paths = to_relative_paths(markdown_files, sh.current_dir());
        cmd.args(args).args(relative_paths).run()?;
    }

    Ok(())
}

pub fn rust(config: &Config) -> Result<()> {
    lint_rust(config)?;
    format_rust(config)?;
    Ok(())
}

fn lint_workflows(config: &Config) -> Result<()> {
    let sh = Shell::new()?;
    verbose_cd(&sh, project_root());

    let cmd_option = actionlint_cmd(config, &sh);
    if let Some(cmd) = cmd_option {
        cmd.run()?;
    }

    Ok(())
}

fn lint_rust(config: &Config) -> Result<()> {
    let sh = Shell::new()?;
    verbose_cd(&sh, project_root());

    let cmd_option = cargo_cmd(config, &sh);
    if let Some(_cmd) = cmd_option {
        let args = vec!["fix", "--allow-no-vcs", "--all-targets", "--edition-idioms"];
        cargo_cmd(config, &sh).unwrap().args(args).run()?;

        let args = vec!["clippy", "--fix", "--allow-no-vcs", "--all-targets"];
        cargo_cmd(config, &sh).unwrap().args(args).run()?;

        let args = vec!["clippy", "--all-targets", "--", "-D", "warnings"];
        cargo_cmd(config, &sh).unwrap().args(args).run()?;
    }

    Ok(())
}

fn format_rust(config: &Config) -> Result<()> {
    let sh = Shell::new()?;
    verbose_cd(&sh, project_root());

    let cmd_option = cargo_cmd(config, &sh);
    if let Some(cmd) = cmd_option {
        let args = vec!["+nightly", "fmt"];
        cmd.args(args).run()?;
    }

    Ok(())
}