2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-03-23 11:37:24 -04:00
|
|
|
|
|
|
|
//! This module provides feature to upgrade deno executable
|
|
|
|
|
2022-12-12 21:30:44 -05:00
|
|
|
use crate::args::Flags;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::UpgradeFlags;
|
2022-10-20 19:50:55 -04:00
|
|
|
use crate::colors;
|
2023-05-01 14:35:23 -04:00
|
|
|
use crate::factory::CliFactory;
|
2022-12-12 21:30:44 -05:00
|
|
|
use crate::http_util::HttpClient;
|
2024-02-13 11:22:30 -05:00
|
|
|
use crate::standalone::binary::unpack_into_dir;
|
2022-12-01 18:05:49 -05:00
|
|
|
use crate::util::progress_bar::ProgressBar;
|
2022-12-12 20:52:10 -05:00
|
|
|
use crate::util::progress_bar::ProgressBarStyle;
|
2023-03-15 03:14:22 -04:00
|
|
|
use crate::util::time;
|
2022-10-20 10:15:21 -04:00
|
|
|
use crate::version;
|
2022-10-20 19:50:55 -04:00
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
use async_trait::async_trait;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2022-12-02 14:53:10 -05:00
|
|
|
use deno_core::anyhow::Context;
|
2021-11-09 05:06:45 -05:00
|
|
|
use deno_core::error::AnyError;
|
2023-08-23 19:03:05 -04:00
|
|
|
use deno_core::unsync::spawn;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::Version;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2022-10-22 14:31:25 -04:00
|
|
|
use std::borrow::Cow;
|
2022-03-18 18:40:44 -04:00
|
|
|
use std::env;
|
2020-03-23 11:37:24 -04:00
|
|
|
use std::fs;
|
2023-08-25 09:43:07 -04:00
|
|
|
use std::io::IsTerminal;
|
2022-10-21 11:07:17 -04:00
|
|
|
use std::ops::Sub;
|
2020-03-23 11:37:24 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
2023-05-01 16:42:05 -04:00
|
|
|
use std::sync::Arc;
|
2022-10-20 10:15:21 -04:00
|
|
|
use std::time::Duration;
|
2020-03-23 11:37:24 -04:00
|
|
|
|
2020-11-22 10:07:05 -05:00
|
|
|
const RELEASE_URL: &str = "https://github.com/denoland/deno/releases";
|
2020-03-23 11:37:24 -04:00
|
|
|
|
2024-02-13 11:22:30 -05:00
|
|
|
pub static ARCHIVE_NAME: Lazy<String> =
|
|
|
|
Lazy::new(|| format!("deno-{}.zip", env!("TARGET")));
|
|
|
|
|
2022-10-20 10:15:21 -04:00
|
|
|
// How often query server for new version. In hours.
|
|
|
|
const UPGRADE_CHECK_INTERVAL: i64 = 24;
|
|
|
|
|
|
|
|
const UPGRADE_CHECK_FETCH_DELAY: Duration = Duration::from_millis(500);
|
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
/// Environment necessary for doing the update checker.
|
|
|
|
/// An alternate trait implementation can be provided for testing purposes.
|
2023-11-17 16:40:12 -05:00
|
|
|
trait UpdateCheckerEnvironment: Clone {
|
2022-10-22 14:31:25 -04:00
|
|
|
fn read_check_file(&self) -> String;
|
|
|
|
fn write_check_file(&self, text: &str);
|
|
|
|
fn current_time(&self) -> chrono::DateTime<chrono::Utc>;
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct RealUpdateCheckerEnvironment {
|
2022-11-25 19:04:30 -05:00
|
|
|
cache_file_path: PathBuf,
|
2022-10-22 14:31:25 -04:00
|
|
|
current_time: chrono::DateTime<chrono::Utc>,
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
impl RealUpdateCheckerEnvironment {
|
2023-11-17 16:40:12 -05:00
|
|
|
pub fn new(cache_file_path: PathBuf) -> Self {
|
2022-10-22 14:31:25 -04:00
|
|
|
Self {
|
2022-11-25 19:04:30 -05:00
|
|
|
cache_file_path,
|
2022-10-22 14:31:25 -04:00
|
|
|
// cache the current time
|
2023-03-15 03:14:22 -04:00
|
|
|
current_time: time::utc_now(),
|
2022-10-20 17:01:17 -04:00
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
impl UpdateCheckerEnvironment for RealUpdateCheckerEnvironment {
|
|
|
|
fn read_check_file(&self) -> String {
|
2022-11-25 19:04:30 -05:00
|
|
|
std::fs::read_to_string(&self.cache_file_path).unwrap_or_default()
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_check_file(&self, text: &str) {
|
2022-11-25 19:04:30 -05:00
|
|
|
let _ = std::fs::write(&self.cache_file_path, text);
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
2022-10-20 19:50:55 -04:00
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
fn current_time(&self) -> chrono::DateTime<chrono::Utc> {
|
|
|
|
self.current_time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
enum UpgradeCheckKind {
|
|
|
|
Execution,
|
|
|
|
Lsp,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
trait VersionProvider: Clone {
|
|
|
|
fn is_canary(&self) -> bool;
|
|
|
|
async fn latest_version(&self) -> Result<String, AnyError>;
|
|
|
|
fn current_version(&self) -> Cow<str>;
|
|
|
|
|
|
|
|
fn release_kind(&self) -> UpgradeReleaseKind {
|
|
|
|
if self.is_canary() {
|
|
|
|
UpgradeReleaseKind::Canary
|
|
|
|
} else {
|
|
|
|
UpgradeReleaseKind::Stable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct RealVersionProvider {
|
|
|
|
http_client: Arc<HttpClient>,
|
|
|
|
check_kind: UpgradeCheckKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RealVersionProvider {
|
|
|
|
pub fn new(
|
|
|
|
http_client: Arc<HttpClient>,
|
|
|
|
check_kind: UpgradeCheckKind,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
http_client,
|
|
|
|
check_kind,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait(?Send)]
|
|
|
|
impl VersionProvider for RealVersionProvider {
|
|
|
|
fn is_canary(&self) -> bool {
|
|
|
|
version::is_canary()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn latest_version(&self) -> Result<String, AnyError> {
|
|
|
|
get_latest_version(&self.http_client, self.release_kind(), self.check_kind)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_version(&self) -> Cow<str> {
|
|
|
|
Cow::Borrowed(version::release_version_or_canary_commit_hash())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UpdateChecker<
|
|
|
|
TEnvironment: UpdateCheckerEnvironment,
|
|
|
|
TVersionProvider: VersionProvider,
|
|
|
|
> {
|
2022-10-22 14:31:25 -04:00
|
|
|
env: TEnvironment,
|
2023-11-17 16:40:12 -05:00
|
|
|
version_provider: TVersionProvider,
|
2022-10-22 14:31:25 -04:00
|
|
|
maybe_file: Option<CheckVersionFile>,
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
impl<
|
|
|
|
TEnvironment: UpdateCheckerEnvironment,
|
|
|
|
TVersionProvider: VersionProvider,
|
|
|
|
> UpdateChecker<TEnvironment, TVersionProvider>
|
|
|
|
{
|
|
|
|
pub fn new(env: TEnvironment, version_provider: TVersionProvider) -> Self {
|
2022-10-22 14:31:25 -04:00
|
|
|
let maybe_file = CheckVersionFile::parse(env.read_check_file());
|
2023-11-17 16:40:12 -05:00
|
|
|
Self {
|
|
|
|
env,
|
|
|
|
version_provider,
|
|
|
|
maybe_file,
|
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn should_check_for_new_version(&self) -> bool {
|
|
|
|
match &self.maybe_file {
|
|
|
|
Some(file) => {
|
|
|
|
let last_check_age = self
|
|
|
|
.env
|
|
|
|
.current_time()
|
|
|
|
.signed_duration_since(file.last_checked);
|
|
|
|
last_check_age > chrono::Duration::hours(UPGRADE_CHECK_INTERVAL)
|
|
|
|
}
|
|
|
|
None => true,
|
2022-10-20 19:50:55 -04:00
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the version if a new one is available and it should be prompted about.
|
|
|
|
pub fn should_prompt(&self) -> Option<String> {
|
|
|
|
let file = self.maybe_file.as_ref()?;
|
2023-06-26 09:10:27 -04:00
|
|
|
// If the current version saved is not the actually current version of the binary
|
2022-11-05 19:06:58 -04:00
|
|
|
// It means
|
2023-02-07 22:32:30 -05:00
|
|
|
// - We already check for a new version today
|
2022-11-05 19:06:58 -04:00
|
|
|
// - The user have probably upgraded today
|
|
|
|
// So we should not prompt and wait for tomorrow for the latest version to be updated again
|
2023-11-17 16:40:12 -05:00
|
|
|
let current_version = self.version_provider.current_version();
|
|
|
|
if file.current_version != current_version {
|
2022-11-05 19:06:58 -04:00
|
|
|
return None;
|
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
if file.latest_version == current_version {
|
2022-10-22 14:31:25 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
if let Ok(current) = Version::parse_standard(¤t_version) {
|
2023-02-15 13:20:40 -05:00
|
|
|
if let Ok(latest) = Version::parse_standard(&file.latest_version) {
|
2022-10-28 12:43:49 -04:00
|
|
|
if current >= latest {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
let last_prompt_age = self
|
|
|
|
.env
|
|
|
|
.current_time()
|
|
|
|
.signed_duration_since(file.last_prompt);
|
|
|
|
if last_prompt_age > chrono::Duration::hours(UPGRADE_CHECK_INTERVAL) {
|
|
|
|
Some(file.latest_version.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Store that we showed the update message to the user.
|
|
|
|
pub fn store_prompted(self) {
|
|
|
|
if let Some(file) = self.maybe_file {
|
|
|
|
self.env.write_check_file(
|
|
|
|
&file.with_last_prompt(self.env.current_time()).serialize(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-24 07:30:45 -05:00
|
|
|
fn get_minor_version(version: &str) -> &str {
|
|
|
|
version.rsplitn(2, '.').collect::<Vec<&str>>()[1]
|
|
|
|
}
|
|
|
|
|
2023-01-25 10:29:27 -05:00
|
|
|
fn print_release_notes(current_version: &str, new_version: &str) {
|
|
|
|
if get_minor_version(current_version) != get_minor_version(new_version) {
|
|
|
|
log::info!(
|
|
|
|
"{}{}",
|
|
|
|
"Release notes: https://github.com/denoland/deno/releases/tag/v",
|
|
|
|
&new_version,
|
|
|
|
);
|
|
|
|
log::info!(
|
|
|
|
"{}{}",
|
|
|
|
"Blog post: https://deno.com/blog/v",
|
|
|
|
get_minor_version(new_version)
|
|
|
|
);
|
|
|
|
}
|
2023-01-24 07:30:45 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:10:51 -05:00
|
|
|
pub fn upgrade_check_enabled() -> bool {
|
|
|
|
matches!(
|
|
|
|
env::var("DENO_NO_UPDATE_CHECK"),
|
|
|
|
Err(env::VarError::NotPresent)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-01 16:42:05 -04:00
|
|
|
pub fn check_for_upgrades(
|
|
|
|
http_client: Arc<HttpClient>,
|
|
|
|
cache_file_path: PathBuf,
|
|
|
|
) {
|
2023-11-14 17:10:51 -05:00
|
|
|
if !upgrade_check_enabled() {
|
2022-10-22 14:31:25 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
let env = RealUpdateCheckerEnvironment::new(cache_file_path);
|
|
|
|
let version_provider =
|
|
|
|
RealVersionProvider::new(http_client, UpgradeCheckKind::Execution);
|
|
|
|
let update_checker = UpdateChecker::new(env, version_provider);
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
if update_checker.should_check_for_new_version() {
|
|
|
|
let env = update_checker.env.clone();
|
2023-11-17 16:40:12 -05:00
|
|
|
let version_provider = update_checker.version_provider.clone();
|
2022-10-22 14:31:25 -04:00
|
|
|
// do this asynchronously on a separate task
|
2023-05-14 17:40:01 -04:00
|
|
|
spawn(async move {
|
2022-10-22 14:31:25 -04:00
|
|
|
// Sleep for a small amount of time to not unnecessarily impact startup
|
|
|
|
// time.
|
|
|
|
tokio::time::sleep(UPGRADE_CHECK_FETCH_DELAY).await;
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
fetch_and_store_latest_version(&env, &version_provider).await;
|
2023-11-23 11:20:40 -05:00
|
|
|
|
|
|
|
// text is used by the test suite
|
|
|
|
log::debug!("Finished upgrade checker.")
|
2022-10-22 14:31:25 -04:00
|
|
|
});
|
|
|
|
}
|
2022-10-20 19:50:55 -04:00
|
|
|
|
2022-10-26 17:20:41 -04:00
|
|
|
// Print a message if an update is available
|
2022-10-22 14:31:25 -04:00
|
|
|
if let Some(upgrade_version) = update_checker.should_prompt() {
|
2023-08-25 09:43:07 -04:00
|
|
|
if log::log_enabled!(log::Level::Info) && std::io::stderr().is_terminal() {
|
2022-10-26 17:20:41 -04:00
|
|
|
if version::is_canary() {
|
|
|
|
eprint!(
|
|
|
|
"{} ",
|
|
|
|
colors::green("A new canary release of Deno is available.")
|
|
|
|
);
|
2022-10-31 06:48:28 -04:00
|
|
|
eprintln!(
|
|
|
|
"{}",
|
|
|
|
colors::italic_gray("Run `deno upgrade --canary` to install it.")
|
|
|
|
);
|
2022-10-26 17:20:41 -04:00
|
|
|
} else {
|
|
|
|
eprint!(
|
|
|
|
"{} {} → {} ",
|
|
|
|
colors::green("A new release of Deno is available:"),
|
|
|
|
colors::cyan(version::deno()),
|
2023-01-24 07:30:45 -05:00
|
|
|
colors::cyan(&upgrade_version)
|
2022-10-26 17:20:41 -04:00
|
|
|
);
|
2022-10-31 06:48:28 -04:00
|
|
|
eprintln!(
|
|
|
|
"{}",
|
|
|
|
colors::italic_gray("Run `deno upgrade` to install it.")
|
|
|
|
);
|
2022-10-26 17:20:41 -04:00
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
update_checker.store_prompted();
|
2022-10-20 19:50:55 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub struct LspVersionUpgradeInfo {
|
|
|
|
pub latest_version: String,
|
|
|
|
pub is_canary: bool,
|
|
|
|
}
|
|
|
|
|
2023-11-14 17:10:51 -05:00
|
|
|
pub async fn check_for_upgrades_for_lsp(
|
|
|
|
http_client: Arc<HttpClient>,
|
2023-11-17 16:40:12 -05:00
|
|
|
) -> Result<Option<LspVersionUpgradeInfo>, AnyError> {
|
2023-11-14 17:10:51 -05:00
|
|
|
if !upgrade_check_enabled() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
|
|
|
|
let version_provider =
|
|
|
|
RealVersionProvider::new(http_client, UpgradeCheckKind::Lsp);
|
|
|
|
check_for_upgrades_for_lsp_with_provider(&version_provider).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn check_for_upgrades_for_lsp_with_provider(
|
|
|
|
version_provider: &impl VersionProvider,
|
|
|
|
) -> Result<Option<LspVersionUpgradeInfo>, AnyError> {
|
|
|
|
let latest_version = version_provider.latest_version().await?;
|
|
|
|
let current_version = version_provider.current_version();
|
|
|
|
if current_version == latest_version {
|
|
|
|
Ok(None) // nothing to upgrade
|
|
|
|
} else if version_provider.is_canary() {
|
|
|
|
Ok(Some(LspVersionUpgradeInfo {
|
|
|
|
latest_version,
|
|
|
|
is_canary: true,
|
|
|
|
}))
|
2023-11-14 17:10:51 -05:00
|
|
|
} else {
|
2023-11-17 16:40:12 -05:00
|
|
|
if let Ok(current) = Version::parse_standard(¤t_version) {
|
2023-11-14 17:10:51 -05:00
|
|
|
if let Ok(latest) = Version::parse_standard(&latest_version) {
|
|
|
|
if current >= latest {
|
2023-11-17 16:40:12 -05:00
|
|
|
return Ok(None); // nothing to upgrade
|
2023-11-14 17:10:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
Ok(Some(LspVersionUpgradeInfo {
|
|
|
|
latest_version,
|
|
|
|
is_canary: false,
|
|
|
|
}))
|
|
|
|
}
|
2023-11-14 17:10:51 -05:00
|
|
|
}
|
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
async fn fetch_and_store_latest_version<
|
|
|
|
TEnvironment: UpdateCheckerEnvironment,
|
2023-11-17 16:40:12 -05:00
|
|
|
TVersionProvider: VersionProvider,
|
2022-10-22 14:31:25 -04:00
|
|
|
>(
|
|
|
|
env: &TEnvironment,
|
2023-11-17 16:40:12 -05:00
|
|
|
version_provider: &TVersionProvider,
|
2022-10-22 14:31:25 -04:00
|
|
|
) {
|
|
|
|
// Fetch latest version or commit hash from server.
|
2023-11-17 16:40:12 -05:00
|
|
|
let latest_version = match version_provider.latest_version().await {
|
2022-10-22 14:31:25 -04:00
|
|
|
Ok(latest_version) => latest_version,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
env.write_check_file(
|
|
|
|
&CheckVersionFile {
|
|
|
|
// put a date in the past here so that prompt can be shown on next run
|
|
|
|
last_prompt: env
|
|
|
|
.current_time()
|
|
|
|
.sub(chrono::Duration::hours(UPGRADE_CHECK_INTERVAL + 1)),
|
|
|
|
last_checked: env.current_time(),
|
2023-11-17 16:40:12 -05:00
|
|
|
current_version: version_provider.current_version().to_string(),
|
2022-10-22 14:31:25 -04:00
|
|
|
latest_version,
|
|
|
|
}
|
|
|
|
.serialize(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-12 21:30:44 -05:00
|
|
|
pub async fn upgrade(
|
|
|
|
flags: Flags,
|
|
|
|
upgrade_flags: UpgradeFlags,
|
|
|
|
) -> Result<(), AnyError> {
|
2024-03-11 23:48:00 -04:00
|
|
|
let factory = CliFactory::from_flags(flags)?;
|
2023-05-01 16:42:05 -04:00
|
|
|
let client = factory.http_client();
|
2022-12-02 14:53:10 -05:00
|
|
|
let current_exe_path = std::env::current_exe()?;
|
2023-07-20 15:51:02 -04:00
|
|
|
let output_exe_path =
|
|
|
|
upgrade_flags.output.as_ref().unwrap_or(¤t_exe_path);
|
|
|
|
|
|
|
|
let permissions = if let Ok(metadata) = fs::metadata(output_exe_path) {
|
|
|
|
let permissions = metadata.permissions();
|
|
|
|
if permissions.readonly() {
|
|
|
|
bail!(
|
|
|
|
"You do not have write permission to {}",
|
|
|
|
output_exe_path.display()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
|
|
if std::os::unix::fs::MetadataExt::uid(&metadata) == 0
|
|
|
|
&& !nix::unistd::Uid::effective().is_root()
|
|
|
|
{
|
|
|
|
bail!(concat!(
|
|
|
|
"You don't have write permission to {} because it's owned by root.\n",
|
|
|
|
"Consider updating deno through your package manager if its installed from it.\n",
|
|
|
|
"Otherwise run `deno upgrade` as root.",
|
|
|
|
), output_exe_path.display());
|
|
|
|
}
|
|
|
|
permissions
|
|
|
|
} else {
|
|
|
|
fs::metadata(¤t_exe_path)?.permissions()
|
|
|
|
};
|
2022-02-23 16:11:46 -05:00
|
|
|
|
2021-12-21 09:49:27 -05:00
|
|
|
let install_version = match upgrade_flags.version {
|
2020-11-22 10:07:05 -05:00
|
|
|
Some(passed_version) => {
|
2023-04-12 21:08:01 -04:00
|
|
|
let re_hash = lazy_regex::regex!("^[0-9a-f]{40}$");
|
2023-05-15 21:21:59 -04:00
|
|
|
let passed_version = passed_version
|
|
|
|
.strip_prefix('v')
|
|
|
|
.unwrap_or(&passed_version)
|
|
|
|
.to_string();
|
2023-04-12 21:08:01 -04:00
|
|
|
|
|
|
|
if upgrade_flags.canary && !re_hash.is_match(&passed_version) {
|
2021-07-07 14:59:39 -04:00
|
|
|
bail!("Invalid commit hash passed");
|
2022-08-10 15:23:58 -04:00
|
|
|
} else if !upgrade_flags.canary
|
2023-02-15 13:20:40 -05:00
|
|
|
&& Version::parse_standard(&passed_version).is_err()
|
2021-12-21 09:49:27 -05:00
|
|
|
{
|
2023-02-15 13:20:40 -05:00
|
|
|
bail!("Invalid version passed");
|
2021-07-07 14:59:39 -04:00
|
|
|
}
|
|
|
|
|
2021-12-21 09:49:27 -05:00
|
|
|
let current_is_passed = if upgrade_flags.canary {
|
2021-01-19 07:53:23 -05:00
|
|
|
crate::version::GIT_COMMIT_HASH == passed_version
|
2020-11-29 14:00:35 -05:00
|
|
|
} else if !crate::version::is_canary() {
|
|
|
|
crate::version::deno() == passed_version
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
|
2021-12-21 09:49:27 -05:00
|
|
|
if !upgrade_flags.force
|
|
|
|
&& upgrade_flags.output.is_none()
|
|
|
|
&& current_is_passed
|
|
|
|
{
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Version {} is already installed", crate::version::deno());
|
2020-11-22 10:07:05 -05:00
|
|
|
return Ok(());
|
2020-05-09 06:31:15 -04:00
|
|
|
}
|
2023-05-15 21:21:59 -04:00
|
|
|
|
|
|
|
passed_version
|
2020-11-22 10:07:05 -05:00
|
|
|
}
|
2020-05-09 06:31:15 -04:00
|
|
|
None => {
|
2023-11-17 16:40:12 -05:00
|
|
|
let release_kind = if upgrade_flags.canary {
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Looking up latest canary version");
|
2023-11-17 16:40:12 -05:00
|
|
|
UpgradeReleaseKind::Canary
|
2020-11-29 14:00:35 -05:00
|
|
|
} else {
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Looking up latest version");
|
2023-11-17 16:40:12 -05:00
|
|
|
UpgradeReleaseKind::Stable
|
2020-11-29 14:00:35 -05:00
|
|
|
};
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
let latest_version =
|
|
|
|
get_latest_version(client, release_kind, UpgradeCheckKind::Execution)
|
|
|
|
.await?;
|
|
|
|
|
2021-12-21 09:49:27 -05:00
|
|
|
let current_is_most_recent = if upgrade_flags.canary {
|
2023-06-05 20:35:39 -04:00
|
|
|
let latest_hash = &latest_version;
|
2020-11-29 14:00:35 -05:00
|
|
|
crate::version::GIT_COMMIT_HASH == latest_hash
|
|
|
|
} else if !crate::version::is_canary() {
|
2023-03-23 18:27:58 -04:00
|
|
|
let current = Version::parse_standard(crate::version::deno()).unwrap();
|
2023-02-15 13:20:40 -05:00
|
|
|
let latest = Version::parse_standard(&latest_version).unwrap();
|
2020-11-29 14:00:35 -05:00
|
|
|
current >= latest
|
|
|
|
} else {
|
|
|
|
false
|
2020-11-22 10:07:05 -05:00
|
|
|
};
|
|
|
|
|
2021-12-21 09:49:27 -05:00
|
|
|
if !upgrade_flags.force
|
|
|
|
&& upgrade_flags.output.is_none()
|
|
|
|
&& current_is_most_recent
|
|
|
|
{
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!(
|
2020-05-09 06:31:15 -04:00
|
|
|
"Local deno version {} is the most recent release",
|
2022-12-12 21:30:44 -05:00
|
|
|
if upgrade_flags.canary {
|
2023-03-23 18:27:58 -04:00
|
|
|
crate::version::GIT_COMMIT_HASH
|
2022-12-12 21:30:44 -05:00
|
|
|
} else {
|
|
|
|
crate::version::deno()
|
|
|
|
}
|
2020-05-09 06:31:15 -04:00
|
|
|
);
|
2020-07-06 18:21:26 -04:00
|
|
|
return Ok(());
|
2020-05-09 06:31:15 -04:00
|
|
|
} else {
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Found latest version {}", latest_version);
|
2020-05-09 06:31:15 -04:00
|
|
|
latest_version
|
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
2020-05-09 06:31:15 -04:00
|
|
|
};
|
|
|
|
|
2021-12-21 09:49:27 -05:00
|
|
|
let download_url = if upgrade_flags.canary {
|
2020-11-29 14:00:35 -05:00
|
|
|
format!(
|
|
|
|
"https://dl.deno.land/canary/{}/{}",
|
|
|
|
install_version, *ARCHIVE_NAME
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{}/download/v{}/{}",
|
|
|
|
RELEASE_URL, install_version, *ARCHIVE_NAME
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2023-01-13 16:39:19 -05:00
|
|
|
let archive_data = download_package(client, &download_url)
|
2022-12-14 08:47:18 -05:00
|
|
|
.await
|
2024-03-04 09:28:57 -05:00
|
|
|
.with_context(|| format!("Failed downloading {download_url}. The version you requested may not have been built for the current architecture."))?;
|
2020-11-22 10:07:05 -05:00
|
|
|
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Deno is upgrading to version {}", &install_version);
|
2020-11-22 10:07:05 -05:00
|
|
|
|
2023-03-22 14:55:19 -04:00
|
|
|
let temp_dir = tempfile::TempDir::new()?;
|
2024-02-13 11:22:30 -05:00
|
|
|
let new_exe_path = unpack_into_dir(
|
|
|
|
"deno",
|
|
|
|
&ARCHIVE_NAME,
|
|
|
|
archive_data,
|
|
|
|
cfg!(windows),
|
|
|
|
&temp_dir,
|
|
|
|
)?;
|
2020-05-09 06:31:15 -04:00
|
|
|
fs::set_permissions(&new_exe_path, permissions)?;
|
2020-11-29 14:00:35 -05:00
|
|
|
check_exe(&new_exe_path)?;
|
2020-05-09 06:31:15 -04:00
|
|
|
|
2022-12-12 20:52:10 -05:00
|
|
|
if upgrade_flags.dry_run {
|
|
|
|
fs::remove_file(&new_exe_path)?;
|
|
|
|
log::info!("Upgraded successfully (dry run)");
|
2023-01-25 10:29:27 -05:00
|
|
|
if !upgrade_flags.canary {
|
2023-03-23 18:27:58 -04:00
|
|
|
print_release_notes(version::deno(), &install_version);
|
2023-01-25 10:29:27 -05:00
|
|
|
}
|
2022-12-12 20:52:10 -05:00
|
|
|
} else {
|
2022-12-02 14:53:10 -05:00
|
|
|
let output_exe_path =
|
|
|
|
upgrade_flags.output.as_ref().unwrap_or(¤t_exe_path);
|
|
|
|
let output_result = if *output_exe_path == current_exe_path {
|
|
|
|
replace_exe(&new_exe_path, output_exe_path)
|
|
|
|
} else {
|
|
|
|
fs::rename(&new_exe_path, output_exe_path)
|
|
|
|
.or_else(|_| fs::copy(&new_exe_path, output_exe_path).map(|_| ()))
|
|
|
|
};
|
|
|
|
if let Err(err) = output_result {
|
|
|
|
const WIN_ERROR_ACCESS_DENIED: i32 = 5;
|
|
|
|
if cfg!(windows) && err.raw_os_error() == Some(WIN_ERROR_ACCESS_DENIED) {
|
|
|
|
return Err(err).with_context(|| {
|
|
|
|
format!(
|
|
|
|
concat!(
|
2022-12-14 16:21:43 -05:00
|
|
|
"Could not replace the deno executable. This may be because an ",
|
|
|
|
"existing deno process is running. Please ensure there are no ",
|
|
|
|
"running deno processes (ex. Stop-Process -Name deno ; deno {}), ",
|
|
|
|
"close any editors before upgrading, and ensure you have ",
|
|
|
|
"sufficient permission to '{}'."
|
2022-12-02 14:53:10 -05:00
|
|
|
),
|
2022-12-14 16:21:43 -05:00
|
|
|
// skip the first argument, which is the executable path
|
|
|
|
std::env::args().skip(1).collect::<Vec<_>>().join(" "),
|
2022-12-02 14:53:10 -05:00
|
|
|
output_exe_path.display(),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Err(err.into());
|
2020-07-06 18:21:26 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 20:52:10 -05:00
|
|
|
log::info!("Upgraded successfully");
|
2023-01-25 10:29:27 -05:00
|
|
|
if !upgrade_flags.canary {
|
2023-03-23 18:27:58 -04:00
|
|
|
print_release_notes(version::deno(), &install_version);
|
2023-01-25 10:29:27 -05:00
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
2020-05-09 06:31:15 -04:00
|
|
|
|
2023-01-26 09:02:25 -05:00
|
|
|
drop(temp_dir); // delete the temp dir
|
2020-03-23 11:37:24 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
enum UpgradeReleaseKind {
|
|
|
|
Stable,
|
|
|
|
Canary,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_latest_version(
|
2022-12-12 21:30:44 -05:00
|
|
|
client: &HttpClient,
|
2023-11-17 16:40:12 -05:00
|
|
|
release_kind: UpgradeReleaseKind,
|
|
|
|
check_kind: UpgradeCheckKind,
|
2020-11-29 14:00:35 -05:00
|
|
|
) -> Result<String, AnyError> {
|
2024-01-30 14:50:54 -05:00
|
|
|
let url = get_url(release_kind, env!("TARGET"), check_kind);
|
2023-11-14 17:10:51 -05:00
|
|
|
let text = client.download_text(url).await?;
|
2023-11-17 16:40:12 -05:00
|
|
|
Ok(normalize_version_from_server(release_kind, &text))
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
fn normalize_version_from_server(
|
|
|
|
release_kind: UpgradeReleaseKind,
|
|
|
|
text: &str,
|
|
|
|
) -> String {
|
|
|
|
let text = text.trim();
|
|
|
|
match release_kind {
|
|
|
|
UpgradeReleaseKind::Stable => text.trim_start_matches('v').to_string(),
|
|
|
|
UpgradeReleaseKind::Canary => text.to_string(),
|
2023-11-14 17:10:51 -05:00
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_url(
|
|
|
|
release_kind: UpgradeReleaseKind,
|
2024-01-30 14:50:54 -05:00
|
|
|
target_tuple: &str,
|
2023-11-17 16:40:12 -05:00
|
|
|
check_kind: UpgradeCheckKind,
|
|
|
|
) -> String {
|
|
|
|
let file_name = match release_kind {
|
2024-01-30 14:50:54 -05:00
|
|
|
UpgradeReleaseKind::Stable => Cow::Borrowed("release-latest.txt"),
|
|
|
|
UpgradeReleaseKind::Canary => {
|
|
|
|
Cow::Owned(format!("canary-{target_tuple}-latest.txt"))
|
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
};
|
|
|
|
let query_param = match check_kind {
|
|
|
|
UpgradeCheckKind::Execution => "",
|
|
|
|
UpgradeCheckKind::Lsp => "?lsp",
|
|
|
|
};
|
2023-11-17 22:46:15 -05:00
|
|
|
format!("{}/{}{}", base_upgrade_url(), file_name, query_param)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn base_upgrade_url() -> Cow<'static, str> {
|
2023-11-23 11:20:40 -05:00
|
|
|
// this is used by the test suite
|
2023-11-17 22:46:15 -05:00
|
|
|
if let Ok(url) = env::var("DENO_DONT_USE_INTERNAL_BASE_UPGRADE_URL") {
|
|
|
|
Cow::Owned(url)
|
|
|
|
} else {
|
|
|
|
Cow::Borrowed("https://dl.deno.land")
|
|
|
|
}
|
2020-11-29 14:00:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-22 10:07:05 -05:00
|
|
|
async fn download_package(
|
2022-12-12 21:30:44 -05:00
|
|
|
client: &HttpClient,
|
2020-11-29 14:00:35 -05:00
|
|
|
download_url: &str,
|
2020-11-22 10:07:05 -05:00
|
|
|
) -> Result<Vec<u8>, AnyError> {
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Downloading {}", &download_url);
|
2022-12-14 08:47:18 -05:00
|
|
|
let maybe_bytes = {
|
2022-12-12 20:52:10 -05:00
|
|
|
let progress_bar = ProgressBar::new(ProgressBarStyle::DownloadBars);
|
2022-12-14 08:47:18 -05:00
|
|
|
// provide an empty string here in order to prefer the downloading
|
|
|
|
// text above which will stay alive after the progress bars are complete
|
2022-12-12 20:52:10 -05:00
|
|
|
let progress = progress_bar.update("");
|
2022-12-14 08:47:18 -05:00
|
|
|
client
|
|
|
|
.download_with_progress(download_url, &progress)
|
|
|
|
.await?
|
|
|
|
};
|
|
|
|
match maybe_bytes {
|
|
|
|
Some(bytes) => Ok(bytes),
|
|
|
|
None => {
|
2023-09-27 17:57:58 -04:00
|
|
|
log::error!("Download could not be found, aborting");
|
2022-12-14 08:47:18 -05:00
|
|
|
std::process::exit(1)
|
2021-04-24 13:37:43 -04:00
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 14:53:10 -05:00
|
|
|
fn replace_exe(from: &Path, to: &Path) -> Result<(), std::io::Error> {
|
2020-03-23 11:37:24 -04:00
|
|
|
if cfg!(windows) {
|
|
|
|
// On windows you cannot replace the currently running executable.
|
|
|
|
// so first we rename it to deno.old.exe
|
2022-12-02 14:53:10 -05:00
|
|
|
fs::rename(to, to.with_extension("old.exe"))?;
|
2020-03-23 11:37:24 -04:00
|
|
|
} else {
|
2022-12-02 14:53:10 -05:00
|
|
|
fs::remove_file(to)?;
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
// Windows cannot rename files across device boundaries, so if rename fails,
|
|
|
|
// we try again with copy.
|
2022-12-02 14:53:10 -05:00
|
|
|
fs::rename(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))?;
|
2020-03-23 11:37:24 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
fn check_exe(exe_path: &Path) -> Result<(), AnyError> {
|
2020-03-23 11:37:24 -04:00
|
|
|
let output = Command::new(exe_path)
|
|
|
|
.arg("-V")
|
|
|
|
.stderr(std::process::Stdio::inherit())
|
|
|
|
.output()?;
|
|
|
|
assert!(output.status.success());
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
#[derive(Debug)]
|
2022-10-20 17:01:17 -04:00
|
|
|
struct CheckVersionFile {
|
2022-10-20 19:50:55 -04:00
|
|
|
pub last_prompt: chrono::DateTime<chrono::Utc>,
|
2022-10-20 17:01:17 -04:00
|
|
|
pub last_checked: chrono::DateTime<chrono::Utc>,
|
2022-11-05 19:06:58 -04:00
|
|
|
pub current_version: String,
|
2022-10-20 17:01:17 -04:00
|
|
|
pub latest_version: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CheckVersionFile {
|
|
|
|
pub fn parse(content: String) -> Option<Self> {
|
|
|
|
let split_content = content.split('!').collect::<Vec<_>>();
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-11-05 19:06:58 -04:00
|
|
|
if split_content.len() != 4 {
|
2022-10-20 17:01:17 -04:00
|
|
|
return None;
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-20 19:50:55 -04:00
|
|
|
let latest_version = split_content[2].trim().to_owned();
|
2022-10-20 17:01:17 -04:00
|
|
|
if latest_version.is_empty() {
|
|
|
|
return None;
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|
2022-11-05 19:06:58 -04:00
|
|
|
let current_version = split_content[3].trim().to_owned();
|
|
|
|
if current_version.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
2022-10-20 17:01:17 -04:00
|
|
|
|
2022-10-20 19:50:55 -04:00
|
|
|
let last_prompt = chrono::DateTime::parse_from_rfc3339(split_content[0])
|
|
|
|
.map(|dt| dt.with_timezone(&chrono::Utc))
|
|
|
|
.ok()?;
|
|
|
|
let last_checked = chrono::DateTime::parse_from_rfc3339(split_content[1])
|
2022-10-20 17:01:17 -04:00
|
|
|
.map(|dt| dt.with_timezone(&chrono::Utc))
|
|
|
|
.ok()?;
|
|
|
|
|
|
|
|
Some(CheckVersionFile {
|
2022-10-20 19:50:55 -04:00
|
|
|
last_prompt,
|
2022-10-20 17:01:17 -04:00
|
|
|
last_checked,
|
2022-11-05 19:06:58 -04:00
|
|
|
current_version,
|
2022-10-20 17:01:17 -04:00
|
|
|
latest_version,
|
|
|
|
})
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|
|
|
|
|
2022-10-20 19:50:55 -04:00
|
|
|
fn serialize(&self) -> String {
|
|
|
|
format!(
|
2022-11-05 19:06:58 -04:00
|
|
|
"{}!{}!{}!{}",
|
2022-10-20 19:50:55 -04:00
|
|
|
self.last_prompt.to_rfc3339(),
|
|
|
|
self.last_checked.to_rfc3339(),
|
2022-11-05 19:06:58 -04:00
|
|
|
self.latest_version,
|
|
|
|
self.current_version,
|
2022-10-20 19:50:55 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_last_prompt(self, dt: chrono::DateTime<chrono::Utc>) -> Self {
|
|
|
|
Self {
|
|
|
|
last_prompt: dt,
|
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|
|
|
|
|
2022-10-20 17:01:17 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-11-17 16:40:12 -05:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2022-10-22 14:31:25 -04:00
|
|
|
|
2022-10-20 17:01:17 -04:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse_upgrade_check_file() {
|
2022-10-20 19:50:55 -04:00
|
|
|
let file = CheckVersionFile::parse(
|
2022-11-05 19:06:58 -04:00
|
|
|
"2020-01-01T00:00:00+00:00!2020-01-01T00:00:00+00:00!1.2.3!1.2.2"
|
|
|
|
.to_string(),
|
2022-10-20 19:50:55 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
file.last_prompt.to_rfc3339(),
|
|
|
|
"2020-01-01T00:00:00+00:00".to_string()
|
|
|
|
);
|
2022-10-20 17:01:17 -04:00
|
|
|
assert_eq!(
|
|
|
|
file.last_checked.to_rfc3339(),
|
|
|
|
"2020-01-01T00:00:00+00:00".to_string()
|
|
|
|
);
|
|
|
|
assert_eq!(file.latest_version, "1.2.3".to_string());
|
2022-11-05 19:06:58 -04:00
|
|
|
assert_eq!(file.current_version, "1.2.2".to_string());
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-20 17:01:17 -04:00
|
|
|
let result =
|
|
|
|
CheckVersionFile::parse("2020-01-01T00:00:00+00:00!".to_string());
|
|
|
|
assert!(result.is_none());
|
|
|
|
|
|
|
|
let result = CheckVersionFile::parse("garbage!test".to_string());
|
|
|
|
assert!(result.is_none());
|
|
|
|
|
|
|
|
let result = CheckVersionFile::parse("test".to_string());
|
|
|
|
assert!(result.is_none());
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
|
2022-10-20 17:01:17 -04:00
|
|
|
#[test]
|
|
|
|
fn test_serialize_upgrade_check_file() {
|
|
|
|
let file = CheckVersionFile {
|
2022-10-20 19:50:55 -04:00
|
|
|
last_prompt: chrono::DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z")
|
|
|
|
.unwrap()
|
|
|
|
.with_timezone(&chrono::Utc),
|
2022-10-20 17:01:17 -04:00
|
|
|
last_checked: chrono::DateTime::parse_from_rfc3339(
|
|
|
|
"2020-01-01T00:00:00Z",
|
|
|
|
)
|
2022-10-20 10:15:21 -04:00
|
|
|
.unwrap()
|
|
|
|
.with_timezone(&chrono::Utc),
|
2022-10-20 17:01:17 -04:00
|
|
|
latest_version: "1.2.3".to_string(),
|
2022-11-05 19:06:58 -04:00
|
|
|
current_version: "1.2.2".to_string(),
|
2022-10-20 17:01:17 -04:00
|
|
|
};
|
2022-10-20 19:50:55 -04:00
|
|
|
assert_eq!(
|
|
|
|
file.serialize(),
|
2022-11-05 19:06:58 -04:00
|
|
|
"2020-01-01T00:00:00+00:00!2020-01-01T00:00:00+00:00!1.2.3!1.2.2"
|
2022-10-20 19:50:55 -04:00
|
|
|
);
|
2022-10-20 17:01:17 -04:00
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct TestUpdateCheckerEnvironment {
|
2023-11-17 16:40:12 -05:00
|
|
|
file_text: Rc<RefCell<String>>,
|
|
|
|
is_canary: Rc<RefCell<bool>>,
|
|
|
|
current_version: Rc<RefCell<String>>,
|
|
|
|
latest_version: Rc<RefCell<Result<String, String>>>,
|
|
|
|
time: Rc<RefCell<chrono::DateTime<chrono::Utc>>>,
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestUpdateCheckerEnvironment {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
file_text: Default::default(),
|
|
|
|
current_version: Default::default(),
|
2023-11-17 16:40:12 -05:00
|
|
|
is_canary: Default::default(),
|
|
|
|
latest_version: Rc::new(RefCell::new(Ok("".to_string()))),
|
|
|
|
time: Rc::new(RefCell::new(crate::util::time::utc_now())),
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_hours(&self, hours: i64) {
|
2023-11-17 16:40:12 -05:00
|
|
|
let mut time = self.time.borrow_mut();
|
2022-10-22 14:31:25 -04:00
|
|
|
*time = time
|
|
|
|
.checked_add_signed(chrono::Duration::hours(hours))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_file_text(&self, text: &str) {
|
2023-11-17 16:40:12 -05:00
|
|
|
*self.file_text.borrow_mut() = text.to_string();
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_current_version(&self, version: &str) {
|
2023-11-17 16:40:12 -05:00
|
|
|
*self.current_version.borrow_mut() = version.to_string();
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_latest_version(&self, version: &str) {
|
2023-11-17 16:40:12 -05:00
|
|
|
*self.latest_version.borrow_mut() = Ok(version.to_string());
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_latest_version_err(&self, err: &str) {
|
2023-11-17 16:40:12 -05:00
|
|
|
*self.latest_version.borrow_mut() = Err(err.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_is_canary(&self, is_canary: bool) {
|
|
|
|
*self.is_canary.borrow_mut() = is_canary;
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
#[async_trait(?Send)]
|
|
|
|
impl VersionProvider for TestUpdateCheckerEnvironment {
|
|
|
|
fn is_canary(&self) -> bool {
|
|
|
|
*self.is_canary.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn latest_version(&self) -> Result<String, AnyError> {
|
|
|
|
match self.latest_version.borrow().clone() {
|
|
|
|
Ok(result) => Ok(result),
|
|
|
|
Err(err) => bail!("{}", err),
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_version(&self) -> Cow<str> {
|
2023-11-17 16:40:12 -05:00
|
|
|
Cow::Owned(self.current_version.borrow().clone())
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
impl UpdateCheckerEnvironment for TestUpdateCheckerEnvironment {
|
2022-10-22 14:31:25 -04:00
|
|
|
fn read_check_file(&self) -> String {
|
2023-11-17 16:40:12 -05:00
|
|
|
self.file_text.borrow().clone()
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_check_file(&self, text: &str) {
|
|
|
|
self.set_file_text(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_time(&self) -> chrono::DateTime<chrono::Utc> {
|
2023-11-17 16:40:12 -05:00
|
|
|
*self.time.borrow()
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_update_checker() {
|
|
|
|
let env = TestUpdateCheckerEnvironment::new();
|
|
|
|
env.set_current_version("1.0.0");
|
|
|
|
env.set_latest_version("1.1.0");
|
2023-11-17 16:40:12 -05:00
|
|
|
let checker = UpdateChecker::new(env.clone(), env.clone());
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
// no version, so we should check, but not prompt
|
|
|
|
assert!(checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
|
|
|
|
// store the latest version
|
2023-11-17 16:40:12 -05:00
|
|
|
fetch_and_store_latest_version(&env, &env).await;
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
// reload
|
2023-11-17 16:40:12 -05:00
|
|
|
let checker = UpdateChecker::new(env.clone(), env.clone());
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
// should not check for latest version because we just did
|
|
|
|
assert!(!checker.should_check_for_new_version());
|
|
|
|
// but should prompt
|
|
|
|
assert_eq!(checker.should_prompt(), Some("1.1.0".to_string()));
|
|
|
|
|
|
|
|
// fast forward an hour and bump the latest version
|
|
|
|
env.add_hours(1);
|
|
|
|
env.set_latest_version("1.2.0");
|
|
|
|
assert!(!checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), Some("1.1.0".to_string()));
|
|
|
|
|
|
|
|
// fast forward again and it should check for a newer version
|
|
|
|
env.add_hours(UPGRADE_CHECK_INTERVAL);
|
|
|
|
assert!(checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), Some("1.1.0".to_string()));
|
|
|
|
|
2023-11-17 16:40:12 -05:00
|
|
|
fetch_and_store_latest_version(&env, &env).await;
|
2022-10-22 14:31:25 -04:00
|
|
|
|
|
|
|
// reload and store that we prompted
|
2023-11-17 16:40:12 -05:00
|
|
|
let checker = UpdateChecker::new(env.clone(), env.clone());
|
2022-10-22 14:31:25 -04:00
|
|
|
assert!(!checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), Some("1.2.0".to_string()));
|
|
|
|
checker.store_prompted();
|
|
|
|
|
|
|
|
// reload and it should now say not to prompt
|
2023-11-17 16:40:12 -05:00
|
|
|
let checker = UpdateChecker::new(env.clone(), env.clone());
|
2022-10-22 14:31:25 -04:00
|
|
|
assert!(!checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
|
|
|
|
// but if we fast forward past the upgrade interval it should prompt again
|
|
|
|
env.add_hours(UPGRADE_CHECK_INTERVAL + 1);
|
|
|
|
assert!(checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), Some("1.2.0".to_string()));
|
|
|
|
|
|
|
|
// upgrade the version and it should stop prompting
|
|
|
|
env.set_current_version("1.2.0");
|
|
|
|
assert!(checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
|
|
|
|
// now try failing when fetching the latest version
|
|
|
|
env.add_hours(UPGRADE_CHECK_INTERVAL + 1);
|
|
|
|
env.set_latest_version_err("Failed");
|
|
|
|
env.set_latest_version("1.3.0");
|
|
|
|
|
|
|
|
// this will silently fail
|
2023-11-17 16:40:12 -05:00
|
|
|
fetch_and_store_latest_version(&env, &env).await;
|
2022-10-22 14:31:25 -04:00
|
|
|
assert!(checker.should_check_for_new_version());
|
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
}
|
2022-10-28 12:43:49 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_update_checker_current_newer_than_latest() {
|
|
|
|
let env = TestUpdateCheckerEnvironment::new();
|
|
|
|
let file_content = CheckVersionFile {
|
|
|
|
last_prompt: env
|
|
|
|
.current_time()
|
|
|
|
.sub(chrono::Duration::hours(UPGRADE_CHECK_INTERVAL + 1)),
|
|
|
|
last_checked: env.current_time(),
|
|
|
|
latest_version: "1.26.2".to_string(),
|
2022-11-05 19:06:58 -04:00
|
|
|
current_version: "1.27.0".to_string(),
|
2022-10-28 12:43:49 -04:00
|
|
|
}
|
|
|
|
.serialize();
|
|
|
|
env.write_check_file(&file_content);
|
|
|
|
env.set_current_version("1.27.0");
|
|
|
|
env.set_latest_version("1.26.2");
|
2023-11-17 16:40:12 -05:00
|
|
|
let checker = UpdateChecker::new(env.clone(), env);
|
2022-10-28 12:43:49 -04:00
|
|
|
|
|
|
|
// since currently running version is newer than latest available (eg. CDN
|
|
|
|
// propagation might be delated) we should not prompt
|
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
}
|
2022-11-05 19:06:58 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_should_not_prompt_if_current_cli_version_has_changed() {
|
|
|
|
let env = TestUpdateCheckerEnvironment::new();
|
|
|
|
let file_content = CheckVersionFile {
|
|
|
|
last_prompt: env
|
|
|
|
.current_time()
|
|
|
|
.sub(chrono::Duration::hours(UPGRADE_CHECK_INTERVAL + 1)),
|
|
|
|
last_checked: env.current_time(),
|
|
|
|
latest_version: "1.26.2".to_string(),
|
|
|
|
current_version: "1.25.0".to_string(),
|
|
|
|
}
|
|
|
|
.serialize();
|
|
|
|
env.write_check_file(&file_content);
|
|
|
|
// simulate an upgrade done to a canary version
|
|
|
|
env.set_current_version("61fbfabe440f1cfffa7b8d17426ffdece4d430d0");
|
2023-11-17 16:40:12 -05:00
|
|
|
let checker = UpdateChecker::new(env.clone(), env);
|
2022-11-05 19:06:58 -04:00
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
}
|
2023-11-17 16:40:12 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_url() {
|
|
|
|
assert_eq!(
|
2024-01-30 14:50:54 -05:00
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Canary,
|
|
|
|
"aarch64-apple-darwin",
|
|
|
|
UpgradeCheckKind::Execution
|
|
|
|
),
|
|
|
|
"https://dl.deno.land/canary-aarch64-apple-darwin-latest.txt"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Canary,
|
|
|
|
"aarch64-apple-darwin",
|
|
|
|
UpgradeCheckKind::Lsp
|
|
|
|
),
|
|
|
|
"https://dl.deno.land/canary-aarch64-apple-darwin-latest.txt?lsp"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Canary,
|
|
|
|
"x86_64-pc-windows-msvc",
|
|
|
|
UpgradeCheckKind::Execution
|
|
|
|
),
|
|
|
|
"https://dl.deno.land/canary-x86_64-pc-windows-msvc-latest.txt"
|
2023-11-17 16:40:12 -05:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-01-30 14:50:54 -05:00
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Canary,
|
|
|
|
"x86_64-pc-windows-msvc",
|
|
|
|
UpgradeCheckKind::Lsp
|
|
|
|
),
|
|
|
|
"https://dl.deno.land/canary-x86_64-pc-windows-msvc-latest.txt?lsp"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Stable,
|
|
|
|
"aarch64-apple-darwin",
|
|
|
|
UpgradeCheckKind::Execution
|
|
|
|
),
|
|
|
|
"https://dl.deno.land/release-latest.txt"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Stable,
|
|
|
|
"aarch64-apple-darwin",
|
|
|
|
UpgradeCheckKind::Lsp
|
|
|
|
),
|
|
|
|
"https://dl.deno.land/release-latest.txt?lsp"
|
2023-11-17 16:40:12 -05:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-01-30 14:50:54 -05:00
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Stable,
|
|
|
|
"x86_64-pc-windows-msvc",
|
|
|
|
UpgradeCheckKind::Execution
|
|
|
|
),
|
2023-11-17 16:40:12 -05:00
|
|
|
"https://dl.deno.land/release-latest.txt"
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2024-01-30 14:50:54 -05:00
|
|
|
get_url(
|
|
|
|
UpgradeReleaseKind::Stable,
|
|
|
|
"x86_64-pc-windows-msvc",
|
|
|
|
UpgradeCheckKind::Lsp
|
|
|
|
),
|
2023-11-17 16:40:12 -05:00
|
|
|
"https://dl.deno.land/release-latest.txt?lsp"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_version_server() {
|
|
|
|
// should strip v for stable
|
|
|
|
assert_eq!(
|
|
|
|
normalize_version_from_server(UpgradeReleaseKind::Stable, "v1.0.0"),
|
|
|
|
"1.0.0"
|
|
|
|
);
|
|
|
|
// should not replace v after start
|
|
|
|
assert_eq!(
|
|
|
|
normalize_version_from_server(
|
|
|
|
UpgradeReleaseKind::Stable,
|
|
|
|
" v1.0.0-test-v\n\n "
|
|
|
|
),
|
|
|
|
"1.0.0-test-v"
|
|
|
|
);
|
|
|
|
// should not strip v for canary
|
|
|
|
assert_eq!(
|
|
|
|
normalize_version_from_server(
|
|
|
|
UpgradeReleaseKind::Canary,
|
|
|
|
" v1452345asdf \n\n "
|
|
|
|
),
|
|
|
|
"v1452345asdf"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_upgrades_lsp() {
|
|
|
|
let env = TestUpdateCheckerEnvironment::new();
|
|
|
|
env.set_current_version("1.0.0");
|
|
|
|
env.set_latest_version("2.0.0");
|
|
|
|
|
|
|
|
// greater
|
|
|
|
{
|
|
|
|
let maybe_info = check_for_upgrades_for_lsp_with_provider(&env)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
maybe_info,
|
|
|
|
Some(LspVersionUpgradeInfo {
|
|
|
|
latest_version: "2.0.0".to_string(),
|
|
|
|
is_canary: false,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// equal
|
|
|
|
{
|
|
|
|
env.set_latest_version("1.0.0");
|
|
|
|
let maybe_info = check_for_upgrades_for_lsp_with_provider(&env)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(maybe_info, None);
|
|
|
|
}
|
|
|
|
// less
|
|
|
|
{
|
|
|
|
env.set_latest_version("0.9.0");
|
|
|
|
let maybe_info = check_for_upgrades_for_lsp_with_provider(&env)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(maybe_info, None);
|
|
|
|
}
|
|
|
|
// canary equal
|
|
|
|
{
|
|
|
|
env.set_current_version("123");
|
|
|
|
env.set_latest_version("123");
|
|
|
|
env.set_is_canary(true);
|
|
|
|
let maybe_info = check_for_upgrades_for_lsp_with_provider(&env)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(maybe_info, None);
|
|
|
|
}
|
|
|
|
// canary different
|
|
|
|
{
|
|
|
|
env.set_latest_version("1234");
|
|
|
|
let maybe_info = check_for_upgrades_for_lsp_with_provider(&env)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
maybe_info,
|
|
|
|
Some(LspVersionUpgradeInfo {
|
|
|
|
latest_version: "1234".to_string(),
|
|
|
|
is_canary: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|