Skip to content

Commit

Permalink
feat: elixir core tool
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 16, 2024
1 parent 8d5214d commit 798f74c
Show file tree
Hide file tree
Showing 27 changed files with 138 additions and 9 deletions.
11 changes: 11 additions & 0 deletions docs/lang/elixir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Elixir <Badge type="warning" text="experimental" />

Swift is supported for macos and linux.

## Usage

Use the latest stable version of elixir:

```sh
mise use -g erlang elixir
```
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.
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 Down Expand Up @@ -39,12 +40,14 @@ mod rust;
mod swift;
#[cfg(unix)]
mod zig;
mod elixir;

pub static CORE_PLUGINS: Lazy<BackendMap> = Lazy::new(|| {
#[cfg(unix)]
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

0 comments on commit 798f74c

Please sign in to comment.