Skip to content

Commit

Permalink
Fix updater doesn't cleanup before exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Legend-Master committed Mar 12, 2024
1 parent ef3b70c commit f2fe9d6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
1 change: 1 addition & 0 deletions plugins/updater/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub(crate) async fn download_and_install<R: Runtime>(

update
.download_and_install(
app.app_handle(),
|chunk_length, content_length| {
if first_chunk {
first_chunk = !first_chunk;
Expand Down
32 changes: 19 additions & 13 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use reqwest::{
};
use semver::Version;
use serde::{de::Error as DeError, Deserialize, Deserializer, Serialize};
use tauri::{utils::platform::current_exe, Resource};
use tauri::{utils::platform::current_exe, AppHandle, Resource};
use time::OffsetDateTime;
use url::Url;

Expand Down Expand Up @@ -475,22 +475,27 @@ impl Update {
}

/// Installs the updater package downloaded by [`Update::download`]
pub fn install(&self, bytes: Vec<u8>) -> Result<()> {
self.install_inner(bytes)
pub fn install<R: tauri::Runtime>(&self, app: &AppHandle<R>, bytes: Vec<u8>) -> Result<()> {
self.install_inner(app, bytes)
}

/// Downloads and installs the updater package
pub async fn download_and_install<C: FnMut(usize, Option<u64>), D: FnOnce()>(
pub async fn download_and_install<
R: tauri::Runtime,
C: FnMut(usize, Option<u64>),
D: FnOnce(),
>(
&self,
app: &AppHandle<R>,
on_chunk: C,
on_download_finish: D,
) -> Result<()> {
let bytes = self.download(on_chunk, on_download_finish).await?;
self.install(bytes)
self.install(app, bytes)
}

#[cfg(mobile)]
fn install_inner(&self, bytes: Vec<u8>) -> Result<()> {
fn install_inner<R: tauri::Runtime>(&self, _app: &AppHandle<R>, _bytes: Vec<u8>) -> Result<()> {
Ok(())
}

Expand All @@ -511,7 +516,7 @@ impl Update {
// ## EXE
// Update server can provide a custom EXE (installer) who can run any task.
#[cfg(windows)]
fn install_inner(&self, bytes: Vec<u8>) -> Result<()> {
fn install_inner<R: tauri::Runtime>(&self, app: &AppHandle<R>, bytes: Vec<u8>) -> Result<()> {
use std::fs;

use windows_sys::{
Expand Down Expand Up @@ -575,9 +580,12 @@ impl Update {
} else {
continue;
}

app.cleanup_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"),
Expand All @@ -587,9 +595,7 @@ impl Update {
SW_SHOW,
)
};
if ret <= 32 {
return Err(Error::Io(std::io::Error::last_os_error()));
}

std::process::exit(0);
}

Expand All @@ -613,7 +619,7 @@ impl Update {
target_os = "netbsd",
target_os = "openbsd"
))]
fn install_inner(&self, bytes: Vec<u8>) -> Result<()> {
fn install_inner<R: tauri::Runtime>(&self, _app: &AppHandle<R>, bytes: Vec<u8>) -> Result<()> {
use flate2::read::GzDecoder;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
let archive = Cursor::new(bytes);
Expand Down Expand Up @@ -679,7 +685,7 @@ impl Update {
// │ └── ...
// └── ...
#[cfg(target_os = "macos")]
fn install_inner(&self, bytes: Vec<u8>) -> Result<()> {
fn install_inner<R: tauri::Runtime>(&self, _app: &AppHandle<R>, bytes: Vec<u8>) -> Result<()> {
use flate2::read::GzDecoder;

let cursor = Cursor::new(bytes);
Expand Down
6 changes: 5 additions & 1 deletion plugins/updater/tests/app-updater/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use tauri::Manager;
use tauri_plugin_updater::UpdaterExt;

fn main() {
Expand Down Expand Up @@ -35,7 +36,10 @@ fn main() {

match updater.check().await {
Ok(Some(update)) => {
if let Err(e) = update.download_and_install(|_, _| {}, || {}).await {
if let Err(e) = update
.download_and_install(handle.app_handle(), |_, _| {}, || {})
.await
{
println!("{e}");
std::process::exit(1);
}
Expand Down

0 comments on commit f2fe9d6

Please sign in to comment.