Skip to content

Commit

Permalink
chore: add platform field to registry backends
Browse files Browse the repository at this point in the history
Fixes #3624
  • Loading branch information
jdx committed Dec 17, 2024
1 parent 3afde3a commit 8c6fae4
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
50 changes: 36 additions & 14 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use heck::ToUpperCamelCase;
use indexmap::IndexMap;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::{env, fs};

Expand Down Expand Up @@ -29,7 +28,6 @@ fn codegen_registry() {
.unwrap();

let tools = registry.get("tools").unwrap().as_table().unwrap();
let mut trusted_ids = HashSet::new();
for (short, info) in tools {
let info = info.as_table().unwrap();
let aliases = info
Expand All @@ -48,20 +46,41 @@ fn codegen_registry() {
t[1].as_str().unwrap().to_string(),
)
});
let backends = info.get("backends").unwrap().as_array().unwrap();
let mut fulls = vec![];
for backend in backends {
let mut backends = vec![];
for backend in info.get("backends").unwrap().as_array().unwrap() {
match backend {
toml::Value::String(backend) => {
fulls.push(backend.to_string());
backends.push(format!(
r##"RegistryBackend{{
full: r#"{backend}"#,
platforms: &[],
}}"##,
backend = backend
));
}
toml::Value::Table(backend) => {
fulls.push(backend.get("full").unwrap().as_str().unwrap().to_string());
if let Some(trust) = backend.get("trust") {
if trust.as_bool().unwrap() {
trusted_ids.insert(short);
}
}
let full = backend.get("full").unwrap().as_str().unwrap();
let platforms = backend
.get("platforms")
.map(|p| {
p.as_array()
.unwrap()
.iter()
.map(|p| p.as_str().unwrap().to_string())
.collect::<Vec<_>>()
})
.unwrap_or_default();
backends.push(format!(
r##"RegistryBackend{{
full: r#"{full}"#,
platforms: &[{platforms}],
}}"##,
platforms = platforms
.into_iter()
.map(|p| format!("\"{p}\""))
.collect::<Vec<_>>()
.join(", ")
));
}
_ => panic!("Unknown backend type"),
}
Expand Down Expand Up @@ -105,11 +124,14 @@ fn codegen_registry() {
})
.unwrap_or_default();
let rt = format!(
r#"RegistryTool{{short: "{short}", description: {description}, backends: vec!["{backends}"], aliases: &[{aliases}], test: &{test}, os: &[{os}], depends: &[{depends}], idiomatic_files: &[{idiomatic_files}]}}"#,
r#"RegistryTool{{short: "{short}", description: {description}, backends: &[{backends}], aliases: &[{aliases}], test: &{test}, os: &[{os}], depends: &[{depends}], idiomatic_files: &[{idiomatic_files}]}}"#,
description = description
.map(|d| format!("Some(r###\"{}\"###)", d))
.unwrap_or("None".to_string()),
backends = fulls.join("\", \""),
backends = backends
.into_iter()
.collect::<Vec<_>>()
.join(", "),
aliases = aliases
.iter()
.map(|a| format!("\"{a}\""))
Expand Down
2 changes: 1 addition & 1 deletion registry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ bashbot.backends = [
]
bashly.backends = ["asdf:pcrockett/asdf-bashly"]
# aqua is available but uses cargo
bat.backends = ["ubi:sharkdp/bat", "asdf:https://gitlab.com/wt0f/asdf-bat"]
bat.backends = [{full="ubi:sharkdp/bat",platforms=["linux", "macos", "windows"]}, "cargo:bat", "asdf:https://gitlab.com/wt0f/asdf-bat"]
bat.test = ["bat --version", "bat {{version}}"]
bat-extras.backends = ["asdf:vhdirk/asdf-bat-extras"]
bats.backends = ["aqua:bats-core/bats-core", "asdf:timgluz/asdf-bats"]
Expand Down
2 changes: 1 addition & 1 deletion src/backend/aqua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl AquaBackend {
if !id.contains("/") {
id = REGISTRY
.get(id)
.and_then(|t| t.backends.iter().find_map(|s| s.strip_prefix("aqua:")))
.and_then(|t| t.backends.iter().find_map(|s| s.full.strip_prefix("aqua:")))
.unwrap_or_else(|| {
warn!("invalid aqua tool: {}", id);
id
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/vfox_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ fn vfox_to_url(name: &str) -> eyre::Result<Url> {
let name = name.strip_prefix("vfox:").unwrap_or(name);
if let Some(rt) = registry::REGISTRY.get(name.trim_start_matches("vfox-")) {
// bun -> version-fox/vfox-bun
if let Some(full) = rt.backends.iter().find(|f| f.starts_with("vfox:")) {
return vfox_to_url(full.split_once(':').unwrap().1);
if let Some((_, tool_name)) = rt.backends.iter().find_map(|f| f.full.split_once("vfox:")) {
return vfox_to_url(tool_name);
}
}
let res = if let Some(caps) = regex!(r#"^([^/]+)/([^/]+)$"#).captures(name) {
Expand Down
14 changes: 11 additions & 3 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::cli::args::BackendArg;
use crate::config::SETTINGS;
use once_cell::sync::Lazy;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::env::consts::OS;
use std::env::consts::{ARCH, OS};
use std::fmt::Display;
use std::iter::Iterator;
use strum::IntoEnumIterator;
Expand All @@ -17,7 +17,7 @@ pub static REGISTRY: Lazy<BTreeMap<&'static str, RegistryTool>> =
pub struct RegistryTool {
pub short: &'static str,
pub description: Option<&'static str>,
pub backends: Vec<&'static str>,
pub backends: &'static [RegistryBackend],
#[allow(unused)]
pub aliases: &'static [&'static str],
pub test: &'static Option<(&'static str, &'static str)>,
Expand All @@ -26,6 +26,12 @@ pub struct RegistryTool {
pub idiomatic_files: &'static [&'static str],
}

#[derive(Debug, Clone)]
pub struct RegistryBackend {
pub full: &'static str,
pub platforms: &'static [&'static str],
}

impl RegistryTool {
pub fn backends(&self) -> Vec<&'static str> {
static BACKEND_TYPES: Lazy<HashSet<String>> = Lazy::new(|| {
Expand All @@ -42,14 +48,16 @@ impl RegistryTool {
}
backend_types
});
let platform = format!("{OS}-{ARCH}");
self.backends
.iter()
.filter(|rb| rb.platforms.is_empty() || rb.platforms.contains(&OS) || rb.platforms.contains(&ARCH) || rb.platforms.contains(&&*platform))
.map(|rb| rb.full)
.filter(|full| {
full.split(':')
.next()
.is_some_and(|b| BACKEND_TYPES.contains(b))
})
.copied()
.collect()
}

Expand Down

0 comments on commit 8c6fae4

Please sign in to comment.