From e38a25765628e447408f4dc0e636d93585547fa0 Mon Sep 17 00:00:00 2001 From: Morten Scheel Date: Thu, 12 Dec 2019 03:05:39 +0100 Subject: [PATCH] feat: add composer.json version to Package module (#716) --- README.md | 1 + docs/config/README.md | 2 ++ src/modules/package.rs | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/README.md b/README.md index 6b9ecaf7..6894bfc0 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,7 @@ The prompt shows information you need while you're working, while staying sleek - npm (Node.js) - cargo (Rust) - poetry (Python) + - composer (PHP) - Current Git branch and rich repo status: - `=` — conflicting changes - `⇡` — ahead of remote branch diff --git a/docs/config/README.md b/docs/config/README.md index 41562482..41b38585 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -860,6 +860,8 @@ and `poetry` packages. in the current directory - **poetry** – The `poetry` package version is extracted from the `pyproject.toml` present 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 > current directory, not your package manager. diff --git a/src/modules/package.rs b/src/modules/package.rs index 9a5394d5..b6364ced 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -59,6 +59,17 @@ fn extract_poetry_version(file_contents: &str) -> Option { Some(formatted_version) } +fn extract_composer_version(file_contents: &str) -> Option { + 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 { if let Ok(cargo_toml) = utils::read_file("Cargo.toml") { extract_cargo_version(&cargo_toml) @@ -66,6 +77,8 @@ fn get_package_version() -> Option { extract_package_version(&package_json) } else if let Ok(poetry_toml) = utils::read_file("pyproject.toml") { extract_poetry_version(&poetry_toml) + } else if let Ok(composer_json) = utils::read_file("composer.json") { + extract_composer_version(&composer_json) } else { None } @@ -177,4 +190,30 @@ mod tests { 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 + ); + } }