Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lingo Clean #63

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also remove Lingo.toml here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

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}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More or less the same error message is produced by remove_dir_all

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes remove_dir_all produces and error and here we handle it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it seems like the error is: "lingo was unable to delete build folder! Could not delete folder: ..." which seems a little redundant

}
}
_ => {}
}

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>;
16 changes: 14 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,15 @@ fn do_clone_and_checkout(
Ok(git_rev)
}

fn do_remove_folder(path: &str) -> Result<(), RemoveFolderError> {
if fs::metadata(path).is_ok() {
fs::remove_dir_all(path)
.map_err(|_| RemoveFolderError(format!("Could not delete folder: {}", path)))
} else {
Ok(())
}
}

fn do_read_to_string(p: &Path) -> io::Result<String> {
std::fs::read_to_string(p)
}
Expand Down Expand Up @@ -223,6 +234,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
Loading