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

refactor: simplify ForgeArg building #2208

Merged
merged 1 commit into from
May 28, 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
20 changes: 12 additions & 8 deletions src/cli/args/forge_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ use crate::registry::REGISTRY;

#[derive(Clone, PartialOrd, Ord)]
pub struct ForgeArg {
/// user-specified identifier, "node", "npm:prettier", "cargo:eza", "vfox:version-fox/vfox-nodejs"
/// multiple ids may point to a single tool, e.g.: "node", "core:node" or "vfox:version-fox/vfox-nodejs"
/// and "vfox:https://github.com/version-fox/vfox-nodejs"
pub id: String,
/// the name of the tool within the forge, e.g.: "node", "prettier", "eza", "vfox-nodejs"
pub name: String,
/// type of forge, "asdf", "cargo", "core", "npm", "vfox"
pub forge_type: ForgeType,
/// ~/.local/share/mise/cache/<THIS>
pub cache_path: PathBuf,
/// ~/.local/share/mise/installs/<THIS>
pub installs_path: PathBuf,
/// ~/.local/share/mise/downloads/<THIS>
pub downloads_path: PathBuf,
}

impl From<&str> for ForgeArg {
fn from(s: &str) -> Self {
impl<A: AsRef<str>> From<A> for ForgeArg {
fn from(s: A) -> Self {
let s = s.as_ref();
if let Some(fa) = FORGE_MAP.get(s) {
return fa.clone();
}
Expand All @@ -32,18 +41,13 @@ impl From<&str> for ForgeArg {
Self::new(ForgeType::Asdf, s)
}
}
impl From<&String> for ForgeArg {
fn from(s: &String) -> Self {
Self::from(s.as_str())
}
}

impl ForgeArg {
pub fn new(forge_type: ForgeType, name: &str) -> Self {
let name = unalias_forge(name).to_string();
let id = match forge_type {
ForgeType::Asdf | ForgeType::Core => name.clone(),
forge_type => format!("{}:{}", forge_type.as_ref(), name),
forge_type => format!("{forge_type}:{name}"),
};
let pathname = regex!(r#"[/:]"#).replace_all(&id, "-").to_string();
Self {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct BunPlugin {

impl BunPlugin {
pub fn new() -> Self {
let core = CorePlugin::new("bun");
let core = CorePlugin::new("bun".into());
Self { core }
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct DenoPlugin {

impl DenoPlugin {
pub fn new() -> Self {
let core = CorePlugin::new("deno");
let core = CorePlugin::new("deno".into());
Self { core }
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const KERL_VERSION: &str = "4.1.1";
impl ErlangPlugin {
pub fn new() -> Self {
Self {
core: CorePlugin::new("erlang"),
core: CorePlugin::new("erlang".into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct GoPlugin {
impl GoPlugin {
pub fn new() -> Self {
Self {
core: CorePlugin::new("go"),
core: CorePlugin::new("go".into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct JavaPlugin {

impl JavaPlugin {
pub fn new() -> Self {
let core = CorePlugin::new("java");
let core = CorePlugin::new("java".into());
let java_metadata_ga_cache_filename =
format!("java_metadata_ga_{}_{}-$KEY.msgpack.z", os(), arch());
let java_metadata_ea_cache_filename =
Expand Down
9 changes: 3 additions & 6 deletions src/plugins/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::cache::CacheManager;
use crate::cli::args::ForgeArg;
use crate::config::Settings;
use crate::env;
use crate::forge::{Forge, ForgeList, ForgeType};
use crate::forge::{Forge, ForgeList};
use crate::http::HTTP_FETCH;
use crate::plugins::core::bun::BunPlugin;
use crate::plugins::core::deno::DenoPlugin;
Expand Down Expand Up @@ -55,15 +55,12 @@ pub static CORE_PLUGINS: Lazy<ForgeList> = Lazy::new(|| {
#[derive(Debug)]
pub struct CorePlugin {
pub fa: ForgeArg,
pub name: &'static str,
pub remote_version_cache: CacheManager<Vec<String>>,
}

impl CorePlugin {
pub fn new(name: &'static str) -> Self {
let fa = ForgeArg::new(ForgeType::Asdf, name);
pub fn new(fa: ForgeArg) -> Self {
Self {
name,
remote_version_cache: CacheManager::new(
fa.cache_path.join("remote_versions-$KEY.msgpack.z"),
)
Expand All @@ -90,7 +87,7 @@ impl CorePlugin {
if !*env::MISE_USE_VERSIONS_HOST {
return Ok(None);
}
let raw = HTTP_FETCH.get_text(format!("http://mise-versions.jdx.dev/{}", &self.name))?;
let raw = HTTP_FETCH.get_text(format!("http://mise-versions.jdx.dev/{}", &self.fa.name))?;
let versions = raw
.lines()
.map(|v| v.trim().to_string())
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct NodePlugin {
impl NodePlugin {
pub fn new() -> Self {
Self {
core: CorePlugin::new("node"),
core: CorePlugin::new("node".into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct PythonPlugin {

impl PythonPlugin {
pub fn new() -> Self {
let core = CorePlugin::new("python");
let core = CorePlugin::new("python".into());
Self {
precompiled_cache: CacheManager::new(
core.fa.cache_path.join("precompiled-$KEY.msgpack.z"),
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/ruby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct RubyPlugin {
impl RubyPlugin {
pub fn new() -> Self {
Self {
core: CorePlugin::new("ruby"),
core: CorePlugin::new("ruby".into()),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/plugins/core/zig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct ZigPlugin {

impl ZigPlugin {
pub fn new() -> Self {
let core = CorePlugin::new("zig");
let core = CorePlugin::new("zig".into());
Self { core }
}

Expand Down
Loading