summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: cdc7d037bf85c0b1121dc49ae0dd050b54732457 (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
use std::path::Path;

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

fn main() {
    let paths = std::fs::read_dir("./").unwrap();
    for path in paths {
        let p = path.unwrap();
        handle_path(&p.path(), 0, 2);
    }
}

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

fn handle_path(path: &Path, iter: u8, max_iter: u8) {
    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()).unwrap();
        return;
    };
    if path.is_dir() {
        if iter > max_iter { return; };
        let paths = std::fs::read_dir(path).unwrap();
        for path in paths {
            let p = path.unwrap();
            handle_path(&p.path(), iter + 1, 1);
        };
    }
}

fn clean_project(path: &Path) -> 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: false,
        dry_run: true,
    };

    clean(&workspace, &opts)
}