2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 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;
|
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
|
|
|
|
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;
|
2022-10-22 14:31:25 -04:00
|
|
|
use deno_core::futures::future::BoxFuture;
|
|
|
|
use deno_core::futures::FutureExt;
|
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
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
static ARCHIVE_NAME: Lazy<String> =
|
|
|
|
Lazy::new(|| format!("deno-{}.zip", env!("TARGET")));
|
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
|
|
|
|
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.
|
|
|
|
trait UpdateCheckerEnvironment: Clone + Send + Sync {
|
|
|
|
fn latest_version(&self) -> BoxFuture<'static, Result<String, AnyError>>;
|
|
|
|
fn current_version(&self) -> Cow<str>;
|
|
|
|
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 {
|
2023-05-01 16:42:05 -04:00
|
|
|
http_client: Arc<HttpClient>,
|
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-05-01 16:42:05 -04:00
|
|
|
pub fn new(http_client: Arc<HttpClient>, cache_file_path: PathBuf) -> Self {
|
2022-10-22 14:31:25 -04:00
|
|
|
Self {
|
2022-12-12 21:30:44 -05:00
|
|
|
http_client,
|
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 latest_version(&self) -> BoxFuture<'static, Result<String, AnyError>> {
|
2022-12-12 21:30:44 -05:00
|
|
|
let http_client = self.http_client.clone();
|
|
|
|
async move {
|
2022-10-22 14:31:25 -04:00
|
|
|
if version::is_canary() {
|
2022-12-12 21:30:44 -05:00
|
|
|
get_latest_canary_version(&http_client).await
|
2022-10-20 17:01:17 -04:00
|
|
|
} else {
|
2022-12-12 21:30:44 -05:00
|
|
|
get_latest_release_version(&http_client).await
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
2022-10-20 17:01:17 -04:00
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
fn current_version(&self) -> Cow<str> {
|
|
|
|
Cow::Borrowed(version::release_version_or_canary_commit_hash())
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UpdateChecker<TEnvironment: UpdateCheckerEnvironment> {
|
|
|
|
env: TEnvironment,
|
|
|
|
maybe_file: Option<CheckVersionFile>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TEnvironment: UpdateCheckerEnvironment> UpdateChecker<TEnvironment> {
|
|
|
|
pub fn new(env: TEnvironment) -> Self {
|
|
|
|
let maybe_file = CheckVersionFile::parse(env.read_check_file());
|
|
|
|
Self { env, maybe_file }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
if file.current_version != self.env.current_version() {
|
|
|
|
return None;
|
|
|
|
}
|
2022-10-22 14:31:25 -04:00
|
|
|
if file.latest_version == self.env.current_version() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2023-02-15 13:20:40 -05:00
|
|
|
if let Ok(current) = Version::parse_standard(&self.env.current_version()) {
|
|
|
|
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-05-01 16:42:05 -04:00
|
|
|
pub fn check_for_upgrades(
|
|
|
|
http_client: Arc<HttpClient>,
|
|
|
|
cache_file_path: PathBuf,
|
|
|
|
) {
|
2022-10-22 14:31:25 -04:00
|
|
|
if env::var("DENO_NO_UPDATE_CHECK").is_ok() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-12 21:30:44 -05:00
|
|
|
let env = RealUpdateCheckerEnvironment::new(http_client, cache_file_path);
|
2022-10-22 14:31:25 -04:00
|
|
|
let update_checker = UpdateChecker::new(env);
|
|
|
|
|
|
|
|
if update_checker.should_check_for_new_version() {
|
|
|
|
let env = update_checker.env.clone();
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
fetch_and_store_latest_version(&env).await;
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-10-22 14:31:25 -04:00
|
|
|
async fn fetch_and_store_latest_version<
|
|
|
|
TEnvironment: UpdateCheckerEnvironment,
|
|
|
|
>(
|
|
|
|
env: &TEnvironment,
|
|
|
|
) {
|
|
|
|
// Fetch latest version or commit hash from server.
|
|
|
|
let latest_version = match env.latest_version().await {
|
|
|
|
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(),
|
2022-11-05 19:06:58 -04:00
|
|
|
current_version: env.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> {
|
2023-05-01 14:35:23 -04:00
|
|
|
let factory = CliFactory::from_flags(flags).await?;
|
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 => {
|
2021-12-21 09:49:27 -05:00
|
|
|
let latest_version = if upgrade_flags.canary {
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Looking up latest canary version");
|
2023-01-13 16:39:19 -05:00
|
|
|
get_latest_canary_version(client).await?
|
2020-11-29 14:00:35 -05:00
|
|
|
} else {
|
2022-12-01 18:05:49 -05:00
|
|
|
log::info!("Looking up latest version");
|
2023-01-13 16:39:19 -05:00
|
|
|
get_latest_release_version(client).await?
|
2020-11-29 14:00:35 -05:00
|
|
|
};
|
|
|
|
|
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 {
|
2022-10-26 09:51:22 -04:00
|
|
|
if env!("TARGET") == "aarch64-apple-darwin" {
|
2023-05-15 21:21:59 -04:00
|
|
|
bail!("Canary builds are not available for M1/M2");
|
2022-10-26 09:51:22 -04:00
|
|
|
}
|
|
|
|
|
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
|
2023-01-27 10:43:16 -05:00
|
|
|
.with_context(|| format!("Failed downloading {download_url}"))?;
|
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()?;
|
2023-01-26 09:02:25 -05:00
|
|
|
let new_exe_path = unpack_into_dir(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(())
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
async fn get_latest_release_version(
|
2022-12-12 21:30:44 -05:00
|
|
|
client: &HttpClient,
|
2020-11-29 14:00:35 -05:00
|
|
|
) -> Result<String, AnyError> {
|
2022-12-14 08:47:18 -05:00
|
|
|
let text = client
|
|
|
|
.download_text("https://dl.deno.land/release-latest.txt")
|
2020-11-22 10:07:05 -05:00
|
|
|
.await?;
|
2022-12-14 08:47:18 -05:00
|
|
|
let version = text.trim().to_string();
|
2022-02-24 20:03:12 -05:00
|
|
|
Ok(version.replace('v', ""))
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
async fn get_latest_canary_version(
|
2022-12-12 21:30:44 -05:00
|
|
|
client: &HttpClient,
|
2020-11-29 14:00:35 -05:00
|
|
|
) -> Result<String, AnyError> {
|
2022-12-14 08:47:18 -05:00
|
|
|
let text = client
|
|
|
|
.download_text("https://dl.deno.land/canary-latest.txt")
|
2020-11-29 14:00:35 -05:00
|
|
|
.await?;
|
2022-12-14 08:47:18 -05:00
|
|
|
let version = text.trim().to_string();
|
2020-11-29 14:00:35 -05:00
|
|
|
Ok(version)
|
|
|
|
}
|
|
|
|
|
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 => {
|
|
|
|
log::info!("Download could not be found, aborting");
|
|
|
|
std::process::exit(1)
|
2021-04-24 13:37:43 -04:00
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-26 09:02:25 -05:00
|
|
|
pub fn unpack_into_dir(
|
2021-01-18 21:40:22 -05:00
|
|
|
archive_data: Vec<u8>,
|
2021-01-23 12:40:13 -05:00
|
|
|
is_windows: bool,
|
2023-03-22 14:55:19 -04:00
|
|
|
temp_dir: &tempfile::TempDir,
|
2023-09-26 17:52:47 -04:00
|
|
|
) -> Result<PathBuf, AnyError> {
|
2021-07-07 14:59:39 -04:00
|
|
|
const EXE_NAME: &str = "deno";
|
2023-01-26 09:02:25 -05:00
|
|
|
let temp_dir_path = temp_dir.path();
|
2021-01-23 12:40:13 -05:00
|
|
|
let exe_ext = if is_windows { "exe" } else { "" };
|
2023-01-26 09:02:25 -05:00
|
|
|
let archive_path = temp_dir_path.join(EXE_NAME).with_extension("zip");
|
|
|
|
let exe_path = temp_dir_path.join(EXE_NAME).with_extension(exe_ext);
|
2020-03-23 11:37:24 -04:00
|
|
|
assert!(!exe_path.exists());
|
|
|
|
|
2020-11-08 05:54:35 -05:00
|
|
|
let archive_ext = Path::new(&*ARCHIVE_NAME)
|
2020-03-23 11:37:24 -04:00
|
|
|
.extension()
|
|
|
|
.and_then(|ext| ext.to_str())
|
|
|
|
.unwrap();
|
|
|
|
let unpack_status = match archive_ext {
|
2020-06-05 15:46:03 -04:00
|
|
|
"zip" if cfg!(windows) => {
|
|
|
|
fs::write(&archive_path, &archive_data)?;
|
|
|
|
Command::new("powershell.exe")
|
|
|
|
.arg("-NoLogo")
|
|
|
|
.arg("-NoProfile")
|
|
|
|
.arg("-NonInteractive")
|
|
|
|
.arg("-Command")
|
|
|
|
.arg(
|
|
|
|
"& {
|
|
|
|
param($Path, $DestinationPath)
|
|
|
|
trap { $host.ui.WriteErrorLine($_.Exception); exit 1 }
|
|
|
|
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
|
|
[System.IO.Compression.ZipFile]::ExtractToDirectory(
|
|
|
|
$Path,
|
|
|
|
$DestinationPath
|
|
|
|
);
|
|
|
|
}",
|
|
|
|
)
|
|
|
|
.arg("-Path")
|
2020-06-29 09:13:07 -04:00
|
|
|
.arg(format!("'{}'", &archive_path.to_str().unwrap()))
|
2020-06-05 15:46:03 -04:00
|
|
|
.arg("-DestinationPath")
|
2023-01-26 09:02:25 -05:00
|
|
|
.arg(format!("'{}'", &temp_dir_path.to_str().unwrap()))
|
2023-02-16 14:14:57 -05:00
|
|
|
.spawn()
|
|
|
|
.map_err(|err| {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::NotFound,
|
|
|
|
"`powershell.exe` was not found in your PATH",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err
|
|
|
|
}
|
|
|
|
})?
|
2020-06-05 15:46:03 -04:00
|
|
|
.wait()?
|
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
"zip" => {
|
2020-06-05 15:46:03 -04:00
|
|
|
fs::write(&archive_path, &archive_data)?;
|
|
|
|
Command::new("unzip")
|
2023-01-26 09:02:25 -05:00
|
|
|
.current_dir(temp_dir_path)
|
2022-12-08 17:48:48 -05:00
|
|
|
.arg(&archive_path)
|
2021-11-09 05:06:45 -05:00
|
|
|
.spawn()
|
|
|
|
.map_err(|err| {
|
|
|
|
if err.kind() == std::io::ErrorKind::NotFound {
|
|
|
|
std::io::Error::new(
|
|
|
|
std::io::ErrorKind::NotFound,
|
2023-02-16 14:14:57 -05:00
|
|
|
"`unzip` was not found in your PATH, please install `unzip`",
|
2021-11-09 05:06:45 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err
|
|
|
|
}
|
|
|
|
})?
|
2020-06-05 15:46:03 -04:00
|
|
|
.wait()?
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
2023-09-26 17:52:47 -04:00
|
|
|
ext => bail!("Unsupported archive type: '{ext}'"),
|
2020-03-23 11:37:24 -04:00
|
|
|
};
|
2023-09-26 17:52:47 -04:00
|
|
|
if !unpack_status.success() {
|
|
|
|
bail!("Failed to unpack archive.");
|
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
assert!(exe_path.exists());
|
2022-12-08 17:48:48 -05:00
|
|
|
fs::remove_file(&archive_path)?;
|
2020-03-23 11:37:24 -04:00
|
|
|
Ok(exe_path)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-10-22 14:31:25 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_core::parking_lot::Mutex;
|
|
|
|
|
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 {
|
|
|
|
file_text: Arc<Mutex<String>>,
|
|
|
|
current_version: Arc<Mutex<String>>,
|
|
|
|
latest_version: Arc<Mutex<Result<String, String>>>,
|
|
|
|
time: Arc<Mutex<chrono::DateTime<chrono::Utc>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TestUpdateCheckerEnvironment {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
file_text: Default::default(),
|
|
|
|
current_version: Default::default(),
|
|
|
|
latest_version: Arc::new(Mutex::new(Ok("".to_string()))),
|
2023-03-15 03:14:22 -04:00
|
|
|
time: Arc::new(Mutex::new(crate::util::time::utc_now())),
|
2022-10-22 14:31:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_hours(&self, hours: i64) {
|
|
|
|
let mut time = self.time.lock();
|
|
|
|
*time = time
|
|
|
|
.checked_add_signed(chrono::Duration::hours(hours))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_file_text(&self, text: &str) {
|
|
|
|
*self.file_text.lock() = text.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_current_version(&self, version: &str) {
|
|
|
|
*self.current_version.lock() = version.to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_latest_version(&self, version: &str) {
|
|
|
|
*self.latest_version.lock() = Ok(version.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_latest_version_err(&self, err: &str) {
|
|
|
|
*self.latest_version.lock() = Err(err.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UpdateCheckerEnvironment for TestUpdateCheckerEnvironment {
|
|
|
|
fn latest_version(&self) -> BoxFuture<'static, Result<String, AnyError>> {
|
|
|
|
let env = self.clone();
|
|
|
|
async move {
|
|
|
|
match env.latest_version.lock().clone() {
|
|
|
|
Ok(result) => Ok(result),
|
|
|
|
Err(err) => bail!("{}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_version(&self) -> Cow<str> {
|
|
|
|
Cow::Owned(self.current_version.lock().clone())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_check_file(&self) -> String {
|
|
|
|
self.file_text.lock().clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_check_file(&self, text: &str) {
|
|
|
|
self.set_file_text(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn current_time(&self) -> chrono::DateTime<chrono::Utc> {
|
|
|
|
*self.time.lock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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");
|
|
|
|
let checker = UpdateChecker::new(env.clone());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
fetch_and_store_latest_version(&env).await;
|
|
|
|
|
|
|
|
// reload
|
|
|
|
let checker = UpdateChecker::new(env.clone());
|
|
|
|
|
|
|
|
// 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()));
|
|
|
|
|
|
|
|
fetch_and_store_latest_version(&env).await;
|
|
|
|
|
|
|
|
// reload and store that we prompted
|
|
|
|
let checker = UpdateChecker::new(env.clone());
|
|
|
|
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
|
|
|
|
let checker = UpdateChecker::new(env.clone());
|
|
|
|
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
|
|
|
|
fetch_and_store_latest_version(&env).await;
|
|
|
|
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");
|
|
|
|
let checker = UpdateChecker::new(env);
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
let checker = UpdateChecker::new(env);
|
|
|
|
assert_eq!(checker.should_prompt(), None);
|
|
|
|
}
|
2022-10-20 10:15:21 -04:00
|
|
|
}
|