diff --git a/src/backends/mod.rs b/src/backends/mod.rs index 5c7c790..d2b4b7a 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -11,7 +11,7 @@ use crate::package::{ OUTPUT_DIRECTORY, }; use crate::util::errors::{AnyError, BuildResult, LingoError}; -use crate::{GitCloneAndCheckoutCap, WhichCapability}; +use crate::{GitCloneAndCheckoutCap, RemoveFolderCap, WhichCapability}; pub mod cmake_c; pub mod cmake_cpp; @@ -25,6 +25,7 @@ pub fn execute_command<'a>( config: &'a mut Config, which: WhichCapability, clone: GitCloneAndCheckoutCap, + remove_dir_all: RemoveFolderCap, ) -> BatchBuildResults<'a> { let mut result = BatchBuildResults::new(); let dependencies = Vec::from_iter(config.dependencies.clone()); @@ -54,6 +55,12 @@ pub fn execute_command<'a>( } } } + CommandSpec::Clean => { + let output_root = &config.apps[0].output_root; + if let Err(e) = remove_dir_all(&output_root.display().to_string()) { + error!("lingo was unable to delete build folder! {e}"); + } + } _ => {} } diff --git a/src/lib.rs b/src/lib.rs index 48531f2..6426408 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,9 @@ pub enum WhichError { #[derive(Debug)] pub struct GitCloneError(pub String); // TODO: create a more domain-specific error time like the actual git2::Error +#[derive(Debug)] +pub struct RemoveFolderError(pub String); + impl std::fmt::Display for WhichError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -36,10 +39,18 @@ impl std::fmt::Display for GitCloneError { } } +impl std::fmt::Display for RemoveFolderError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + impl std::error::Error for WhichError {} impl std::error::Error for GitCloneError {} +impl std::error::Error for RemoveFolderError {} + pub struct GitUrl<'a>(&'a str); impl<'a> From<&'a str> for GitUrl<'a> { @@ -61,3 +72,4 @@ pub type FsReadCapability<'a> = Box io::Result = Box< dyn Fn(GitUrl, &std::path::Path, Option) -> Result, GitCloneError> + 'a, >; +pub type RemoveFolderCap<'a> = Box Result<(), RemoveFolderError> + 'a>; diff --git a/src/main.rs b/src/main.rs index 7541c24..7770278 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use liblingo::args::TargetLanguage; use std::io::ErrorKind; use std::path::{Path, PathBuf}; use std::process::Command; -use std::{env, io}; +use std::{env, fs, io}; use clap::Parser; use git2::BranchType::{Local, Remote}; @@ -13,7 +13,9 @@ use liblingo::backends::{BatchBuildResults, BuildCommandOptions, CommandSpec}; use liblingo::package::tree::GitLock; use liblingo::package::{Config, ConfigFile}; use liblingo::util::errors::{BuildResult, LingoError}; -use liblingo::{GitCloneAndCheckoutCap, GitCloneError, GitUrl, WhichCapability, WhichError}; +use liblingo::{ + GitCloneAndCheckoutCap, GitCloneError, GitUrl, RemoveFolderError, WhichCapability, WhichError, +}; fn do_which(cmd: &str) -> Result { which::which(cmd).map_err(|err| match err { @@ -87,6 +89,11 @@ fn do_clone_and_checkout( Ok(git_rev) } +fn do_remove_folder(path: &str) -> Result<(), RemoveFolderError> { + fs::remove_dir_all(path) + .map_err(|_| RemoveFolderError(format!("Could not delete folder: {}", path))) +} + fn do_read_to_string(p: &Path) -> io::Result { std::fs::read_to_string(p) } @@ -223,6 +230,7 @@ fn run_command(task: CommandSpec, config: &mut Config, _fail_at_end: bool) -> Ba config, Box::new(do_which), Box::new(do_clone_and_checkout), + Box::new(do_remove_folder), ) }