diff --git a/.changes/fix-updater-cleanup-md b/.changes/fix-updater-cleanup-md new file mode 100644 index 000000000..fe69a0049 --- /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/permissions/schemas/schema.json b/plugins/updater/permissions/schemas/schema.json index 691359767..6d6a3c211 100644 --- a/plugins/updater/permissions/schemas/schema.json +++ b/plugins/updater/permissions/schemas/schema.json @@ -139,10 +139,14 @@ }, "platforms": { "description": "Target platforms this permission applies. By default all platforms are affected by this permission.", - "type": [ - "array", - "null" + "default": [ + "linux", + "macOS", + "windows", + "android", + "iOS" ], + "type": "array", "items": { "$ref": "#/definitions/Target" } diff --git a/plugins/updater/src/lib.rs b/plugins/updater/src/lib.rs index e060827d2..37198150d 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 4ba744358..27749bc37 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); }