From 4f51ef8db1cbdeda7a70eb7df2cbd55bd7d538b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8A=B1=E8=9B=84?= <15226118+TiceCosmos@users.noreply.github.com> Date: Sat, 19 Dec 2020 21:17:34 +0800 Subject: [PATCH] feat(package): Meson package support (#2001) * Add package version support for Meson meson.build. * Update docs with Meson package version support. --- docs/config/README.md | 1 + src/modules/package.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/docs/config/README.md b/docs/config/README.md index 33d92c31..13bd3684 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1846,6 +1846,7 @@ package, and shows its current version. The module currently supports `npm`, `ca - **mix** - The `mix` package version is extracted from the `mix.exs` present - **helm** - The `helm` chart version is extracted from the `Chart.yaml` present - **maven** - The `maven` package version is extracted from the `pom.xml` present +- **meson** - The `meson` package version is extracted from the `meson.build` 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 9b3dad29..0df57c65 100644 --- a/src/modules/package.rs +++ b/src/modules/package.rs @@ -162,6 +162,16 @@ fn extract_maven_version(file_contents: &str) -> Option { None } +fn extract_meson_version(file_contents: &str) -> Option { + let file_contents = file_contents.split_ascii_whitespace().collect::(); + + let re = Regex::new(r#"project\([^())]*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, config: &PackageConfig) -> Option { if let Ok(cargo_toml) = utils::read_file(base_dir.join("Cargo.toml")) { extract_cargo_version(&cargo_toml) @@ -181,6 +191,8 @@ fn get_package_version(base_dir: &PathBuf, config: &PackageConfig) -> Option io::Result<()> { + let config_name = "meson.build"; + let config_content = "project('starship', 'rust', version: '0.1.0')".to_string(); + + let project_dir = create_project_dir()?; + fill_config(&project_dir, config_name, Some(&config_content))?; + expect_output(&project_dir, Some("v0.1.0"), None)?; + project_dir.close() + } + + #[test] + fn test_extract_meson_version_without_version() -> io::Result<()> { + let config_name = "meson.build"; + let config_content = "project('starship', 'rust')".to_string(); + + let project_dir = create_project_dir()?; + fill_config(&project_dir, config_name, Some(&config_content))?; + expect_output(&project_dir, None, None)?; + project_dir.close() + } + fn create_project_dir() -> io::Result { Ok(tempfile::tempdir()?) }