mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
feat(upgrade): allow specifying a version (#5156)
This commit is contained in:
parent
7d3728e3f4
commit
abbf0339cd
4 changed files with 105 additions and 30 deletions
22
cli/flags.rs
22
cli/flags.rs
|
@ -65,6 +65,7 @@ pub enum DenoSubcommand {
|
||||||
Upgrade {
|
Upgrade {
|
||||||
dry_run: bool,
|
dry_run: bool,
|
||||||
force: bool,
|
force: bool,
|
||||||
|
version: Option<String>,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -563,7 +564,12 @@ fn test_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||||
fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
fn upgrade_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||||
let dry_run = matches.is_present("dry-run");
|
let dry_run = matches.is_present("dry-run");
|
||||||
let force = matches.is_present("force");
|
let force = matches.is_present("force");
|
||||||
flags.subcommand = DenoSubcommand::Upgrade { dry_run, force };
|
let version = matches.value_of("version").map(|s| s.to_string());
|
||||||
|
flags.subcommand = DenoSubcommand::Upgrade {
|
||||||
|
dry_run,
|
||||||
|
force,
|
||||||
|
version,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) {
|
||||||
|
@ -811,14 +817,21 @@ Future runs of this module will trigger no downloads or compilation unless
|
||||||
|
|
||||||
fn upgrade_subcommand<'a, 'b>() -> App<'a, 'b> {
|
fn upgrade_subcommand<'a, 'b>() -> App<'a, 'b> {
|
||||||
SubCommand::with_name("upgrade")
|
SubCommand::with_name("upgrade")
|
||||||
.about("Upgrade deno executable to newest version")
|
.about("Upgrade deno executable to given version")
|
||||||
.long_about(
|
.long_about(
|
||||||
"Upgrade deno executable to newest available version.
|
"Upgrade deno executable to the given version.
|
||||||
|
Defaults to latest.
|
||||||
|
|
||||||
The latest version is downloaded from
|
The version is downloaded from
|
||||||
https://github.com/denoland/deno/releases
|
https://github.com/denoland/deno/releases
|
||||||
and is used to replace the current executable.",
|
and is used to replace the current executable.",
|
||||||
)
|
)
|
||||||
|
.arg(
|
||||||
|
Arg::with_name("version")
|
||||||
|
.long("version")
|
||||||
|
.help("The version to upgrade to")
|
||||||
|
.takes_value(true),
|
||||||
|
)
|
||||||
.arg(
|
.arg(
|
||||||
Arg::with_name("dry-run")
|
Arg::with_name("dry-run")
|
||||||
.long("dry-run")
|
.long("dry-run")
|
||||||
|
@ -1354,6 +1367,7 @@ mod tests {
|
||||||
subcommand: DenoSubcommand::Upgrade {
|
subcommand: DenoSubcommand::Upgrade {
|
||||||
force: true,
|
force: true,
|
||||||
dry_run: true,
|
dry_run: true,
|
||||||
|
version: None
|
||||||
},
|
},
|
||||||
..Flags::default()
|
..Flags::default()
|
||||||
}
|
}
|
||||||
|
|
|
@ -616,9 +616,11 @@ pub fn main() {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DenoSubcommand::Upgrade { force, dry_run } => {
|
DenoSubcommand::Upgrade {
|
||||||
upgrade_command(dry_run, force).boxed_local()
|
force,
|
||||||
}
|
dry_run,
|
||||||
|
version,
|
||||||
|
} => upgrade_command(dry_run, force, version).boxed_local(),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -224,6 +224,37 @@ fn upgrade_in_tmpdir() {
|
||||||
// TODO(ry) assert!(mtime1 < mtime2);
|
// TODO(ry) assert!(mtime1 < mtime2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Warning: this test requires internet access.
|
||||||
|
#[test]
|
||||||
|
fn upgrade_with_version_in_tmpdir() {
|
||||||
|
let temp_dir = TempDir::new().unwrap();
|
||||||
|
let exe_path = if cfg!(windows) {
|
||||||
|
temp_dir.path().join("deno")
|
||||||
|
} else {
|
||||||
|
temp_dir.path().join("deno.exe")
|
||||||
|
};
|
||||||
|
let _ = std::fs::copy(util::deno_exe_path(), &exe_path).unwrap();
|
||||||
|
assert!(exe_path.exists());
|
||||||
|
let _mtime1 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
|
||||||
|
let status = Command::new(&exe_path)
|
||||||
|
.arg("upgrade")
|
||||||
|
.arg("--force")
|
||||||
|
.arg("--version")
|
||||||
|
.arg("0.42.0")
|
||||||
|
.spawn()
|
||||||
|
.unwrap()
|
||||||
|
.wait()
|
||||||
|
.unwrap();
|
||||||
|
assert!(status.success());
|
||||||
|
let upgraded_deno_version = String::from_utf8(
|
||||||
|
Command::new(&exe_path).arg("-V").output().unwrap().stdout,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert!(upgraded_deno_version.contains("0.42.0"));
|
||||||
|
let _mtime2 = std::fs::metadata(&exe_path).unwrap().modified().unwrap();
|
||||||
|
// TODO(ry) assert!(mtime1 < mtime2);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn installer_test_local_module_run() {
|
fn installer_test_local_module_run() {
|
||||||
let temp_dir = TempDir::new().expect("tempdir fail");
|
let temp_dir = TempDir::new().expect("tempdir fail");
|
||||||
|
|
|
@ -52,36 +52,64 @@ async fn get_latest_version(client: &Client) -> Result<Version, ErrBox> {
|
||||||
|
|
||||||
/// Asynchronously updates deno executable to greatest version
|
/// Asynchronously updates deno executable to greatest version
|
||||||
/// if greatest version is available.
|
/// if greatest version is available.
|
||||||
pub async fn upgrade_command(dry_run: bool, force: bool) -> Result<(), ErrBox> {
|
pub async fn upgrade_command(
|
||||||
|
dry_run: bool,
|
||||||
|
force: bool,
|
||||||
|
version: Option<String>,
|
||||||
|
) -> Result<(), ErrBox> {
|
||||||
let client = Client::builder().redirect(Policy::none()).build()?;
|
let client = Client::builder().redirect(Policy::none()).build()?;
|
||||||
let latest_version = get_latest_version(&client).await?;
|
|
||||||
let current_version = semver_parse(crate::version::DENO).unwrap();
|
let current_version = semver_parse(crate::version::DENO).unwrap();
|
||||||
|
|
||||||
if !force && current_version >= latest_version {
|
let install_version = match version {
|
||||||
println!(
|
Some(passed_version) => match semver_parse(&passed_version) {
|
||||||
"Local deno version {} is the most recent release",
|
Ok(ver) => {
|
||||||
&crate::version::DENO
|
if !force && current_version == ver {
|
||||||
);
|
println!("Version {} is already installed", &ver);
|
||||||
} else {
|
std::process::exit(1)
|
||||||
println!(
|
} else {
|
||||||
"New version has been found\nDeno is upgrading to version {}",
|
ver
|
||||||
&latest_version
|
}
|
||||||
);
|
}
|
||||||
let archive_data =
|
Err(_) => {
|
||||||
download_package(&compose_url_to_exec(&latest_version)?, client).await?;
|
eprintln!("Invalid semver passed");
|
||||||
|
std::process::exit(1)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let latest_version = get_latest_version(&client).await?;
|
||||||
|
|
||||||
let old_exe_path = std::env::current_exe()?;
|
if !force && current_version >= latest_version {
|
||||||
let new_exe_path = unpack(archive_data)?;
|
println!(
|
||||||
let permissions = fs::metadata(&old_exe_path)?.permissions();
|
"Local deno version {} is the most recent release",
|
||||||
fs::set_permissions(&new_exe_path, permissions)?;
|
&crate::version::DENO
|
||||||
check_exe(&new_exe_path, &latest_version)?;
|
);
|
||||||
|
std::process::exit(1)
|
||||||
if !dry_run {
|
} else {
|
||||||
replace_exe(&new_exe_path, &old_exe_path)?;
|
latest_version
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
println!("Upgrade done successfully")
|
println!(
|
||||||
|
"Version has been found\nDeno is upgrading to version {}",
|
||||||
|
&install_version
|
||||||
|
);
|
||||||
|
|
||||||
|
let archive_data =
|
||||||
|
download_package(&compose_url_to_exec(&install_version)?, client).await?;
|
||||||
|
|
||||||
|
let old_exe_path = std::env::current_exe()?;
|
||||||
|
let new_exe_path = unpack(archive_data)?;
|
||||||
|
let permissions = fs::metadata(&old_exe_path)?.permissions();
|
||||||
|
fs::set_permissions(&new_exe_path, permissions)?;
|
||||||
|
check_exe(&new_exe_path, &install_version)?;
|
||||||
|
|
||||||
|
if !dry_run {
|
||||||
|
replace_exe(&new_exe_path, &old_exe_path)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("Upgrade done successfully");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue