From 1d7dc86ec3da382e0b2f23cbd052c6ce77173848 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Wed, 13 Mar 2024 22:12:21 +0800 Subject: [PATCH] fix(updater): run cleanup before exit on Windows (#1070) --- .changes/fix-updater-cleanup-md | 5 +++++ plugins/updater/src/lib.rs | 5 +++++ plugins/updater/src/updater.rs | 31 ++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 .changes/fix-updater-cleanup-md diff --git a/.changes/fix-updater-cleanup-md b/.changes/fix-updater-cleanup-md new file mode 100644 index 00000000..fe69a004 --- /dev/null +++ b/.changes/fix-updater-cleanup-md @@ -0,0 +1,5 @@ +--- +"updater": patch +--- + +Add a `on_before_exit` hook for cleanup before spawning the updater on Windows, defaults to `app.cleanup_before_exit` when used through `UpdaterExt` diff --git a/plugins/updater/src/lib.rs b/plugins/updater/src/lib.rs index e060827d..37198150 100644 --- a/plugins/updater/src/lib.rs +++ b/plugins/updater/src/lib.rs @@ -98,6 +98,11 @@ impl> UpdaterExt for T { } } + let app_handle = app.app_handle().clone(); + builder = builder.on_before_exit(move || { + app_handle.cleanup_before_exit(); + }); + builder } diff --git a/plugins/updater/src/updater.rs b/plugins/updater/src/updater.rs index 4ba74435..27749bc3 100644 --- a/plugins/updater/src/updater.rs +++ b/plugins/updater/src/updater.rs @@ -8,6 +8,7 @@ use std::{ io::{Cursor, Read}, path::{Path, PathBuf}, str::FromStr, + sync::Arc, time::Duration, }; @@ -88,6 +89,8 @@ impl RemoteRelease { } } +pub type OnBeforeExit = Arc; + pub struct UpdaterBuilder { current_version: Version, config: Config, @@ -99,6 +102,7 @@ pub struct UpdaterBuilder { timeout: Option, proxy: Option, installer_args: Vec, + on_before_exit: Option, } impl UpdaterBuilder { @@ -118,6 +122,7 @@ impl UpdaterBuilder { headers: Default::default(), timeout: None, proxy: None, + on_before_exit: None, } } @@ -197,6 +202,11 @@ impl UpdaterBuilder { self } + pub fn on_before_exit(mut self, f: F) -> Self { + self.on_before_exit.replace(Arc::new(f)); + self + } + pub fn build(self) -> Result { let endpoints = self .endpoints @@ -236,6 +246,7 @@ impl UpdaterBuilder { json_target, headers: self.headers, extract_path, + on_before_exit: self.on_before_exit, }) } } @@ -256,6 +267,7 @@ pub struct Updater { json_target: String, headers: HeaderMap, extract_path: PathBuf, + on_before_exit: Option, } impl Updater { @@ -354,6 +366,7 @@ impl Updater { let update = if should_update { Some(Update { config: self.config.clone(), + on_before_exit: self.on_before_exit.clone(), current_version: self.current_version.to_string(), target: self.target.clone(), extract_path: self.extract_path.clone(), @@ -375,9 +388,11 @@ impl Updater { } } -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct Update { config: Config, + #[allow(unused)] + on_before_exit: Option, /// Update description pub body: Option, /// Version used to check for update @@ -490,7 +505,7 @@ impl Update { } #[cfg(mobile)] - fn install_inner(&self, bytes: Vec) -> Result<()> { + fn install_inner(&self, _bytes: Vec) -> Result<()> { Ok(()) } @@ -541,7 +556,7 @@ impl Update { let mut installer_args = self .installer_args .iter() - .map(|a| OsStr::new(a)) + .map(OsStr::new) .collect::>(); for path in paths { @@ -557,9 +572,13 @@ impl Update { continue; } + if let Some(on_before_exit) = self.on_before_exit.as_ref() { + on_before_exit(); + } + let file = encode_wide(found_path.as_os_str()); let parameters = encode_wide(installer_args.join(OsStr::new(" ")).as_os_str()); - let ret = unsafe { + unsafe { ShellExecuteW( 0, w!("open"), @@ -569,9 +588,7 @@ impl Update { SW_SHOW, ) }; - if ret <= 32 { - return Err(Error::Io(std::io::Error::last_os_error())); - } + std::process::exit(0); }