feat: add composer.json version to Package module (#716)

This commit is contained in:
Morten Scheel 2019-12-12 03:05:39 +01:00 committed by Matan Kushner
parent dce52390c8
commit e38a257656
3 changed files with 42 additions and 0 deletions

View File

@ -96,6 +96,7 @@ The prompt shows information you need while you're working, while staying sleek
- npm (Node.js) - npm (Node.js)
- cargo (Rust) - cargo (Rust)
- poetry (Python) - poetry (Python)
- composer (PHP)
- Current Git branch and rich repo status: - Current Git branch and rich repo status:
- `=` — conflicting changes - `=` — conflicting changes
- `⇡` — ahead of remote branch - `⇡` — ahead of remote branch

View File

@ -860,6 +860,8 @@ and `poetry` packages.
in the current directory in the current directory
- **poetry** The `poetry` package version is extracted from the `pyproject.toml` present - **poetry** The `poetry` package version is extracted from the `pyproject.toml` present
in the current directory in the current directory
- **composer** The `composer` package version is extracted from the `composer.json` present
in the current directory
> ⚠️ The version being shown is that of the package whose source code is in your > ⚠️ The version being shown is that of the package whose source code is in your
> current directory, not your package manager. > current directory, not your package manager.

View File

@ -59,6 +59,17 @@ fn extract_poetry_version(file_contents: &str) -> Option<String> {
Some(formatted_version) Some(formatted_version)
} }
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)
}
fn get_package_version() -> Option<String> { fn get_package_version() -> Option<String> {
if let Ok(cargo_toml) = utils::read_file("Cargo.toml") { if let Ok(cargo_toml) = utils::read_file("Cargo.toml") {
extract_cargo_version(&cargo_toml) extract_cargo_version(&cargo_toml)
@ -66,6 +77,8 @@ fn get_package_version() -> Option<String> {
extract_package_version(&package_json) extract_package_version(&package_json)
} else if let Ok(poetry_toml) = utils::read_file("pyproject.toml") { } else if let Ok(poetry_toml) = utils::read_file("pyproject.toml") {
extract_poetry_version(&poetry_toml) extract_poetry_version(&poetry_toml)
} else if let Ok(composer_json) = utils::read_file("composer.json") {
extract_composer_version(&composer_json)
} else { } else {
None None
} }
@ -177,4 +190,30 @@ mod tests {
expected_version expected_version
); );
} }
#[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
);
}
} }