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-01-07 21:08:51 -05:00
|
|
|
use deno_core::error::AnyError;
|
2020-12-13 13:45:53 -05:00
|
|
|
use deno_runtime::deno_fetch::reqwest;
|
|
|
|
use deno_runtime::deno_fetch::reqwest::Client;
|
2020-03-23 11:37:24 -04:00
|
|
|
use semver_parser::version::parse as semver_parse;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::process::Command;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2020-11-08 05:54:35 -05:00
|
|
|
lazy_static! {
|
|
|
|
static ref ARCHIVE_NAME: String = 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) => {
|
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() {
|
|
|
|
let current = semver_parse(&*crate::version::deno()).unwrap();
|
|
|
|
let latest = match semver_parse(&latest_version) {
|
|
|
|
Ok(v) => v,
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("Invalid semver passed");
|
|
|
|
std::process::exit(1)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
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 {
|
2020-11-22 10:07:05 -05: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
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
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-01-23 12:40:13 -05:00
|
|
|
let new_exe_path = unpack(archive_data, "deno", 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() {
|
|
|
|
println!("Download has been found");
|
|
|
|
Ok(res.bytes().await?.to_vec())
|
|
|
|
} 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>,
|
|
|
|
exe_name: &str,
|
2021-01-23 12:40:13 -05:00
|
|
|
is_windows: bool,
|
2021-01-18 21:40:22 -05:00
|
|
|
) -> Result<PathBuf, std::io::Error> {
|
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-01-18 21:40:22 -05: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)
|
|
|
|
.spawn()?
|
|
|
|
.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(())
|
|
|
|
}
|