diff --git a/cli/Cargo.toml b/cli/Cargo.toml index e763e02..f641124 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" anyhow = "1.0.98" clap = { version = "4.5.27", features = ["derive"] } colorize = "0.1.0" +rayon = "1.10.0" serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.138" serde_yaml = "0.9.34" diff --git a/cli/src/commands/replication/apply.rs b/cli/src/commands/replication/apply.rs index 0ad092a..8fb3828 100644 --- a/cli/src/commands/replication/apply.rs +++ b/cli/src/commands/replication/apply.rs @@ -2,41 +2,53 @@ use std::{fs::create_dir_all, path::PathBuf}; use anyhow::Result; use clap::Parser; +use colorize::AnsiColor; +use rayon::iter::{IntoParallelIterator, ParallelIterator}; use source_wand_common::{project_manipulator::{local_project_manipulator::LocalProjectManipulator, project_manipulator::ProjectManipulator}, utils::read_yaml_file::read_yaml_file}; -use source_wand_replication::model::{package_destination::PackageDestination, package_origin::PackageOrigin, replication_plan::ReplicationPlan}; +use source_wand_replication::model::{package::Package, package_destination::PackageDestination, package_origin::PackageOrigin, replication_plan::ReplicationPlan}; #[derive(Debug, Parser)] pub struct ReplicationApplyArgs; +fn replicate_package(package: Package) -> Result<()> { + if let PackageOrigin::GoCache(origin) = package.origin { + let PackageDestination::Git(destination) = package.destination; + let dependency_directory: PathBuf = PathBuf::from(format!("./source-wand/dependencies/{}/{}", origin.name, origin.version)); + create_dir_all(&dependency_directory)?; + + let sh: LocalProjectManipulator = LocalProjectManipulator::new(dependency_directory, true); + + let ls_remote: Result = sh.run_shell(format!("git ls-remote --exit-code --heads {} {}", destination.git, destination.reference)); + + if ls_remote.is_ok() { + println!("{}", format!("[skipped] {} ({}), already exists on remote", origin.name, origin.version).yellow()); + return Ok(()); + } + + sh.run_shell(format!("cp -r {}/* .", origin.path))?; + sh.run_shell("git init".to_string())?; + sh.run_shell(format!("git remote add origin {}", destination.git))?; + sh.run_shell(format!("git checkout --orphan {}", destination.reference))?; + sh.run_shell("git add .".to_string())?; + sh.run_shell("git commit -m 'Replicate source code'".to_string())?; + sh.run_shell(format!("git push -u origin {}", destination.reference))?; + + println!("{}", format!("[replicated] {} ({})", origin.name, origin.version).green()); + } + Ok(()) +} + pub fn replicate_apply_command(_args: &ReplicationApplyArgs) -> Result<()> { let replication_plan: ReplicationPlan = read_yaml_file("source-wand/replication-plan.yaml")?; - for package in replication_plan.packages { - if let PackageOrigin::GoCache(origin) = package.origin { - let PackageDestination::Git(destination) = package.destination; - let dependency_directory: PathBuf = PathBuf::from(format!("./source-wand/dependencies/{}/{}", origin.name, origin.version)); - create_dir_all(&dependency_directory)?; - - let sh: LocalProjectManipulator = LocalProjectManipulator::new(dependency_directory, true); - - println!("Fetching {} ({}) from the local Go Cache", origin.name, origin.version); - sh.run_shell(format!("cp -r {}/* .", origin.path))?; - - let ls_remote: Result = sh.run_shell(format!("git ls-remote --exit-code --heads {} {}", destination.git, destination.reference)); - - if ls_remote.is_ok() { - println!("{} ({}) already exists on remote, skipping", origin.name, origin.version); - continue; - } - - println!("Pushing {} ({}) source code to remote repository", origin.name, origin.version); - sh.run_shell("git init".to_string())?; - sh.run_shell(format!("git remote add origin {}", destination.git))?; - sh.run_shell(format!("git checkout --orphan {}", destination.reference))?; - sh.run_shell("git add .".to_string())?; - sh.run_shell("git commit -m 'Replicate source code'".to_string())?; - sh.run_shell(format!("git push -u origin {}", destination.reference))?; - } + let results: Vec> = replication_plan + .packages + .into_par_iter() + .map(replicate_package) + .collect(); + + for result in results { + result?; } Ok(())