diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-08-15 22:47:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-15 23:47:16 +0200 |
commit | 2bb013f9baa927fc7b392766b0182b91380b9aa8 (patch) | |
tree | ca8a7ea961b50213adf0904d724974c317cefb55 /cli/version.rs | |
parent | 5ec3c5c3a46ca95f355a4520676b85ac619ca102 (diff) |
refactor: `version` module exports a single const struct (#25014)
This commit rewrites the internal `version` module that exported
various information about the current executable. Instead of exporting
several consts, we are now exporting a single const structure that
contains all the necessary information.
This is the first step towards cleaning up how we use this information
and should allow us to use SUI to be able to patch this information
in already produced binary making it easier to cut new releases.
---------
Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'cli/version.rs')
-rw-r--r-- | cli/version.rs | 109 |
1 files changed, 74 insertions, 35 deletions
diff --git a/cli/version.rs b/cli/version.rs index e4801e108..4a71d7090 100644 --- a/cli/version.rs +++ b/cli/version.rs @@ -1,47 +1,86 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use once_cell::sync::Lazy; + use crate::shared::ReleaseChannel; -pub const GIT_COMMIT_HASH: &str = env!("GIT_COMMIT_HASH"); -pub const TYPESCRIPT: &str = env!("TS_VERSION"); +const GIT_COMMIT_HASH: &str = env!("GIT_COMMIT_HASH"); +const TYPESCRIPT: &str = env!("TS_VERSION"); +const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); // TODO(bartlomieju): ideally we could remove this const. const IS_CANARY: bool = option_env!("DENO_CANARY").is_some(); -pub const RELEASE_CHANNEL: ReleaseChannel = if IS_CANARY { - ReleaseChannel::Canary -} else { - ReleaseChannel::Stable -}; - -pub fn deno() -> &'static str { - if IS_CANARY { - concat!( - env!("CARGO_PKG_VERSION"), - "+", - env!("GIT_COMMIT_HASH_SHORT") - ) - } else { - env!("CARGO_PKG_VERSION") - } -} -// Keep this in sync with `deno()` above -pub fn get_user_agent() -> &'static str { - if IS_CANARY { - concat!( - "Deno/", - env!("CARGO_PKG_VERSION"), - "+", - env!("GIT_COMMIT_HASH_SHORT") - ) - } else { - concat!("Deno/", env!("CARGO_PKG_VERSION")) +pub static DENO_VERSION_INFO: Lazy<DenoVersionInfo> = Lazy::new(|| { + let release_channel = libsui::find_section("denover") + .and_then(|buf| std::str::from_utf8(buf).ok()) + .and_then(|str_| ReleaseChannel::deserialize(str_).ok()) + .unwrap_or({ + if IS_CANARY { + ReleaseChannel::Canary + } else { + ReleaseChannel::Stable + } + }); + + DenoVersionInfo { + // TODO(bartlomieju): fix further for RC and LTS releases + deno: if IS_CANARY { + concat!( + env!("CARGO_PKG_VERSION"), + "+", + env!("GIT_COMMIT_HASH_SHORT") + ) + } else { + env!("CARGO_PKG_VERSION") + }, + + release_channel, + + git_hash: GIT_COMMIT_HASH, + + // Keep in sync with `deno` field. + // TODO(bartlomieju): fix further for RC and LTS releases + user_agent: if IS_CANARY { + concat!( + "Deno/", + env!("CARGO_PKG_VERSION"), + "+", + env!("GIT_COMMIT_HASH_SHORT") + ) + } else { + concat!("Deno/", env!("CARGO_PKG_VERSION")) + }, + + typescript: TYPESCRIPT, } +}); + +pub struct DenoVersionInfo { + /// Human-readable version of the current Deno binary. + /// + /// For stable release, a semver, eg. `v1.46.2`. + /// For canary release, a semver + 7-char git hash, eg. `v1.46.3+asdfqwq`. + pub deno: &'static str, + + pub release_channel: ReleaseChannel, + + /// A full git hash. + pub git_hash: &'static str, + + /// A user-agent header that will be used in HTTP client. + pub user_agent: &'static str, + + pub typescript: &'static str, } -pub fn release_version_or_canary_commit_hash() -> &'static str { - if IS_CANARY { - GIT_COMMIT_HASH - } else { - env!("CARGO_PKG_VERSION") +impl DenoVersionInfo { + /// For stable release, a semver like, eg. `v1.46.2`. + /// For canary release a full git hash, eg. `9bdab6fb6b93eb43b1930f40987fa4997287f9c8`. + pub fn version_or_git_hash(&self) -> &'static str { + if IS_CANARY { + self.git_hash + } else { + CARGO_PKG_VERSION + } } } |