xtask\tasks\fmt/
verify_flowey.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use super::FmtCtx;
5use crate::shell::XtaskShell;
6use crate::tasks::fmt::FmtPass;
7use anyhow::Context;
8
9pub struct VerifyFlowey;
10
11impl FmtPass for VerifyFlowey {
12    fn run(self, ctx: FmtCtx) -> anyhow::Result<()> {
13        let FmtCtx {
14            ctx,
15            fix,
16            only_diffed: _,
17        } = ctx;
18
19        // need to go through all this rigamarole because `cargo --quiet
20        // xflowey regen` doesn't do what you'd hope it'd do
21        let cmd = {
22            let data = fs_err::read_to_string(ctx.root.join(".cargo/config.toml"))?;
23            let mut cmd = None;
24            for ln in data.lines() {
25                if let Some(ln) = ln.trim().strip_prefix(r#"xflowey = ""#) {
26                    let alias = ln
27                        .strip_suffix('"')
28                        .context("invalid .cargo/config.toml")?
29                        .split(' ')
30                        .map(|s| s.to_owned())
31                        .collect::<Vec<_>>();
32                    cmd = Some(alias);
33                }
34            }
35            cmd.context("could not find `xflowey` alias in .cargo/config.toml")?
36        };
37
38        let check = (!fix).then_some("--check");
39
40        let sh = XtaskShell::new()?;
41        sh.cmd("cargo")
42            .arg("--quiet")
43            .args(&cmd)
44            .args(["regen", "--quiet"])
45            .args(check)
46            .quiet()
47            .run()?;
48
49        Ok(())
50    }
51}