Skip to content

Commit

Permalink
Linker in one folder
Browse files Browse the repository at this point in the history
  • Loading branch information
shahen94 committed Feb 24, 2024
1 parent fbd0474 commit da73220
Show file tree
Hide file tree
Showing 19 changed files with 207 additions and 66 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
16 changes: 14 additions & 2 deletions src/actors/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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,6 +53,8 @@ impl Actor<PipeResult> for InstallActor {
format!("Resolved: {:?}", resolve_artifacts.get_artifacts().len()),
);

// ─── Start Downloading ──────────────────────

CraftLogger::verbose_n(3, "Downloading dependencies");
let download_artifacts = DownloaderPipe::new(&resolve_artifacts, tx.clone())
.run()
Expand All @@ -60,9 +64,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 @@ -71,6 +76,9 @@ 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(),
Expand All @@ -80,12 +88,16 @@ impl Actor<PipeResult> for InstallActor {
.run()
.await?;

// ─── Sync Lock File ────────────────────────

LockFile::sync(
resolve_artifacts.get_artifacts(),
env::current_dir().unwrap(),
)
.await;

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

ExtractorPipe::cleanup().await;

drop(tx);
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
40 changes: 40 additions & 0 deletions src/fs/copy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::fs;
use std::path::Path;
use fs_extra::dir::CopyOptions;

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(&vec![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;
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod perf;
mod registry;
mod tar;
mod ui;
mod fs;

mod pipeline;

Expand Down
5 changes: 2 additions & 3 deletions src/lock/file.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::{contracts::LOCK_FILE_NAME, package::NpmPackage};
use crate::{contracts::LOCK_FILE_NAME, package::NpmPackage, pipeline::ResolvedItem};

#[derive(Debug, Deserialize, Serialize, Default)]
pub struct LockFile {
Expand All @@ -25,7 +24,7 @@ impl LockFile {
lock_file
}

pub async fn sync(package: Vec<NpmPackage>, path: PathBuf) {
pub async fn sync(package: Vec<ResolvedItem>, path: PathBuf) {
let file_path = path.join(LOCK_FILE_NAME);

if file_path.exists() {
Expand Down
1 change: 1 addition & 0 deletions src/lock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod file;
mod shape;

pub use file::LockFile;
8 changes: 8 additions & 0 deletions src/lock/shape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use serde::{Deserialize, Serialize};

use crate::package::NpmPackage;

#[derive(Debug, Serialize, Deserialize)]
pub struct LockShape {
pub package: NpmPackage,
}
23 changes: 7 additions & 16 deletions src/pipeline/artifacts/linker_artifacts.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
use std::path::PathBuf;

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

#[derive(Debug, Default)]
pub struct LinkerArtifacts {
pub artifacts: Vec<LinkArtifactItem>,
}
// ─────────────────────────────────────────────────────────────────────────────

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

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

impl LinkArtifactItem {
pub fn new(path: PathBuf) -> Self {
Self { path }
pub fn new(from: PathBuf, to: PathBuf) -> Self {
Self { from, to }
}
}

impl LinkerArtifacts {
pub fn add(&mut self, path: PathBuf) {
self.artifacts.push(LinkArtifactItem::new(path));
}
}
}
4 changes: 2 additions & 2 deletions src/pipeline/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ mod resolve_artifacts;

pub use download_artifacts::{DownloadArtifacts, StoredArtifact};
pub use extract_artifacts::{ExtractArtifacts, ExtractArtifactsMap};
pub use linker_artifacts::LinkerArtifacts;
pub use resolve_artifacts::ResolveArtifacts;
pub use resolve_artifacts::{ResolveArtifacts, ResolvedItem};
pub use linker_artifacts::LinkArtifactItem;
34 changes: 26 additions & 8 deletions src/pipeline/artifacts/resolve_artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@ use crate::{contracts::PipeArtifact, package::NpmPackage};

#[derive(Debug, Clone)]
pub struct ResolveArtifacts {
packages: HashMap<String, NpmPackage>,
packages: HashMap<String, ResolvedItem>,
}

#[derive(Debug, Clone)]
pub struct ResolvedItem {
pub package: NpmPackage,
pub parent: Option<String>,
}

// --------------------------------------------------------------------------------

impl ResolvedItem {
pub fn new(package: NpmPackage, parent: Option<String>) -> Self {
Self { package, parent }
}

pub fn with_no_parent(package: NpmPackage) -> Self {
Self::new(package, None)
}
}

// --------------------------------------------------------------------------------
Expand All @@ -18,19 +36,19 @@ impl ResolveArtifacts {
}
}

pub fn get(&self, key: &str) -> Option<&NpmPackage> {
pub fn get(&self, key: &str) -> Option<&ResolvedItem> {
self.packages.get(key)
}

pub fn insert(&mut self, key: String, value: NpmPackage) {
pub fn insert(&mut self, key: String, value: ResolvedItem) {
self.packages.insert(key, value);
}
}

// --------------------------------------------------------------------------------

impl PipeArtifact<Vec<NpmPackage>> for ResolveArtifacts {
fn get_artifacts(&self) -> Vec<NpmPackage> {
impl PipeArtifact<Vec<ResolvedItem>> for ResolveArtifacts {
fn get_artifacts(&self) -> Vec<ResolvedItem> {
self.packages.values().cloned().collect()
}
}
Expand Down Expand Up @@ -58,9 +76,9 @@ mod tests {
"#,
)
.unwrap();
resolve_artifacts.insert("package".to_string(), package);
resolve_artifacts.insert("package".to_string(), ResolvedItem::with_no_parent(package));

assert_eq!(resolve_artifacts.get("package").unwrap().version, "1.0.0");
assert_eq!(resolve_artifacts.get("package").unwrap().package.version, "1.0.0");
}

#[test]
Expand All @@ -80,7 +98,7 @@ mod tests {
"#,
)
.unwrap();
resolve_artifacts.insert("package".to_string(), package);
resolve_artifacts.insert("package".to_string(), ResolvedItem::with_no_parent(package));

assert_eq!(resolve_artifacts.get_artifacts().len(), 1);
}
Expand Down
6 changes: 3 additions & 3 deletions src/pipeline/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
package::NpmPackage,
};

use super::artifacts::DownloadArtifacts;
use super::artifacts::{DownloadArtifacts, ResolvedItem};

// ─── DownloaderPipe ─────────────────────────────────────────────────────────────

Expand All @@ -30,9 +30,9 @@ pub struct DownloaderPipe<C: PersistentCache<PathBuf>> {
// ─── Implementation ───────────────────────────────────────────────────────────

impl DownloaderPipe<PackagesCache> {
pub fn new(artifacts: &dyn PipeArtifact<Vec<NpmPackage>>, tx: Sender<ProgressAction>) -> Self {
pub fn new(artifacts: &dyn PipeArtifact<Vec<ResolvedItem>>, tx: Sender<ProgressAction>) -> Self {
Self {
packages: artifacts.get_artifacts(),
packages: artifacts.get_artifacts().iter().map(|item| item.package.clone()).collect(),
cache: Arc::new(Mutex::new(PackagesCache::default())),
artifacts: Arc::new(Mutex::new(DownloadArtifacts::new())),
tx,
Expand Down
Loading

0 comments on commit da73220

Please sign in to comment.