From d27b263023c2442fa1beef358d97ccf8465cf149 Mon Sep 17 00:00:00 2001 From: Udo Kramer Date: Wed, 8 Apr 2020 18:39:54 +0200 Subject: [PATCH] feat(package): Add project version for Elixir (#1067) --- docs/config/README.md | 3 +- src/modules/package.rs | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/docs/config/README.md b/docs/config/README.md index 7d1cd722..64869269 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1030,7 +1030,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`, `cargo`, -`poetry`, `composer`, and `gradle` packages. +`poetry`, `composer`, `gradle`, `julia` and `mix` packages. - **npm** – The `npm` package version is extracted from the `package.json` present in the current directory @@ -1042,6 +1042,7 @@ package, and shows its current version. The module currently supports `npm`, `ca in the current directory - **gradle** – The `gradle` package version is extracted from the `build.gradle` present - **julia** - The package version is extracted from the `Project.toml` present +- **mix** - The `mix` package version is extracted from the `mix.exs` present > ⚠️ 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 a5ce51db..c309ffeb 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -94,6 +94,14 @@ fn extract_project_version(file_contents: &str) -> Option { Some(formatted_version) } +fn extract_mix_version(file_contents: &str) -> Option { + let re = Regex::new(r#"(?m)version: "(?P[^"]+)""#).unwrap(); + let caps = re.captures(file_contents)?; + + let formatted_version = format_version(&caps["version"]); + Some(formatted_version) +} + fn get_package_version(base_dir: &PathBuf) -> Option { if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) { extract_cargo_version(&cargo_toml) @@ -107,6 +115,8 @@ fn get_package_version(base_dir: &PathBuf) -> Option { extract_gradle_version(&build_gradle) } else if let Ok(project_toml) = utils::read_file(base_dir.join("Project.toml")) { extract_project_version(&project_toml) + } else if let Ok(mix_file) = utils::read_file(base_dir.join("mix.exs")) { + extract_mix_version(&mix_file) } else { None } @@ -331,6 +341,67 @@ java { ); } + #[test] + fn test_extract_mix_version() { + let mix_complete = "defmodule MyApp.MixProject do + use Mix.Project + + def project do + [ + app: :my_app, + version: \"1.2.3\", + elixir: \"~> 1.10\", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run \"mix help compile.app\" to learn about applications. + def application do + [extra_applications: [:logger]] + end + + # Run \"mix help deps\" to learn about dependencies. + defp deps do + [] + end +end"; + + let expected_version = Some("v1.2.3".to_string()); + assert_eq!(extract_mix_version(&mix_complete), expected_version); + + let mix_partial_oneline = " def project, do: [app: :my_app,version: \"3.2.1\"]"; + + let expected_version = Some("v3.2.1".to_string()); + assert_eq!(extract_mix_version(&mix_partial_oneline), expected_version); + + let mix_partial_prerelease = " def project do + [ + app: :my_app, + version: \"1.0.0-alpha.3\" + ] + end"; + + let expected_version = Some("v1.0.0-alpha.3".to_string()); + assert_eq!( + extract_mix_version(&mix_partial_prerelease), + expected_version + ); + + let mix_partial_prerelease_and_build_info = " def project do + [ + app: :my_app, + version: \"0.9.9-dev+20130417140000.amd64\" + ] + end"; + + let expected_version = Some("v0.9.9-dev+20130417140000.amd64".to_string()); + assert_eq!( + extract_mix_version(&mix_partial_prerelease_and_build_info), + expected_version + ); + } + #[test] fn test_extract_composer_version() { let composer_with_version = json::json!({