From 47867a58dfc50f3c49bdfa9752c00f8abf5e85bf Mon Sep 17 00:00:00 2001 From: Poliorcetics Date: Sat, 20 Apr 2024 17:01:51 +0200 Subject: [PATCH] Ab/version details (#12593) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #12561 # Description Add version details in `version`'s output. The intended use is for third-party tools to be able to quickly check version numbers without having to the parsing from `(version).version` or `$env.NU_VERSION`. # User-Facing Changes This adds 4 new values to the record from `version`: ``` ... │ major │ 0 │ │ minor │ 92 │ │ patch │ 3 │ │ pre │ a-value │ ... ``` `pre` is optional and won't be present most of the time I think. # Tests + Formatting I ran the new command using `cargo run -- -c version`: ``` ╭────────────────────┬─────────────────────────────────────────────────╮ │ version │ 0.92.3 │ │ major │ 0 │ │ minor │ 92 │ │ patch │ 3 │ │ branch │ │ │ commit_hash │ │ │ build_os │ macos-aarch64 │ │ build_target │ aarch64-apple-darwin │ │ rust_version │ rustc 1.77.2 (25ef9e3d8 2024-04-09) │ │ rust_channel │ 1.77.2-aarch64-apple-darwin │ │ cargo_version │ cargo 1.77.2 (e52e36006 2024-03-26) │ │ build_time │ 2024-04-20 15:09:36 +02:00 │ │ build_rust_channel │ release │ │ allocator │ mimalloc │ │ features │ default, sqlite, system-clipboard, trash, which │ │ installed_plugins │ │ ╰────────────────────┴─────────────────────────────────────────────────╯ ``` # After Submitting After this is merged I would like to write docs somewhere for scripts writer to advise using these members instead of the string values. Where should I put said docs ? --- .../nu-cmd-lang/src/core_commands/version.rs | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/crates/nu-cmd-lang/src/core_commands/version.rs b/crates/nu-cmd-lang/src/core_commands/version.rs index ae70ec0dd8..6af44035d5 100644 --- a/crates/nu-cmd-lang/src/core_commands/version.rs +++ b/crates/nu-cmd-lang/src/core_commands/version.rs @@ -1,3 +1,5 @@ +use std::sync::OnceLock; + use nu_engine::command_prelude::*; use nu_protocol::engine::StateWorkingSet; use shadow_rs::shadow; @@ -56,8 +58,12 @@ impl Command for Version { } pub fn version(engine_state: &EngineState, call: &Call) -> Result { - // Pre-allocate the arrays in the worst case (12 items): + // Pre-allocate the arrays in the worst case (17 items): // - version + // - major + // - minor + // - patch + // - pre // - branch // - commit_hash // - build_os @@ -66,15 +72,23 @@ pub fn version(engine_state: &EngineState, call: &Call) -> Result Result = OnceLock::new(); + + let &(major, minor, patch) = VERSION_NUMBERS.get_or_init(|| { + ( + build::PKG_VERSION_MAJOR.parse().expect("Always set"), + build::PKG_VERSION_MINOR.parse().expect("Always set"), + build::PKG_VERSION_PATCH.parse().expect("Always set"), + ) + }); + record.push("major", Value::int(major as _, head)); + record.push("minor", Value::int(minor as _, head)); + record.push("patch", Value::int(patch as _, head)); +} + fn global_allocator() -> &'static str { if cfg!(feature = "mimalloc") { "mimalloc"