From 614e0e2763b3abf3ba19f4697e01f0c214de4f42 Mon Sep 17 00:00:00 2001 From: vypxl Date: Wed, 13 Oct 2021 07:42:55 +0200 Subject: [PATCH] feat(package): Add support for crystal shards (#3147) * add crystal shard (package) version support * module package: crystal shard version: read shard.yml directly * module package: add test for crystal shard version * format src/modules/package.rs * use yaml-rust instead of serde-yaml * document shards package support * Update docs/config/README.md Co-authored-by: David Knaack Co-authored-by: David Knaack --- docs/config/README.md | 3 ++- src/modules/package.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/docs/config/README.md b/docs/config/README.md index c6ae7b04..8b32f787 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2216,7 +2216,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` and `helm` packages. +`poetry`, `composer`, `gradle`, `julia`, `mix`, `helm` and `shards` packages. - [**npm**](https://docs.npmjs.com/cli/commands/npm) – The `npm` package version is extracted from the `package.json` present in the current directory @@ -2233,6 +2233,7 @@ package, and shows its current version. The module currently supports `npm`, `ni - [**Helm**](https://helm.sh/docs/helm/helm_package/) - The `helm` chart version is extracted from the `Chart.yaml` present - [**Maven**](https://maven.apache.org/) - The `maven` package version is extracted from the `pom.xml` present - [**Meson**](https://mesonbuild.com/) - The `meson` package version is extracted from the `meson.build` present +- [**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 > ⚠️ The version being shown is that of the package whose source code is in your diff --git a/src/modules/package.rs b/src/modules/package.rs index 885a3806..d34dbdbc 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -223,6 +223,15 @@ fn get_nimble_version(context: &Context, config: &PackageConfig) -> Option Option { + let file_contents = utils::read_file(&context.current_dir.join("shard.yml")).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, @@ -237,6 +246,7 @@ fn get_version(context: &Context, config: &PackageConfig) -> Option { get_helm_package_version, get_maven_version, get_meson_version, + get_shard_version, get_vmod_version, get_vpkg_version, ]; @@ -546,6 +556,19 @@ license = "MIT" project_dir.close() } + #[test] + fn test_crystal_shard_version() -> io::Result<()> { + let config_name = "shard.yml"; + let config_content = "name: starship\nversion: 1.2.3\n".to_string(); + + let project_dir = create_project_dir()?; + + fill_config(&project_dir, config_name, Some(&config_content))?; + expect_output(&project_dir, Some("v1.2.3"), None); + + project_dir.close() + } + #[test] fn test_node_package_version_with_non_semantic_tag() -> io::Result<()> { let config_name = "package.json";