Skip to content

Commit

Permalink
Match ex vers
Browse files Browse the repository at this point in the history
  • Loading branch information
robjmorrissey authored and pxseu committed Oct 2, 2023
1 parent 803b789 commit ddd3851
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/providers/elixir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,66 @@ impl Provider for ElixirProvider {

fn start(&self, _app: &App, _env: &Environment) -> Result<Option<StartPhase>> {
let start_phase = StartPhase::new("mix run --no-halt".to_string());
Ok(Some(start_phase))
plan.set_start_phase(start_phase);

Ok(Some(plan))
}
}

impl ElixirProvider {
fn get_nix_elixir_package(app: &App, env: &Environment) -> Result<Pkg> {
fn as_default(v: Option<Match>) -> &str {
match v {
Some(m) => m.as_str(),
None => "_",
}
}

let mix_exs_content = app.read_file("mix.exs")?;
let custom_version = env.get_config_variable("ELIXIR_VERSION");

let mix_elixir_version_regex = Regex::new(r#"(elixir:[\s].*[> ])([0-9|\.]*)"#)?;

// If not from env variable, get it from the .elixir-version file then try to parse from mix.exs
let custom_version = if custom_version.is_some() {
custom_version
} else if custom_version.is_none() && app.includes_file(".elixir-version") {
Some(app.read_file(".elixir-version")?)
} else {
mix_elixir_version_regex
.captures(&mix_exs_content)
.map(|c| c.get(2).unwrap().as_str().to_owned())
};

// If it's still none, return default
if custom_version.is_none() {
return Ok(Pkg::new(DEFAULT_ELIXIR_PKG_NAME));
}
let custom_version = custom_version.unwrap();

// Regex for reading Elixir versions (e.g. 1.8 or 1.12)
let elixir_version_regex =
Regex::new(r#"^(?:[\sa-zA-Z-"']*)(\d*)(?:\.*)(\d*)(?:\.*\d*)(?:["']?)$"#)?;

// Capture matches
let matches = elixir_version_regex.captures(custom_version.as_str().trim());

// If no matches, just use default
if matches.is_none() {
return Ok(Pkg::new(DEFAULT_ELIXIR_PKG_NAME));
}
let matches = matches.unwrap();
let parsed_version = (as_default(matches.get(1)), as_default(matches.get(2)));

// Match major and minor versions
match parsed_version {
("1", "9") => Ok(Pkg::new("elixir_1_9")),
("1", "10") => Ok(Pkg::new("elixir_1_10")),
("1", "11") => Ok(Pkg::new("elixir_1_11")),
("1", "12") => Ok(Pkg::new("elixir_1_12")),
("1", "13") => Ok(Pkg::new("elixir_1_13")),
("1", "14") => Ok(Pkg::new("elixir_1_14")),
_ => Ok(Pkg::new(DEFAULT_ELIXIR_PKG_NAME)),
}
}
}

0 comments on commit ddd3851

Please sign in to comment.