Skip to content

Commit

Permalink
Fixed clippy and cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTV12345 committed Sep 23, 2024
1 parent 6cd5797 commit 96096d5
Show file tree
Hide file tree
Showing 13 changed files with 209 additions and 195 deletions.
48 changes: 25 additions & 23 deletions src/actors/exec_actor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use async_trait::async_trait;
use crate::actors::install::PipeResult;
use crate::contracts::Actor;
use crate::errors::ExecutionError;
use async_trait::async_trait;
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};

pub struct ExecActor {
pub script: String,
Expand All @@ -23,7 +23,6 @@ pub enum ScriptType {
Pwsh,
}


impl ScriptType {
pub fn get_script_ending(&self) -> String {
match self {
Expand Down Expand Up @@ -53,26 +52,25 @@ fn find_file_to_execute(script: &str, path_to_scan: &PathBuf) -> Option<(ScriptT

let bin_dir = std::fs::read_dir(path_to_scan).unwrap();

for entry in bin_dir {
if let Ok(entry) = entry {
let path_to_file = entry.path();
let file_name = entry.file_name();
if let Some(file_name) = file_name.to_str() {
if file_name_to_find == file_name && entry.metadata().unwrap().is_file() {
return Some((file_ending, path_to_file));
}
for entry in bin_dir.flatten() {
let path_to_file = entry.path();
let file_name = entry.file_name();
if let Some(file_name) = file_name.to_str() {
if file_name_to_find == file_name && entry.metadata().unwrap().is_file() {
return Some((file_ending, path_to_file));
}
}
}
None
}


fn get_possible_script_paths() -> Vec<PathBuf> {
let mut paths = vec![];

let bin_path = env::current_dir().unwrap().join("node_modules").join("\
.bin");
let bin_path = env::current_dir().unwrap().join("node_modules").join(
"\
.bin",
);
let bin_dir = std::fs::metadata(&bin_path);

if bin_dir.is_ok() {
Expand Down Expand Up @@ -105,22 +103,25 @@ impl Actor<PipeResult> for ExecActor {
match shell {
ScriptType::Bash => {
command_to_execute = Command::new("sh");
command_to_execute.arg("-c")
command_to_execute
.arg("-c")
.stdout(Stdio::inherit())
.current_dir(exec_path)
.stderr(Stdio::inherit())
.arg(script);
}
ScriptType::Cmd => {
command_to_execute = Command::new("cmd");
command_to_execute.args(["/C", script.to_str().unwrap()])
command_to_execute = Command::new("cmd");
command_to_execute
.args(["/C", script.to_str().unwrap()])
.current_dir(exec_path)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
}
ScriptType::Pwsh => {
command_to_execute = Command::new("pwsh");
command_to_execute.args(["-File", script.to_str().unwrap()])
command_to_execute
.args(["-File", script.to_str().unwrap()])
.current_dir(exec_path)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit());
Expand All @@ -131,11 +132,12 @@ impl Actor<PipeResult> for ExecActor {
command_to_execute.args(args);
}

let mut child = command_to_execute.spawn().expect("failed to execute process");

let mut child = command_to_execute
.spawn()
.expect("failed to execute process");

let _ = child.wait().expect("child process wasn't running");

Ok(())
}
}
}
16 changes: 3 additions & 13 deletions src/actors/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use async_trait::async_trait;

use crate::actors::peer_resolver::PeerResolver;
use crate::cache::PackagesCache;
use crate::contracts::{Lockfile, PersistentCache};
use crate::lockfile::lock_file_actor::LockFileActor;
Expand All @@ -15,7 +16,6 @@ use crate::{
pipeline::{DownloaderPipe, ExtractorPipe, LinkerPipe, ResolverPipe},
ui::UIProgress,
};
use crate::actors::peer_resolver::PeerResolver;

#[derive(Debug, Clone)]
pub enum PackageType {
Expand Down Expand Up @@ -106,13 +106,8 @@ impl Actor<PipeResult> for InstallActor {
resolve_artifacts.0.get_artifacts().len()
));


// ─── Start Mutating ───────────────────────
let recorder = PeerResolver::new(resolve_artifacts.1)
.run()
.await?;


let recorder = PeerResolver::new(resolve_artifacts.1).run().await?;

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

Expand All @@ -126,8 +121,6 @@ impl Actor<PipeResult> for InstallActor {
download_artifacts.get_artifacts().len()
));



// ─── Start Extracting ───────────────────────

CraftLogger::verbose("Extracting dependencies");
Expand All @@ -147,12 +140,11 @@ impl Actor<PipeResult> for InstallActor {
tx.clone(),
resolve_artifacts.0.get_artifacts(),
extracted_artifacts.get_artifacts(),
recorder.clone()
recorder.clone(),
)
.run()
.await?;


// ─── Sync Lock File ────────────────────────
LockFileActor::new(resolve_artifacts.0.get_artifacts(), recorder)
.run()
Expand All @@ -162,10 +154,8 @@ impl Actor<PipeResult> for InstallActor {

ExtractorPipe::cleanup(resolve_artifacts.0.get_artifacts()).await?;


// ─── Link binaries ────────────────────────────────


drop(tx);
ui_thread.join().unwrap();
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/actors/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod cache_clean;
mod exec_actor;
mod install;
mod peer_resolver;
mod preprocesse_dependency_install;
mod run;
mod peer_resolver;
mod exec_actor;

pub use cache_clean::CacheCleanActor;
pub use exec_actor::ExecActor;
pub use install::InstallActor;
pub use install::PackageType;
pub use preprocesse_dependency_install::PreprocessDependencyInstall;
pub use run::RunActor;
pub use exec_actor::ExecActor;
146 changes: 76 additions & 70 deletions src/actors/peer_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
use async_trait::async_trait;
use crate::cache::RegistryKey;
use crate::contracts::{Pipe};
use crate::contracts::Pipe;
use crate::errors::ExecutionError;
use crate::package::{BinType, PackageMetaRecorder, PackageRecorder, ResolvedBinary};
use async_trait::async_trait;

pub struct PeerResolver {
recorder: PackageRecorder
recorder: PackageRecorder,
}

impl PeerResolver {
pub fn new(recorder: PackageRecorder) -> Self {
PeerResolver {
recorder
}
PeerResolver { recorder }
}

fn handle_insert( req: &mut PackageMetaRecorder, outer_dep: (&RegistryKey, &PackageMetaRecorder)) {
fn handle_insert(
req: &mut PackageMetaRecorder,
outer_dep: (&RegistryKey, &PackageMetaRecorder),
) {
match &mut req.resolved_dependencies {
Some(deps) => {
deps.insert(outer_dep.0.name.clone(), outer_dep.0
.version.clone());
deps.insert(outer_dep.0.name.clone(), outer_dep.0.version.clone());
}
None => {
let mut deps = std::collections::HashMap::new();
Expand All @@ -29,52 +29,51 @@ impl PeerResolver {
}
if let Some(bin) = &outer_dep.1.bin {
match &mut req.resolved_binaries {
Some(b) => {
match bin {
BinType::Bin(s) => {
let splitted_name = s.rsplit('/').next().unwrap().replace(".js", "");
b.push(ResolvedBinary{
name: splitted_name,
path: format!("{}/{}", outer_dep.1.name, s),
package_name: outer_dep.1.name.clone()
});
}
BinType::BinMappings(a) => {
a.iter().for_each(|s| {
b.push(ResolvedBinary{
name: s.0.to_string(),
path: format!("{}/{}", outer_dep.1.name, s.1),
package_name: outer_dep.1.name.clone()
});
Some(b) => match bin {
BinType::Bin(s) => {
let splitted_name = s.rsplit('/').next().unwrap().replace(".js", "");
b.push(ResolvedBinary {
name: splitted_name,
path: format!("{}/{}", outer_dep.1.name, s),
package_name: outer_dep.1.name.clone(),
});
}
BinType::BinMappings(a) => {
a.iter().for_each(|s| {
b.push(ResolvedBinary {
name: s.0.to_string(),
path: format!("{}/{}", outer_dep.1.name, s.1),
package_name: outer_dep.1.name.clone(),
});
}
});
}
}
None => {
match bin {
BinType::Bin(s) => {
let splitted_name = s.rsplit('/').next().unwrap().replace("\
.js", "");
let vec = vec![ResolvedBinary{
name: splitted_name,
path: format!("{}/{}", outer_dep.1.name, s),
package_name: outer_dep.1.name.clone()
}];
req.resolved_binaries = Some(vec);
}
BinType::BinMappings(a) => {
let mut bin_vec = vec![];
a.iter().for_each(|s| {
bin_vec.push(ResolvedBinary{
name: s.0.to_string(),
path: format!("{}/{}", outer_dep.1.name, s.1),
package_name: outer_dep.1.name.clone()
});
},
None => match bin {
BinType::Bin(s) => {
let splitted_name = s.rsplit('/').next().unwrap().replace(
"\
.js",
"",
);
let vec = vec![ResolvedBinary {
name: splitted_name,
path: format!("{}/{}", outer_dep.1.name, s),
package_name: outer_dep.1.name.clone(),
}];
req.resolved_binaries = Some(vec);
}
BinType::BinMappings(a) => {
let mut bin_vec = vec![];
a.iter().for_each(|s| {
bin_vec.push(ResolvedBinary {
name: s.0.to_string(),
path: format!("{}/{}", outer_dep.1.name, s.1),
package_name: outer_dep.1.name.clone(),
});
req.resolved_binaries = Some(bin_vec);
}
});
req.resolved_binaries = Some(bin_vec);
}
}
},
}
}
}
Expand All @@ -83,25 +82,32 @@ impl PeerResolver {
#[async_trait]
impl Pipe<PackageRecorder> for PeerResolver {
async fn run(&mut self) -> Result<PackageRecorder, ExecutionError> {
self.recorder.sub_dependencies.clone().iter().for_each(|outer_dep|{
if let Some(mut deps) = outer_dep.1.depth_traces.clone() {
// Iterate over the traces
deps.iter_mut().for_each(|d|{
d.reverse();
for (i, parent_of_outer_dep) in d.iter().enumerate() {
// This is a direct parent
if i == 0 {
if let Some(reg) = self.recorder.sub_dependencies.get_mut(parent_of_outer_dep) {
PeerResolver::handle_insert(reg, outer_dep);
} else if let Some(req) = self.recorder.main_packages.get_mut
(parent_of_outer_dep) {
PeerResolver::handle_insert(req, outer_dep);
}
self.recorder
.sub_dependencies
.clone()
.iter()
.for_each(|outer_dep| {
if let Some(mut deps) = outer_dep.1.depth_traces.clone() {
// Iterate over the traces
deps.iter_mut().for_each(|d| {
d.reverse();
for (i, parent_of_outer_dep) in d.iter().enumerate() {
// This is a direct parent
if i == 0 {
if let Some(reg) =
self.recorder.sub_dependencies.get_mut(parent_of_outer_dep)
{
PeerResolver::handle_insert(reg, outer_dep);
} else if let Some(req) =
self.recorder.main_packages.get_mut(parent_of_outer_dep)
{
PeerResolver::handle_insert(req, outer_dep);
}
}
}
}
})
}
});
})
}
});
Ok(self.recorder.clone())
}
}
}
2 changes: 1 addition & 1 deletion src/cache/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::fs::get_config_dir;
use crate::{contracts::PersistentCache, errors::CacheError, package::NpmPackage};
use async_trait::async_trait;
use nodejs_semver::{Range, Version};
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::{collections::HashMap, fs::File, io, path::PathBuf};
use serde::{Deserialize, Serialize};

//
#[derive(Eq, Debug, Hash, PartialEq, Clone, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit 96096d5

Please sign in to comment.