diff options
author | fxqnlr <[email protected]> | 2024-08-17 00:14:10 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-08-17 00:14:10 +0200 |
commit | 137346307248bc9e327847e549c3d6c24b3b11f3 (patch) | |
tree | 94d73000aa881c912f93eccbfb9e0d0a7c5857ea | |
parent | 371a77a994aeb0beae53f24a0edbf99d70133c33 (diff) | |
download | rsrclean-137346307248bc9e327847e549c3d6c24b3b11f3.tar rsrclean-137346307248bc9e327847e549c3d6c24b3b11f3.tar.gz rsrclean-137346307248bc9e327847e549c3d6c24b3b11f3.zip |
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 11 | ||||
-rw-r--r-- | src/cargo.rs | 9 | ||||
-rw-r--r-- | src/cargo/external.rs | 19 | ||||
-rw-r--r-- | src/cargo/internal.rs | 28 | ||||
-rw-r--r-- | src/clean.rs | 45 | ||||
-rw-r--r-- | src/cli.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 55 | ||||
-rw-r--r-- | src/spawn_process.rs | 0 |
9 files changed, 120 insertions, 55 deletions
@@ -2396,7 +2396,7 @@ dependencies = [ | |||
2396 | 2396 | ||
2397 | [[package]] | 2397 | [[package]] |
2398 | name = "rsrclean" | 2398 | name = "rsrclean" |
2399 | version = "0.1.0" | 2399 | version = "0.2.0" |
2400 | dependencies = [ | 2400 | dependencies = [ |
2401 | "cargo", | 2401 | "cargo", |
2402 | "clap", | 2402 | "clap", |
@@ -1,9 +1,14 @@ | |||
1 | [package] | 1 | [package] |
2 | name = "rsrclean" | 2 | name = "rsrclean" |
3 | version = "0.1.0" | 3 | version = "0.2.0" |
4 | edition = "2021" | 4 | edition = "2021" |
5 | 5 | ||
6 | [features] | ||
7 | default = ["ext-cargo"] | ||
8 | ext-cargo = [] | ||
9 | int-cargo = ["dep:cargo"] | ||
10 | |||
6 | [dependencies] | 11 | [dependencies] |
7 | cargo = "0.81" | 12 | cargo = { version = "0.81", optional = true } |
8 | clap = { version = "4.5", features = ["derive"] } | 13 | clap = { version = "4.5", features = ["derive"] } |
9 | clap_derive = { version = "4.5" } | 14 | clap_derive = "4.5" |
diff --git a/src/cargo.rs b/src/cargo.rs new file mode 100644 index 0000000..7c89045 --- /dev/null +++ b/src/cargo.rs | |||
@@ -0,0 +1,9 @@ | |||
1 | #[cfg(feature = "int-cargo")] | ||
2 | pub mod internal; | ||
3 | #[cfg(feature = "int-cargo")] | ||
4 | pub use internal::clean_int; | ||
5 | |||
6 | #[cfg(feature = "ext-cargo")] | ||
7 | mod external; | ||
8 | #[cfg(feature = "ext-cargo")] | ||
9 | pub use external::clean_ext; | ||
diff --git a/src/cargo/external.rs b/src/cargo/external.rs new file mode 100644 index 0000000..c956b68 --- /dev/null +++ b/src/cargo/external.rs | |||
@@ -0,0 +1,19 @@ | |||
1 | use std::{path::Path, process::Command}; | ||
2 | |||
3 | use crate::cli::Args; | ||
4 | |||
5 | pub fn clean_ext(path: &Path, cli: &Args) { | ||
6 | let mut args = vec!["clean", "--manifest-path", path.to_str().unwrap()]; | ||
7 | if cli.dry_run { | ||
8 | args.push("--dry-run"); | ||
9 | } | ||
10 | if cli.doc { | ||
11 | args.push("--doc"); | ||
12 | } | ||
13 | Command::new("cargo") | ||
14 | .args(args) | ||
15 | .spawn() | ||
16 | .unwrap() | ||
17 | .wait_with_output() | ||
18 | .unwrap(); | ||
19 | } | ||
diff --git a/src/cargo/internal.rs b/src/cargo/internal.rs new file mode 100644 index 0000000..b3e44e4 --- /dev/null +++ b/src/cargo/internal.rs | |||
@@ -0,0 +1,28 @@ | |||
1 | use std::path::Path; | ||
2 | |||
3 | use cargo::{ | ||
4 | core::Workspace, | ||
5 | ops::{clean, CleanOptions}, | ||
6 | util::{context::GlobalContext, interning::InternedString}, | ||
7 | CargoResult, | ||
8 | }; | ||
9 | |||
10 | use crate::cli::Args; | ||
11 | |||
12 | pub fn clean_int(path: &Path, cli: &Args) -> CargoResult<()> { | ||
13 | let gctx = GlobalContext::default()?; | ||
14 | |||
15 | let workspace = Workspace::new(path, &gctx)?; | ||
16 | |||
17 | let opts = CleanOptions { | ||
18 | gctx: &gctx, | ||
19 | spec: vec![], | ||
20 | targets: vec![], | ||
21 | profile_specified: false, | ||
22 | requested_profile: InternedString::new("dev"), | ||
23 | doc: cli.doc, | ||
24 | dry_run: cli.dry_run, | ||
25 | }; | ||
26 | |||
27 | clean(&workspace, &opts) | ||
28 | } | ||
diff --git a/src/clean.rs b/src/clean.rs new file mode 100644 index 0000000..a34a912 --- /dev/null +++ b/src/clean.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | use std::path::Path; | ||
2 | |||
3 | use crate::cli::Args; | ||
4 | |||
5 | fn is_cargo_toml(path: &Path) -> bool { | ||
6 | path.is_file() && (path.file_name().unwrap() == "Cargo.toml") | ||
7 | } | ||
8 | |||
9 | pub fn handle_path(path: &Path, iter: u8, cli: &Args) { | ||
10 | if is_cargo_toml(path) { | ||
11 | let abs_path = std::fs::canonicalize(path).unwrap(); | ||
12 | println!("Clean: {}", abs_path.as_path().to_str().unwrap()); | ||
13 | |||
14 | #[cfg(all(feature = "int-cargo", feature = "ext-cargo"))] | ||
15 | { | ||
16 | if cli.ext_cargo { | ||
17 | crate::cargo::clean_ext(abs_path.as_path(), cli); | ||
18 | } else { | ||
19 | crate::cargo::clean_int(abs_path.as_path(), cli).unwrap(); | ||
20 | }; | ||
21 | } | ||
22 | |||
23 | #[cfg(all(feature = "int-cargo", not(feature = "ext-cargo")))] | ||
24 | { | ||
25 | crate::cargo::clean_int(abs_path.as_path(), cli).unwrap(); | ||
26 | } | ||
27 | |||
28 | #[cfg(all(feature = "ext-cargo", not(feature = "int-cargo")))] | ||
29 | { | ||
30 | crate::cargo::clean_ext(abs_path.as_path(), cli); | ||
31 | } | ||
32 | |||
33 | return; | ||
34 | }; | ||
35 | if path.is_dir() { | ||
36 | if iter >= cli.level { | ||
37 | return; | ||
38 | }; | ||
39 | let paths = std::fs::read_dir(path).unwrap(); | ||
40 | for path in paths { | ||
41 | let p = path.unwrap(); | ||
42 | handle_path(&p.path(), iter + 1, cli); | ||
43 | } | ||
44 | } | ||
45 | } | ||
@@ -13,6 +13,10 @@ pub struct Args { | |||
13 | #[arg(short, long)] | 13 | #[arg(short, long)] |
14 | pub doc: bool, | 14 | pub doc: bool, |
15 | 15 | ||
16 | #[arg(long, default_value_t = true)] | 16 | #[arg(long)] |
17 | pub dry_run: bool, | 17 | pub dry_run: bool, |
18 | |||
19 | #[cfg(all(feature = "ext-cargo", feature = "int-cargo"))] | ||
20 | #[arg(long)] | ||
21 | pub ext_cargo: bool, | ||
18 | } | 22 | } |
diff --git a/src/main.rs b/src/main.rs index beac641..6bcf5b5 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,15 +1,12 @@ | |||
1 | use std::path::{Path, PathBuf}; | 1 | use std::path::PathBuf; |
2 | 2 | ||
3 | use cargo::{ | ||
4 | core::Workspace, | ||
5 | ops::{clean, CleanOptions}, | ||
6 | util::{context::GlobalContext, interning::InternedString}, | ||
7 | CargoResult, | ||
8 | }; | ||
9 | use clap::Parser; | 3 | use clap::Parser; |
10 | 4 | ||
5 | use clean::handle_path; | ||
11 | use cli::Args; | 6 | use cli::Args; |
12 | 7 | ||
8 | mod cargo; | ||
9 | mod clean; | ||
13 | mod cli; | 10 | mod cli; |
14 | 11 | ||
15 | fn main() { | 12 | fn main() { |
@@ -18,48 +15,6 @@ fn main() { | |||
18 | let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap(); | 15 | let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap(); |
19 | for path in paths { | 16 | for path in paths { |
20 | let p = path.unwrap(); | 17 | let p = path.unwrap(); |
21 | handle_path(&p.path(), 0, cli.clone()); | 18 | handle_path(&p.path(), 0, &cli); |
22 | } | 19 | } |
23 | } | 20 | } |
24 | |||
25 | fn is_cargo_toml(path: &Path) -> bool { | ||
26 | path.is_file() && (path.file_name().unwrap() == "Cargo.toml") | ||
27 | } | ||
28 | |||
29 | fn handle_path(path: &Path, iter: u8, cli: Args) { | ||
30 | if is_cargo_toml(path) { | ||
31 | let abs_path = std::fs::canonicalize(path).unwrap(); | ||
32 | println!("Clean: {}", abs_path.as_path().to_str().unwrap()); | ||
33 | clean_project(abs_path.as_path(), cli).unwrap(); | ||
34 | return; | ||
35 | }; | ||
36 | if path.is_dir() { | ||
37 | if iter >= cli.level { | ||
38 | return; | ||
39 | }; | ||
40 | let paths = std::fs::read_dir(path).unwrap(); | ||
41 | for path in paths { | ||
42 | let p = path.unwrap(); | ||
43 | handle_path(&p.path(), iter + 1, cli.clone()); | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | |||
48 | fn clean_project(path: &Path, cli: Args) -> CargoResult<()> { | ||
49 | let gctx = GlobalContext::default()?; | ||
50 | |||
51 | let workspace = Workspace::new(path, &gctx)?; | ||
52 | |||
53 | let opts = CleanOptions { | ||
54 | gctx: &gctx, | ||
55 | spec: vec![], | ||
56 | targets: vec![], | ||
57 | profile_specified: false, | ||
58 | requested_profile: InternedString::new("dev"), | ||
59 | doc: cli.doc, | ||
60 | dry_run: cli.dry_run, | ||
61 | }; | ||
62 | |||
63 | // return Ok(()); | ||
64 | clean(&workspace, &opts) | ||
65 | } | ||
diff --git a/src/spawn_process.rs b/src/spawn_process.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/spawn_process.rs | |||