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