diff --git a/src/backends/cmake_c.rs b/src/backends/cmake_c.rs index 77e7ba6..235db54 100644 --- a/src/backends/cmake_c.rs +++ b/src/backends/cmake_c.rs @@ -5,9 +5,9 @@ use std::process::Command; use crate::backends::{ BatchBackend, BatchBuildResults, BuildCommandOptions, BuildProfile, BuildResult, CommandSpec, }; +use crate::package::App; use crate::util::errors::LingoError; use crate::util::execute_command_to_build_result; -use crate::package::App; pub struct CmakeC; diff --git a/src/backends/mod.rs b/src/backends/mod.rs index 196920e..5c7c790 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -20,7 +20,12 @@ pub mod npm; pub mod pnpm; #[allow(clippy::single_match)] // there more options will be added to this match block -pub fn execute_command<'a>(command: &CommandSpec, config: &'a mut Config, which: WhichCapability, clone: GitCloneAndCheckoutCap) -> BatchBuildResults<'a> { +pub fn execute_command<'a>( + command: &CommandSpec, + config: &'a mut Config, + which: WhichCapability, + clone: GitCloneAndCheckoutCap, +) -> BatchBuildResults<'a> { let mut result = BatchBuildResults::new(); let dependencies = Vec::from_iter(config.dependencies.clone()); @@ -29,7 +34,7 @@ pub fn execute_command<'a>(command: &CommandSpec, config: &'a mut Config, which: let manager = match DependencyManager::from_dependencies( dependencies.clone(), &PathBuf::from(OUTPUT_DIRECTORY), - &clone + &clone, ) { Ok(value) => value, Err(e) => { diff --git a/src/lib.rs b/src/lib.rs index 28ef491..48531f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ -use std::io; use crate::package::tree::GitLock; +use std::io; pub mod args; pub mod backends; @@ -58,5 +58,6 @@ pub type WhichCapability<'a> = Box Result = Box Result<(), GitCloneError> + 'a>; pub type FsReadCapability<'a> = Box io::Result + 'a>; -pub type GitCloneAndCheckoutCap<'a> = - Box) -> Result, GitCloneError> + 'a>; +pub type GitCloneAndCheckoutCap<'a> = Box< + dyn Fn(GitUrl, &std::path::Path, Option) -> Result, GitCloneError> + 'a, +>; diff --git a/src/main.rs b/src/main.rs index 33d5625..9715753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,10 @@ use git2::Repository; use liblingo::args::InitArgs; use liblingo::args::{BuildArgs, Command as ConsoleCommand, CommandLineArgs}; use liblingo::backends::{BatchBuildResults, BuildCommandOptions, CommandSpec}; +use liblingo::package::tree::GitLock; use liblingo::package::{Config, ConfigFile}; use liblingo::util::errors::{BuildResult, LingoError}; use liblingo::{GitCloneCapability, GitCloneError, GitUrl, WhichCapability, WhichError}; -use liblingo::package::tree::GitLock; fn do_repo_clone(url: GitUrl, into: &std::path::Path) -> Result<(), GitCloneError> { Repository::clone(url.into(), into).map_or_else( @@ -32,8 +32,13 @@ fn do_which(cmd: &str) -> Result { }) } -fn do_clone_and_checkout(git_url: GitUrl, outpath: &Path, git_tag: Option) -> Result, GitCloneError> { - let repo = Repository::clone(<&str>::from(git_url), outpath).map_err(|_|GitCloneError("clone failed".to_string()))?; +fn do_clone_and_checkout( + git_url: GitUrl, + outpath: &Path, + git_tag: Option, +) -> Result, GitCloneError> { + let repo = Repository::clone(<&str>::from(git_url), outpath) + .map_err(|_| GitCloneError("clone failed".to_string()))?; let mut git_rev = None; if let Some(git_lock) = git_tag { @@ -44,8 +49,11 @@ fn do_clone_and_checkout(git_url: GitUrl, outpath: &Path, git_tag: Option repo.set_head_detached(object.id()), - }.map_err(|_|GitCloneError("cannot checkout rev".to_string()))?; + } + .map_err(|_| GitCloneError("cannot checkout rev".to_string()))?; } Ok(git_rev) @@ -87,7 +96,12 @@ fn main() { print_res(result) } - let result = execute_command(&mut wrapped_config, args.command, Box::new(do_which), Box::new(do_repo_clone)); + let result = execute_command( + &mut wrapped_config, + args.command, + Box::new(do_which), + Box::new(do_repo_clone), + ); match result { CommandResult::Batch(res) => res.print_results(), @@ -127,9 +141,16 @@ fn validate(config: &mut Option, command: &ConsoleCommand) -> BuildResul } } -fn execute_command<'a>(config: &'a mut Option, command: ConsoleCommand, _which_capability: WhichCapability, git_clone_capability: GitCloneCapability) -> CommandResult<'a> { +fn execute_command<'a>( + config: &'a mut Option, + command: ConsoleCommand, + _which_capability: WhichCapability, + git_clone_capability: GitCloneCapability, +) -> CommandResult<'a> { match (config, command) { - (_, ConsoleCommand::Init(init_config)) => CommandResult::Single(do_init(init_config, &git_clone_capability)), + (_, ConsoleCommand::Init(init_config)) => { + CommandResult::Single(do_init(init_config, &git_clone_capability)) + } (None, _) => CommandResult::Single(Err(Box::new(io::Error::new( ErrorKind::NotFound, "Error: Missing Lingo.toml file", @@ -159,7 +180,7 @@ fn do_init(init_config: InitArgs, git_clone_capability: &GitCloneCapability) -> initial_config.setup_example( init_config.platform, init_config.language.unwrap_or(TargetLanguage::Cpp), - git_clone_capability + git_clone_capability, ) } @@ -180,7 +201,12 @@ fn build<'a>(args: &BuildArgs, config: &'a mut Config) -> BatchBuildResults<'a> fn run_command(task: CommandSpec, config: &mut Config, _fail_at_end: bool) -> BatchBuildResults { let _apps = config.apps.iter().collect::>(); - liblingo::backends::execute_command(&task, config, Box::new(do_which), Box::new(do_clone_and_checkout)) + liblingo::backends::execute_command( + &task, + config, + Box::new(do_which), + Box::new(do_clone_and_checkout), + ) } enum CommandResult<'a> { diff --git a/src/package/lock.rs b/src/package/lock.rs index e871fcb..9bf4b05 100644 --- a/src/package/lock.rs +++ b/src/package/lock.rs @@ -1,7 +1,7 @@ use colored::Colorize; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use versions::Versioning; use sha1dir; +use versions::Versioning; use log::error; use serde::de::Error as DeserializationError; @@ -208,14 +208,20 @@ impl DependencyLock { } } - pub fn init(&mut self, lfc_include_folder: &Path, git_clone_and_checkout_cap: &GitCloneAndCheckoutCap) -> anyhow::Result<()> { + pub fn init( + &mut self, + lfc_include_folder: &Path, + git_clone_and_checkout_cap: &GitCloneAndCheckoutCap, + ) -> anyhow::Result<()> { for (_, lock) in self.dependencies.iter() { let temp = lfc_include_folder.join(&lock.name); // the Lingo.toml for this dependency doesnt exists, hence we need to fetch this package if !temp.join("Lingo.toml").exists() { let mut details = PackageDetails::try_from(&lock.source)?; - details.fetch(&temp, git_clone_and_checkout_cap).expect("cannot pull package"); + details + .fetch(&temp, git_clone_and_checkout_cap) + .expect("cannot pull package"); } let hash = sha1dir::checksum_current_dir(&temp, false); diff --git a/src/package/management.rs b/src/package/management.rs index 73bd48e..61b9c2e 100644 --- a/src/package/management.rs +++ b/src/package/management.rs @@ -2,6 +2,8 @@ use colored::Colorize; use log::error; use versions::{Requirement, Versioning}; +use crate::{GitCloneAndCheckoutCap, GitUrl}; +use sha1dir; use std::collections::HashMap; use std::fs; use std::fs::File; @@ -9,8 +11,6 @@ use std::io::Write; use std::path::{Path, PathBuf}; use std::str::FromStr; use url::{ParseError, Url}; -use crate::{GitCloneAndCheckoutCap, GitUrl}; -use sha1dir; use crate::package::lock::{PackageLockSource, PackageLockSourceType}; use crate::package::{ @@ -68,7 +68,11 @@ impl TryFrom<&PackageLockSource> for PackageDetails { impl PackageDetails { /// this function fetches the specified location and places it at the given location - pub fn fetch(&mut self, library_path: &PathBuf, clone: &GitCloneAndCheckoutCap) -> anyhow::Result<()> { + pub fn fetch( + &mut self, + library_path: &PathBuf, + clone: &GitCloneAndCheckoutCap, + ) -> anyhow::Result<()> { match &self.mutual_exclusive { ProjectSource::Path(path_buf) => { let src = fs::canonicalize(path_buf)?; @@ -76,9 +80,12 @@ impl PackageDetails { Ok(copy_dir_all(src, dst)?) } ProjectSource::Git(git_url) => { - self.git_rev = clone(GitUrl::from(git_url.as_str()), library_path, self.git_tag.clone())?; + self.git_rev = clone( + GitUrl::from(git_url.as_str()), + library_path, + self.git_tag.clone(), + )?; Ok(()) - } _ => Ok(()), } @@ -89,7 +96,7 @@ impl DependencyManager { pub fn from_dependencies( dependencies: Vec<(String, PackageDetails)>, target_path: &Path, - git_clone_and_checkout_cap: &GitCloneAndCheckoutCap + git_clone_and_checkout_cap: &GitCloneAndCheckoutCap, ) -> anyhow::Result { // create library folder let library_path = target_path.join(LIBRARY_DIRECTORY); @@ -107,7 +114,8 @@ impl DependencyManager { // if a lock file is present it will load the dependencies from it and checks // integrity of the build directory - if let Ok(()) = lock.init(&target_path.join("lfc_include"), git_clone_and_checkout_cap) { + if let Ok(()) = lock.init(&target_path.join("lfc_include"), git_clone_and_checkout_cap) + { return Ok(DependencyManager { pulling_queue: vec![], lock, @@ -119,7 +127,11 @@ impl DependencyManager { manager = DependencyManager::default(); // starts recursively pulling dependencies - let root_nodes = manager.pull(dependencies.clone(), target_path, git_clone_and_checkout_cap)?; + let root_nodes = manager.pull( + dependencies.clone(), + target_path, + git_clone_and_checkout_cap, + )?; // flattens the dependency tree and makes the package selection let selection = DependencyManager::flatten(root_nodes.clone())?; @@ -150,7 +162,7 @@ impl DependencyManager { &mut self, mut dependencies: Vec<(String, PackageDetails)>, root_path: &Path, - git_clone_and_checkout_cap: &GitCloneAndCheckoutCap + git_clone_and_checkout_cap: &GitCloneAndCheckoutCap, ) -> anyhow::Result> { let mut sub_dependencies = vec![]; self.pulling_queue.append(&mut dependencies); @@ -165,7 +177,7 @@ impl DependencyManager { &package_name, package_details, &sub_dependency_path, - git_clone_and_checkout_cap + git_clone_and_checkout_cap, ) { Ok(value) => value, Err(e) => { @@ -188,7 +200,7 @@ impl DependencyManager { name: &str, mut package: PackageDetails, base_path: &Path, - git_clone_and_checkout_cap: &GitCloneAndCheckoutCap + git_clone_and_checkout_cap: &GitCloneAndCheckoutCap, ) -> anyhow::Result { // creating the directory where the library will be housed let library_path = base_path; //.join("libs"); diff --git a/src/package/mod.rs b/src/package/mod.rs index 2875ede..a138206 100644 --- a/src/package/mod.rs +++ b/src/package/mod.rs @@ -437,7 +437,7 @@ impl ConfigFile { &self, platform: Platform, target_language: TargetLanguage, - git_clone_capability: &GitCloneCapability + git_clone_capability: &GitCloneCapability, ) -> BuildResult { if is_valid_location_for_project(Path::new(".")) { match platform { diff --git a/src/util/mod.rs b/src/util/mod.rs index 8bc26f2..ac4782f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,4 +1,3 @@ - pub mod analyzer; mod command_line; pub mod errors;