Skip to content

Commit

Permalink
refactor: forge -> backend (#2227)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored May 31, 2024
1 parent 0e0db86 commit 27ae23b
Show file tree
Hide file tree
Showing 86 changed files with 646 additions and 620 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions src/forge/asdf.rs → src/backend/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ use itertools::Itertools;
use rayon::prelude::*;
use url::Url;

use crate::backend::{ABackend, Backend, BackendList, BackendType};
use crate::cache::CacheManager;
use crate::cli::args::ForgeArg;
use crate::cli::args::BackendArg;
use crate::config::{Config, Settings};
use crate::default_shorthands::{DEFAULT_SHORTHANDS, TRUSTED_SHORTHANDS};
use crate::env::MISE_FETCH_REMOTE_VERSIONS_TIMEOUT;
use crate::env_diff::{EnvDiff, EnvDiffOperation};
use crate::errors::Error::PluginNotInstalled;
use crate::file::{display_path, remove_all};
use crate::forge::{AForge, Forge, ForgeList, ForgeType};
use crate::git::Git;
use crate::hash::hash_to_str;
use crate::http::HTTP_FETCH;
Expand All @@ -40,7 +40,7 @@ use crate::{dirs, env, file, http};

/// This represents a plugin installed to ~/.local/share/mise/plugins
pub struct Asdf {
pub fa: ForgeArg,
pub fa: BackendArg,
pub name: String,
pub plugin_path: PathBuf,
pub repo_url: Option<String>,
Expand All @@ -61,7 +61,7 @@ impl Asdf {
toml_path = plugin_path.join("rtx.plugin.toml");
}
let toml = MisePluginToml::from_file(&toml_path).unwrap();
let fa = ForgeArg::new(ForgeType::Asdf, &name);
let fa = BackendArg::new(BackendType::Asdf, &name);
Self {
script_man: build_script_man(&name, &plugin_path),
cache: ExternalPluginCache::default(),
Expand Down Expand Up @@ -93,10 +93,10 @@ impl Asdf {
}
}

pub fn list() -> Result<ForgeList> {
pub fn list() -> Result<BackendList> {
Ok(file::dir_subdirs(&dirs::PLUGINS)?
.into_par_iter()
.map(|name| Arc::new(Self::new(name)) as AForge)
.map(|name| Arc::new(Self::new(name)) as ABackend)
.collect())
}

Expand Down Expand Up @@ -451,17 +451,17 @@ impl Hash for Asdf {
}
}

impl Forge for Asdf {
fn fa(&self) -> &ForgeArg {
impl Backend for Asdf {
fn fa(&self) -> &BackendArg {
&self.fa
}

fn get_plugin_type(&self) -> PluginType {
PluginType::Asdf
}

fn get_dependencies(&self, tvr: &ToolRequest) -> Result<Vec<ForgeArg>> {
let out = match tvr.forge().name.as_str() {
fn get_dependencies(&self, tvr: &ToolRequest) -> Result<Vec<BackendArg>> {
let out = match tvr.backend().name.as_str() {
"poetry" | "pipenv" | "pipx" => vec!["python"],
"elixir" => vec!["erlang"],
_ => vec![],
Expand Down
44 changes: 22 additions & 22 deletions src/forge/forge_meta.rs → src/backend/backend_meta.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
use serde::{Deserialize, Serialize};

use crate::cli::args::ForgeArg;
use crate::cli::args::BackendArg;
use crate::{dirs, file};

use super::ForgeType;
use super::BackendType;

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct ForgeMeta {
pub struct BackendMeta {
pub id: String,
pub name: String,
pub forge_type: String,
pub backend_type: String,
}

pub const FORGE_META_FILENAME: &str = ".mise.forge.json";
pub const FORGE_META_FILENAME: &str = ".mise.backend.json";

impl ForgeMeta {
pub fn read(dirname: &str) -> ForgeMeta {
impl BackendMeta {
pub fn read(dirname: &str) -> BackendMeta {
let meta_path = &dirs::INSTALLS.join(dirname).join(FORGE_META_FILENAME);
let json = file::read_to_string(meta_path).unwrap_or_default();
serde_json::from_str(&json).unwrap_or(Self::default_meta(dirname))
}

pub fn write(fa: &ForgeArg) -> eyre::Result<()> {
if fa.forge_type == ForgeType::Asdf {
pub fn write(fa: &BackendArg) -> eyre::Result<()> {
if fa.backend_type == BackendType::Asdf {
return Ok(());
}
let meta = ForgeMeta {
let meta = BackendMeta {
id: fa.id.clone(),
name: fa.name.clone(),
forge_type: fa.forge_type.as_ref().to_string(),
backend_type: fa.backend_type.as_ref().to_string(),
};

let json = serde_json::to_string(&meta).expect("Could not encode JSON value");
Expand All @@ -37,30 +37,30 @@ impl ForgeMeta {
Ok(())
}

// Returns a ForgeMeta derived from the dirname for forges without a meta file
fn default_meta(dirname: &str) -> ForgeMeta {
// Returns a BackendMeta derived from the dirname for backends without a meta file
fn default_meta(dirname: &str) -> BackendMeta {
let id = dirname.replacen('-', ":", 1);
match id.split_once(':') {
Some((forge_type, name)) => {
let name = Self::name_for_type(name.to_string(), forge_type);
let id = format!("{}:{}", forge_type, name);
ForgeMeta {
Some((backend_type, name)) => {
let name = Self::name_for_type(name.to_string(), backend_type);
let id = format!("{}:{}", backend_type, name);
BackendMeta {
id,
name,
forge_type: forge_type.to_string(),
backend_type: backend_type.to_string(),
}
}
None => ForgeMeta {
None => BackendMeta {
id: id.to_string(),
name: id.to_string(),
forge_type: ForgeType::Asdf.as_ref().to_string(),
backend_type: BackendType::Asdf.as_ref().to_string(),
},
}
}

// TODO: remove this when backends come out of experimental
fn name_for_type(name: String, forge_type: &str) -> String {
match forge_type {
fn name_for_type(name: String, backend_type: &str) -> String {
match backend_type {
"go" => name.replace('-', "/"),
"npm" => {
if name.contains('@') {
Expand Down
22 changes: 11 additions & 11 deletions src/forge/cargo.rs → src/backend/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ use std::fmt::Debug;
use serde_json::Deserializer;
use url::Url;

use crate::backend::{Backend, BackendType};
use crate::cache::CacheManager;
use crate::cli::args::ForgeArg;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::{Config, Settings};
use crate::env::GITHUB_TOKEN;
use crate::file;
use crate::forge::{Forge, ForgeType};
use crate::http::HTTP_FETCH;
use crate::install_context::InstallContext;
use crate::toolset::ToolRequest;

#[derive(Debug)]
pub struct CargoForge {
fa: ForgeArg,
pub struct CargoBackend {
fa: BackendArg,
remote_version_cache: CacheManager<Vec<String>>,
}

impl Forge for CargoForge {
fn get_type(&self) -> ForgeType {
ForgeType::Cargo
impl Backend for CargoBackend {
fn get_type(&self) -> BackendType {
BackendType::Cargo
}

fn fa(&self) -> &ForgeArg {
fn fa(&self) -> &BackendArg {
&self.fa
}

fn get_dependencies(&self, _tvr: &ToolRequest) -> eyre::Result<Vec<ForgeArg>> {
fn get_dependencies(&self, _tvr: &ToolRequest) -> eyre::Result<Vec<BackendArg>> {
Ok(vec!["cargo".into(), "rust".into()])
}

Expand Down Expand Up @@ -76,9 +76,9 @@ impl Forge for CargoForge {
}
}

impl CargoForge {
impl CargoBackend {
pub fn new(name: String) -> Self {
let fa = ForgeArg::new(ForgeType::Cargo, &name);
let fa = BackendArg::new(BackendType::Cargo, &name);
Self {
remote_version_cache: CacheManager::new(
fa.cache_path.join("remote_versions-$KEY.msgpack.z"),
Expand Down
22 changes: 11 additions & 11 deletions src/forge/go.rs → src/backend/go.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use std::fmt::Debug;

use crate::backend::{Backend, BackendType};
use crate::cache::CacheManager;
use crate::cli::args::ForgeArg;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::config::Settings;
use crate::forge::{Forge, ForgeType};
use crate::install_context::InstallContext;
use crate::toolset::ToolRequest;

#[derive(Debug)]
pub struct GoForge {
fa: ForgeArg,
pub struct GoBackend {
fa: BackendArg,
remote_version_cache: CacheManager<Vec<String>>,
}

impl Forge for GoForge {
fn get_type(&self) -> ForgeType {
ForgeType::Go
impl Backend for GoBackend {
fn get_type(&self) -> BackendType {
BackendType::Go
}

fn fa(&self) -> &ForgeArg {
fn fa(&self) -> &BackendArg {
&self.fa
}

fn get_dependencies(&self, _tvr: &ToolRequest) -> eyre::Result<Vec<ForgeArg>> {
fn get_dependencies(&self, _tvr: &ToolRequest) -> eyre::Result<Vec<BackendArg>> {
Ok(vec!["go".into()])
}

Expand Down Expand Up @@ -82,9 +82,9 @@ impl Forge for GoForge {
}
}

impl GoForge {
impl GoBackend {
pub fn new(name: String) -> Self {
let fa = ForgeArg::new(ForgeType::Go, &name);
let fa = BackendArg::new(BackendType::Go, &name);
Self {
remote_version_cache: CacheManager::new(
fa.cache_path.join("remote_versions-$KEY.msgpack.z"),
Expand Down
Loading

0 comments on commit 27ae23b

Please sign in to comment.