feat(package): Add project version for julia (#1036)

* add project version (for julia) to package module

* update docs
This commit is contained in:
Shu Kutsuzawa 2020-04-04 03:16:22 +09:00 committed by GitHub
parent 3ab4bab885
commit edcec7eed8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -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.

View File

@ -86,6 +86,14 @@ fn extract_composer_version(file_contents: &str) -> Option<String> {
Some(formatted_version)
}
fn extract_project_version(file_contents: &str) -> Option<String> {
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<String> {
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<String> {
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
);
}
}