feat(package): support JSR package version (#6502)

---------

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
Caleb Cox 2025-03-02 04:49:47 -06:00 committed by GitHub
parent 721e7f11f1
commit bd2321a12f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 92 additions and 0 deletions

10
Cargo.lock generated
View File

@ -1927,6 +1927,15 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "jsonc-parser"
version = "0.26.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b558af6b49fd918e970471374e7a798b2c9bbcda624a210ffa3901ee5614bc8e"
dependencies = [
"serde_json",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
@ -3064,6 +3073,7 @@ dependencies = [
"guess_host_triple",
"home",
"indexmap 2.7.1",
"jsonc-parser",
"libz-ng-sys",
"log",
"mockall",

View File

@ -51,6 +51,7 @@ dunce = "1.0.5"
gix = { version = "0.70.0", default-features = false, features = ["max-performance-safe", "revision"] }
gix-features = { version = "0.40.0", optional = true }
indexmap = { version = "2.7.1", features = ["serde"] }
jsonc-parser = { version = "0.26.2", features = ["serde"] }
log = { version = "0.4.26", features = ["std"] }
# notify-rust is optional (on by default) because the crate doesn't currently build for darwin with nix
# see: https://github.com/NixOS/nixpkgs/issues/160876

View File

@ -3426,6 +3426,7 @@ package, and shows its current version. The module currently supports `npm`, `ni
- [**npm**](https://docs.npmjs.com/cli/commands/npm) The `npm` package version is extracted from the `package.json` present
in the current directory
- [**JSR**](https://jsr.io/) The `jsr` package version is extracted from the `jsr.json`/`jsr.jsonc` or `deno.json`/`deno.jsonc` present in the current directory
- [**Cargo**](https://doc.rust-lang.org/cargo/) The `cargo` package version is extracted from the `Cargo.toml` present in the current directory
- [**Nimble**](https://github.com/nim-lang/nimble) - The `nimble` package version is extracted from the `*.nimble` file present in the current directory with the `nimble dump` command
- [**Poetry**](https://python-poetry.org/) The `poetry` package version is extracted from the `pyproject.toml` present

View File

@ -68,6 +68,25 @@ fn get_node_package_version(context: &Context, config: &PackageConfig) -> Option
Some(formatted_version)
}
fn get_jsr_package_version(context: &Context, config: &PackageConfig) -> Option<String> {
let (filename, contents) = ["deno.json", "deno.jsonc", "jsr.json", "jsr.jsonc"]
.iter()
.find_map(|filename| {
context
.read_file_from_pwd(filename)
.map(|contents| (filename, contents))
})?;
let json_content: json::Value = if filename.ends_with(".jsonc") {
jsonc_parser::parse_to_serde_value(&contents, &Default::default()).ok()??
} else {
json::from_str(&contents).ok()?
};
let raw_version = json_content.get("version")?.as_str()?;
format_version(raw_version, config.version_format)
}
fn get_poetry_version(pyproject: &toml::Table) -> Option<&str> {
pyproject
.get("tool")?
@ -322,6 +341,7 @@ fn get_version(context: &Context, config: &PackageConfig) -> Option<String> {
get_cargo_version,
get_nimble_version,
get_node_package_version,
get_jsr_package_version,
get_pyproject_version,
get_setup_cfg_version,
get_composer_version,
@ -727,6 +747,66 @@ license = "MIT"
project_dir.close()
}
#[test]
fn test_jsr_package_version_with_jsr_json() -> io::Result<()> {
let config_name = "jsr.json";
let config_content = json::json!({
"name": "starship",
"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_jsr_package_version_with_jsr_jsonc() -> io::Result<()> {
let config_name = "jsr.jsonc";
let config_content = r#"{
"name": "starship", // comment
"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_jsr_package_version_with_deno_json() -> io::Result<()> {
let config_name = "deno.json";
let config_content = json::json!({
"name": "starship",
"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_jsr_package_version_with_deno_jsonc() -> io::Result<()> {
let config_name = "deno.jsonc";
let config_content = r#"{
"name": "starship", // comment
"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_crystal_shard_version() -> io::Result<()> {
let config_name = "shard.yml";