From 137346307248bc9e327847e549c3d6c24b3b11f3 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Sat, 17 Aug 2024 00:14:10 +0200 Subject: add external cargo --- src/cargo.rs | 9 +++++++++ src/cargo/external.rs | 19 ++++++++++++++++++ src/cargo/internal.rs | 28 ++++++++++++++++++++++++++ src/clean.rs | 45 +++++++++++++++++++++++++++++++++++++++++ src/cli.rs | 6 +++++- src/main.rs | 55 +++++---------------------------------------------- src/spawn_process.rs | 0 7 files changed, 111 insertions(+), 51 deletions(-) create mode 100644 src/cargo.rs create mode 100644 src/cargo/external.rs create mode 100644 src/cargo/internal.rs create mode 100644 src/clean.rs create mode 100644 src/spawn_process.rs (limited to 'src') 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 @@ +#[cfg(feature = "int-cargo")] +pub mod internal; +#[cfg(feature = "int-cargo")] +pub use internal::clean_int; + +#[cfg(feature = "ext-cargo")] +mod external; +#[cfg(feature = "ext-cargo")] +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 @@ +use std::{path::Path, process::Command}; + +use crate::cli::Args; + +pub fn clean_ext(path: &Path, cli: &Args) { + let mut args = vec!["clean", "--manifest-path", path.to_str().unwrap()]; + if cli.dry_run { + args.push("--dry-run"); + } + if cli.doc { + args.push("--doc"); + } + Command::new("cargo") + .args(args) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); +} 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 @@ +use std::path::Path; + +use cargo::{ + core::Workspace, + ops::{clean, CleanOptions}, + util::{context::GlobalContext, interning::InternedString}, + CargoResult, +}; + +use crate::cli::Args; + +pub fn clean_int(path: &Path, cli: &Args) -> 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: cli.doc, + dry_run: cli.dry_run, + }; + + clean(&workspace, &opts) +} 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 @@ +use std::path::Path; + +use crate::cli::Args; + +fn is_cargo_toml(path: &Path) -> bool { + path.is_file() && (path.file_name().unwrap() == "Cargo.toml") +} + +pub fn handle_path(path: &Path, iter: u8, cli: &Args) { + if is_cargo_toml(path) { + let abs_path = std::fs::canonicalize(path).unwrap(); + println!("Clean: {}", abs_path.as_path().to_str().unwrap()); + + #[cfg(all(feature = "int-cargo", feature = "ext-cargo"))] + { + if cli.ext_cargo { + crate::cargo::clean_ext(abs_path.as_path(), cli); + } else { + crate::cargo::clean_int(abs_path.as_path(), cli).unwrap(); + }; + } + + #[cfg(all(feature = "int-cargo", not(feature = "ext-cargo")))] + { + crate::cargo::clean_int(abs_path.as_path(), cli).unwrap(); + } + + #[cfg(all(feature = "ext-cargo", not(feature = "int-cargo")))] + { + crate::cargo::clean_ext(abs_path.as_path(), cli); + } + + return; + }; + if path.is_dir() { + if iter >= cli.level { + return; + }; + let paths = std::fs::read_dir(path).unwrap(); + for path in paths { + let p = path.unwrap(); + handle_path(&p.path(), iter + 1, cli); + } + } +} 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 { #[arg(short, long)] pub doc: bool, - #[arg(long, default_value_t = true)] + #[arg(long)] pub dry_run: bool, + + #[cfg(all(feature = "ext-cargo", feature = "int-cargo"))] + #[arg(long)] + pub ext_cargo: bool, } 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 @@ -use std::path::{Path, PathBuf}; +use std::path::PathBuf; -use cargo::{ - core::Workspace, - ops::{clean, CleanOptions}, - util::{context::GlobalContext, interning::InternedString}, - CargoResult, -}; use clap::Parser; +use clean::handle_path; use cli::Args; +mod cargo; +mod clean; mod cli; fn main() { @@ -18,48 +15,6 @@ fn main() { let paths = std::fs::read_dir(cli.clone().dir.unwrap_or(PathBuf::from("./"))).unwrap(); for path in paths { let p = path.unwrap(); - handle_path(&p.path(), 0, cli.clone()); + handle_path(&p.path(), 0, &cli); } } - -fn is_cargo_toml(path: &Path) -> bool { - path.is_file() && (path.file_name().unwrap() == "Cargo.toml") -} - -fn handle_path(path: &Path, iter: u8, cli: Args) { - 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(), cli).unwrap(); - return; - }; - if path.is_dir() { - if iter >= cli.level { - return; - }; - let paths = std::fs::read_dir(path).unwrap(); - for path in paths { - let p = path.unwrap(); - handle_path(&p.path(), iter + 1, cli.clone()); - } - } -} - -fn clean_project(path: &Path, cli: Args) -> 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: cli.doc, - dry_run: cli.dry_run, - }; - - // return Ok(()); - clean(&workspace, &opts) -} diff --git a/src/spawn_process.rs b/src/spawn_process.rs new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3