1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-16 18:15:16 +00:00

feat: Display Python package version from poetry (#153)

This commit is contained in:
Kevin Lane 2019-08-15 13:41:06 -07:00 committed by Matan Kushner
parent e250e71019
commit e262187f4c
2 changed files with 50 additions and 10 deletions

View File

@ -348,13 +348,15 @@ symbol = "🤖 "
## Package Version ## Package Version
The `package` module is shown when the current directory is the repository for a The `package` module is shown when the current directory is the repository for a
package, and shows its current version. The module currently supports `npm` and package, and shows its current version. The module currently supports `npm`, `cargo`,
`cargo` packages. and `poetry` packages.
- **npm** The `npm` package version is extracted from the `package.json` present - **npm** The `npm` package version is extracted from the `package.json` present
in the current directory in the current directory
- **cargo** The `cargo` package version is extracted from the `Cargo.toml` present - **cargo** The `cargo` package version is extracted from the `Cargo.toml` present
in the current directory in the current directory
- **poetry** The `poetry` package version is extracted from the `pyproject.toml` 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

@ -46,18 +46,28 @@ fn extract_package_version(file_contents: &str) -> Option<String> {
Some(formatted_version) Some(formatted_version)
} }
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> { fn get_package_version() -> Option<String> {
let cargo_toml = utils::read_file("Cargo.toml"); if let Ok(cargo_toml) = utils::read_file("Cargo.toml") {
if let Ok(cargo_toml) = cargo_toml {
return extract_cargo_version(&cargo_toml); return extract_cargo_version(&cargo_toml);
} } else if let Ok(package_json) = utils::read_file("package.json") {
let package_json = utils::read_file("package.json");
if let Ok(package_json) = package_json {
return extract_package_version(&package_json); return extract_package_version(&package_json);
} else if let Ok(poetry_toml) = utils::read_file("pyproject.toml") {
return extract_poetry_version(&poetry_toml);
} else {
None
} }
None
} }
fn format_version(version: &str) -> String { fn format_version(version: &str) -> String {
@ -123,4 +133,32 @@ mod tests {
expected_version expected_version
); );
} }
#[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
);
}
} }