2020-04-27 10:09:42 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
|
|
|
|
|
|
|
use crate::configs::erlang::ErlangConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2020-04-27 10:09:42 +00:00
|
|
|
|
|
|
|
/// Create a module with the current Erlang version
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-02-21 12:20:14 +00:00
|
|
|
let mut module = context.new_module("erlang");
|
|
|
|
let config = ErlangConfig::try_load(module.config);
|
|
|
|
|
2020-04-27 10:09:42 +00:00
|
|
|
let is_erlang_project = context
|
|
|
|
.try_begin_scan()?
|
2021-02-21 12:20:14 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
|
|
|
.set_folders(&config.detect_folders)
|
2020-04-27 10:09:42 +00:00
|
|
|
.is_match();
|
|
|
|
|
|
|
|
if !is_erlang_project {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
2021-02-11 20:34:47 +00:00
|
|
|
"version" => get_erlang_version(context).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 `erlang`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2020-04-27 10:09:42 +00:00
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
2021-02-11 20:34:47 +00:00
|
|
|
fn get_erlang_version(context: &Context) -> Option<String> {
|
|
|
|
Some(context.exec_cmd(
|
2020-04-27 10:09:42 +00:00
|
|
|
"erl",
|
|
|
|
&[
|
|
|
|
"-noshell",
|
|
|
|
"-eval",
|
2020-08-24 17:05:43 +00:00
|
|
|
"Fn=filename:join([code:root_dir(),\"releases\",erlang:system_info(otp_release),\"OTP_VERSION\"]),\
|
2020-04-27 10:09:42 +00:00
|
|
|
{ok,Content}=file:read_file(Fn),\
|
|
|
|
io:format(\"~s\",[Content]),\
|
2020-08-24 17:05:43 +00:00
|
|
|
halt(0)."
|
2020-04-27 10:09:42 +00:00
|
|
|
]
|
|
|
|
)?.stdout.trim().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-07 19:13:12 +00:00
|
|
|
use crate::test::ModuleRenderer;
|
2020-04-27 10:09:42 +00:00
|
|
|
use ansi_term::Color;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_without_config() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
|
|
|
|
let expected = None;
|
2020-08-07 19:13:12 +00:00
|
|
|
let output = ModuleRenderer::new("erlang").path(dir.path()).collect();
|
2020-04-27 10:09:42 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_with_config() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("rebar.config"))?.sync_all()?;
|
|
|
|
|
2021-01-20 17:46:26 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Red.bold().paint(" 22.1.3 ")));
|
2020-08-07 19:13:12 +00:00
|
|
|
let output = ModuleRenderer::new("erlang").path(dir.path()).collect();
|
2020-04-27 10:09:42 +00:00
|
|
|
|
|
|
|
assert_eq!(output, expected);
|
|
|
|
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
}
|