Skip to main content

xtask/
fixup.rs

1use anyhow::Result;
2use xshell::Shell;
3
4use crate::Config;
5use crate::commands::{actionlint_cmd, cargo_cmd, prettier_cmd, typos_cmd};
6use crate::utils::{find_files, project_root, to_relative_paths, verbose_cd};
7
8pub fn everything(config: &Config) -> Result<()> {
9    spelling(config)?; // affects all file types; run this first
10    github_actions(config)?;
11    markdown(config)?;
12    rust(config)?;
13    Ok(())
14}
15
16pub fn spelling(config: &Config) -> Result<()> {
17    let sh = Shell::new()?;
18    verbose_cd(&sh, project_root());
19
20    let cmd_option = typos_cmd(config, &sh);
21    if let Some(cmd) = cmd_option {
22        let args = vec!["--write-changes"];
23        cmd.args(args).run()?;
24    }
25
26    Ok(())
27}
28
29pub fn github_actions(config: &Config) -> Result<()> {
30    lint_workflows(config)?;
31    Ok(())
32}
33
34pub fn markdown(config: &Config) -> Result<()> {
35    let sh = Shell::new()?;
36    verbose_cd(&sh, project_root());
37
38    let cmd_option = prettier_cmd(config, &sh);
39    if let Some(cmd) = cmd_option {
40        let args = vec!["--prose-wrap", "always", "--write"];
41        let markdown_files = find_files(sh.current_dir(), "md")?;
42        let relative_paths = to_relative_paths(markdown_files, sh.current_dir());
43        cmd.args(args).args(relative_paths).run()?;
44    }
45
46    Ok(())
47}
48
49pub fn rust(config: &Config) -> Result<()> {
50    lint_rust(config)?;
51    format_rust(config)?;
52    Ok(())
53}
54
55fn lint_workflows(config: &Config) -> Result<()> {
56    let sh = Shell::new()?;
57    verbose_cd(&sh, project_root());
58
59    let cmd_option = actionlint_cmd(config, &sh);
60    if let Some(cmd) = cmd_option {
61        cmd.run()?;
62    }
63
64    Ok(())
65}
66
67fn lint_rust(config: &Config) -> Result<()> {
68    let sh = Shell::new()?;
69    verbose_cd(&sh, project_root());
70
71    let cmd_option = cargo_cmd(config, &sh);
72    if let Some(_cmd) = cmd_option {
73        let args = vec!["fix", "--allow-no-vcs", "--all-targets", "--edition-idioms"];
74        cargo_cmd(config, &sh).unwrap().args(args).run()?;
75
76        let args = vec!["clippy", "--fix", "--allow-no-vcs", "--all-targets"];
77        cargo_cmd(config, &sh).unwrap().args(args).run()?;
78
79        let args = vec!["clippy", "--all-targets", "--", "-D", "warnings"];
80        cargo_cmd(config, &sh).unwrap().args(args).run()?;
81    }
82
83    Ok(())
84}
85
86fn format_rust(config: &Config) -> Result<()> {
87    let sh = Shell::new()?;
88    verbose_cd(&sh, project_root());
89
90    let cmd_option = cargo_cmd(config, &sh);
91    if let Some(cmd) = cmd_option {
92        let args = vec!["+nightly", "fmt"];
93        cmd.args(args).run()?;
94    }
95
96    Ok(())
97}