2020-03-02 03:29:27 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
|
|
|
|
|
|
|
use crate::configs::elixir::ElixirConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
|
|
|
use crate::utils;
|
2020-03-02 03:29:27 +00:00
|
|
|
|
2021-01-20 17:59:21 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2020-07-07 22:45:32 +00:00
|
|
|
use regex::Regex;
|
2021-01-20 17:59:21 +00:00
|
|
|
use std::ops::Deref;
|
2020-03-02 03:29:27 +00:00
|
|
|
const ELIXIR_VERSION_PATTERN: &str = "\
|
|
|
|
Erlang/OTP (?P<otp>\\d+)[^\\n]+
|
|
|
|
|
|
|
|
Elixir (?P<elixir>\\d[.\\d]+).*";
|
|
|
|
|
|
|
|
/// Create a module with the current Elixir version
|
|
|
|
///
|
2020-07-07 22:45:32 +00:00
|
|
|
/// Will display the Elixir version if any of the following criteria are met:
|
2020-03-02 03:29:27 +00:00
|
|
|
/// - Current directory contains a `mix.exs` file
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|
|
|
let is_elixir_project = context.try_begin_scan()?.set_files(&["mix.exs"]).is_match();
|
|
|
|
|
|
|
|
if !is_elixir_project {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-01-20 17:59:21 +00:00
|
|
|
let versions = Lazy::new(get_elixir_version);
|
2020-03-02 03:29:27 +00:00
|
|
|
|
|
|
|
let mut module = context.new_module("elixir");
|
|
|
|
let config = ElixirConfig::try_load(module.config);
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2021-01-20 17:59:21 +00:00
|
|
|
"version" => versions
|
|
|
|
.deref()
|
|
|
|
.as_ref()
|
|
|
|
.map(|(_, elixir_version)| elixir_version)
|
|
|
|
.map(Ok),
|
|
|
|
"otp_version" => versions
|
|
|
|
.deref()
|
|
|
|
.as_ref()
|
|
|
|
.map(|(otp_version, _)| otp_version)
|
|
|
|
.map(Ok),
|
2020-07-07 22:45:32 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `elixir`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2020-03-02 03:29:27 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_elixir_version() -> Option<(String, String)> {
|
|
|
|
let output = utils::exec_cmd("elixir", &["--version"])?.stdout;
|
|
|
|
|
|
|
|
parse_elixir_version(&output)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_elixir_version(version: &str) -> Option<(String, String)> {
|
|
|
|
let version_regex = Regex::new(ELIXIR_VERSION_PATTERN).ok()?;
|
|
|
|
let captures = version_regex.captures(version)?;
|
|
|
|
|
|
|
|
let otp_version = captures["otp"].to_owned();
|
|
|
|
let elixir_version = captures["elixir"].to_owned();
|
|
|
|
|
|
|
|
Some((otp_version, elixir_version))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-08-07 19:13:12 +00:00
|
|
|
use crate::test::ModuleRenderer;
|
2020-03-02 03:29:27 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_elixir_version() {
|
|
|
|
const OUTPUT: &str = "\
|
|
|
|
Erlang/OTP 22 [erts-10.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
|
|
|
|
|
|
|
|
Elixir 1.10 (compiled with Erlang/OTP 22)
|
|
|
|
";
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
parse_elixir_version(OUTPUT),
|
|
|
|
Some(("22".to_owned(), "1.10".to_owned()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_without_mix_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let expected = None;
|
2020-08-07 19:13:12 +00:00
|
|
|
let output = ModuleRenderer::new("elixir").path(dir.path()).collect();
|
2020-03-02 03:29:27 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-03-02 03:29:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_mix_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("mix.exs"))?.sync_all()?;
|
|
|
|
|
|
|
|
let expected = Some(format!(
|
2021-01-20 17:59:21 +00:00
|
|
|
"via {}",
|
|
|
|
Color::Purple.bold().paint("💧 1.10 (OTP 22) ")
|
2020-03-02 03:29:27 +00:00
|
|
|
));
|
2020-08-07 19:13:12 +00:00
|
|
|
let output = ModuleRenderer::new("elixir").path(dir.path()).collect();
|
2020-03-02 03:29:27 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
2020-03-15 17:12:25 +00:00
|
|
|
dir.close()
|
2020-03-02 03:29:27 +00:00
|
|
|
}
|
|
|
|
}
|