refactor: make SelfUpdate a step (#585)

This commit is contained in:
SteveLauC
2023-10-18 12:19:53 +08:00
committed by GitHub
parent e1754707d8
commit a2fbe92a25
3 changed files with 56 additions and 52 deletions

View File

@@ -127,6 +127,7 @@ pub enum Step {
Rustup, Rustup,
Scoop, Scoop,
Sdkman, Sdkman,
SelfUpdate,
Sheldon, Sheldon,
Shell, Shell,
Snap, Snap,

View File

@@ -108,7 +108,7 @@ fn run() -> Result<()> {
debug!("OS: {}", env!("TARGET")); debug!("OS: {}", env!("TARGET"));
debug!("{:?}", std::env::args()); debug!("{:?}", std::env::args());
debug!("Binary path: {:?}", std::env::current_exe()); debug!("Binary path: {:?}", std::env::current_exe());
debug!("Self Update: {:?}", cfg!(feature = "self-update")); debug!("self-update Feature Enabled: {:?}", cfg!(feature = "self-update"));
debug!("Configuration: {:?}", config); debug!("Configuration: {:?}", config);
if config.run_in_tmux() && env::var("TOPGRADE_INSIDE_TMUX").is_err() { if config.run_in_tmux() && env::var("TOPGRADE_INSIDE_TMUX").is_err() {
@@ -132,22 +132,15 @@ fn run() -> Result<()> {
let ctx = execution_context::ExecutionContext::new(run_type, sudo, &git, &config); let ctx = execution_context::ExecutionContext::new(run_type, sudo, &git, &config);
let mut runner = runner::Runner::new(&ctx); let mut runner = runner::Runner::new(&ctx);
// Self-Update step, this will execute only if:
// 1. the `self-update` feature is enabled
// 2. it is not disabled from configuration (env var/CLI opt/file)
#[cfg(feature = "self-update")] #[cfg(feature = "self-update")]
{ {
let config_self_upgrade = env::var("TOPGRADE_NO_SELF_UPGRADE").is_err() && !config.no_self_update(); let should_self_update = env::var("TOPGRADE_NO_SELF_UPGRADE").is_err() && !config.no_self_update();
if !run_type.dry() && config_self_upgrade { if should_self_update {
let result = self_update::self_update(); runner.execute(Step::SelfUpdate, "Self Update", || self_update::self_update(&ctx))?;
if let Err(e) = &result {
#[cfg(windows)]
{
if e.downcast_ref::<Upgraded>().is_some() {
return result;
}
}
print_warning(format!("Self update error: {e}"));
}
} }
} }

View File

@@ -3,6 +3,7 @@ use std::env;
use std::os::unix::process::CommandExt as _; use std::os::unix::process::CommandExt as _;
use std::process::Command; use std::process::Command;
use crate::config::Step;
use color_eyre::eyre::{bail, Result}; use color_eyre::eyre::{bail, Result};
use self_update_crate::backends::github::Update; use self_update_crate::backends::github::Update;
use self_update_crate::update::UpdateStatus; use self_update_crate::update::UpdateStatus;
@@ -11,8 +12,16 @@ use super::terminal::*;
#[cfg(windows)] #[cfg(windows)]
use crate::error::Upgraded; use crate::error::Upgraded;
pub fn self_update() -> Result<()> { use crate::execution_context::ExecutionContext;
pub fn self_update(ctx: &ExecutionContext) -> Result<()> {
print_separator("Self update"); print_separator("Self update");
if ctx.run_type().dry() {
println!("Would self-update");
Ok(())
} else {
let assume_yes = ctx.config().yes(Step::SelfUpdate);
let current_exe = env::current_exe(); let current_exe = env::current_exe();
let target = self_update_crate::get_target(); let target = self_update_crate::get_target();
@@ -21,10 +30,10 @@ pub fn self_update() -> Result<()> {
.repo_name("topgrade") .repo_name("topgrade")
.target(target) .target(target)
.bin_name(if cfg!(windows) { "topgrade.exe" } else { "topgrade" }) .bin_name(if cfg!(windows) { "topgrade.exe" } else { "topgrade" })
.show_output(false) .show_output(true)
.show_download_progress(true) .show_download_progress(true)
.current_version(self_update_crate::cargo_crate_version!()) .current_version(self_update_crate::cargo_crate_version!())
.no_confirm(true) .no_confirm(assume_yes)
.build()? .build()?
.update_extended()?; .update_extended()?;
@@ -39,7 +48,7 @@ pub fn self_update() -> Result<()> {
{ {
if result.updated() { if result.updated() {
print_warning("Respawning..."); print_info("Respawning...");
let mut command = Command::new(current_exe?); let mut command = Command::new(current_exe?);
command.args(env::args().skip(1)).env("TOPGRADE_NO_SELF_UPGRADE", ""); command.args(env::args().skip(1)).env("TOPGRADE_NO_SELF_UPGRADE", "");
@@ -59,4 +68,5 @@ pub fn self_update() -> Result<()> {
} }
Ok(()) Ok(())
}
} }