1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-13 06:27:46 +00:00
starship/src/modules/scala.rs
filip 540c2c2475
feat: Add version formating for modules (#2611)
* format crystal version with VersionFormatter

* update crystal dosc

* format crystal module

* fix typos

* format dart version with VersionFormatter

* fix dart malformed test

* update dart docs

* format cmake version with VersionFormatter

* update cmake docs

* format deno version with VersionFormatter

* update deno docs

* remove Version type

* format dotnet version with VersionFormatter

* update dotnet docs

* format erlang version with VersionFormatter

* update erlang docs

* format golang version with VersionFormatter

* refactor formatting in my modules

* format helm version with VersionFormatter

* format julia version with VersionFormatter

* format kotlin version with VersionFormatter

* format lua version with VersionFormatter

* format nim version with VersionFormatter

* format perl version with VersionFormatter

* format php version with VersionFormatter

* format purescript version with VersionFormatter

* format scala version with VersionFormatter

* format swift version with VersionFormatter

* format terraform version with VersionFormatter

* format vagrant version with VersionFormatter

* format zig version with VersionFormatter

* format elixir version with VersionFormatter

* format ocaml version with VersionFormatter

* update elixir docs

* update golang docs

* update helm docs

* update julia docs

* update kotlin docs

* update lua docs

* update nim docs

* update ocaml docs

* update perl docs

* update php docs

* update purescript docs

* update scala docs

* update swift docs

* update terraform docs

* update vagrant docs

* update zig docs

* format elm version with VersionFormatter

* update elm docs

* pass module_name as &str to format_module_version
2021-04-29 23:22:20 +02:00

175 lines
5.6 KiB
Rust

use crate::configs::scala::ScalaConfig;
use crate::formatter::StringFormatter;
use super::{Context, Module, RootModuleConfig};
use crate::formatter::VersionFormatter;
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("scala");
let config: ScalaConfig = ScalaConfig::try_load(module.config);
let is_scala_project = context
.try_begin_scan()?
.set_files(&config.detect_files)
.set_extensions(&config.detect_extensions)
.set_folders(&config.detect_folders)
.is_match();
if !is_scala_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 {
"version" => {
let scala_version = get_scala_version(context)?;
VersionFormatter::format_module_version(
module.get_name(),
&scala_version,
config.version_format,
)
.map(Ok)
}
_ => None,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `scala`:\n{}", error);
return None;
}
});
Some(module)
}
fn get_scala_version(context: &Context) -> Option<String> {
let output = context.exec_cmd("scalac", &["-version"])?;
let scala_version = if output.stdout.is_empty() {
output.stderr
} else {
output.stdout
};
parse_scala_version(&scala_version)
}
fn parse_scala_version(scala_version: &str) -> Option<String> {
let version = scala_version
// split into ["Scala", "compiler", "version", "2.13.5", "--", ...]
.split_whitespace()
// take "2.13.5"
.nth(3)?;
Some(version.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::ModuleRenderer;
use ansi_term::Color;
use std::fs::{self, File};
use std::io;
#[test]
fn test_parse_scala_version() {
let scala_2_13 =
"Scala compiler version 2.13.5 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.";
assert_eq!(parse_scala_version(scala_2_13), Some("2.13.5".to_string()));
}
#[test]
fn test_parse_dotty_version() {
let dotty_version = "Scala compiler version 3.0.0-RC1 -- Copyright 2002-2021, LAMP/EPFL";
assert_eq!(
parse_scala_version(dotty_version),
Some("3.0.0-RC1".to_string())
);
}
#[test]
fn folder_without_scala_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = None;
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_scala_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("Test.scala"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_scala_file_no_scala_installed() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("Test.scala"))?.sync_all()?;
let actual = ModuleRenderer::new("scala")
.cmd("scalac -version", None)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_sbt_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("build.sbt"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_scala_env_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".scalaenv"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_sbt_env_file() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".sbtenv"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn folder_with_metals_dir() -> io::Result<()> {
let dir = tempfile::tempdir()?;
fs::create_dir_all(dir.path().join(".metals"))?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}
}