summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: beac6416198a3b577e9d605f6db6af11de71e0e5 (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
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
use std::path::{Path, PathBuf};

use cargo::{
    core::Workspace,
    ops::{clean, CleanOptions},
    util::{context::GlobalContext, interning::InternedString},
    CargoResult,
};
use clap::Parser;

use cli::Args;

mod cli;

fn main() {
    let cli = Args::parse();

    let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap();
    for path in paths {
        let p = path.unwrap();
        handle_path(&p.path(), 0, cli.clone());
    }
}

fn is_cargo_toml(path: &Path) -> bool {
    path.is_file() && (path.file_name().unwrap() == "Cargo.toml")
}

fn handle_path(path: &Path, iter: u8, cli: Args) {
    if is_cargo_toml(path) {
        let abs_path = std::fs::canonicalize(path).unwrap();
        println!("Clean: {}", abs_path.as_path().to_str().unwrap());
        clean_project(abs_path.as_path(), cli).unwrap();
        return;
    };
    if path.is_dir() {
        if iter >= cli.level {
            return;
        };
        let paths = std::fs::read_dir(path).unwrap();
        for path in paths {
            let p = path.unwrap();
            handle_path(&p.path(), iter + 1, cli.clone());
        }
    }
}

fn clean_project(path: &Path, cli: Args) -> CargoResult<()> {
    let gctx = GlobalContext::default()?;

    let workspace = Workspace::new(path, &gctx)?;

    let opts = CleanOptions {
        gctx: &gctx,
        spec: vec![],
        targets: vec![],
        profile_specified: false,
        requested_profile: InternedString::new("dev"),
        doc: cli.doc,
        dry_run: cli.dry_run,
    };

    // return Ok(());
    clean(&workspace, &opts)
}