fix: fix a bug make auto update not woking on windows

This commit is contained in:
Kuingsmile
2025-07-13 11:44:21 +08:00
parent 7b0565f210
commit 9263ad9810
6 changed files with 81 additions and 42 deletions

View File

@@ -398,6 +398,7 @@ pub async fn install_update_and_restart(
"linux" => install_linux_update(&path).await,
_ => Err("Unsupported platform for auto-update".to_string()),
};
log::info!("Update installation result: {result:?}");
match result {
Ok(_) => {
@@ -407,8 +408,8 @@ pub async fn install_update_and_restart(
log::error!("Failed to emit install completed event: {e}");
}
if let Err(e) = app.emit("app-restarting", ()) {
log::error!("Failed to emit app restarting event: {e}");
if let Err(e) = app.emit("quit-app", ()) {
log::error!("Failed to emit app quit event: {e}");
}
tokio::time::sleep(Duration::from_millis(1000)).await;
@@ -423,29 +424,47 @@ pub async fn install_update_and_restart(
}
}
}
async fn install_windows_update(installer_path: &PathBuf) -> Result<(), String> {
log::info!("Installing Windows update...");
let mut cmd = Command::new(installer_path);
cmd.arg("/SILENT");
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW
}
let mut cmd = Command::new("powershell");
cmd.args(&[
"-Command",
&format!(
"Start-Process -FilePath '{}' -Verb runAs",
installer_path.display()
),
]);
log::info!("Running command: {cmd:?}");
let _ = tokio::task::spawn_blocking(move || {
cmd.spawn()
.map_err(|e| format!("Failed to start Windows installer: {e}"))
let child = cmd
.spawn()
.map_err(|e| format!("Failed to start Windows installer: {e}"))?;
log::info!("Started installer process with PID: {}", child.id());
let output = child
.wait_with_output()
.map_err(|e| format!("Failed to wait for installer: {e}"))?;
log::info!("Installer output: {:?}", output);
if output.status.success() {
log::info!(
"Installer completed successfully. Output: {}",
String::from_utf8_lossy(&output.stdout)
);
Ok(())
} else {
log::error!(
"Installer failed. Error: {}",
String::from_utf8_lossy(&output.stderr)
);
Err(format!("Installer exited with status: {:?}", output.status))
}
})
.await
.map_err(|e| format!("Task error: {e}"))?;
Ok(())
}
async fn install_macos_update(installer_path: &PathBuf) -> Result<(), String> {
log::info!("Installing macOS update...");
@@ -453,8 +472,25 @@ async fn install_macos_update(installer_path: &PathBuf) -> Result<(), String> {
cmd.arg(installer_path);
let _ = tokio::task::spawn_blocking(move || {
cmd.spawn()
.map_err(|e| format!("Failed to start macOS installer: {e}"))
let child = cmd
.spawn()
.map_err(|e| format!("Failed to start macOS installer: {e}"))?;
let output = child
.wait_with_output()
.map_err(|e| format!("Failed to wait for installer: {e}"))?;
if output.status.success() {
log::info!(
"Installer completed successfully. Output: {}",
String::from_utf8_lossy(&output.stdout)
);
Ok(())
} else {
log::error!(
"Installer failed. Error: {}",
String::from_utf8_lossy(&output.stderr)
);
Err(format!("Installer exited with status: {:?}", output.status))
}
})
.await
.map_err(|e| format!("Task error: {e}"))?;
@@ -489,8 +525,25 @@ async fn install_linux_update(installer_path: &PathBuf) -> Result<(), String> {
};
let _ = tokio::task::spawn_blocking(move || {
cmd.spawn()
.map_err(|e| format!("Failed to start Linux installer: {e}"))
let child = cmd
.spawn()
.map_err(|e| format!("Failed to start Linux installer: {e}"))?;
let output = child
.wait_with_output()
.map_err(|e| format!("Failed to wait for installer: {e}"))?;
if output.status.success() {
log::info!(
"Installer completed successfully. Output: {}",
String::from_utf8_lossy(&output.stdout)
);
Ok(())
} else {
log::error!(
"Installer failed. Error: {}",
String::from_utf8_lossy(&output.stderr)
);
Err(format!("Installer exited with status: {:?}", output.status))
}
})
.await
.map_err(|e| format!("Task error: {e}"))?;
@@ -641,16 +694,3 @@ pub async fn perform_background_update_check(app: AppHandle) -> Result<(), Strin
}
}
}
#[tauri::command]
pub async fn restart_app(app: AppHandle) {
log::info!("Restarting application...");
if let Err(e) = app.emit("app-restarting", ()) {
log::error!("Failed to emit app-restarting event: {e}");
}
tokio::time::sleep(Duration::from_millis(500)).await;
app.restart();
}

View File

@@ -11,7 +11,7 @@ use cmd::binary::get_binary_version;
use cmd::config::{load_settings, reset_settings, save_settings, save_settings_with_update_port};
use cmd::custom_updater::{
check_for_updates, download_update, get_current_version, install_update_and_restart,
is_auto_check_enabled, restart_app, set_auto_check_enabled,
is_auto_check_enabled, set_auto_check_enabled,
};
use cmd::http_api::{
delete_process, get_process_list, restart_process, start_process, stop_process, update_process,
@@ -164,8 +164,7 @@ pub fn run() {
install_update_and_restart,
get_current_version,
set_auto_check_enabled,
is_auto_check_enabled,
restart_app,
is_auto_check_enabled
])
.setup(|app| {
let app_handle = app.app_handle();

View File

@@ -119,6 +119,6 @@ export class TauriAPI {
listen('download-progress', e => cb(e.payload as DownloadProgress)),
onInstallStarted: (cb: () => void) => listen('update-install-started', () => cb()),
onInstallError: (cb: (err: string) => void) => listen('update-install-error', e => cb(e.payload as string)),
onAppRestarting: (cb: () => void) => listen('app-restarting', () => cb())
onAppQuit: (cb: () => void) => listen('quit-app', () => cb())
}
}

View File

@@ -177,7 +177,7 @@ let backgroundUpdateUnlisten: (() => void) | null = null
let downloadProgressUnlisten: (() => void) | null = null
let installStartedUnlisten: (() => void) | null = null
let installErrorUnlisten: (() => void) | null = null
let appRestartingUnlisten: (() => void) | null = null
let appQuitEventUnsubscriber: (() => void) | null = null
const checkForUpdates = async () => {
if (checking.value || downloading.value || installing.value) return
@@ -358,13 +358,13 @@ onMounted(async () => {
}
try {
appRestartingUnlisten = await TauriAPI.updater.onAppRestarting(() => {
installationStatus.value = t('update.restartingApp')
appQuitEventUnsubscriber = await TauriAPI.updater.onAppQuit(() => {
installationStatus.value = t('update.quitApp')
installationStatusType.value = 'success'
})
} catch (err) {
console.warn('App restarting listener not available:', err)
appRestartingUnlisten = null
appQuitEventUnsubscriber = null
}
if (autoCheckEnabled.value) {
await checkForUpdates()
@@ -400,7 +400,7 @@ onUnmounted(() => {
}
try {
appRestartingUnlisten?.()
appQuitEventUnsubscriber?.()
} catch (err) {
console.warn('Error unregistering app restarting listener:', err)
}

View File

@@ -480,7 +480,7 @@
"startingDownload": "Starting download...",
"downloading": "Downloading",
"installingUpdate": "Installing update...",
"restartingApp": "Restarting application...",
"quitApp": "Quitting application...",
"noUpdatesFound": "No updates available",
"aboutUpdates": "About Updates",
"autoCheckInfo": "Automatic update checks keep your app secure and up-to-date",

View File

@@ -480,7 +480,7 @@
"startingDownload": "开始下载...",
"downloading": "下载中...",
"installingUpdate": "安装更新中...",
"restartingApp": "重启应用中...",
"quitApp": "退出应用中...",
"noUpdatesFound": "没有可用更新",
"aboutUpdates": "关于更新",
"autoCheckInfo": "自动更新检查让您的应用保持安全和最新状态",