Skip to content

Commit

Permalink
fix: explode backend dependencies (#2945)
Browse files Browse the repository at this point in the history
Fixes #2943
  • Loading branch information
jdx authored Nov 6, 2024
1 parent b36acc3 commit 4e3fb72
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
18 changes: 16 additions & 2 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::sync::{Arc, Mutex};
use console::style;
use contracts::requires;
use eyre::{bail, eyre, WrapErr};
use indexmap::IndexSet;
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::Regex;
Expand All @@ -23,6 +24,7 @@ use crate::file::{display_path, remove_all, remove_all_with_warning};
use crate::install_context::InstallContext;
use crate::plugins::core::{CorePlugin, CORE_PLUGINS};
use crate::plugins::{Plugin, PluginType, VERSION_REGEX};
use crate::registry::REGISTRY_BACKEND_MAP;
use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::{is_outdated_version, ToolRequest, ToolSource, ToolVersion, Toolset};
use crate::ui::progress_report::SingleReport;
Expand Down Expand Up @@ -194,14 +196,26 @@ pub trait Backend: Debug + Send + Sync {
Ok(vec![])
}
fn get_all_dependencies(&self, tvr: &ToolRequest) -> eyre::Result<Vec<BackendArg>> {
let mut deps = self.get_dependencies(tvr)?;
let mut deps: IndexSet<_> = self
.get_dependencies(tvr)?
.into_iter()
.flat_map(|fa| {
let short = fa.short.clone();
[fa].into_iter().chain(
REGISTRY_BACKEND_MAP
.get(short.as_str())
.cloned()
.unwrap_or_default(),
)
})
.collect();
let dep_backends = deps.iter().map(|fa| fa.into()).collect::<Vec<ABackend>>();
for dep in dep_backends {
// TODO: pass the right tvr
let tvr = ToolRequest::System(dep.id().into(), ToolSource::Unknown);
deps.extend(dep.get_all_dependencies(&tvr)?);
}
Ok(deps)
Ok(deps.into_iter().collect())
}

fn list_remote_versions(&self) -> eyre::Result<Vec<String>> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/args/backend_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct BackendArg {
impl<A: AsRef<str>> From<A> for BackendArg {
fn from(s: A) -> Self {
let s = s.as_ref();
if let Some(fa) = REGISTRY_BACKEND_MAP.get(s) {
if let Some(fa) = REGISTRY_BACKEND_MAP.get(s).and_then(|rbm| rbm.first()) {
fa.clone()
} else {
Self::new(s, s)
Expand Down
4 changes: 2 additions & 2 deletions src/cli/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ pub struct Registry {
impl Registry {
pub fn run(self) -> Result<()> {
if let Some(name) = &self.name {
if let Some(full) = REGISTRY.get(name.as_str()) {
if let Some(full) = REGISTRY.get(name.as_str()).and_then(|r| r.first()) {
miseprintln!("{full}");
} else {
bail!("tool not found in registry: {name}");
}
} else {
let data = REGISTRY
.iter()
.map(|(short, full)| (short.to_string(), full.to_string()).into())
.map(|(short, full)| (short.to_string(), full.first().unwrap().to_string()).into())
.collect::<Vec<Row>>();
let mut table = Table::new(data);
table::default_style(&mut table, false);
Expand Down
23 changes: 16 additions & 7 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use url::Url;
include!(concat!(env!("OUT_DIR"), "/registry.rs"));

// a rust representation of registry.toml
pub static REGISTRY: Lazy<BTreeMap<&str, String>> = Lazy::new(|| {
pub static REGISTRY: Lazy<BTreeMap<&str, Vec<String>>> = Lazy::new(|| {
let backend_types = vec!["ubi", "vfox", "asdf", "cargo", "go", "npm", "pipx", "spm"]
.into_iter()
.filter(|b| cfg!(unix) || *b != "asdf")
Expand All @@ -21,23 +21,31 @@ pub static REGISTRY: Lazy<BTreeMap<&str, String>> = Lazy::new(|| {
_REGISTRY
.iter()
.filter(|(id, _)| !CORE_PLUGINS.contains_key(*id))
.filter_map(|(id, fulls)| {
fulls
.map(|(id, fulls)| {
let fulls = fulls
.iter()
.find(|full| {
.filter(|full| {
full.split(':')
.next()
.map_or(false, |b| backend_types.contains(b))
})
.map(|full| (*id, full.to_string()))
.map(|full| full.to_string())
.collect::<Vec<_>>();
(*id, fulls)
})
.filter(|(_, fulls)| !fulls.is_empty())
.collect()
});

pub static REGISTRY_BACKEND_MAP: Lazy<HashMap<&'static str, BackendArg>> = Lazy::new(|| {
pub static REGISTRY_BACKEND_MAP: Lazy<HashMap<&'static str, Vec<BackendArg>>> = Lazy::new(|| {
REGISTRY
.iter()
.map(|(short, full)| (*short, BackendArg::new(short, full)))
.map(|(short, full)| {
(
*short,
full.iter().map(|f| BackendArg::new(short, f)).collect(),
)
})
.collect()
});

Expand Down Expand Up @@ -66,6 +74,7 @@ pub fn is_trusted_plugin(name: &str, remote: &str) -> bool {
let normalized_url = normalize_remote(remote).unwrap_or("INVALID_URL".into());
let is_shorthand = REGISTRY
.get(name)
.and_then(|fulls| fulls.first())
.map(|full| full_to_url(full))
.is_some_and(|s| normalize_remote(&s).unwrap_or_default() == normalized_url);
let is_mise_url = normalized_url.starts_with("github.com/mise-plugins/");
Expand Down
4 changes: 2 additions & 2 deletions src/shorthands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub fn get_shorthands(settings: &Settings) -> Shorthands {
REGISTRY
.iter()
.filter(|(_, full)| {
let backend = &full.split(':').next().unwrap().to_string();
let backend = &full[0].split(':').next().unwrap().to_string();
backend == "asdf" || backend == "vfox"
})
.map(|(k, v)| (k.to_string(), v.to_string())),
.map(|(k, v)| (k.to_string(), v[0].to_string())),
);
};
if let Some(f) = &settings.shorthands_file {
Expand Down
2 changes: 1 addition & 1 deletion src/versions_host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn list_versions(ba: &BackendArg) -> eyre::Result<Option<Vec<String>>> {
let normalized_remote = normalize_remote(&remote_url).unwrap_or("INVALID_URL".into());
let shorthand_remote = REGISTRY
.get(plugin.name())
.map(|s| registry::full_to_url(s))
.map(|s| registry::full_to_url(&s[0]))
.unwrap_or_default();
if normalized_remote != normalize_remote(&shorthand_remote).unwrap_or_default() {
trace!(
Expand Down

0 comments on commit 4e3fb72

Please sign in to comment.