From ebb8fff3d3acf592920e5b17a363715c7f79972c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20H=C3=B6lting?= <87192362+moritz-hoelting@users.noreply.github.com> Date: Sun, 9 Jun 2024 20:17:48 +0200 Subject: [PATCH] add clean subcommand to CLI --- src/cli.rs | 6 +++++- src/main.rs | 14 ++++++++------ src/subcommands/clean.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/subcommands/mod.rs | 3 +++ 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/subcommands/clean.rs diff --git a/src/cli.rs b/src/cli.rs index ffe35b2..29062ad 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,4 +1,4 @@ -use crate::subcommands::{self, BuildArgs, InitArgs}; +use crate::subcommands::{self, BuildArgs, CleanArgs, InitArgs}; use clap::{Parser, Subcommand}; use color_eyre::eyre::Result; @@ -18,6 +18,9 @@ pub enum Command { Init(InitArgs), /// Build the project. Build(BuildArgs), + /// Clean build artifacts. + /// This will remove the `dist` directory. + Clean(CleanArgs), #[cfg(feature = "zip")] /// Build and package the project. Package(subcommands::PackageArgs), @@ -31,6 +34,7 @@ impl Args { match &self.cmd { Command::Init(args) => subcommands::init(self.verbose, args)?, Command::Build(args) => subcommands::build(self.verbose, args)?, + Command::Clean(args) => subcommands::clean(self.verbose, args)?, #[cfg(feature = "zip")] Command::Package(args) => subcommands::package(self.verbose, args)?, #[cfg(feature = "lang-debug")] diff --git a/src/main.rs b/src/main.rs index d67dfa4..b246dbf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,14 @@ +use std::process::ExitCode; + use clap::Parser; -use color_eyre::eyre::Result; use shulkerscript::cli::Args; -fn main() -> Result<()> { - color_eyre::install()?; +fn main() -> ExitCode { + color_eyre::install().unwrap(); let args = Args::parse(); - args.run()?; - - Ok(()) + match args.run() { + Ok(_) => ExitCode::SUCCESS, + Err(_) => ExitCode::FAILURE, + } } diff --git a/src/subcommands/clean.rs b/src/subcommands/clean.rs new file mode 100644 index 0000000..44d7f3c --- /dev/null +++ b/src/subcommands/clean.rs @@ -0,0 +1,40 @@ +use std::path::PathBuf; + +use color_eyre::eyre::Result; +use path_absolutize::Absolutize; + +use crate::{ + error::Error, + terminal_output::{print_error, print_info}, +}; + +#[derive(Debug, clap::Args, Clone)] +pub struct CleanArgs { + /// The path of the project to clean. + #[clap(default_value = ".")] + pub path: PathBuf, +} + +pub fn clean(_verbose: bool, args: &CleanArgs) -> Result<()> { + let path = args.path.as_path(); + + print_info(&format!( + "Cleaning project at {}", + path.absolutize_from(path)?.display() + )); + + let dist_path = path.join("dist"); + + if !path.join("pack.toml").exists() { + print_error("The specified directory is not a ShulkerScript project."); + return Err(Error::InvalidPackPathError(path.to_path_buf()).into()); + } + + if dist_path.exists() { + std::fs::remove_dir_all(&dist_path)?; + } + + print_info("Project cleaned successfully."); + + Ok(()) +} diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs index edfbcc3..f45cbdd 100644 --- a/src/subcommands/mod.rs +++ b/src/subcommands/mod.rs @@ -4,6 +4,9 @@ pub use init::{init, InitArgs}; mod build; pub use build::{build, BuildArgs}; +mod clean; +pub use clean::{clean, CleanArgs}; + #[cfg(feature = "zip")] mod package; #[cfg(feature = "zip")]