diff --git a/src/providers/elixir.rs b/src/providers/elixir.rs index d91f2a7ab..53ff570f3 100644 --- a/src/providers/elixir.rs +++ b/src/providers/elixir.rs @@ -40,6 +40,66 @@ impl Provider for ElixirProvider { fn start(&self, _app: &App, _env: &Environment) -> Result> { 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 { + fn as_default(v: Option) -> &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)), + } } }