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)
}