summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfxqnlr <[email protected]>2024-08-17 00:14:10 +0200
committerfxqnlr <[email protected]>2024-08-17 00:14:10 +0200
commit137346307248bc9e327847e549c3d6c24b3b11f3 (patch)
tree94d73000aa881c912f93eccbfb9e0d0a7c5857ea
parent371a77a994aeb0beae53f24a0edbf99d70133c33 (diff)
downloadrsrclean-137346307248bc9e327847e549c3d6c24b3b11f3.tar
rsrclean-137346307248bc9e327847e549c3d6c24b3b11f3.tar.gz
rsrclean-137346307248bc9e327847e549c3d6c24b3b11f3.zip
add external cargoHEADmain
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml11
-rw-r--r--src/cargo.rs9
-rw-r--r--src/cargo/external.rs19
-rw-r--r--src/cargo/internal.rs28
-rw-r--r--src/clean.rs45
-rw-r--r--src/cli.rs6
-rw-r--r--src/main.rs55
-rw-r--r--src/spawn_process.rs0
9 files changed, 120 insertions, 55 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5e9d079..b6bbda4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2396,7 +2396,7 @@ dependencies = [
2396 2396
2397[[package]] 2397[[package]]
2398name = "rsrclean" 2398name = "rsrclean"
2399version = "0.1.0" 2399version = "0.2.0"
2400dependencies = [ 2400dependencies = [
2401 "cargo", 2401 "cargo",
2402 "clap", 2402 "clap",
diff --git a/Cargo.toml b/Cargo.toml
index 40dbdab..5255f1f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,9 +1,14 @@
1[package] 1[package]
2name = "rsrclean" 2name = "rsrclean"
3version = "0.1.0" 3version = "0.2.0"
4edition = "2021" 4edition = "2021"
5 5
6[features]
7default = ["ext-cargo"]
8ext-cargo = []
9int-cargo = ["dep:cargo"]
10
6[dependencies] 11[dependencies]
7cargo = "0.81" 12cargo = { version = "0.81", optional = true }
8clap = { version = "4.5", features = ["derive"] } 13clap = { version = "4.5", features = ["derive"] }
9clap_derive = { version = "4.5" } 14clap_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")]
2pub mod internal;
3#[cfg(feature = "int-cargo")]
4pub use internal::clean_int;
5
6#[cfg(feature = "ext-cargo")]
7mod external;
8#[cfg(feature = "ext-cargo")]
9pub 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 @@
1use std::{path::Path, process::Command};
2
3use crate::cli::Args;
4
5pub 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 @@
1use std::path::Path;
2
3use cargo::{
4 core::Workspace,
5 ops::{clean, CleanOptions},
6 util::{context::GlobalContext, interning::InternedString},
7 CargoResult,
8};
9
10use crate::cli::Args;
11
12pub 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 @@
1use std::path::Path;
2
3use crate::cli::Args;
4
5fn is_cargo_toml(path: &Path) -> bool {
6 path.is_file() && (path.file_name().unwrap() == "Cargo.toml")
7}
8
9pub 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}
diff --git a/src/cli.rs b/src/cli.rs
index 6b1a043..b0ec61b 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -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 @@
1use std::path::{Path, PathBuf}; 1use std::path::PathBuf;
2 2
3use cargo::{
4 core::Workspace,
5 ops::{clean, CleanOptions},
6 util::{context::GlobalContext, interning::InternedString},
7 CargoResult,
8};
9use clap::Parser; 3use clap::Parser;
10 4
5use clean::handle_path;
11use cli::Args; 6use cli::Args;
12 7
8mod cargo;
9mod clean;
13mod cli; 10mod cli;
14 11
15fn main() { 12fn 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
25fn is_cargo_toml(path: &Path) -> bool {
26 path.is_file() && (path.file_name().unwrap() == "Cargo.toml")
27}
28
29fn 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
48fn 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