feat: add version format configuration (#2499)

This commit is contained in:
Chris Rose 2021-04-08 15:37:54 -07:00 committed by GitHub
parent 1ff1f2b5dd
commit 6817e7f698
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 413 additions and 94 deletions

94
Cargo.lock generated
View File

@ -133,6 +133,18 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "bitvec"
version = "0.19.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8942c8d352ae1838c9dda0b0ca2ab657696ef2232a20147cf1b30ae1a9cb4321"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "blake2b_simd"
version = "0.5.11"
@ -485,6 +497,12 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "funty"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]]
name = "futures"
version = "0.3.13"
@ -706,6 +724,15 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "itertools"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37d572918e350e82412fe766d24b15e6682fb2ed2bbe018280caa810397cb319"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "0.4.7"
@ -733,6 +760,19 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "lexical-core"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21f866863575d0e1d654fbeeabdc927292fdf862873dc3c96c6f753357e13374"
dependencies = [
"arrayvec",
"bitflags 1.2.1",
"cfg-if 1.0.0",
"ryu",
"static_assertions",
]
[[package]]
name = "libc"
version = "0.2.91"
@ -900,6 +940,19 @@ dependencies = [
"libc",
]
[[package]]
name = "nom"
version = "6.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7413f999671bd4745a7b624bd370a569fb6bc574b23c83a3c5ed2e453f3d5e2"
dependencies = [
"bitvec",
"funty",
"lexical-core",
"memchr",
"version_check",
]
[[package]]
name = "notify-rust"
version = "4.3.0"
@ -1221,6 +1274,12 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "941ba9d78d8e2f7ce474c015eea4d9c6d25b6a3327f9832ee29a4de27f91bbb8"
[[package]]
name = "rand"
version = "0.7.3"
@ -1622,6 +1681,7 @@ dependencies = [
"unicode-segmentation",
"unicode-width",
"urlencoding",
"versions",
"which",
"winapi",
"yaml-rust",
@ -1636,6 +1696,12 @@ dependencies = [
"syn 1.0.64",
]
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "strsim"
version = "0.8.0"
@ -1705,6 +1771,12 @@ dependencies = [
"libc",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "tempfile"
version = "3.2.0"
@ -1880,6 +1952,22 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "versions"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb914ab4802fe7bc8295485cc9f05c98c466ecd82afc0a10eae72a295e3dbdc"
dependencies = [
"itertools",
"nom",
]
[[package]]
name = "void"
version = "1.0.2"
@ -1973,6 +2061,12 @@ dependencies = [
"xml-rs",
]
[[package]]
name = "wyz"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85e60b0d1b5f99db2556934e21937020776a5d31520bf169e851ac44e6420214"
[[package]]
name = "xml-rs"
version = "0.6.1"

View File

@ -67,6 +67,7 @@ notify-rust = { version = "4.3.0", optional = true }
semver = "0.11.0"
which = "4.1.0"
shadow-rs = "0.5.25"
versions = "2.1.0"
strsim = "0.10.0"
process_control = { version = "3.0.1", features = ["crossbeam-channel"] }

View File

@ -1376,15 +1376,16 @@ By default the module will be shown if any of the following conditions are met:
### Options
| Option | Default | Description |
| ------------------- | --------------------------------------------------------------------------------------------------------- | ----------------------------------------------- |
| `format` | `"via [${symbol}(${version} )]($style)"` | The format for the module. |
| `detect_extensions` | `["java", "class", "gradle", "jar", "cljs", "cljc"]` | Which extensions should trigger this module. |
| `detect_files` | `["pom.xml", "build.gradle.kts", "build.sbt", ".java-version", ".deps.edn", "project.clj", "build.boot"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this modules. |
| `symbol` | `"☕ "` | A format string representing the symbol of Java |
| `style` | `"red dimmed"` | The style for the module. |
| `disabled` | `false` | Disables the `java` module. |
| Option | Default | Description |
| ------------------- | --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| `format` | `"via [${symbol}(${version} )]($style)"` | The format for the module. |
| `version_format`. | `v{raw}` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `detect_extensions` | `["java", "class", "gradle", "jar", "cljs", "cljc"]` | Which extensions should trigger this module. |
| `detect_files` | `["pom.xml", "build.gradle.kts", "build.sbt", ".java-version", ".deps.edn", "project.clj", "build.boot"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this modules. |
| `symbol` | `"☕ "` | A format string representing the symbol of Java |
| `style` | `"red dimmed"` | The style for the module. |
| `disabled` | `false` | Disables the `java` module. |
### Variables
@ -1821,16 +1822,17 @@ By default the module will be shown if any of the following conditions are met:
### Options
| Option | Default | Description |
| ------------------- | ------------------------------------ | -------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `" "` | A format string representing the symbol of NodeJS. |
| `detect_extensions` | `["js", "mjs", "cjs", "ts"]` | Which extensions should trigger this module. |
| `detect_files` | `["package.json", ".node-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `["node_modules"]` | Which folders should trigger this module. |
| `style` | `"bold green"` | The style for the module. |
| `disabled` | `false` | Disables the `nodejs` module. |
| `not_capable_style` | `bold red` | The style for the module when an engines property in `package.json` does not match the NodeJS version. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | ---------------------------------------------------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `version_format`. | `v{raw}` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `" "` | A format string representing the symbol of NodeJS. |
| `detect_extensions` | `["js", "mjs", "cjs", "ts"]` | Which extensions should trigger this moudle. |
| `detect_files` | `["package.json", ".node-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `["node_modules"]` | Which folders should trigger this module. |
| `style` | `"bold green"` | The style for the module. |
| `disabled` | `false` | Disables the `nodejs` module. |
| `not_capable_style` | `bold red` | The style for the module when an engines property in package.json does not match the NodeJS version. |
### Variables
@ -2133,6 +2135,7 @@ By default the module will be shown if any of the following conditions are met:
| Option | Default | Description |
| -------------------- | ------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------- |
| `format` | `'via [${symbol}${pyenv_prefix}(${version} )(\($virtualenv\) )]($style)'` | The format for the module. |
| `version_format`. | `v{raw}` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `"🐍 "` | A format string representing the symbol of Python |
| `style` | `"yellow bold"` | The style for the module. |
| `pyenv_version_name` | `false` | Use pyenv to get Python version |
@ -2208,15 +2211,16 @@ The module will be shown if any of the following conditions are met:
### Options
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------ |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"💎 "` | A format string representing the symbol of Ruby. |
| `detect_extensions` | `["rb"]` | Which extensions should trigger this module. |
| `detect_files` | `["Gemfile", ".ruby-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `ruby` module. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `version_format`. | `v{raw}` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `"💎 "` | A format string representing the symbol of Ruby. |
| `detect_extensions` | `["rb"]` | Which extensions should trigger this module. |
| `detect_files` | `["Gemfile", ".ruby-version"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `ruby` module. |
### Variables
@ -2247,15 +2251,16 @@ The module will be shown if any of the following conditions are met:
### Options
| Option | Default | Description |
| ------------------- | ------------------------------------ | ----------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"🦀 "` | A format string representing the symbol of Rust |
| `detect_extensions` | `["rs"]` | Which extensions should trigger this module. |
| `detect_files` | `["Cargo.toml"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `rust` module. |
| Option | Default | Description |
| ------------------- | ------------------------------------ | ------------------------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `version_format`. | `v{raw}` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `symbol` | `"🦀 "` | A format string representing the symbol of Rust |
| `detect_extensions` | `["rs"]` | Which extensions should trigger this module. |
| `detect_files` | `["Cargo.toml"]` | Which filenames should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. |
| `style` | `"bold red"` | The style for the module. |
| `disabled` | `false` | Disables the `rust` module. |
### Variables

View File

@ -7,6 +7,7 @@ use starship_module_config_derive::ModuleConfig;
pub struct JavaConfig<'a> {
pub disabled: bool,
pub format: &'a str,
pub version_format: &'a str,
pub style: &'a str,
pub symbol: &'a str,
pub detect_extensions: Vec<&'a str>,
@ -18,6 +19,7 @@ impl<'a> Default for JavaConfig<'a> {
fn default() -> Self {
JavaConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
disabled: false,
style: "red dimmed",
symbol: "",

View File

@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)]
pub struct NodejsConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
@ -19,6 +20,7 @@ impl<'a> Default for NodejsConfig<'a> {
fn default() -> Self {
NodejsConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "",
style: "bold green",
disabled: false,

View File

@ -9,6 +9,7 @@ pub struct PythonConfig<'a> {
pub pyenv_prefix: &'a str,
pub python_binary: VecOr<&'a str>,
pub format: &'a str,
pub version_format: &'a str,
pub style: &'a str,
pub symbol: &'a str,
pub disabled: bool,
@ -24,6 +25,7 @@ impl<'a> Default for PythonConfig<'a> {
pyenv_prefix: "pyenv ",
python_binary: VecOr(vec!["python", "python3", "python2"]),
format: "via [${symbol}${pyenv_prefix}(${version} )(\\($virtualenv\\) )]($style)",
version_format: "v${raw}",
style: "yellow bold",
symbol: "🐍 ",
disabled: false,

View File

@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)]
pub struct RubyConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
@ -18,6 +19,7 @@ impl<'a> Default for RubyConfig<'a> {
fn default() -> Self {
RubyConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "💎 ",
style: "bold red",
disabled: false,

View File

@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)]
pub struct RustConfig<'a> {
pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
@ -18,6 +19,7 @@ impl<'a> Default for RustConfig<'a> {
fn default() -> Self {
RustConfig {
format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "🦀 ",
style: "bold red",
disabled: false,

View File

@ -1,6 +1,8 @@
pub mod model;
mod parser;
pub mod string_formatter;
mod version;
pub use model::{StyleVariableHolder, VariableHolder};
pub use string_formatter::StringFormatter;
pub use version::VersionFormatter;

97
src/formatter/version.rs Normal file
View File

@ -0,0 +1,97 @@
use super::string_formatter::StringFormatterError;
use super::StringFormatter;
use versions::Versioning;
pub struct VersionFormatter<'a> {
formatter: StringFormatter<'a>,
}
impl<'a> VersionFormatter<'a> {
/// Creates an instance of a VersionFormatter from a format string
///
/// Like the StringFormatter, this will throw an error when the string isn't
/// parseable.
pub fn new(format: &'a str) -> Result<Self, StringFormatterError> {
let formatter = StringFormatter::new(format)?;
Ok(Self { formatter })
}
/// Formats a version structure into a readable string
///
/// No matter what comes in, this will return some usable string
pub fn format_version(self, version: &str) -> String {
let parsed = Versioning::new(version);
let formatted = self
.formatter
.map(|variable| match variable {
"raw" => Some(Ok(version.to_string())),
"major" => match parsed.as_ref() {
Some(Versioning::Ideal(v)) => Some(Ok(v.major.to_string())),
Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(0)?.to_string())),
_ => None,
},
"minor" => match parsed.as_ref() {
Some(Versioning::Ideal(v)) => Some(Ok(v.minor.to_string())),
Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(1)?.to_string())),
_ => None,
},
"patch" => match parsed.as_ref() {
Some(Versioning::Ideal(v)) => Some(Ok(v.patch.to_string())),
Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(2)?.to_string())),
_ => None,
},
_ => None,
})
.parse(None);
match formatted {
Ok(segments) => segments
.iter()
.map(|segment| segment.value.as_str())
.collect::<Vec<&str>>()
.join(""),
Err(_) => version.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_semver_full() {
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
let result = VersionFormatter::new(FORMAT_STR)
.unwrap()
.format_version("1.2.3");
assert_eq!(result, "major:1 minor:2 patch:3 raw:1.2.3");
}
#[test]
fn test_semver_partial() {
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
let result = VersionFormatter::new(FORMAT_STR)
.unwrap()
.format_version("1.2");
assert_eq!(result, "major:1 minor:2 patch: raw:1.2");
}
#[test]
fn test_general() {
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
let result = VersionFormatter::new(FORMAT_STR)
.unwrap()
.format_version("1.2-a.3");
assert_eq!(result, "major:1 minor:2 patch: raw:1.2-a.3");
}
#[test]
fn test_mess() {
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
let result = VersionFormatter::new(FORMAT_STR)
.unwrap()
.format_version("utter junk");
assert_eq!(result, "major: minor: patch: raw:utter junk");
}
}

View File

@ -1,5 +1,5 @@
use crate::configs::java::JavaConfig;
use crate::formatter::StringFormatter;
use crate::formatter::{StringFormatter, VersionFormatter};
use std::path::PathBuf;
use super::{Context, Module, RootModuleConfig};
@ -35,7 +35,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
})
.map(|variable| match variable {
"version" => {
let java_version = get_java_version(context)?;
let java_version = get_java_version(context, &config)?;
Some(Ok(java_version))
}
_ => None,
@ -54,7 +54,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module)
}
fn get_java_version(context: &Context) -> Option<String> {
fn get_java_version(context: &Context, config: &JavaConfig) -> Option<String> {
let java_command = context
.get_env("JAVA_HOME")
.map(PathBuf::from)
@ -74,15 +74,19 @@ fn get_java_version(context: &Context) -> Option<String> {
output.stdout
};
parse_java_version(&java_version)
parse_java_version(config.version_format, &java_version)
}
fn parse_java_version(java_version: &str) -> Option<String> {
fn parse_java_version(version_format: &str, java_version: &str) -> Option<String> {
let re = Regex::new(JAVA_VERSION_PATTERN).ok()?;
let captures = re.captures(java_version)?;
let version = &captures["version"];
Some(format!("v{}", &version))
Some(
VersionFormatter::new(version_format)
.ok()?
.format_version(version),
)
}
#[cfg(test)]
@ -97,64 +101,103 @@ mod tests {
fn test_parse_java_version_openjdk() {
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 10:18:43 by \"openjdk\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-23)";
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-post-Ubuntu-1ubuntu219.04) for linux-amd64 JRE (11.0.4+11-post-Ubuntu-1ubuntu219.04), built on Jul 18 2019 18:21:46 by \"build\" with gcc 8.3.0";
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_11),
Some("v11.0.4".to_string())
);
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v1.8.0".to_string())
);
}
#[test]
fn test_parse_java_version_oracle() {
let java_8 = "Java HotSpot(TM) Client VM (25.65-b01) for linux-arm-vfp-hflt JRE (1.8.0_65-b17), built on Oct 6 2015 16:19:04 by \"java_re\" with gcc 4.7.2 20120910 (prerelease)";
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v1.8.0".to_string())
);
}
#[test]
fn test_parse_java_version_redhat() {
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 20:48:53 by \"root\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
let java_12 = "OpenJDK 64-Bit Server VM (12.0.2+10) for linux-amd64 JRE (12.0.2+10), built on Jul 18 2019 14:41:47 by \"jenkins\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
assert_eq!(parse_java_version(java_12), Some("v12.0.2".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v1.8.0".to_string())
);
assert_eq!(
parse_java_version("v${raw}", java_12),
Some("v12.0.2".to_string())
);
}
#[test]
fn test_parse_java_version_zulu() {
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (Zulu 8.40.0.25-CA-linux64) (1.8.0_222-b10), built on Jul 11 2019 11:36:39 by \"zulu_re\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)";
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (Zulu11.33+15-CA) (11.0.4+11-LTS), built on Jul 11 2019 21:37:17 by \"zulu_re\" with gcc 4.9.2 20150212 (Red Hat 4.9.2-6)";
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v1.8.0".to_string())
);
assert_eq!(
parse_java_version("v${raw}", java_11),
Some("v11.0.4".to_string())
);
}
#[test]
fn test_parse_java_version_eclipse_openj9() {
let java_8 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_222-b10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.222.0, built on Jul 17 2019 21:29:18 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
let java_11 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.4+11) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.4.0, built on Jul 17 2019 21:51:37 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v1.8.0".to_string())
);
assert_eq!(
parse_java_version("v${raw}", java_11),
Some("v11.0.4".to_string())
);
}
#[test]
fn test_parse_java_version_graalvm() {
let java_8 = "OpenJDK 64-Bit GraalVM CE 19.2.0.1 (25.222-b08-jvmci-19.2-b02) for linux-amd64 JRE (8u222), built on Jul 19 2019 17:37:13 by \"buildslave\" with gcc 7.3.0";
assert_eq!(parse_java_version(java_8), Some("v8".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v8".to_string())
);
}
#[test]
fn test_parse_java_version_amazon_corretto() {
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 20:48:53 by \"root\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (11.0.4+11-LTS), built on Jul 11 2019 20:06:11 by \"\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_8),
Some("v1.8.0".to_string())
);
assert_eq!(
parse_java_version("v${raw}", java_11),
Some("v11.0.4".to_string())
);
}
#[test]
fn test_parse_java_version_sapmachine() {
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS-sapmachine) for linux-amd64 JRE (11.0.4+11-LTS-sapmachine), built on Jul 17 2019 08:58:43 by \"\" with gcc 7.3.0";
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
assert_eq!(
parse_java_version("v${raw}", java_11),
Some("v11.0.4".to_string())
);
}
#[test]
fn test_parse_java_version_unknown() {
let unknown_jre = "Unknown JRE";
assert_eq!(parse_java_version(unknown_jre), None);
assert_eq!(parse_java_version("v${raw}", unknown_jre), None);
}
#[test]

View File

@ -1,7 +1,7 @@
use super::{Context, Module, RootModuleConfig};
use crate::configs::nodejs::NodejsConfig;
use crate::formatter::StringFormatter;
use crate::formatter::{StringFormatter, VersionFormatter};
use crate::utils;
use once_cell::sync::Lazy;
@ -57,11 +57,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"version" => nodejs_version
.deref()
.as_ref()
.map(|version| version.trim())
.map(Ok),
"version" => {
let nodejs_detected_version = &nodejs_version
.deref()
.as_ref()
.map(|version| version.trim());
match nodejs_detected_version {
Some(version) => Some(Ok(VersionFormatter::new(config.version_format)
.ok()?
.format_version(&version.to_string().drain(1..).collect::<String>()))),
None => None,
}
}
_ => None,
})
.parse(None)

View File

@ -4,6 +4,7 @@ use std::path::Path;
use super::{Context, Module, RootModuleConfig};
use crate::configs::python::PythonConfig;
use crate::formatter::StringFormatter;
use crate::formatter::VersionFormatter;
/// Creates a module with the current Python version and, if active, virtual environment.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -82,17 +83,21 @@ fn get_python_version(context: &Context, config: &PythonConfig) -> Option<String
}
})?;
format_python_version(&version)
format_python_version(config.version_format, &version)
}
fn format_python_version(python_version: &str) -> Option<String> {
fn format_python_version(version_format: &str, python_version: &str) -> Option<String> {
let version = python_version
// split into ["Python", "3.8.6", ...]
.split_whitespace()
// return "3.8.6"
// get down to "3.8.6"
.nth(1)?;
Some(format!("v{}", version))
Some(
VersionFormatter::new(version_format)
.ok()?
.format_version(version),
)
}
fn get_python_virtual_env(context: &Context) -> Option<String> {
@ -124,19 +129,46 @@ mod tests {
#[test]
fn test_format_python_version() {
let input = "Python 3.7.2";
assert_eq!(format_python_version(input), Some("v3.7.2".to_string()));
assert_eq!(
format_python_version("v${major}.${minor}.${patch}", input),
Some("v3.7.2".to_string())
);
}
#[test]
fn test_format_python_version_truncated() {
let input = "Python 3.7.2";
assert_eq!(
format_python_version("v${major}.${minor}", input),
Some("v3.7".to_string())
);
}
#[test]
fn test_format_python_version_is_malformed() {
let input = "Python 3.7";
assert_eq!(
format_python_version("v${major}.${minor}.${patch}", input),
Some("v3.7.".to_string())
);
}
#[test]
fn test_format_python_version_anaconda() {
let input = "Python 3.6.10 :: Anaconda, Inc.";
assert_eq!(format_python_version(input), Some("v3.6.10".to_string()));
assert_eq!(
format_python_version("v${major}.${minor}.${patch}", input),
Some("v3.6.10".to_string())
);
}
#[test]
fn test_format_python_version_pypy() {
let input = "Python 3.7.9 (7e6e2bb30ac5fbdbd443619cae28c51d5c162a02, Nov 24 2020, 10:03:59)\n[PyPy 7.3.3-beta0 with GCC 10.2.0]";
assert_eq!(format_python_version(input), Some("v3.7.9".to_string()));
assert_eq!(
format_python_version("v${major}.${minor}.${patch}", input),
Some("v3.7.9".to_string())
);
}
#[test]

View File

@ -1,7 +1,7 @@
use super::{Context, Module, RootModuleConfig};
use crate::configs::ruby::RubyConfig;
use crate::formatter::StringFormatter;
use crate::formatter::{StringFormatter, VersionFormatter};
/// Creates a module with the current Ruby version
///
@ -34,9 +34,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"version" => {
format_ruby_version(&context.exec_cmd("ruby", &["-v"])?.stdout.as_str()).map(Ok)
}
"version" => format_ruby_version(
&config,
&context.exec_cmd("ruby", &["-v"])?.stdout.as_str(),
)
.map(Ok),
_ => None,
})
.parse(None)
@ -53,7 +55,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module)
}
fn format_ruby_version(ruby_version: &str) -> Option<String> {
fn format_ruby_version(config: &RubyConfig, ruby_version: &str) -> Option<String> {
let version = ruby_version
// split into ["ruby", "2.6.0p0", "linux/amd64"]
.split_whitespace()
@ -64,10 +66,11 @@ fn format_ruby_version(ruby_version: &str) -> Option<String> {
// return "2.6.0"
.next()?;
let mut formatted_version = String::with_capacity(version.len() + 1);
formatted_version.push('v');
formatted_version.push_str(version);
Some(formatted_version)
Some(
VersionFormatter::new(config.version_format)
.ok()?
.format_version(version),
)
}
#[cfg(test)]
@ -127,16 +130,24 @@ mod tests {
#[test]
fn test_format_ruby_version() {
let test_config = RubyConfig::default();
assert_eq!(
format_ruby_version("ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-darwin19.0]"),
format_ruby_version(
&test_config,
"ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-darwin19.0]"
),
Some("v2.1.10".to_string())
);
assert_eq!(
format_ruby_version("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"),
format_ruby_version(
&test_config,
"ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"
),
Some("v2.5.1".to_string())
);
assert_eq!(
format_ruby_version(
&test_config,
"ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-musl]"
),
Some("v2.7.0".to_string())

View File

@ -7,7 +7,7 @@ use serde::Deserialize;
use super::{Context, Module, RootModuleConfig};
use crate::configs::rust::RustConfig;
use crate::formatter::StringFormatter;
use crate::formatter::{StringFormatter, VersionFormatter};
/// Creates a module with the current Rust version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -38,7 +38,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.map(|variable| match variable {
// This may result in multiple calls to `get_module_version` when a user have
// multiple `$version` variables defined in `format`.
"version" => get_module_version(context).map(Ok),
"version" => get_module_version(context, &config).map(Ok),
_ => None,
})
.parse(None)
@ -55,7 +55,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module)
}
fn get_module_version(context: &Context) -> Option<String> {
fn get_module_version(context: &Context, config: &RustConfig) -> Option<String> {
// `$CARGO_HOME/bin/rustc(.exe) --version` may attempt installing a rustup toolchain.
// https://github.com/starship/starship/issues/417
//
@ -77,17 +77,19 @@ fn get_module_version(context: &Context) -> Option<String> {
.or_else(|| find_rust_toolchain_file(&context))
{
match execute_rustup_run_rustc_version(&toolchain) {
RustupRunRustcVersionOutcome::RustcVersion(stdout) => format_rustc_version(stdout),
RustupRunRustcVersionOutcome::RustcVersion(stdout) => {
format_rustc_version(config, stdout)
}
RustupRunRustcVersionOutcome::ToolchainName(toolchain) => toolchain,
RustupRunRustcVersionOutcome::RustupNotWorking => {
// If `rustup` is not in `$PATH` or cannot be executed for other reasons, we can
// safely execute `rustc --version`.
format_rustc_version(execute_rustc_version()?)
format_rustc_version(config, execute_rustc_version()?)
}
RustupRunRustcVersionOutcome::Err => return None,
}
} else {
format_rustc_version(execute_rustc_version()?)
format_rustc_version(config, execute_rustc_version()?)
};
Some(module_version)
@ -203,11 +205,15 @@ fn execute_rustc_version() -> Option<String> {
}
}
fn format_rustc_version(mut rustc_stdout: String) -> String {
fn format_rustc_version(config: &RustConfig, mut rustc_stdout: String) -> String {
let offset = &rustc_stdout.find('(').unwrap_or_else(|| rustc_stdout.len());
let formatted_version: String = rustc_stdout.drain(..offset).collect();
format!("v{}", formatted_version.replace("rustc", "").trim())
let full_version_string = formatted_version.replace("rustc", "");
let version_string = full_version_string.trim();
match VersionFormatter::new(config.version_format) {
Ok(formatter) => formatter.format_version(&version_string),
_ => format!("v{}", version_string),
}
}
#[derive(Debug, PartialEq)]
@ -320,17 +326,28 @@ mod tests {
#[test]
fn test_format_rustc_version() {
let test_config: RustConfig = RustConfig::default();
let nightly_input = String::from("rustc 1.34.0-nightly (b139669f3 2019-04-10)");
assert_eq!(format_rustc_version(nightly_input), "v1.34.0-nightly");
assert_eq!(
format_rustc_version(&test_config, nightly_input),
"v1.34.0-nightly"
);
let beta_input = String::from("rustc 1.34.0-beta.1 (2bc1d406d 2019-04-10)");
assert_eq!(format_rustc_version(beta_input), "v1.34.0-beta.1");
assert_eq!(
format_rustc_version(&test_config, beta_input),
"v1.34.0-beta.1"
);
let stable_input = String::from("rustc 1.34.0 (91856ed52 2019-04-10)");
assert_eq!(format_rustc_version(stable_input), "v1.34.0");
assert_eq!(format_rustc_version(&test_config, stable_input), "v1.34.0");
let version_without_hash = String::from("rustc 1.34.0");
assert_eq!(format_rustc_version(version_without_hash), "v1.34.0");
assert_eq!(
format_rustc_version(&test_config, version_without_hash),
"v1.34.0"
);
}
#[test]