From 0cf179e17ac60f72aba85e978379fb0957ae7aaa Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 14 Aug 2024 22:42:03 +0200 Subject: base working --- src/main.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..cdc7d03 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,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) +} -- cgit v1.2.3