Browse Source

feat(updater): relaunch on Windows, closes #4220 (#4568)

Lucas Fernandes Nogueira 3 years ago
parent
commit
0fa745344e

+ 5 - 0
.changes/windows-updater-relaunch.md

@@ -0,0 +1,5 @@
+---
+"tauri": patch
+---
+
+Configure the updater to relaunch after installing the update on Windows.

+ 2 - 0
core/tauri-utils/src/config.rs

@@ -2130,6 +2130,8 @@ pub enum WindowsUpdateInstallMode {
   Quiet,
   /// Specifies unattended mode, which means the installation only shows a progress bar.
   Passive,
+  // to add more modes, we need to check if the updater relaunch makes sense
+  // i.e. for a full UI mode, the user can also mark the installer to start the app
 }
 
 impl WindowsUpdateInstallMode {

+ 31 - 8
core/tauri/src/updater/core.rs

@@ -784,14 +784,37 @@ fn copy_files_and_run<R: Read + Seek>(
         }
       }
 
-      // restart should be handled by WIX as we exit the process
-      Command::new("msiexec.exe")
-        .arg("/i")
-        .arg(found_path)
-        .args(msiexec_args)
-        .arg("/promptrestart")
-        .spawn()
-        .expect("installer failed to start");
+      // we need to wrap the current exe path in quotes for Start-Process
+      let mut current_exe_arg = std::ffi::OsString::new();
+      current_exe_arg.push("\"");
+      current_exe_arg.push(current_exe()?);
+      current_exe_arg.push("\"");
+      // run the installer and relaunch the application
+      let powershell_install_res = Command::new("powershell.exe")
+        .args(["-NoProfile", "-windowstyle", "hidden"])
+        .args([
+          "Start-Process",
+          "-Wait",
+          "-FilePath",
+          "msiexec",
+          "-ArgumentList",
+        ])
+        .arg("/i,")
+        .arg(&found_path)
+        .arg(format!(", {}, /promptrestart;", msiexec_args.join(", ")))
+        .arg("Start-Process")
+        .arg(current_exe_arg)
+        .spawn();
+      if powershell_install_res.is_err() {
+        // fallback to running msiexec directly - relaunch won't be available
+        // we use this here in case powershell fails in an older machine somehow
+        let _ = Command::new("msiexec.exe")
+          .arg("/i")
+          .arg(found_path)
+          .args(msiexec_args)
+          .arg("/promptrestart")
+          .spawn();
+      }
 
       exit(0);
     }