diff options
-rw-r--r-- | Cargo.lock | 21 | ||||
-rw-r--r-- | Cargo.toml | 4 | ||||
-rw-r--r-- | src/cli.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 39 |
4 files changed, 69 insertions, 13 deletions
@@ -384,6 +384,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
384 | checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" | 384 | checksum = "11d8838454fda655dafd3accb2b6e2bea645b9e4078abe84a22ceb947235c5cc" |
385 | dependencies = [ | 385 | dependencies = [ |
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]] |
404 | name = "clap_derive" | ||
405 | version = "4.5.13" | ||
406 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
407 | checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" | ||
408 | dependencies = [ | ||
409 | "heck", | ||
410 | "proc-macro2", | ||
411 | "quote", | ||
412 | "syn", | ||
413 | ] | ||
414 | |||
415 | [[package]] | ||
403 | name = "clap_lex" | 416 | name = "clap_lex" |
404 | version = "0.7.2" | 417 | version = "0.7.2" |
405 | source = "registry+https://github.com/rust-lang/crates.io-index" | 418 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -1662,6 +1675,12 @@ dependencies = [ | |||
1662 | ] | 1675 | ] |
1663 | 1676 | ||
1664 | [[package]] | 1677 | [[package]] |
1678 | name = "heck" | ||
1679 | version = "0.5.0" | ||
1680 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
1681 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" | ||
1682 | |||
1683 | [[package]] | ||
1665 | name = "hex" | 1684 | name = "hex" |
1666 | version = "0.4.3" | 1685 | version = "0.4.3" |
1667 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1686 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -2380,6 +2399,8 @@ name = "rsrclean" | |||
2380 | version = "0.1.0" | 2399 | version = "0.1.0" |
2381 | dependencies = [ | 2400 | dependencies = [ |
2382 | "cargo", | 2401 | "cargo", |
2402 | "clap", | ||
2403 | "clap_derive", | ||
2383 | ] | 2404 | ] |
2384 | 2405 | ||
2385 | [[package]] | 2406 | [[package]] |
@@ -4,4 +4,6 @@ version = "0.1.0" | |||
4 | edition = "2021" | 4 | edition = "2021" |
5 | 5 | ||
6 | [dependencies] | 6 | [dependencies] |
7 | cargo = "0.81.0" | 7 | cargo = "0.81" |
8 | clap = { version = "4.5", features = ["derive"] } | ||
9 | clap_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 @@ | |||
1 | use std::path::PathBuf; | ||
2 | |||
3 | use clap::Parser; | ||
4 | |||
5 | #[derive(Parser, Clone)] | ||
6 | #[command(author, version, about, long_about = None)] | ||
7 | pub 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 @@ | |||
1 | use std::path::Path; | 1 | use std::path::{Path, PathBuf}; |
2 | 2 | ||
3 | use cargo::{core::Workspace, ops::{clean, CleanOptions}, util::{context::GlobalContext, interning::InternedString}, CargoResult}; | 3 | use cargo::{ |
4 | core::Workspace, | ||
5 | ops::{clean, CleanOptions}, | ||
6 | util::{context::GlobalContext, interning::InternedString}, | ||
7 | CargoResult, | ||
8 | }; | ||
9 | use clap::Parser; | ||
10 | |||
11 | use cli::Args; | ||
12 | |||
13 | mod cli; | ||
4 | 14 | ||
5 | fn main() { | 15 | fn 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 | ||
17 | fn handle_path(path: &Path, iter: u8, max_iter: u8) { | 29 | fn 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 | ||
34 | fn clean_project(path: &Path) -> CargoResult<()> { | 48 | fn 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 | } |