1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-14 01:04:11 +00:00
starship/src/modules/package.rs

167 lines
4.5 KiB
Rust
Raw Normal View History

2019-05-01 20:34:24 +00:00
use super::{Context, Module};
use crate::utils;
2019-05-01 20:34:24 +00:00
2019-05-01 14:45:56 +00:00
use ansi_term::Color;
use serde_json as json;
2019-05-01 14:45:56 +00:00
use toml;
/// 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)
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
match get_package_version() {
2019-05-01 14:45:56 +00:00
Some(package_version) => {
2019-05-01 20:34:24 +00:00
const PACKAGE_CHAR: &str = "📦 ";
2019-05-01 14:45:56 +00:00
let mut module = context.new_module("package")?;
let module_style = module
.config_value_style("style")
.unwrap_or_else(|| Color::Red.bold());
module.set_style(module_style);
2019-05-01 20:34:24 +00:00
module.get_prefix().set_value("is ");
2019-05-01 14:45:56 +00:00
2019-05-01 20:34:24 +00:00
module.new_segment("symbol", PACKAGE_CHAR);
module.new_segment("version", &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,
}
}
fn extract_cargo_version(file_contents: &str) -> Option<String> {
let cargo_toml: toml::Value = toml::from_str(file_contents).ok()?;
let raw_version = cargo_toml.get("package")?.get("version")?.as_str()?;
2019-05-01 14:45:56 +00:00
let formatted_version = format_version(raw_version);
Some(formatted_version)
2019-05-01 14:45:56 +00:00
}
fn extract_package_version(file_contents: &str) -> Option<String> {
let package_json: json::Value = json::from_str(file_contents).ok()?;
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
}
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)
}
fn get_package_version() -> Option<String> {
if let Ok(cargo_toml) = utils::read_file("Cargo.toml") {
2019-08-23 17:11:20 +00:00
extract_cargo_version(&cargo_toml)
} else if let Ok(package_json) = utils::read_file("package.json") {
2019-08-23 17:11:20 +00:00
extract_package_version(&package_json)
} else if let Ok(poetry_toml) = utils::read_file("pyproject.toml") {
2019-08-23 17:11:20 +00:00
extract_poetry_version(&poetry_toml)
} else {
None
2019-05-01 14:45:56 +00:00
}
}
fn format_version(version: &str) -> String {
2019-05-01 14:45:56 +00:00
format!("v{}", version.replace('"', "").trim())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_version() {
assert_eq!(format_version("0.1.0"), "v0.1.0");
}
#[test]
fn test_extract_cargo_version() {
let cargo_with_version = toml::toml! {
[package]
name = "starship"
version = "0.1.0"
}
.to_string();
let expected_version = Some("v0.1.0".to_string());
assert_eq!(extract_cargo_version(&cargo_with_version), expected_version);
let cargo_without_version = toml::toml! {
[package]
name = "starship"
}
.to_string();
let expected_version = None;
assert_eq!(
extract_cargo_version(&cargo_without_version),
expected_version
);
}
#[test]
fn test_extract_package_version() {
let package_with_version = json::json!({
"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
);
let package_without_version = json::json!({
"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
}
#[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-05-01 14:45:56 +00:00
}