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

feat: elixir core tool #3620

Merged
merged 1 commit into from
Dec 16, 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
5 changes: 3 additions & 2 deletions docs/core-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
`mise` comes with some plugins built into the CLI written in Rust. These are new and will improve over
time.

They can be easily overridden by installing a plugin with the same name, e.g.: `mise plugin install python https://github.com/asdf-community/asdf-python`.
They can be easily overridden by installing an asdf/vfox plugin with the same name, e.g.: `mise plugin install python https://github.com/asdf-community/asdf-python`.

You can see the core plugins with `mise plugin ls --core`.
You can see the core plugins with `mise registry -b core`.

- [Bun](/lang/bun)
- [Deno](/lang/deno)
- [Elixir](/lang/elixir)
- [Erlang](/lang/erlang)
- [Go](/lang/go)
- [Java](/lang/java)
Expand Down
9 changes: 9 additions & 0 deletions docs/lang/elixir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Elixir <Badge type="warning" text="experimental" />

## Usage

Use the latest stable version of elixir:

```sh
mise use -g erlang elixir
```
2 changes: 1 addition & 1 deletion docs/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ You can also specify the full name for a tool using `mise use aqua:1password/cli
| ejson | [aqua:Shopify/ejson](https://github.com/Shopify/ejson) [asdf:cipherstash/asdf-ejson](https://github.com/cipherstash/asdf-ejson) |
| eksctl | [aqua:eksctl-io/eksctl](https://github.com/eksctl-io/eksctl) [asdf:elementalvoid/asdf-eksctl](https://github.com/elementalvoid/asdf-eksctl) |
| elasticsearch | [asdf:asdf-community/asdf-elasticsearch](https://github.com/asdf-community/asdf-elasticsearch) |
| elixir | [asdf:mise-plugins/mise-elixir](https://github.com/mise-plugins/mise-elixir) [vfox:version-fox/vfox-elixir](https://github.com/version-fox/vfox-elixir) |
| elixir | [core:elixir](https://mise.jdx.dev/lang/elixir.html) |
| elixir-ls | [asdf:juantascon/asdf-elixir-ls](https://github.com/juantascon/asdf-elixir-ls) |
| elm | [ubi:elm/compiler](https://github.com/elm/compiler) [asdf:asdf-community/asdf-elm](https://github.com/asdf-community/asdf-elm) |
| emsdk | [asdf:RobLoach/asdf-emsdk](https://github.com/RobLoach/asdf-emsdk) |
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions e2e/core/test_erlang_slow
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

assert "mise use erlang@27.2"
assert_contains "mise x -- erl -eval 'erlang:display(erlang:system_info(otp_release)), halt().' -noshell" "27"
assert_contains "mise x elixir@1.17.3 -- elixir --version 2>&1" "Elixir 1.17.3"
assert_contains "mise x elixir@1.17.3 -- mix --version 2>&1" "Mix 1.17.3"
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions e2e/plugins/core/test_erlang_slow

This file was deleted.

2 changes: 1 addition & 1 deletion registry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ editorconfig-checker.test = ["ec --version", "v{{version}}"]
ejson.backends = ["aqua:Shopify/ejson", "asdf:cipherstash/asdf-ejson"]
eksctl.backends = ["aqua:eksctl-io/eksctl", "asdf:elementalvoid/asdf-eksctl"]
elasticsearch.backends = ["asdf:asdf-community/asdf-elasticsearch"]
elixir.backends = ["asdf:mise-plugins/mise-elixir", "vfox:version-fox/vfox-elixir"]
elixir.backends = ["core:elixir"]
elixir.depends = ["erlang"]
elixir-ls.backends = ["asdf:juantascon/asdf-elixir-ls"]
elm.backends = ["ubi:elm/compiler[exe=elm]", "asdf:asdf-community/asdf-elm"]
Expand Down
115 changes: 115 additions & 0 deletions src/plugins/core/elixir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
use std::path::{Path, PathBuf};

use crate::backend::Backend;
use crate::cli::args::BackendArg;
use crate::cmd::CmdLineRunner;
use crate::http::{HTTP, HTTP_FETCH};
use crate::install_context::InstallContext;
use crate::plugins::VERSION_REGEX;
use crate::toolset::ToolVersion;
use crate::ui::progress_report::SingleReport;
use crate::{file, plugins};
use eyre::Result;
use itertools::Itertools;
use versions::Versioning;
use xx::regex;

#[derive(Debug)]
pub struct ElixirPlugin {
ba: BackendArg,
}

impl ElixirPlugin {
pub fn new() -> Self {
Self {
ba: plugins::core::new_backend_arg("elixir"),
}
}

fn elixir_bin(&self, tv: &ToolVersion) -> PathBuf {
tv.install_path().join("bin").join("elixir")
}

fn test_elixir(&self, ctx: &InstallContext, tv: &ToolVersion) -> Result<()> {
ctx.pr.set_message("elixir --version".into());
CmdLineRunner::new(self.elixir_bin(tv))
.with_pr(ctx.pr.as_ref())
.envs(self.dependency_env()?)
.arg("--version")
.execute()
}

fn download(&self, tv: &ToolVersion, pr: &dyn SingleReport) -> Result<PathBuf> {
let version = &tv.version;
let url = format!("https://builds.hex.pm/builds/elixir/v{version}.zip");

let filename = url.split('/').last().unwrap();
let tarball_path = tv.download_path().join(filename);

pr.set_message(format!("download {filename}"));
if !tarball_path.exists() {
HTTP.download_file(&url, &tarball_path, Some(pr))?;
}

Ok(tarball_path)
}

fn install(&self, ctx: &InstallContext, tv: &ToolVersion, tarball_path: &Path) -> Result<()> {
let filename = tarball_path.file_name().unwrap().to_string_lossy();
ctx.pr.set_message(format!("extract {filename}"));
file::remove_all(tv.install_path())?;
file::unzip(tarball_path, &tv.install_path())?;

Ok(())
}

fn verify(&self, ctx: &InstallContext, tv: &ToolVersion) -> Result<()> {
self.test_elixir(ctx, tv)
}
}

impl Backend for ElixirPlugin {
fn ba(&self) -> &BackendArg {
&self.ba
}

fn _list_remote_versions(&self) -> Result<Vec<String>> {
let versions: Vec<String> = HTTP_FETCH
.get_text("https://builds.hex.pm/builds/elixir/builds.txt")?
.lines()
.unique()
.filter_map(|s| s.split_once(' ').map(|(v, _)| v.trim_start_matches('v')))
.filter(|s| regex!(r"^[0-9]+\.[0-9]+\.[0-9]").is_match(s))
.sorted_by_cached_key(|s| {
(
Versioning::new(s.split_once('-').map(|(v, _)| v).unwrap_or(s)),
!VERSION_REGEX.is_match(s),
s.contains("-otp-"),
Versioning::new(s),
s.to_string(),
)
})
.map(|s| s.to_string())
.collect();
Ok(versions)
}

fn get_dependencies(&self) -> Result<Vec<&str>> {
Ok(vec!["erlang"])
}

fn install_version_(&self, ctx: &InstallContext, mut tv: ToolVersion) -> Result<ToolVersion> {
let tarball_path = self.download(&tv, ctx.pr.as_ref())?;
self.verify_checksum(ctx, &mut tv, &tarball_path)?;
self.install(ctx, &tv, &tarball_path)?;
self.verify(ctx, &tv)?;
Ok(tv)
}

fn list_bin_paths(&self, tv: &ToolVersion) -> Result<Vec<PathBuf>> {
Ok(["bin", ".mix/escripts"]
.iter()
.map(|p| tv.install_path().join(p))
.collect())
}
}
4 changes: 4 additions & 0 deletions src/plugins/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::env;
use crate::env::PATH_KEY;
use crate::plugins::core::bun::BunPlugin;
use crate::plugins::core::deno::DenoPlugin;
use crate::plugins::core::elixir::ElixirPlugin;
#[cfg(unix)]
use crate::plugins::core::erlang::ErlangPlugin;
use crate::plugins::core::go::GoPlugin;
Expand All @@ -27,6 +28,7 @@ use crate::toolset::ToolVersion;

mod bun;
mod deno;
mod elixir;
#[cfg(unix)]
mod erlang;
mod go;
Expand All @@ -45,6 +47,7 @@ pub static CORE_PLUGINS: Lazy<BackendMap> = Lazy::new(|| {
let plugins: Vec<Arc<dyn Backend>> = vec![
Arc::new(BunPlugin::new()),
Arc::new(DenoPlugin::new()),
Arc::new(ElixirPlugin::new()),
Arc::new(ErlangPlugin::new()),
Arc::new(GoPlugin::new()),
Arc::new(JavaPlugin::new()),
Expand All @@ -60,6 +63,7 @@ pub static CORE_PLUGINS: Lazy<BackendMap> = Lazy::new(|| {
Arc::new(BunPlugin::new()),
Arc::new(DenoPlugin::new()),
// Arc::new(ErlangPlugin::new()),
// Arc::new(ElixirPlugin::new()),
Arc::new(GoPlugin::new()),
Arc::new(JavaPlugin::new()),
Arc::new(NodePlugin::new()),
Expand Down
6 changes: 1 addition & 5 deletions src/plugins/core/zig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,7 @@ impl Backend for ZigPlugin {
}

#[requires(matches!(tv.request, ToolRequest::Version { .. } | ToolRequest::Prefix { .. } | ToolRequest::Ref { .. }), "unsupported tool version request type")]
fn install_version_(
&self,
ctx: &InstallContext,
mut tv: ToolVersion,
) -> eyre::Result<ToolVersion> {
fn install_version_(&self, ctx: &InstallContext, mut tv: ToolVersion) -> Result<ToolVersion> {
let tarball_path = self.download(&tv, ctx.pr.as_ref())?;
self.verify_checksum(ctx, &mut tv, &tarball_path)?;
self.install(ctx, &tv, &tarball_path)?;
Expand Down
5 changes: 4 additions & 1 deletion src/shorthands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ mod tests {
let mut settings = Settings::get().deref().clone();
settings.shorthands_file = Some("../fixtures/shorthands.toml".into());
let shorthands = get_shorthands(&settings);
assert_str_eq!(shorthands["elixir"][0], "asdf:mise-plugins/mise-elixir");
assert_str_eq!(
shorthands["ephemeral-postgres"][0],
"asdf:smashedtoatoms/asdf-ephemeral-postgres"
);
assert_str_eq!(shorthands["node"][0], "https://node");
assert_str_eq!(shorthands["xxxxxx"][0], "https://xxxxxx");
}
Expand Down
Loading