Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
tanneberger committed Oct 11, 2024
1 parent 397a7a6 commit 362df8b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/backends/cmake_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 7 additions & 2 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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) => {
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::io;
use crate::package::tree::GitLock;
use std::io;

pub mod args;
pub mod backends;
Expand Down Expand Up @@ -58,5 +58,6 @@ pub type WhichCapability<'a> = Box<dyn Fn(&str) -> Result<std::path::PathBuf, Wh
pub type GitCloneCapability<'a> =
Box<dyn Fn(GitUrl, &std::path::Path) -> Result<(), GitCloneError> + 'a>;
pub type FsReadCapability<'a> = Box<dyn Fn(&std::path::Path) -> io::Result<String> + 'a>;
pub type GitCloneAndCheckoutCap<'a> =
Box<dyn Fn(GitUrl, &std::path::Path, Option<GitLock>) -> Result<Option<String>, GitCloneError> + 'a>;
pub type GitCloneAndCheckoutCap<'a> = Box<
dyn Fn(GitUrl, &std::path::Path, Option<GitLock>) -> Result<Option<String>, GitCloneError> + 'a,
>;
48 changes: 37 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -32,8 +32,13 @@ fn do_which(cmd: &str) -> Result<PathBuf, WhichError> {
})
}

fn do_clone_and_checkout(git_url: GitUrl, outpath: &Path, git_tag: Option<GitLock>) -> Result<Option<String>, 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<GitLock>,
) -> Result<Option<String>, 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 {
Expand All @@ -44,8 +49,11 @@ fn do_clone_and_checkout(git_url: GitUrl, outpath: &Path, git_tag: Option<GitLoc
};

// TODO: this produces hard to debug output
let (object, reference) = repo.revparse_ext(&name).map_err(|_|GitCloneError("cannot parse rev".to_string()))?;
repo.checkout_tree(&object, None).map_err(|_|GitCloneError("cannot checkout rev".to_string()))?;
let (object, reference) = repo
.revparse_ext(&name)
.map_err(|_| GitCloneError("cannot parse rev".to_string()))?;
repo.checkout_tree(&object, None)
.map_err(|_| GitCloneError("cannot checkout rev".to_string()))?;

match reference {
// gref is an actual reference like branches or tags
Expand All @@ -55,7 +63,8 @@ fn do_clone_and_checkout(git_url: GitUrl, outpath: &Path, git_tag: Option<GitLoc
}
// this is a commit, not a reference
None => 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)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -127,9 +141,16 @@ fn validate(config: &mut Option<Config>, command: &ConsoleCommand) -> BuildResul
}
}

fn execute_command<'a>(config: &'a mut Option<Config>, command: ConsoleCommand, _which_capability: WhichCapability, git_clone_capability: GitCloneCapability) -> CommandResult<'a> {
fn execute_command<'a>(
config: &'a mut Option<Config>,
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",
Expand Down Expand Up @@ -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,
)
}

Expand All @@ -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::<Vec<_>>();
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> {
Expand Down
12 changes: 9 additions & 3 deletions src/package/lock.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
34 changes: 23 additions & 11 deletions src/package/management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ 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;
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::{
Expand Down Expand Up @@ -68,17 +68,24 @@ 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)?;
let dst = fs::canonicalize(library_path)?;
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(()),
}
Expand All @@ -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<DependencyManager> {
// create library folder
let library_path = target_path.join(LIBRARY_DIRECTORY);
Expand All @@ -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,
Expand All @@ -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())?;
Expand Down Expand Up @@ -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<Vec<DependencyTreeNode>> {
let mut sub_dependencies = vec![];
self.pulling_queue.append(&mut dependencies);
Expand All @@ -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) => {
Expand All @@ -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<DependencyTreeNode> {
// creating the directory where the library will be housed
let library_path = base_path; //.join("libs");
Expand Down
2 changes: 1 addition & 1 deletion src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pub mod analyzer;
mod command_line;
pub mod errors;
Expand Down

0 comments on commit 362df8b

Please sign in to comment.