2020-12-26 14:26:50 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
|
|
|
|
|
|
|
use crate::configs::kotlin::KotlinConfig;
|
|
|
|
use crate::formatter::StringFormatter;
|
2021-04-29 21:22:20 +00:00
|
|
|
use crate::formatter::VersionFormatter;
|
2020-12-26 14:26:50 +00:00
|
|
|
|
|
|
|
use regex::Regex;
|
|
|
|
const KOTLIN_VERSION_PATTERN: &str = "(?P<version>[\\d\\.]+[\\d\\.]+[\\d\\.]+)";
|
|
|
|
|
|
|
|
/// Creates a module with the current Kotlin version
|
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2021-02-21 18:56:48 +00:00
|
|
|
let mut module = context.new_module("kotlin");
|
|
|
|
let config = KotlinConfig::try_load(module.config);
|
|
|
|
|
2020-12-26 14:26:50 +00:00
|
|
|
let is_kotlin_project = context
|
|
|
|
.try_begin_scan()?
|
2021-02-21 18:56:48 +00:00
|
|
|
.set_files(&config.detect_files)
|
|
|
|
.set_extensions(&config.detect_extensions)
|
|
|
|
.set_folders(&config.detect_folders)
|
2020-12-26 14:26:50 +00:00
|
|
|
.is_match();
|
|
|
|
|
|
|
|
if !is_kotlin_project {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 22:01:30 +00:00
|
|
|
"version" => {
|
2021-07-29 18:27:46 +00:00
|
|
|
let kotlin_version = get_kotlin_version(context, config.kotlin_binary)?;
|
2021-04-29 21:22:20 +00:00
|
|
|
VersionFormatter::format_module_version(
|
|
|
|
module.get_name(),
|
|
|
|
&kotlin_version,
|
|
|
|
config.version_format,
|
|
|
|
)
|
|
|
|
.map(Ok)
|
2021-01-21 22:01:30 +00:00
|
|
|
}
|
2020-12-26 14:26:50 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `kotlin`:\n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Some(module)
|
|
|
|
}
|
|
|
|
|
2021-02-11 20:34:47 +00:00
|
|
|
fn get_kotlin_version(context: &Context, kotlin_binary: &str) -> Option<String> {
|
|
|
|
match context.exec_cmd(kotlin_binary, &["-version"]) {
|
2020-12-26 14:26:50 +00:00
|
|
|
Some(output) => {
|
2021-04-29 21:22:20 +00:00
|
|
|
let kotlin_output = if output.stdout.is_empty() {
|
|
|
|
output.stderr
|
2020-12-26 14:26:50 +00:00
|
|
|
} else {
|
2021-04-29 21:22:20 +00:00
|
|
|
output.stdout
|
|
|
|
};
|
|
|
|
parse_kotlin_version(&kotlin_output)
|
2020-12-26 14:26:50 +00:00
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:22:20 +00:00
|
|
|
fn parse_kotlin_version(kotlin_stdout: &str) -> Option<String> {
|
2020-12-26 14:26:50 +00:00
|
|
|
// kotlin -version output looks like this:
|
|
|
|
// Kotlin version 1.4.21-release-411 (JRE 14.0.1+7)
|
|
|
|
// kotlinc -version output looks like this:
|
|
|
|
// info: kotlinc-jvm 1.4.21 (JRE 14.0.1+7)
|
|
|
|
let re = Regex::new(KOTLIN_VERSION_PATTERN).ok()?;
|
|
|
|
let captures = re.captures(kotlin_stdout)?;
|
|
|
|
let version = &captures["version"];
|
2021-04-29 21:22:20 +00:00
|
|
|
Some(version.to_string())
|
2020-12-26 14:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::test::ModuleRenderer;
|
|
|
|
use ansi_term::Color;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_without_kotlin_files() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
let actual = ModuleRenderer::new("kotlin").path(dir.path()).collect();
|
|
|
|
let expected = None;
|
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_kotlin_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("main.kt"))?.sync_all()?;
|
|
|
|
let actual = ModuleRenderer::new("kotlin").path(dir.path()).collect();
|
2021-01-21 22:01:30 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Blue.bold().paint("🅺 v1.4.21 ")));
|
2020-12-26 14:26:50 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn folder_with_kotlin_script_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("main.kts"))?.sync_all()?;
|
|
|
|
let actual = ModuleRenderer::new("kotlin").path(dir.path()).collect();
|
2021-01-21 22:01:30 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Blue.bold().paint("🅺 v1.4.21 ")));
|
2020-12-26 14:26:50 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn kotlin_binary_is_kotlin_runtime() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("main.kt"))?.sync_all()?;
|
|
|
|
|
|
|
|
let config = toml::toml! {
|
|
|
|
[kotlin]
|
|
|
|
kotlin_binary = "kotlin"
|
|
|
|
};
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("kotlin")
|
|
|
|
.path(dir.path())
|
|
|
|
.config(config)
|
|
|
|
.collect();
|
|
|
|
|
2021-01-21 22:01:30 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Blue.bold().paint("🅺 v1.4.21 ")));
|
2020-12-26 14:26:50 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn kotlin_binary_is_kotlin_compiler() -> io::Result<()> {
|
|
|
|
let dir = tempfile::tempdir()?;
|
|
|
|
File::create(dir.path().join("main.kt"))?.sync_all()?;
|
|
|
|
|
|
|
|
let config = toml::toml! {
|
|
|
|
[kotlin]
|
|
|
|
kotlin_binary = "kotlinc"
|
|
|
|
};
|
|
|
|
|
|
|
|
let actual = ModuleRenderer::new("kotlin")
|
|
|
|
.path(dir.path())
|
|
|
|
.config(config)
|
|
|
|
.collect();
|
|
|
|
|
2021-01-21 22:01:30 +00:00
|
|
|
let expected = Some(format!("via {}", Color::Blue.bold().paint("🅺 v1.4.21 ")));
|
2020-12-26 14:26:50 +00:00
|
|
|
assert_eq!(expected, actual);
|
|
|
|
dir.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-29 21:22:20 +00:00
|
|
|
fn test_parse_kotlin_version_from_runtime() {
|
2020-12-26 14:26:50 +00:00
|
|
|
let kotlin_input = "Kotlin version 1.4.21-release-411 (JRE 14.0.1+7)";
|
|
|
|
assert_eq!(
|
2021-04-29 21:22:20 +00:00
|
|
|
parse_kotlin_version(kotlin_input),
|
|
|
|
Some("1.4.21".to_string())
|
2020-12-26 14:26:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-04-29 21:22:20 +00:00
|
|
|
fn test_parse_kotlin_version_from_compiler() {
|
2020-12-26 14:26:50 +00:00
|
|
|
let kotlin_input = "info: kotlinc-jvm 1.4.21 (JRE 14.0.1+7)";
|
|
|
|
assert_eq!(
|
2021-04-29 21:22:20 +00:00
|
|
|
parse_kotlin_version(kotlin_input),
|
|
|
|
Some("1.4.21".to_string())
|
2020-12-26 14:26:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|