Skip to content

Commit

Permalink
Merge pull request #5 from shahen94/feature/linker
Browse files Browse the repository at this point in the history
Linker: Combining all pipes into one result
  • Loading branch information
shahen94 authored Mar 2, 2024
2 parents 3d9f4ef + 3ea31e8 commit 4bf777f
Show file tree
Hide file tree
Showing 21 changed files with 284 additions and 97 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
craft.lock
craft.lock
node_modules
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ regex = "1.10.2"

indicatif = "0.17.7"
console = { version = "0.15", default-features = false, features = ["ansi-parsing"] }

lazy_static = "1.4.0"
fs_extra = "1.3.0"
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "module",
"dependencies": {
"express": "^4.18.2",
"mongoose": "^5.9.7",
Expand Down
31 changes: 21 additions & 10 deletions src/actors/install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
env,
sync::mpsc::Receiver,
thread::{self, JoinHandle},
};
Expand All @@ -9,7 +8,6 @@ use async_trait::async_trait;
use crate::{
contracts::{Actor, Pipe, PipeArtifact, Progress, ProgressAction},
errors::ExecutionError,
lock::LockFile,
logger::CraftLogger,
pipeline::{DownloaderPipe, ExtractorPipe, LinkerPipe, ResolverPipe},
ui::UIProgress,
Expand Down Expand Up @@ -42,6 +40,8 @@ impl Actor<PipeResult> for InstallActor {

let ui_thread = self.start_progress(rx);

// ─── Start Resolving ─────────────────────────

CraftLogger::verbose_n(3, "Resolving dependencies");
let resolve_artifacts = ResolverPipe::new(self.packages.clone(), tx.clone())
.run()
Expand All @@ -51,11 +51,7 @@ impl Actor<PipeResult> for InstallActor {
format!("Resolved: {:?}", resolve_artifacts.get_artifacts().len()),
);

LockFile::sync(
resolve_artifacts.get_artifacts(),
env::current_dir().unwrap(),
)
.await;
// ─── Start Downloading ──────────────────────

CraftLogger::verbose_n(3, "Downloading dependencies");
let download_artifacts = DownloaderPipe::new(&resolve_artifacts, tx.clone())
Expand All @@ -66,9 +62,10 @@ impl Actor<PipeResult> for InstallActor {
3,
format!("Downloaded {:?}", download_artifacts.get_artifacts().len()),
);
CraftLogger::verbose_n(3, "Extracting dependencies");

#[allow(unused_variables)]
// ─── Start Extracting ───────────────────────

CraftLogger::verbose_n(3, "Extracting dependencies");
let extracted_artifacts = ExtractorPipe::new(&download_artifacts, tx.clone())
.run()
.await?;
Expand All @@ -77,8 +74,22 @@ impl Actor<PipeResult> for InstallActor {
3,
format!("Extracted {:?}", extracted_artifacts.get_artifacts().len()),
);

// ─── Start Linking ──────────────────────────

CraftLogger::verbose_n(3, "Linking dependencies");
LinkerPipe::new(tx.clone()).run().await?;
LinkerPipe::new(
tx.clone(),
resolve_artifacts.get_artifacts(),
extracted_artifacts.get_artifacts(),
)
.run()
.await?;

// ─── Sync Lock File ────────────────────────
// TODO: Sync lock file

// ─── Cleanup ────────────────────────────────

ExtractorPipe::cleanup().await;

Expand Down
2 changes: 1 addition & 1 deletion src/cache/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct RegistryCache {
// ───────────────────────────────────────────────────────────────────────────────

impl RegistryCache {
pub async fn save(&self) -> Result<(), CacheError> {
pub async fn persist(&self) -> Result<(), CacheError> {
let cache_file = File::create(self.directory.join(REGISTRY_CACHE_FILE)).unwrap();

serde_json::to_writer(cache_file, &self.cache).unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/contracts/constants.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub const CRAFT_VERBOSE_LOGGING: &str = "CRAFT_VERBOSE";
#[allow(dead_code)]
pub const LOCK_FILE_NAME: &str = "craft.lock";
2 changes: 1 addition & 1 deletion src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod registry;
pub use cache::PersistentCache;

pub use actor::Actor;
pub use constants::{CRAFT_VERBOSE_LOGGING, LOCK_FILE_NAME};
pub use constants::CRAFT_VERBOSE_LOGGING;
pub use logger::Logger;
pub use pipe::Pipe;
pub use pipe_artifact::PipeArtifact;
Expand Down
44 changes: 44 additions & 0 deletions src/fs/copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use fs_extra::dir::CopyOptions;
use std::fs;
use std::path::Path;

pub fn copy_dir(from: &Path, to: &Path) -> Result<(), Box<dyn std::error::Error>> {
if to.exists() {
fs::remove_dir_all(to).unwrap();
}

fs::create_dir_all(to).unwrap();

let options = CopyOptions::new().overwrite(true);

copy_recursive(from, to, &options)
}

fn copy_recursive(
from: &std::path::Path,
to: &std::path::Path,
options: &CopyOptions,
) -> Result<(), Box<dyn std::error::Error>> {
let from_meta = fs::metadata(from)?;

if from_meta.is_dir() {
let dir_entries = fs::read_dir(from)?;

for entry in dir_entries {
let entry = entry?;
let entry_path = entry.path();
let entry_name = entry.file_name().into_string().unwrap(); // Convert OsString to String

let dest_path = to.join(&entry_name);

if entry_path.is_dir() {
fs::create_dir_all(&dest_path)?;
copy_recursive(&entry_path, &dest_path, options)?;
} else {
fs_extra::copy_items(&[entry_path], to, options)?;
}
}
}

Ok(())
}
3 changes: 3 additions & 0 deletions src/fs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod copy;

pub use copy::copy_dir;
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod actors;
mod cache;
mod contracts;
mod errors;
mod lock;
mod fs;
mod logger;
mod network;
mod package;
Expand Down
43 changes: 0 additions & 43 deletions src/lock/file.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/lock/mod.rs

This file was deleted.

12 changes: 8 additions & 4 deletions src/logger/craft_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ impl CraftLogger {

impl Logger for CraftLogger {
fn log<S: AsRef<str>>(message: S) {
println!("{}", message.as_ref().green());
let prefix = "[LOG]:".green().bold();
println!("{} {}", prefix, message.as_ref().green());
}

fn info<S: AsRef<str>>(message: S) {
println!("{}", message.as_ref().blue());
let prefix = "[INFO]:".green().bold();
println!("{} {}", prefix, message.as_ref().blue());
}

fn error<S: AsRef<str>>(message: S) {
println!("{}", message.as_ref().red());
let prefix = "[ERROR]:".red().bold();
println!("{} {}", prefix, message.as_ref().red());
}

fn warn<S: AsRef<str>>(message: S) {
println!("{}", message.as_ref().yellow());
let prefix = "[WARN]:".yellow().bold();
println!("{} {}", prefix, message.as_ref().yellow());
}
}
24 changes: 16 additions & 8 deletions src/pipeline/artifacts/extract_artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ use std::{collections::HashMap, env, path::PathBuf};

use crate::{cache::TMP_CACHE_FOLDER, contracts::PipeArtifact, package::NpmPackage};

// ─────────────────────────────────────────────────────────────────────────────

pub type ExtractArtifactsMap = HashMap<String, ExtractArtifactItem>;

// ─── ExtractArtifacts ─────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct ExtractArtifacts {
#[allow(dead_code)]
tmp_cache_folder: PathBuf,
tmp_cache: HashMap<String, ExtractArtifactItem>,
tmp_cache: ExtractArtifactsMap,
}

// ───────────────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct ExtractArtifactItem {
#[allow(dead_code)]
package: NpmPackage,
pub package: NpmPackage,
#[allow(dead_code)]
unzip_at: PathBuf,
pub unzip_at: PathBuf,
}

// ───────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -55,7 +59,7 @@ impl ExtractArtifacts {
}

pub fn add(&mut self, package: NpmPackage, unzip_at: PathBuf) {
let name = package.name.clone();
let name = format!("{}@{}", package.name.clone(), package.version.clone());
let item = ExtractArtifactItem::new(package.clone(), unzip_at);

self.tmp_cache.insert(name, item);
Expand All @@ -69,9 +73,9 @@ impl ExtractArtifacts {

// ───────────────────────────────────────────────────────────────────────────────

impl PipeArtifact<Vec<ExtractArtifactItem>> for ExtractArtifacts {
fn get_artifacts(&self) -> Vec<ExtractArtifactItem> {
self.tmp_cache.values().cloned().collect()
impl PipeArtifact<ExtractArtifactsMap> for ExtractArtifacts {
fn get_artifacts(&self) -> ExtractArtifactsMap {
self.tmp_cache.clone()
}
}

Expand Down Expand Up @@ -101,7 +105,11 @@ mod tests {
extract_artifacts.add(package.clone(), PathBuf::from("/tmp/package"));

assert_eq!(
extract_artifacts.get("package").unwrap().package.version,
extract_artifacts
.get("[email protected]")
.unwrap()
.package
.version,
"1.0.0"
);
}
Expand Down
17 changes: 17 additions & 0 deletions src/pipeline/artifacts/linker_artifacts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::path::PathBuf;

// ─────────────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LinkArtifactItem {
pub to: PathBuf,
pub from: PathBuf,
}

// ─────────────────────────────────────────────────────────────────────────────

impl LinkArtifactItem {
pub fn new(from: PathBuf, to: PathBuf) -> Self {
Self { from, to }
}
}
6 changes: 4 additions & 2 deletions src/pipeline/artifacts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod download_artifacts;
mod extract_artifacts;
mod linker_artifacts;
mod resolve_artifacts;

pub use download_artifacts::{DownloadArtifacts, StoredArtifact};
pub use extract_artifacts::ExtractArtifacts;
pub use resolve_artifacts::ResolveArtifacts;
pub use extract_artifacts::{ExtractArtifacts, ExtractArtifactsMap};
pub use linker_artifacts::LinkArtifactItem;
pub use resolve_artifacts::{ResolveArtifacts, ResolvedItem};
Loading

0 comments on commit 4bf777f

Please sign in to comment.