1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-24 21:57:41 +00:00

feat(package): Meson package support (#2001)

* Add package version support for Meson meson.build.

* Update docs with Meson package version support.
This commit is contained in:
花蛄 2020-12-19 21:17:34 +08:00 committed by GitHub
parent f17556d389
commit 4f51ef8db1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -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.

View File

@ -162,6 +162,16 @@ fn extract_maven_version(file_contents: &str) -> Option<String> {
None
}
fn extract_meson_version(file_contents: &str) -> Option<String> {
let file_contents = file_contents.split_ascii_whitespace().collect::<String>();
let re = Regex::new(r#"project\([^())]*version:'(?P<version>[^']+)'[^())]*\)"#).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<String> {
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<Str
extract_helm_package_version(&chart_file)
} else if let Ok(pom_file) = utils::read_file(base_dir.join("pom.xml")) {
extract_maven_version(&pom_file)
} else if let Ok(meson_build) = utils::read_file(base_dir.join("meson.build")) {
extract_meson_version(&meson_build)
} else {
None
}
@ -722,6 +734,28 @@ end";
project_dir.close()
}
#[test]
fn test_extract_meson_version() -> 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<TempDir> {
Ok(tempfile::tempdir()?)
}