2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-03-23 11:37:24 -04:00
|
|
|
|
|
|
|
//! This module provides feature to upgrade deno executable
|
|
|
|
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::bail;
|
2021-11-09 05:06:45 -05:00
|
|
|
use deno_core::error::AnyError;
|
2021-04-24 13:37:43 -04:00
|
|
|
use deno_core::futures::StreamExt;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::Client;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2020-03-23 11:37:24 -04:00
|
|
|
use semver_parser::version::parse as semver_parse;
|
|
|
|
use std::fs;
|
2021-04-24 13:37:43 -04:00
|
|
|
use std::io::Write;
|
2020-03-23 11:37:24 -04:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
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
|
|
|
|
2020-05-09 06:31:15 -04:00
|
|
|
pub async fn upgrade_command(
|
|
|
|
dry_run: bool,
|
|
|
|
force: bool,
|
2020-11-29 14:00:35 -05:00
|
|
|
canary: bool,
|
2020-05-09 06:31:15 -04:00
|
|
|
version: Option<String>,
|
2020-07-06 18:21:26 -04:00
|
|
|
output: Option<PathBuf>,
|
2020-07-05 23:58:23 -04:00
|
|
|
ca_file: Option<String>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<(), AnyError> {
|
2020-11-22 10:07:05 -05:00
|
|
|
let mut client_builder = Client::builder();
|
2020-07-05 23:58:23 -04:00
|
|
|
|
|
|
|
// If we have been provided a CA Certificate, add it into the HTTP client
|
|
|
|
if let Some(ca_file) = ca_file {
|
2020-11-22 10:07:05 -05:00
|
|
|
let buf = std::fs::read(ca_file)?;
|
|
|
|
let cert = reqwest::Certificate::from_pem(&buf)?;
|
2020-07-05 23:58:23 -04:00
|
|
|
client_builder = client_builder.add_root_certificate(cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
let client = client_builder.build()?;
|
|
|
|
|
2020-05-09 06:31:15 -04:00
|
|
|
let install_version = match version {
|
2020-11-22 10:07:05 -05:00
|
|
|
Some(passed_version) => {
|
2021-07-07 14:59:39 -04:00
|
|
|
if canary
|
|
|
|
&& !regex::Regex::new("^[0-9a-f]{40}$")?.is_match(&passed_version)
|
|
|
|
{
|
|
|
|
bail!("Invalid commit hash passed");
|
|
|
|
} else if !canary && semver_parse(&passed_version).is_err() {
|
|
|
|
bail!("Invalid semver passed");
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
let current_is_passed = if 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
|
|
|
|
};
|
|
|
|
|
|
|
|
if !force && output.is_none() && current_is_passed {
|
|
|
|
println!("Version {} is already installed", crate::version::deno());
|
2020-11-22 10:07:05 -05:00
|
|
|
return Ok(());
|
|
|
|
} else {
|
|
|
|
passed_version
|
2020-05-09 06:31:15 -04:00
|
|
|
}
|
2020-11-22 10:07:05 -05:00
|
|
|
}
|
2020-05-09 06:31:15 -04:00
|
|
|
None => {
|
2020-11-29 14:00:35 -05:00
|
|
|
let latest_version = if canary {
|
|
|
|
get_latest_canary_version(&client).await?
|
|
|
|
} else {
|
|
|
|
get_latest_release_version(&client).await?
|
|
|
|
};
|
|
|
|
|
|
|
|
let current_is_most_recent = if canary {
|
|
|
|
let mut latest_hash = latest_version.clone();
|
|
|
|
latest_hash.truncate(7);
|
|
|
|
crate::version::GIT_COMMIT_HASH == latest_hash
|
|
|
|
} else if !crate::version::is_canary() {
|
2021-07-07 14:59:39 -04:00
|
|
|
let current = semver_parse(&crate::version::deno()).unwrap();
|
|
|
|
let latest = semver_parse(&latest_version).unwrap();
|
2020-11-29 14:00:35 -05:00
|
|
|
current >= latest
|
|
|
|
} else {
|
|
|
|
false
|
2020-11-22 10:07:05 -05:00
|
|
|
};
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
if !force && output.is_none() && current_is_most_recent {
|
2020-05-09 06:31:15 -04:00
|
|
|
println!(
|
|
|
|
"Local deno version {} is the most recent release",
|
2020-11-25 05:30:14 -05:00
|
|
|
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 {
|
2021-07-07 14:59:39 -04:00
|
|
|
println!("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
|
|
|
};
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
let download_url = if canary {
|
|
|
|
format!(
|
|
|
|
"https://dl.deno.land/canary/{}/{}",
|
|
|
|
install_version, *ARCHIVE_NAME
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"{}/download/v{}/{}",
|
|
|
|
RELEASE_URL, install_version, *ARCHIVE_NAME
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-07-07 14:59:39 -04:00
|
|
|
let archive_data = download_package(client, &download_url).await?;
|
2020-11-22 10:07:05 -05:00
|
|
|
|
|
|
|
println!("Deno is upgrading to version {}", &install_version);
|
|
|
|
|
2020-05-09 06:31:15 -04:00
|
|
|
let old_exe_path = std::env::current_exe()?;
|
2021-07-07 14:59:39 -04:00
|
|
|
let new_exe_path = unpack(archive_data, cfg!(windows))?;
|
2020-05-09 06:31:15 -04:00
|
|
|
let permissions = fs::metadata(&old_exe_path)?.permissions();
|
|
|
|
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
|
|
|
|
|
|
|
if !dry_run {
|
2020-07-06 18:21:26 -04:00
|
|
|
match output {
|
|
|
|
Some(path) => {
|
|
|
|
fs::rename(&new_exe_path, &path)
|
|
|
|
.or_else(|_| fs::copy(&new_exe_path, &path).map(|_| ()))?;
|
|
|
|
}
|
|
|
|
None => replace_exe(&new_exe_path, &old_exe_path)?,
|
|
|
|
}
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
2020-05-09 06:31:15 -04:00
|
|
|
|
2020-11-22 10:07:05 -05:00
|
|
|
println!("Upgraded successfully");
|
2020-05-09 06:31:15 -04:00
|
|
|
|
2020-03-23 11:37:24 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
async fn get_latest_release_version(
|
|
|
|
client: &Client,
|
|
|
|
) -> Result<String, AnyError> {
|
2020-11-22 10:07:05 -05:00
|
|
|
println!("Looking up latest version");
|
|
|
|
|
|
|
|
let res = client
|
|
|
|
.get(&format!("{}/latest", RELEASE_URL))
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
let version = res.url().path_segments().unwrap().last().unwrap();
|
|
|
|
|
|
|
|
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(
|
|
|
|
client: &Client,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
println!("Looking up latest version");
|
|
|
|
|
|
|
|
let res = client
|
|
|
|
.get("https://dl.deno.land/canary-latest.txt")
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
let version = res.text().await?.trim().to_string();
|
|
|
|
|
|
|
|
Ok(version)
|
|
|
|
}
|
|
|
|
|
2020-11-22 10:07:05 -05:00
|
|
|
async fn download_package(
|
|
|
|
client: Client,
|
2020-11-29 14:00:35 -05:00
|
|
|
download_url: &str,
|
2020-11-22 10:07:05 -05:00
|
|
|
) -> Result<Vec<u8>, AnyError> {
|
|
|
|
println!("Checking {}", &download_url);
|
|
|
|
|
2020-11-29 14:00:35 -05:00
|
|
|
let res = client.get(download_url).send().await?;
|
2020-11-22 10:07:05 -05:00
|
|
|
|
|
|
|
if res.status().is_success() {
|
2021-04-24 13:37:43 -04:00
|
|
|
let total_size = res.content_length().unwrap() as f64;
|
|
|
|
let mut current_size = 0.0;
|
|
|
|
let mut data = Vec::with_capacity(total_size as usize);
|
|
|
|
let mut stream = res.bytes_stream();
|
|
|
|
let mut skip_print = 0;
|
|
|
|
let is_tty = atty::is(atty::Stream::Stdout);
|
|
|
|
const MEBIBYTE: f64 = 1024.0 * 1024.0;
|
|
|
|
while let Some(item) = stream.next().await {
|
|
|
|
let bytes = item?;
|
|
|
|
current_size += bytes.len() as f64;
|
|
|
|
data.extend_from_slice(&bytes);
|
|
|
|
if skip_print == 0 {
|
|
|
|
if is_tty {
|
|
|
|
print!("\u{001b}[1G\u{001b}[2K");
|
|
|
|
}
|
|
|
|
print!(
|
2021-05-24 03:55:44 -04:00
|
|
|
"{:>4.1} MiB / {:.1} MiB ({:^5.1}%)",
|
2021-04-24 13:37:43 -04:00
|
|
|
current_size / MEBIBYTE,
|
|
|
|
total_size / MEBIBYTE,
|
|
|
|
(current_size / total_size) * 100.0,
|
|
|
|
);
|
|
|
|
std::io::stdout().flush()?;
|
|
|
|
skip_print = 10;
|
|
|
|
} else {
|
|
|
|
skip_print -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if is_tty {
|
|
|
|
print!("\u{001b}[1G\u{001b}[2K");
|
|
|
|
}
|
|
|
|
println!(
|
|
|
|
"{:.1} MiB / {:.1} MiB (100.0%)",
|
|
|
|
current_size / MEBIBYTE,
|
|
|
|
total_size / MEBIBYTE
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(data)
|
2020-11-22 10:07:05 -05:00
|
|
|
} else {
|
|
|
|
println!("Download could not be found, aborting");
|
|
|
|
std::process::exit(1)
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:40:22 -05:00
|
|
|
pub fn unpack(
|
|
|
|
archive_data: Vec<u8>,
|
2021-01-23 12:40:13 -05:00
|
|
|
is_windows: bool,
|
2021-01-18 21:40:22 -05:00
|
|
|
) -> Result<PathBuf, std::io::Error> {
|
2021-07-07 14:59:39 -04:00
|
|
|
const EXE_NAME: &str = "deno";
|
2020-03-23 11:37:24 -04:00
|
|
|
// We use into_path so that the tempdir is not automatically deleted. This is
|
|
|
|
// useful for debugging upgrade, but also so this function can return a path
|
|
|
|
// to the newly uncompressed file without fear of the tempdir being deleted.
|
|
|
|
let temp_dir = TempDir::new()?.into_path();
|
2021-01-23 12:40:13 -05:00
|
|
|
let exe_ext = if is_windows { "exe" } else { "" };
|
2021-07-07 14:59:39 -04:00
|
|
|
let archive_path = temp_dir.join(EXE_NAME).with_extension("zip");
|
|
|
|
let exe_path = temp_dir.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")
|
2020-06-29 09:13:07 -04:00
|
|
|
.arg(format!("'{}'", &temp_dir.to_str().unwrap()))
|
2020-06-05 15:46:03 -04:00
|
|
|
.spawn()?
|
|
|
|
.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")
|
|
|
|
.current_dir(&temp_dir)
|
|
|
|
.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,
|
|
|
|
"`unzip` was not found on your PATH, please install `unzip`",
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
err
|
|
|
|
}
|
|
|
|
})?
|
2020-06-05 15:46:03 -04:00
|
|
|
.wait()?
|
2020-03-23 11:37:24 -04:00
|
|
|
}
|
|
|
|
ext => panic!("Unsupported archive type: '{}'", ext),
|
|
|
|
};
|
|
|
|
assert!(unpack_status.success());
|
|
|
|
assert!(exe_path.exists());
|
|
|
|
Ok(exe_path)
|
|
|
|
}
|
|
|
|
|
2020-08-25 18:22:15 -04:00
|
|
|
fn replace_exe(new: &Path, old: &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
|
|
|
|
fs::rename(old, old.with_extension("old.exe"))?;
|
|
|
|
} else {
|
|
|
|
fs::remove_file(old)?;
|
|
|
|
}
|
|
|
|
// Windows cannot rename files across device boundaries, so if rename fails,
|
|
|
|
// we try again with copy.
|
|
|
|
fs::rename(new, old).or_else(|_| fs::copy(new, old).map(|_| ()))?;
|
|
|
|
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(())
|
|
|
|
}
|