Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add platform field to registry backends #3626

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 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,11 @@ 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 docs/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ You can also specify the full name for a tool using `mise use aqua:1password/cli
| balena | [ubi:balena-io/balena-cli](https://github.com/balena-io/balena-cli) [asdf:boatkit-io/asdf-balena-cli](https://github.com/boatkit-io/asdf-balena-cli) |
| bashbot | [aqua:mathew-fleisch/bashbot](https://github.com/mathew-fleisch/bashbot) [asdf:mathew-fleisch/asdf-bashbot](https://github.com/mathew-fleisch/asdf-bashbot) |
| bashly | [asdf:pcrockett/asdf-bashly](https://github.com/pcrockett/asdf-bashly) |
| bat | [ubi:sharkdp/bat](https://github.com/sharkdp/bat) [asdf:https://gitlab.com/wt0f/asdf-bat](https://gitlab.com/wt0f/asdf-bat) |
| bat | [ubi:sharkdp/bat](https://github.com/sharkdp/bat) [cargo:bat](https://crates.io/crates/bat) [asdf:https://gitlab.com/wt0f/asdf-bat](https://gitlab.com/wt0f/asdf-bat) |
| bat-extras | [asdf:vhdirk/asdf-bat-extras](https://github.com/vhdirk/asdf-bat-extras) |
| bats | [aqua:bats-core/bats-core](https://github.com/bats-core/bats-core) [asdf:timgluz/asdf-bats](https://github.com/timgluz/asdf-bats) |
| bazel | [ubi:bazelbuild/bazel](https://github.com/bazelbuild/bazel) [asdf:rajatvig/asdf-bazel](https://github.com/rajatvig/asdf-bazel) |
Expand Down
10 changes: 9 additions & 1 deletion registry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ 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
19 changes: 16 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,21 @@ 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
Loading