From 1109fd69979f48cc6bce69d63d4e497727e51b8f Mon Sep 17 00:00:00 2001 From: pejax Date: Fri, 3 Dec 2021 07:56:06 +0100 Subject: [PATCH] feat: Add package version detection for sbt projects (#3274) --- docs/config/README.md | 1 + src/modules/package.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/docs/config/README.md b/docs/config/README.md index b5be43da..f123ce6c 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2239,6 +2239,7 @@ package, and shows its current version. The module currently supports `npm`, `ni - [**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 +- [**SBT**](https://scala-sbt.org) - The `sbt` package version is extracted from the `build.sbt` 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 31e51ae7..1b09f874 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -197,6 +197,13 @@ fn get_vpkg_version(context: &Context, config: &PackageConfig) -> Option format_version(raw_version, config.version_format) } +fn get_sbt_version(context: &Context, config: &PackageConfig) -> Option { + let file_contents = utils::read_file(context.current_dir.join("build.sbt")).ok()?; + let re = Regex::new(r"(?m)^(.*/)*\s*version\s*:=\s*.(?P[\d\.]+)").unwrap(); + let caps = re.captures(&file_contents)?; + format_version(&caps["version"], config.version_format) +} + fn get_cargo_version(context: &Context, config: &PackageConfig) -> Option { let file_contents = utils::read_file(&context.current_dir.join("Cargo.toml")).ok()?; @@ -249,6 +256,7 @@ fn get_version(context: &Context, config: &PackageConfig) -> Option { get_shard_version, get_vmod_version, get_vpkg_version, + get_sbt_version, ]; package_version_fn.iter().find_map(|f| f(context, config)) @@ -1091,6 +1099,39 @@ Module { project_dir.close() } + #[test] + fn test_extract_sbt_version() -> io::Result<()> { + let config_name = "build.sbt"; + let config_content = "\ +name := \"starship\" +version := \"1.2.3\" +scalaVersion := \"2.13.7\" + +assembly / assemblyMergeStrategy := { + case PathList(\"META-INF\", _ @ _*) => MergeStrategy.discard + case _ => MergeStrategy.first +} +"; + 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_extract_sbt_version_thisbuild() -> io::Result<()> { + let config_name = "build.sbt"; + let config_content = "\ +name := \"starship\" +ThisBuild / version := \"1.2.3\" +scalaVersion := \"2.13.7\" +"; + 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() + } + fn create_project_dir() -> io::Result { tempfile::tempdir() }