refactor: remove version::is_canary(), use ReleaseChannel instead (#25053)

In preparation for https://github.com/denoland/deno/pull/25014, this
commit removes public `is_canary()` method and instead uses an enum
`ReleaseChannel` to be able to designate more "kinds" of builds.
This commit is contained in:
Bartek Iwańczuk 2024-08-15 20:59:16 +01:00 committed by GitHub
parent 8749d651fb
commit e8d57cd3fe
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 113 additions and 74 deletions

View file

@ -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:
// <version>(+<short_git_hash>) (<release_channel>, <profile>, <target>)
// 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")

View file

@ -20,6 +20,7 @@ mod node;
mod npm;
mod ops;
mod resolver;
mod shared;
mod standalone;
mod task_runner;
mod tools;

View file

@ -18,6 +18,7 @@ mod js;
mod node;
mod npm;
mod resolver;
mod shared;
mod task_runner;
mod util;
mod version;

55
cli/shared.rs Normal file
View file

@ -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<Self, AnyError> {
Ok(match str_ {
"stable" => Self::Stable,
"canary" => Self::Canary,
"rc" => Self::Rc,
"lts" => Self::Lts,
unknown => bail!("Unrecognized release channel: {}", unknown),
})
}
}

View file

@ -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() {
let binary_path_suffix = match crate::version::RELEASE_CHANNEL {
ReleaseChannel::Canary => {
format!("canary/{}/{}", crate::version::GIT_COMMIT_HASH, binary_name)
} else {
}
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();

View file

@ -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<ReleaseChannel, AnyError> {
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,7 +638,8 @@ fn select_specific_version_for_upgrade(
) -> Result<Option<AvailableVersion>, AnyError> {
match release_channel {
ReleaseChannel::Stable => {
let current_is_passed = if !version::is_canary() {
let current_is_passed =
if !matches!(version::RELEASE_CHANNEL, ReleaseChannel::Canary) {
version::deno() == version
} else {
false
@ -691,7 +694,8 @@ 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_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)
@ -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<Self, AnyError> {
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 (<commit_hash>, <version_name>)
fn parse_rc_versions_text(
text: &str,

View file

@ -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")