2019-12-24 03:38:00 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
use super::{Context, Module};
|
2019-06-10 14:56:17 +00:00
|
|
|
use crate::utils;
|
2019-05-01 20:34:24 +00:00
|
|
|
|
2020-02-21 19:12:21 +00:00
|
|
|
use regex::Regex;
|
2019-06-10 14:56:17 +00:00
|
|
|
use serde_json as json;
|
2019-05-01 14:45:56 +00:00
|
|
|
use toml;
|
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
use super::{RootModuleConfig, SegmentConfig};
|
|
|
|
use crate::configs::package::PackageConfig;
|
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module with the current package version
|
2019-05-01 14:45:56 +00:00
|
|
|
///
|
|
|
|
/// Will display if a version is defined for your Node.js or Rust project (if one exists)
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-12-24 03:38:00 +00:00
|
|
|
match get_package_version(&context.current_dir) {
|
2019-05-01 14:45:56 +00:00
|
|
|
Some(package_version) => {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("package");
|
2019-10-19 01:51:38 +00:00
|
|
|
let config: PackageConfig = PackageConfig::try_load(module.config);
|
|
|
|
|
|
|
|
module.set_style(config.style);
|
2019-05-01 20:34:24 +00:00
|
|
|
module.get_prefix().set_value("is ");
|
2019-05-01 14:45:56 +00:00
|
|
|
|
2019-10-19 01:51:38 +00:00
|
|
|
module.create_segment("symbol", &config.symbol);
|
|
|
|
module.create_segment("version", &SegmentConfig::new(&package_version));
|
2019-05-01 14:45:56 +00:00
|
|
|
|
2019-05-01 20:34:24 +00:00
|
|
|
Some(module)
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:06:34 +00:00
|
|
|
fn extract_cargo_version(file_contents: &str) -> Option<String> {
|
2019-07-31 23:48:51 +00:00
|
|
|
let cargo_toml: toml::Value = toml::from_str(file_contents).ok()?;
|
2019-05-16 16:06:34 +00:00
|
|
|
let raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;
|
2019-05-01 14:45:56 +00:00
|
|
|
|
2019-05-16 16:06:34 +00:00
|
|
|
let formatted_version = format_version(raw_version);
|
|
|
|
Some(formatted_version)
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|
|
|
|
|
2019-05-16 16:06:34 +00:00
|
|
|
fn extract_package_version(file_contents: &str) -> Option<String> {
|
2019-07-31 23:48:51 +00:00
|
|
|
let package_json: json::Value = json::from_str(file_contents).ok()?;
|
2019-05-16 16:06:34 +00:00
|
|
|
let raw_version = package_json.get("version")?.as_str()?;
|
|
|
|
if raw_version == "null" {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
let formatted_version = format_version(raw_version);
|
|
|
|
Some(formatted_version)
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 20:41:06 +00:00
|
|
|
fn extract_poetry_version(file_contents: &str) -> Option<String> {
|
|
|
|
let poetry_toml: toml::Value = toml::from_str(file_contents).ok()?;
|
|
|
|
let raw_version = poetry_toml
|
|
|
|
.get("tool")?
|
|
|
|
.get("poetry")?
|
|
|
|
.get("version")?
|
|
|
|
.as_str()?;
|
|
|
|
|
|
|
|
let formatted_version = format_version(raw_version);
|
|
|
|
Some(formatted_version)
|
|
|
|
}
|
|
|
|
|
2020-02-21 19:12:21 +00:00
|
|
|
fn extract_gradle_version(file_contents: &str) -> Option<String> {
|
|
|
|
let re = Regex::new(r#"(?m)^version ['"](?P<version>[^'"]+)['"]$"#).unwrap();
|
|
|
|
let caps = re.captures(file_contents)?;
|
|
|
|
|
|
|
|
let formatted_version = format_version(&caps["version"]);
|
|
|
|
Some(formatted_version)
|
|
|
|
}
|
|
|
|
|
2019-12-12 02:05:39 +00:00
|
|
|
fn extract_composer_version(file_contents: &str) -> Option<String> {
|
|
|
|
let composer_json: json::Value = json::from_str(file_contents).ok()?;
|
|
|
|
let raw_version = composer_json.get("version")?.as_str()?;
|
|
|
|
if raw_version == "null" {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
let formatted_version = format_version(raw_version);
|
|
|
|
Some(formatted_version)
|
|
|
|
}
|
|
|
|
|
2019-12-24 03:38:00 +00:00
|
|
|
fn get_package_version(base_dir: &PathBuf) -> Option<String> {
|
|
|
|
if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) {
|
2019-08-23 17:11:20 +00:00
|
|
|
extract_cargo_version(&cargo_toml)
|
2019-12-24 03:38:00 +00:00
|
|
|
} else if let Ok(package_json) = utils::read_file(base_dir.join("package.json")) {
|
2019-08-23 17:11:20 +00:00
|
|
|
extract_package_version(&package_json)
|
2019-12-24 03:38:00 +00:00
|
|
|
} else if let Ok(poetry_toml) = utils::read_file(base_dir.join("pyproject.toml")) {
|
2019-08-23 17:11:20 +00:00
|
|
|
extract_poetry_version(&poetry_toml)
|
2019-12-24 03:38:00 +00:00
|
|
|
} else if let Ok(composer_json) = utils::read_file(base_dir.join("composer.json")) {
|
2019-12-12 02:05:39 +00:00
|
|
|
extract_composer_version(&composer_json)
|
2020-02-21 19:12:21 +00:00
|
|
|
} else if let Ok(build_gradle) = utils::read_file(base_dir.join("build.gradle")) {
|
|
|
|
extract_gradle_version(&build_gradle)
|
2019-08-15 20:41:06 +00:00
|
|
|
} else {
|
|
|
|
None
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:06:34 +00:00
|
|
|
fn format_version(version: &str) -> String {
|
2019-11-20 14:16:07 +00:00
|
|
|
let cleaned = version.replace('"', "").trim().to_string();
|
|
|
|
if cleaned.starts_with('v') {
|
|
|
|
cleaned
|
|
|
|
} else {
|
|
|
|
format!("v{}", cleaned)
|
|
|
|
}
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_format_version() {
|
2019-05-16 16:06:34 +00:00
|
|
|
assert_eq!(format_version("0.1.0"), "v0.1.0");
|
2019-11-20 14:16:07 +00:00
|
|
|
assert_eq!(format_version(" 0.1.0 "), "v0.1.0");
|
|
|
|
assert_eq!(format_version("0.1.0 "), "v0.1.0");
|
|
|
|
assert_eq!(format_version(" 0.1.0"), "v0.1.0");
|
|
|
|
assert_eq!(format_version("\"0.1.0\""), "v0.1.0");
|
|
|
|
|
|
|
|
assert_eq!(format_version("v0.1.0"), "v0.1.0");
|
|
|
|
assert_eq!(format_version(" v0.1.0 "), "v0.1.0");
|
|
|
|
assert_eq!(format_version(" v0.1.0"), "v0.1.0");
|
|
|
|
assert_eq!(format_version("v0.1.0 "), "v0.1.0");
|
|
|
|
assert_eq!(format_version("\"v0.1.0\""), "v0.1.0");
|
2019-05-16 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_cargo_version() {
|
2019-07-02 20:12:53 +00:00
|
|
|
let cargo_with_version = toml::toml! {
|
2019-06-10 14:56:17 +00:00
|
|
|
[package]
|
|
|
|
name = "starship"
|
|
|
|
version = "0.1.0"
|
2019-07-02 20:12:53 +00:00
|
|
|
}
|
|
|
|
.to_string();
|
2019-05-16 16:06:34 +00:00
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0".to_string());
|
|
|
|
assert_eq!(extract_cargo_version(&cargo_with_version), expected_version);
|
|
|
|
|
2019-07-02 20:12:53 +00:00
|
|
|
let cargo_without_version = toml::toml! {
|
2019-06-10 14:56:17 +00:00
|
|
|
[package]
|
|
|
|
name = "starship"
|
2019-07-02 20:12:53 +00:00
|
|
|
}
|
|
|
|
.to_string();
|
2019-05-16 16:06:34 +00:00
|
|
|
|
|
|
|
let expected_version = None;
|
|
|
|
assert_eq!(
|
|
|
|
extract_cargo_version(&cargo_without_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_package_version() {
|
2019-06-10 14:56:17 +00:00
|
|
|
let package_with_version = json::json!({
|
2019-05-16 16:06:34 +00:00
|
|
|
"name": "spacefish",
|
|
|
|
"version": "0.1.0"
|
|
|
|
})
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0".to_string());
|
|
|
|
assert_eq!(
|
|
|
|
extract_package_version(&package_with_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
|
2019-06-10 14:56:17 +00:00
|
|
|
let package_without_version = json::json!({
|
2019-05-16 16:06:34 +00:00
|
|
|
"name": "spacefish"
|
|
|
|
})
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let expected_version = None;
|
|
|
|
assert_eq!(
|
|
|
|
extract_package_version(&package_without_version),
|
|
|
|
expected_version
|
|
|
|
);
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|
2019-08-15 20:41:06 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extract_poetry_version() {
|
|
|
|
let poetry_with_version = toml::toml! {
|
|
|
|
[tool.poetry]
|
|
|
|
name = "starship"
|
|
|
|
version = "0.1.0"
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0".to_string());
|
|
|
|
assert_eq!(
|
|
|
|
extract_poetry_version(&poetry_with_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
|
|
|
|
let poetry_without_version = toml::toml! {
|
|
|
|
[tool.poetry]
|
|
|
|
name = "starship"
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let expected_version = None;
|
|
|
|
assert_eq!(
|
|
|
|
extract_poetry_version(&poetry_without_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
}
|
2019-12-12 02:05:39 +00:00
|
|
|
|
2020-02-21 19:12:21 +00:00
|
|
|
#[test]
|
|
|
|
fn test_extract_gradle_version() {
|
|
|
|
let gradle_single_quotes = "plugins {
|
|
|
|
id 'java'
|
|
|
|
id 'test.plugin' version '0.2.0'
|
|
|
|
}
|
|
|
|
version '0.1.0'
|
|
|
|
java {
|
|
|
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
targetCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0".to_string());
|
|
|
|
assert_eq!(
|
|
|
|
extract_gradle_version(&gradle_single_quotes),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
|
|
|
|
let gradle_double_quotes = "plugins {
|
|
|
|
id 'java'
|
|
|
|
id 'test.plugin' version '0.2.0'
|
|
|
|
}
|
|
|
|
version \"0.1.0\"
|
|
|
|
java {
|
|
|
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
targetCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0".to_string());
|
|
|
|
assert_eq!(
|
|
|
|
extract_gradle_version(&gradle_double_quotes),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
|
|
|
|
let gradle_release_candidate = "plugins {
|
|
|
|
id 'java'
|
|
|
|
id 'test.plugin' version '0.2.0'
|
|
|
|
}
|
|
|
|
version '0.1.0-rc1'
|
|
|
|
java {
|
|
|
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
targetCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0-rc1".to_string());
|
|
|
|
assert_eq!(
|
|
|
|
extract_gradle_version(&gradle_release_candidate),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
|
|
|
|
let gradle_without_version = "plugins {
|
|
|
|
id 'java'
|
|
|
|
id 'test.plugin' version '0.2.0'
|
|
|
|
}
|
|
|
|
java {
|
|
|
|
sourceCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
targetCompatibility = JavaVersion.VERSION_1_8
|
|
|
|
}";
|
|
|
|
|
|
|
|
let expected_version = None;
|
|
|
|
assert_eq!(
|
|
|
|
extract_gradle_version(&gradle_without_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-12 02:05:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_extract_composer_version() {
|
|
|
|
let composer_with_version = json::json!({
|
|
|
|
"name": "spacefish",
|
|
|
|
"version": "0.1.0"
|
|
|
|
})
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let expected_version = Some("v0.1.0".to_string());
|
|
|
|
assert_eq!(
|
|
|
|
extract_composer_version(&composer_with_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
|
|
|
|
let composer_without_version = json::json!({
|
|
|
|
"name": "spacefish"
|
|
|
|
})
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
let expected_version = None;
|
|
|
|
assert_eq!(
|
|
|
|
extract_composer_version(&composer_without_version),
|
|
|
|
expected_version
|
|
|
|
);
|
|
|
|
}
|
2019-05-01 14:45:56 +00:00
|
|
|
}
|