Skip to content
Merged
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
1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
66 changes: 39 additions & 27 deletions cli/src/commands/replication/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String> = 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<Result<()>> = replication_plan
.packages
.into_par_iter()
.map(replicate_package)
.collect();

for result in results {
result?;
}

Ok(())
Expand Down