From 295948bc6c2e8dbd463ab1bcdf12c4a28842693e Mon Sep 17 00:00:00 2001 From: Zhong Liu Date: Sun, 2 Jan 2022 06:57:20 +0800 Subject: [PATCH] feat(package): support for dart pub version (#3373) * feat(package): add support for dart pub version * feat(package): add doc for dat pub version --- docs/config/README.md | 3 ++- src/modules/package.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/config/README.md b/docs/config/README.md index 87461b148..93d32c89d 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2240,7 +2240,7 @@ symbol = "☁️ " 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`, `nimble`, `cargo`, -`poetry`, `composer`, `gradle`, `julia`, `mix`, `helm` and `shards` packages. +`poetry`, `composer`, `gradle`, `julia`, `mix`, `helm`, `shards` and `dart` packages. - [**npm**](https://docs.npmjs.com/cli/commands/npm) – The `npm` package version is extracted from the `package.json` present in the current directory @@ -2260,6 +2260,7 @@ package, and shows its current version. The module currently supports `npm`, `ni - [**Shards**](https://crystal-lang.org/reference/the_shards_command/index.html) - The `shards` package version is extracted from the `shard.yml` present - [**V**](https://vlang.io) - The `vlang` package version is extracted from the `v.mod` present - [**SBT**](https://scala-sbt.org) - The `sbt` package version is extracted from the `build.sbt` present in the current directory +- [**Dart**](https://pub.dev/) - The `dart` package version is extracted from the `pubspec.yaml` 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 1b09f8741..acfcf3897 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -239,6 +239,15 @@ fn get_shard_version(context: &Context, config: &PackageConfig) -> Option Option { + let file_contents = utils::read_file(&context.current_dir.join("pubspec.yaml")).ok()?; + + let data = yaml_rust::YamlLoader::load_from_str(&file_contents).ok()?; + let raw_version = data.first()?["version"].as_str()?; + + format_version(raw_version, config.version_format) +} + fn get_version(context: &Context, config: &PackageConfig) -> Option { let package_version_fn: Vec Option> = vec![ get_cargo_version, @@ -257,6 +266,7 @@ fn get_version(context: &Context, config: &PackageConfig) -> Option { get_vmod_version, get_vpkg_version, get_sbt_version, + get_dart_pub_version, ]; package_version_fn.iter().find_map(|f| f(context, config)) @@ -1132,6 +1142,22 @@ scalaVersion := \"2.13.7\" project_dir.close() } + #[test] + fn test_extract_dart_pub_version() -> io::Result<()> { + let config_name = "pubspec.yaml"; + let config_content = " +name: starship +version: 1.0.0 + +environment: + sdk: '>=2.15.0 <3.0.0' +"; + let project_dir = create_project_dir()?; + fill_config(&project_dir, config_name, Some(config_content))?; + expect_output(&project_dir, Some("v1.0.0"), None); + project_dir.close() + } + fn create_project_dir() -> io::Result { tempfile::tempdir() }