Skip to content

Commit

Permalink
feat: very basic dependency support
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Mar 16, 2024
1 parent a247309 commit da391ef
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/forge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ pub trait Forge: Debug + Send + Sync {
fn get_plugin_type(&self) -> PluginType {
PluginType::Core
}
/// If any of these tools are installing in parallel, we should wait for them to finish
/// before installing this tool.
fn get_dependencies(&self, _tv: &ToolVersion) -> eyre::Result<Vec<String>> {
Ok(vec![])
}
fn list_remote_versions(&self) -> eyre::Result<Vec<String>>;
fn latest_stable_version(&self) -> eyre::Result<Option<String>> {
self.latest_version(Some("latest".into()))
Expand Down
5 changes: 5 additions & 0 deletions src/forge/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::config::{Config, Settings};

use crate::forge::{Forge, ForgeType};
use crate::install_context::InstallContext;
use crate::toolset::ToolVersion;
use serde_json::Value;

#[derive(Debug)]
Expand All @@ -25,6 +26,10 @@ impl Forge for NPMForge {
&self.fa
}

fn get_dependencies(&self, _tv: &ToolVersion) -> eyre::Result<Vec<String>> {
Ok(vec!["node".into()])
}

fn list_remote_versions(&self) -> eyre::Result<Vec<String>> {
self.remote_version_cache
.get_or_try_init(|| {
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/external_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,16 @@ impl Forge for ExternalPlugin {
fn get_plugin_type(&self) -> PluginType {
PluginType::External
}

fn get_dependencies(&self, tv: &ToolVersion) -> Result<Vec<String>> {
let out = match tv.forge.name.as_str() {
"poetry" | "pipenv" => vec!["python"],
"elixir" => vec!["erlang"],
_ => vec![],
};
Ok(out.into_iter().map(|s| s.into()).collect())
}

fn list_remote_versions(&self) -> Result<Vec<String>> {
self.remote_version_cache
.get_or_try_init(|| self.fetch_remote_versions())
Expand Down
16 changes: 16 additions & 0 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread;
use std::thread::sleep;
use std::time::Duration;

use console::truncate_str;
use eyre::Result;
Expand Down Expand Up @@ -154,15 +156,28 @@ impl Toolset {
true => 1,
false => opts.jobs.unwrap_or(settings.jobs),
};
let installing: HashSet<String> = HashSet::new();
let installing = Arc::new(Mutex::new(installing));
thread::scope(|s| {
(0..jobs)
.map(|_| {
let queue = queue.clone();
let installing = installing.clone();
let ts = &*self;
s.spawn(move || {
let next_job = || queue.lock().unwrap().pop();
while let Some((t, versions)) = next_job() {
installing.lock().unwrap().insert(t.id().into());
for tv in versions {
for dep in t.get_dependencies(&tv)? {
while installing.lock().unwrap().contains(dep.as_str()) {
trace!(
"{tv} waiting for dependency {} to finish installing",
dep
);
sleep(Duration::from_millis(100));
}
}
let tv = tv.request.resolve(
t.as_ref(),
tv.opts.clone(),
Expand All @@ -177,6 +192,7 @@ impl Toolset {
};
t.install_version(ctx)?;
}
installing.lock().unwrap().remove(t.id());
}
Ok(())
})
Expand Down

0 comments on commit da391ef

Please sign in to comment.