Skip to content

Commit

Permalink
Adding the possibility to specify and configure the active sweeper
Browse files Browse the repository at this point in the history
  • Loading branch information
WolverinDEV committed Jul 1, 2024
1 parent fc0cc9d commit 3b3ae85
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 31 deletions.
94 changes: 94 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::{
error::Error,
path::PathBuf,
};

use bclean::{
sweeper::{
CMakeSweeper,
NodeSweeper,
RustSweeper,
},
Sweeper,
};
use clap::{
Parser,
ValueEnum,
};

#[derive(Clone, ValueEnum, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum ArgSweeper {
CMake,
Node,
Rust,
}

impl ArgSweeper {
pub fn parse_args(
value: &str,
) -> Result<(ArgSweeper, Option<String>), Box<dyn Error + Send + Sync>> {
let result = if let Some((sweeper, options)) = value.split_once("=") {
(
ArgSweeper::from_str(sweeper, true)?,
Some(options.to_string()),
)
} else {
(ArgSweeper::from_str(value, true)?, None)
};

Ok(result)
}

pub fn default_configuration() -> Vec<(ArgSweeper, Option<String>)> {
vec![
(ArgSweeper::Node, None),
(ArgSweeper::Rust, None),
(ArgSweeper::CMake, None),
]
}

pub fn create_from_options(&self, _options: Option<&str>) -> anyhow::Result<Box<dyn Sweeper>> {
let result: Box<dyn Sweeper> = match self {
Self::CMake => Box::new(CMakeSweeper::new()),
Self::Node => Box::new(NodeSweeper::new()),
Self::Rust => Box::new(RustSweeper::new()),
};

Ok(result)
}
}

/// Automate the cleanup of left over build files
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
/// Specify the root directory where bclean should search for sweepable targets.
/// Note: This can be a relative path.
#[arg(short, long, verbatim_doc_comment)]
pub root: Option<PathBuf>,

/// Display the log in the terminal as a split screen.
#[arg(long)]
pub ui_logger: bool,

/// Do not actually sweep anything. Just simulate it.
#[arg(short, long)]
pub dry_run: bool,

/// Specify a list of sweeper which should be activated.
/// Additionally you can specify sweeper individual arguments.
///
/// Available sweeper:
/// - c-make
/// - rust
/// - node
///
/// Example:
/// -s rust -s "cmake=dirname=cmake,build,dist"
#[arg(value_parser = ArgSweeper::parse_args, short, long, verbatim_doc_comment)]
pub sweeper: Vec<(ArgSweeper, Option<String>)>,

/// Do not apply the default sweeper
#[arg(long)]
pub sweeper_no_defaults: bool,
}
47 changes: 16 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
use std::{
self,
env,
path::PathBuf,
};

use args::{
ArgSweeper,
Args,
};
use bclean::{
sweeper::{
CMakeSweeper,
NodeSweeper,
RustSweeper,
},
self,
CrewOptions,
SweeperCrew,
};
use clap::{
command,
Parser,
};
use clap::Parser;
use crossterm::{
self,
event::{
Expand All @@ -40,28 +36,11 @@ use ui::{
TuiAppLoggerWidget,
};

mod args;
mod term;
mod ui;
mod utils;

/// Automate the cleanup of left over build files
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// Specify the root directory where bclean should search for sweepable targets.
/// Note: This can be a relative path.
#[arg(short, long, verbatim_doc_comment)]
root: Option<PathBuf>,

/// Display the log in the terminal as a split screen.
#[arg(long)]
ui_logger: bool,

/// Do not actually sweep anything. Just simulate it.
#[arg(short, long)]
dry_run: bool,
}

fn main() -> anyhow::Result<()> {
let args = Args::parse();

Expand Down Expand Up @@ -89,10 +68,16 @@ fn main() -> anyhow::Result<()> {

let crew = {
let mut crew = SweeperCrew::new();
crew.register(RustSweeper::new());
crew.register(NodeSweeper::new());
crew.register(CMakeSweeper::new());

let mut sweepers = args.sweeper.clone();
if !args.sweeper_no_defaults {
sweepers.extend(ArgSweeper::default_configuration());
}

for (sweeper, options) in sweepers {
log::info!("Register sweeper {:?} ({:?})", sweeper, options);
crew.register_boxed(sweeper.create_from_options(options.as_ref().map(String::as_str))?);
}
crew
};

Expand Down

0 comments on commit 3b3ae85

Please sign in to comment.