Skip to content

Commit

Permalink
add lingo clean
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Jan 2, 2025
1 parent 6bdc3af commit cffe8e4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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}");
}
}
_ => {}
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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> {
Expand All @@ -61,3 +72,4 @@ pub type FsReadCapability<'a> = Box<dyn Fn(&std::path::Path) -> io::Result<Strin
pub type GitCloneAndCheckoutCap<'a> = Box<
dyn Fn(GitUrl, &std::path::Path, Option<GitLock>) -> Result<Option<String>, GitCloneError> + 'a,
>;
pub type RemoveFolderCap<'a> = Box<dyn Fn(&str) -> Result<(), RemoveFolderError> + 'a>;
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<PathBuf, WhichError> {
which::which(cmd).map_err(|err| match err {
Expand Down Expand Up @@ -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<String> {
std::fs::read_to_string(p)
}
Expand Down Expand Up @@ -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),
)
}

Expand Down

0 comments on commit cffe8e4

Please sign in to comment.