summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock21
-rw-r--r--Cargo.toml4
-rw-r--r--src/cli.rs18
-rw-r--r--src/main.rs39
4 files changed, 69 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e55b4c3..5e9d079 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -384,6 +384,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
384checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" 384checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc"
385dependencies = [ 385dependencies = [
386 "clap_builder", 386 "clap_builder",
387 "clap_derive",
387] 388]
388 389
389[[package]] 390[[package]]
@@ -400,6 +401,18 @@ dependencies = [
400] 401]
401 402
402[[package]] 403[[package]]
404name = "clap_derive"
405version = "4.5.13"
406source = "registry+https://github.com/rust-lang/crates.io-index"
407checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0"
408dependencies = [
409 "heck",
410 "proc-macro2",
411 "quote",
412 "syn",
413]
414
415[[package]]
403name = "clap_lex" 416name = "clap_lex"
404version = "0.7.2" 417version = "0.7.2"
405source = "registry+https://github.com/rust-lang/crates.io-index" 418source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1662,6 +1675,12 @@ dependencies = [
1662] 1675]
1663 1676
1664[[package]] 1677[[package]]
1678name = "heck"
1679version = "0.5.0"
1680source = "registry+https://github.com/rust-lang/crates.io-index"
1681checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
1682
1683[[package]]
1665name = "hex" 1684name = "hex"
1666version = "0.4.3" 1685version = "0.4.3"
1667source = "registry+https://github.com/rust-lang/crates.io-index" 1686source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2380,6 +2399,8 @@ name = "rsrclean"
2380version = "0.1.0" 2399version = "0.1.0"
2381dependencies = [ 2400dependencies = [
2382 "cargo", 2401 "cargo",
2402 "clap",
2403 "clap_derive",
2383] 2404]
2384 2405
2385[[package]] 2406[[package]]
diff --git a/Cargo.toml b/Cargo.toml
index 8d159ff..40dbdab 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,4 +4,6 @@ version = "0.1.0"
4edition = "2021" 4edition = "2021"
5 5
6[dependencies] 6[dependencies]
7cargo = "0.81.0" 7cargo = "0.81"
8clap = { version = "4.5", features = ["derive"] }
9clap_derive = { version = "4.5" }
diff --git a/src/cli.rs b/src/cli.rs
new file mode 100644
index 0000000..6b1a043
--- /dev/null
+++ b/src/cli.rs
@@ -0,0 +1,18 @@
1use std::path::PathBuf;
2
3use clap::Parser;
4
5#[derive(Parser, Clone)]
6#[command(author, version, about, long_about = None)]
7pub struct Args {
8 pub dir: Option<PathBuf>,
9
10 #[arg(short, long, default_value_t = 2)]
11 pub level: u8,
12
13 #[arg(short, long)]
14 pub doc: bool,
15
16 #[arg(long, default_value_t = true)]
17 pub dry_run: bool,
18}
diff --git a/src/main.rs b/src/main.rs
index cdc7d03..beac641 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,24 @@
1use std::path::Path; 1use std::path::{Path, PathBuf};
2 2
3use cargo::{core::Workspace, ops::{clean, CleanOptions}, util::{context::GlobalContext, interning::InternedString}, CargoResult}; 3use cargo::{
4 core::Workspace,
5 ops::{clean, CleanOptions},
6 util::{context::GlobalContext, interning::InternedString},
7 CargoResult,
8};
9use clap::Parser;
10
11use cli::Args;
12
13mod cli;
4 14
5fn main() { 15fn main() {
6 let paths = std::fs::read_dir("./").unwrap(); 16 let cli = Args::parse();
17
18 let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap();
7 for path in paths { 19 for path in paths {
8 let p = path.unwrap(); 20 let p = path.unwrap();
9 handle_path(&p.path(), 0, 2); 21 handle_path(&p.path(), 0, cli.clone());
10 } 22 }
11} 23}
12 24
@@ -14,24 +26,26 @@ fn is_cargo_toml(path: &Path) -> bool {
14 path.is_file() && (path.file_name().unwrap() == "Cargo.toml") 26 path.is_file() && (path.file_name().unwrap() == "Cargo.toml")
15} 27}
16 28
17fn handle_path(path: &Path, iter: u8, max_iter: u8) { 29fn handle_path(path: &Path, iter: u8, cli: Args) {
18 if is_cargo_toml(path) { 30 if is_cargo_toml(path) {
19 let abs_path = std::fs::canonicalize(path).unwrap(); 31 let abs_path = std::fs::canonicalize(path).unwrap();
20 println!("Clean: {}", abs_path.as_path().to_str().unwrap()); 32 println!("Clean: {}", abs_path.as_path().to_str().unwrap());
21 clean_project(abs_path.as_path()).unwrap(); 33 clean_project(abs_path.as_path(), cli).unwrap();
22 return; 34 return;
23 }; 35 };
24 if path.is_dir() { 36 if path.is_dir() {
25 if iter > max_iter { return; }; 37 if iter >= cli.level {
38 return;
39 };
26 let paths = std::fs::read_dir(path).unwrap(); 40 let paths = std::fs::read_dir(path).unwrap();
27 for path in paths { 41 for path in paths {
28 let p = path.unwrap(); 42 let p = path.unwrap();
29 handle_path(&p.path(), iter + 1, 1); 43 handle_path(&p.path(), iter + 1, cli.clone());
30 }; 44 }
31 } 45 }
32} 46}
33 47
34fn clean_project(path: &Path) -> CargoResult<()> { 48fn clean_project(path: &Path, cli: Args) -> CargoResult<()> {
35 let gctx = GlobalContext::default()?; 49 let gctx = GlobalContext::default()?;
36 50
37 let workspace = Workspace::new(path, &gctx)?; 51 let workspace = Workspace::new(path, &gctx)?;
@@ -42,9 +56,10 @@ fn clean_project(path: &Path) -> CargoResult<()> {
42 targets: vec![], 56 targets: vec![],
43 profile_specified: false, 57 profile_specified: false,
44 requested_profile: InternedString::new("dev"), 58 requested_profile: InternedString::new("dev"),
45 doc: false, 59 doc: cli.doc,
46 dry_run: true, 60 dry_run: cli.dry_run,
47 }; 61 };
48 62
63 // return Ok(());
49 clean(&workspace, &opts) 64 clean(&workspace, &opts)
50} 65}