xtask/tasks/
clean.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4use crate::Xtask;
5use crate::shell::XtaskShell;
6
7/// Clean build artifacts beyond what `cargo clean` removes.
8///
9/// By default, removes: flowey-out, flowey-persist, .packages, and
10/// vmm_test_results. Use `--cargo` to also clean the cargo build output.
11#[derive(Debug, clap::Parser)]
12#[clap(about = "Clean build system artifacts (flowey, packages, test results)")]
13pub struct Clean {
14    /// Clean everything (equivalent to --cargo)
15    #[clap(long)]
16    all: bool,
17
18    /// Also run `cargo clean` to remove the target/ directory
19    #[clap(long)]
20    cargo: bool,
21
22    /// Print what would be removed without actually deleting anything
23    #[clap(long)]
24    dry_run: bool,
25}
26
27/// Directories that are always cleaned.
28const DEFAULT_DIRS: &[&str] = &[
29    "flowey-out",
30    "flowey-persist",
31    ".packages",
32    "vmm_test_results",
33];
34
35impl Xtask for Clean {
36    fn run(self, ctx: crate::XtaskCtx) -> anyhow::Result<()> {
37        let do_cargo = self.all || self.cargo;
38
39        let mut removed_anything = false;
40
41        // Remove default directories
42        for dir_name in DEFAULT_DIRS {
43            let path = ctx.root.join(dir_name);
44            if path.exists() {
45                if self.dry_run {
46                    println!("would remove {}", path.display());
47                } else {
48                    println!("removing {}", path.display());
49                    fs_err::remove_dir_all(&path)?;
50                }
51                removed_anything = true;
52            }
53        }
54
55        // Optionally run cargo clean
56        if do_cargo {
57            if self.dry_run {
58                println!("would run `cargo clean`");
59            } else {
60                println!("running `cargo clean`");
61                let sh = XtaskShell::new()?;
62                sh.cmd("cargo").arg("clean").run()?;
63            }
64            removed_anything = true;
65        }
66
67        if !removed_anything {
68            println!("nothing to clean");
69        }
70
71        Ok(())
72    }
73}