Skip to content

Commit 9ff81db

Browse files
committed
add lingo clean
1 parent defe1ce commit 9ff81db

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/backends/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::package::{
1111
OUTPUT_DIRECTORY,
1212
};
1313
use crate::util::errors::{AnyError, BuildResult, LingoError};
14-
use crate::{GitCloneAndCheckoutCap, WhichCapability};
14+
use crate::{GitCloneAndCheckoutCap, RemoveFolderCap, WhichCapability};
1515

1616
pub mod cmake_c;
1717
pub mod cmake_cpp;
@@ -25,6 +25,7 @@ pub fn execute_command<'a>(
2525
config: &'a mut Config,
2626
which: WhichCapability,
2727
clone: GitCloneAndCheckoutCap,
28+
remove_dir_all: RemoveFolderCap,
2829
) -> BatchBuildResults<'a> {
2930
let mut result = BatchBuildResults::new();
3031
let dependencies = Vec::from_iter(config.dependencies.clone());
@@ -54,6 +55,12 @@ pub fn execute_command<'a>(
5455
}
5556
}
5657
}
58+
CommandSpec::Clean => {
59+
let output_root = &config.apps[0].output_root;
60+
if let Err(e) = remove_dir_all(&output_root.display().to_string()) {
61+
error!("lingo was unable to delete build folder! {e}");
62+
}
63+
}
5764
_ => {}
5865
}
5966

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ pub enum WhichError {
1818
#[derive(Debug)]
1919
pub struct GitCloneError(pub String); // TODO: create a more domain-specific error time like the actual git2::Error
2020

21+
#[derive(Debug)]
22+
pub struct RemoveFolderError(pub String);
23+
2124
impl std::fmt::Display for WhichError {
2225
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2326
match self {
@@ -36,10 +39,18 @@ impl std::fmt::Display for GitCloneError {
3639
}
3740
}
3841

42+
impl std::fmt::Display for RemoveFolderError {
43+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44+
write!(f, "{}", self.0)
45+
}
46+
}
47+
3948
impl std::error::Error for WhichError {}
4049

4150
impl std::error::Error for GitCloneError {}
4251

52+
impl std::error::Error for RemoveFolderError {}
53+
4354
pub struct GitUrl<'a>(&'a str);
4455

4556
impl<'a> From<&'a str> for GitUrl<'a> {
@@ -61,3 +72,4 @@ pub type FsReadCapability<'a> = Box<dyn Fn(&std::path::Path) -> io::Result<Strin
6172
pub type GitCloneAndCheckoutCap<'a> = Box<
6273
dyn Fn(GitUrl, &std::path::Path, Option<GitLock>) -> Result<Option<String>, GitCloneError> + 'a,
6374
>;
75+
pub type RemoveFolderCap<'a> = Box<dyn Fn(&str) -> Result<(), RemoveFolderError> + 'a>;

src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use liblingo::args::TargetLanguage;
22
use std::io::ErrorKind;
33
use std::path::{Path, PathBuf};
44
use std::process::Command;
5-
use std::{env, io};
5+
use std::{env, fs, io};
66

77
use clap::Parser;
88
use git2::BranchType::{Local, Remote};
@@ -13,7 +13,9 @@ use liblingo::backends::{BatchBuildResults, BuildCommandOptions, CommandSpec};
1313
use liblingo::package::tree::GitLock;
1414
use liblingo::package::{Config, ConfigFile};
1515
use liblingo::util::errors::{BuildResult, LingoError};
16-
use liblingo::{GitCloneAndCheckoutCap, GitCloneError, GitUrl, WhichCapability, WhichError};
16+
use liblingo::{
17+
GitCloneAndCheckoutCap, GitCloneError, GitUrl, RemoveFolderError, WhichCapability, WhichError,
18+
};
1719

1820
fn do_which(cmd: &str) -> Result<PathBuf, WhichError> {
1921
which::which(cmd).map_err(|err| match err {
@@ -87,6 +89,11 @@ fn do_clone_and_checkout(
8789
Ok(git_rev)
8890
}
8991

92+
fn do_remove_folder(path: &str) -> Result<(), RemoveFolderError> {
93+
fs::remove_dir_all(path)
94+
.map_err(|_| RemoveFolderError(format!("Could not delete folder: {}", path)))
95+
}
96+
9097
fn do_read_to_string(p: &Path) -> io::Result<String> {
9198
std::fs::read_to_string(p)
9299
}
@@ -223,6 +230,7 @@ fn run_command(task: CommandSpec, config: &mut Config, _fail_at_end: bool) -> Ba
223230
config,
224231
Box::new(do_which),
225232
Box::new(do_clone_and_checkout),
233+
Box::new(do_remove_folder),
226234
)
227235
}
228236

0 commit comments

Comments
 (0)