diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 266907b137..79a6fcbd4f 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -36,6 +36,7 @@ use std::path::PathBuf; use std::str::FromStr; use crate::args::resolve_no_prompt; +use crate::shared::ReleaseChannel; use crate::util::fs::canonicalize_path; use super::flags_net; @@ -1334,7 +1335,18 @@ pub fn clap_root() -> Command { let long_version = format!( "{} ({}, {})\nv8 {}\ntypescript {}", crate::version::deno(), - if crate::version::is_canary() { + // TODO(bartlomieju): alter what's printed here. + // I think it's best if we print as follows: + // (+) (, , ) + // For stable it would be: + // v1.46.0 (stable, release, aarch64-apple-darwin) + // For rc it would be: + // v1.46.0-rc.2 (release candidate, release, aarch64-apple-darwin) + // For lts it would be: + // v2.1.13-lts (LTS (long term support), release, aarch64-apple-darwin) + // For canary it would be: + // v1.46.0+25bb59d (canary, release, aarch64-apple-darwin) + if matches!(crate::version::RELEASE_CHANNEL, ReleaseChannel::Canary) { "canary" } else { env!("PROFILE") diff --git a/cli/main.rs b/cli/main.rs index 1752c3373b..48e78becb2 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -20,6 +20,7 @@ mod node; mod npm; mod ops; mod resolver; +mod shared; mod standalone; mod task_runner; mod tools; diff --git a/cli/mainrt.rs b/cli/mainrt.rs index e890b5dc05..9bf3189532 100644 --- a/cli/mainrt.rs +++ b/cli/mainrt.rs @@ -18,6 +18,7 @@ mod js; mod node; mod npm; mod resolver; +mod shared; mod task_runner; mod util; mod version; diff --git a/cli/shared.rs b/cli/shared.rs new file mode 100644 index 0000000000..36e7e1d01e --- /dev/null +++ b/cli/shared.rs @@ -0,0 +1,55 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +use deno_core::anyhow::bail; +use deno_core::error::AnyError; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum ReleaseChannel { + /// Stable version, eg. 1.45.4, 2.0.0, 2.1.0 + Stable, + + /// Pointing to a git hash + Canary, + + /// Long term support release + #[allow(unused)] + Lts, + + /// Release candidate, poiting to a git hash + Rc, +} + +impl ReleaseChannel { + pub fn name(&self) -> &str { + match self { + Self::Stable => "latest", + Self::Canary => "canary", + Self::Rc => "release candidate", + Self::Lts => "LTS (long term support)", + } + } + + // NOTE(bartlomieju): do not ever change these values, tools like `patchver` + // rely on them. + pub fn serialize(&self) -> String { + match self { + Self::Stable => "stable", + Self::Canary => "canary", + Self::Rc => "rc", + Self::Lts => "lts", + } + .to_string() + } + + // NOTE(bartlomieju): do not ever change these values, tools like `patchver` + // rely on them. + pub fn deserialize(str_: &str) -> Result { + Ok(match str_ { + "stable" => Self::Stable, + "canary" => Self::Canary, + "rc" => Self::Rc, + "lts" => Self::Lts, + unknown => bail!("Unrecognized release channel: {}", unknown), + }) + } +} diff --git a/cli/standalone/binary.rs b/cli/standalone/binary.rs index 9d6d5262e8..bbb3a66c1c 100644 --- a/cli/standalone/binary.rs +++ b/cli/standalone/binary.rs @@ -53,6 +53,7 @@ use crate::file_fetcher::FileFetcher; use crate::http_util::HttpClientProvider; use crate::npm::CliNpmResolver; use crate::npm::InnerCliNpmResolverRef; +use crate::shared::ReleaseChannel; use crate::standalone::virtual_fs::VfsEntry; use crate::util::archive; use crate::util::fs::canonicalize_path_maybe_not_exists; @@ -416,10 +417,17 @@ impl<'a> DenoCompileBinaryWriter<'a> { let target = compile_flags.resolve_target(); let binary_name = format!("denort-{target}.zip"); - let binary_path_suffix = if crate::version::is_canary() { - format!("canary/{}/{}", crate::version::GIT_COMMIT_HASH, binary_name) - } else { - format!("release/v{}/{}", env!("CARGO_PKG_VERSION"), binary_name) + let binary_path_suffix = match crate::version::RELEASE_CHANNEL { + ReleaseChannel::Canary => { + format!("canary/{}/{}", crate::version::GIT_COMMIT_HASH, binary_name) + } + ReleaseChannel::Stable => { + format!("release/v{}/{}", env!("CARGO_PKG_VERSION"), binary_name) + } + _ => bail!( + "`deno compile` current doesn't support {} release channel", + crate::version::RELEASE_CHANNEL.name() + ), }; let download_directory = self.deno_dir.dl_folder_path(); diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs index 925a033fed..01239964c8 100644 --- a/cli/tools/upgrade.rs +++ b/cli/tools/upgrade.rs @@ -8,6 +8,7 @@ use crate::colors; use crate::factory::CliFactory; use crate::http_util::HttpClient; use crate::http_util::HttpClientProvider; +use crate::shared::ReleaseChannel; use crate::util::archive; use crate::util::progress_bar::ProgressBar; use crate::util::progress_bar::ProgressBarStyle; @@ -146,7 +147,8 @@ impl VersionProvider for RealVersionProvider { async fn get_current_exe_release_channel( &self, ) -> Result { - if version::is_canary() { + // TODO(bartlomieju): remove hitting a remote server + if matches!(version::RELEASE_CHANNEL, ReleaseChannel::Canary) { // If this fails for whatever reason, just return an empty vector. // It's better to miss that than throw error here. let rc_versions = get_rc_versions( @@ -636,11 +638,12 @@ fn select_specific_version_for_upgrade( ) -> Result, AnyError> { match release_channel { ReleaseChannel::Stable => { - let current_is_passed = if !version::is_canary() { - version::deno() == version - } else { - false - }; + let current_is_passed = + if !matches!(version::RELEASE_CHANNEL, ReleaseChannel::Canary) { + version::deno() == version + } else { + false + }; if !force && current_is_passed { log::info!("Version {} is already installed", version::deno()); @@ -691,15 +694,16 @@ async fn find_latest_version_to_upgrade( let (maybe_newer_latest_version, current_version) = match release_channel { ReleaseChannel::Stable => { let current_version = version::deno(); - let current_is_most_recent = if !version::is_canary() { - let current = Version::parse_standard(current_version).unwrap(); - let latest = - Version::parse_standard(&latest_version_found.version_or_hash) - .unwrap(); - current >= latest - } else { - false - }; + let current_is_most_recent = + if !matches!(version::RELEASE_CHANNEL, ReleaseChannel::Canary) { + let current = Version::parse_standard(current_version).unwrap(); + let latest = + Version::parse_standard(&latest_version_found.version_or_hash) + .unwrap(); + current >= latest + } else { + false + }; if !force && current_is_most_recent { (None, current_version) @@ -751,53 +755,6 @@ async fn find_latest_version_to_upgrade( Ok(maybe_newer_latest_version) } -#[derive(Debug, Clone, Copy, PartialEq)] -enum ReleaseChannel { - /// Stable version, eg. 1.45.4, 2.0.0, 2.1.0 - Stable, - - /// Pointing to a git hash - Canary, - - /// Long term support release - #[allow(unused)] - Lts, - - /// Release candidate, poiting to a git hash - Rc, -} - -impl ReleaseChannel { - fn name(&self) -> &str { - match self { - Self::Stable => "latest", - Self::Canary => "canary", - Self::Rc => "release candidate", - Self::Lts => "LTS (long term support)", - } - } - - fn serialize(&self) -> String { - match self { - Self::Stable => "stable", - Self::Canary => "canary", - Self::Rc => "rc", - Self::Lts => "lts", - } - .to_string() - } - - fn deserialize(str_: &str) -> Result { - Ok(match str_ { - "stable" => Self::Stable, - "canary" => Self::Canary, - "rc" => Self::Rc, - "lts" => Self::Lts, - unknown => bail!("Unrecognized release channel: {}", unknown), - }) - } -} - // Return a list of available RC release in format of (, ) fn parse_rc_versions_text( text: &str, diff --git a/cli/version.rs b/cli/version.rs index aa3e5168e6..e4801e108d 100644 --- a/cli/version.rs +++ b/cli/version.rs @@ -1,10 +1,19 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. +use crate::shared::ReleaseChannel; + pub const GIT_COMMIT_HASH: &str = env!("GIT_COMMIT_HASH"); pub const TYPESCRIPT: &str = env!("TS_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() { + if IS_CANARY { concat!( env!("CARGO_PKG_VERSION"), "+", @@ -17,7 +26,7 @@ pub fn deno() -> &'static str { // Keep this in sync with `deno()` above pub fn get_user_agent() -> &'static str { - if is_canary() { + if IS_CANARY { concat!( "Deno/", env!("CARGO_PKG_VERSION"), @@ -29,12 +38,8 @@ pub fn get_user_agent() -> &'static str { } } -pub fn is_canary() -> bool { - option_env!("DENO_CANARY").is_some() -} - pub fn release_version_or_canary_commit_hash() -> &'static str { - if is_canary() { + if IS_CANARY { GIT_COMMIT_HASH } else { env!("CARGO_PKG_VERSION")