From edcec7eed85aae63ba19f6e14cc7ceec015c1db8 Mon Sep 17 00:00:00 2001 From: Shu Kutsuzawa Date: Sat, 4 Apr 2020 03:16:22 +0900 Subject: [PATCH] feat(package): Add project version for julia (#1036) * add project version (for julia) to package module * update docs --- docs/config/README.md | 1 + src/modules/package.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/docs/config/README.md b/docs/config/README.md index cb8b3713..a76ef28e 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -979,6 +979,7 @@ package, and shows its current version. The module currently supports `npm`, `ca - **composer** – The `composer` package version is extracted from the `composer.json` present in the current directory - **gradle** – The `gradle` package version is extracted from the `build.gradle` present +- **julia** - The package version is extracted from the `Project.toml` present > ⚠️ The version being shown is that of the package whose source code is in your > current directory, not your package manager. diff --git a/src/modules/package.rs b/src/modules/package.rs index 9cd17abb..a5ce51db 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -86,6 +86,14 @@ fn extract_composer_version(file_contents: &str) -> Option { Some(formatted_version) } +fn extract_project_version(file_contents: &str) -> Option { + let project_toml: toml::Value = toml::from_str(file_contents).ok()?; + let raw_version = project_toml.get("version")?.as_str()?; + + let formatted_version = format_version(raw_version); + Some(formatted_version) +} + fn get_package_version(base_dir: &PathBuf) -> Option { if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) { extract_cargo_version(&cargo_toml) @@ -97,6 +105,8 @@ fn get_package_version(base_dir: &PathBuf) -> Option { extract_composer_version(&composer_json) } else if let Ok(build_gradle) = utils::read_file(base_dir.join("build.gradle")) { extract_gradle_version(&build_gradle) + } else if let Ok(project_toml) = utils::read_file(base_dir.join("Project.toml")) { + extract_project_version(&project_toml) } else { None } @@ -346,4 +356,31 @@ java { expected_version ); } + + #[test] + fn test_extract_project_version() { + let project_with_version = toml::toml! { + name = "starship" + version = "0.1.0" + } + .to_string(); + + let expected_version = Some("v0.1.0".to_string()); + assert_eq!( + extract_project_version(&project_with_version), + expected_version + ); + + let project_without_version = toml::toml! { + [package] + name = "starship" + } + .to_string(); + + let expected_version = None; + assert_eq!( + extract_project_version(&project_without_version), + expected_version + ); + } }