From ecf443bf7420e1b59b7c809dfe6a039357b35421 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 30 Aug 2023 19:56:31 +0300 Subject: [PATCH] feat: retain cli args when relaunching after update, closes #7402 --- .changes/tauri-bundler-nsis-args.md | 5 ++++ .changes/tauri-updater-retain-args.md | 5 ++++ .changes/tauri-utils-current-exe.md | 5 ++++ .changes/tauri-utils-nsis-args.md | 5 ++++ core/tauri-utils/src/config.rs | 7 +++-- core/tauri-utils/src/platform.rs | 4 ++- core/tauri/src/updater/core.rs | 29 ++++++++++++++----- .../bundle/windows/templates/installer.nsi | 3 +- 8 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 .changes/tauri-bundler-nsis-args.md create mode 100644 .changes/tauri-updater-retain-args.md create mode 100644 .changes/tauri-utils-current-exe.md create mode 100644 .changes/tauri-utils-nsis-args.md diff --git a/.changes/tauri-bundler-nsis-args.md b/.changes/tauri-bundler-nsis-args.md new file mode 100644 index 00000000000..1457ac5f16d --- /dev/null +++ b/.changes/tauri-bundler-nsis-args.md @@ -0,0 +1,5 @@ +--- +'tauri-bundler': 'minor:feat' +--- + +On Windows, NSIS isntaller now supports `/ARGS` flag to pass arguments to be used when launching the app after installation, only works if `/R` is used. diff --git a/.changes/tauri-updater-retain-args.md b/.changes/tauri-updater-retain-args.md new file mode 100644 index 00000000000..61f673b7809 --- /dev/null +++ b/.changes/tauri-updater-retain-args.md @@ -0,0 +1,5 @@ +--- +'tauri': 'minor:enhance' +--- + +On Windows, retain command line args when relaunching the app after an update. Supports NSIS and WiX (wihtout elevated update task). diff --git a/.changes/tauri-utils-current-exe.md b/.changes/tauri-utils-current-exe.md new file mode 100644 index 00000000000..d083c9af1f4 --- /dev/null +++ b/.changes/tauri-utils-current-exe.md @@ -0,0 +1,5 @@ +--- +'tauri-utils': 'patch:enhance' +--- + +Simplified the path returned from `platform::current_exe` to remove UNC path prefix on Windows. diff --git a/.changes/tauri-utils-nsis-args.md b/.changes/tauri-utils-nsis-args.md new file mode 100644 index 00000000000..5c5e29e451a --- /dev/null +++ b/.changes/tauri-utils-nsis-args.md @@ -0,0 +1,5 @@ +--- +'tauri-utils': 'minor:enhance' +--- + +Changed `WindowsUpdateInstallMode::nsis_args` return to include `/Args` flag at the end so it can be used to pass arguments when launching the app after the updater is done. diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 69e55b0f302..588de2e2a2f 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -2554,10 +2554,13 @@ impl WindowsUpdateInstallMode { } /// Returns the associated nsis arguments. + /// + /// [WindowsUpdateInstallMode::Passive] will return `["/P", "/R", "/ARGS"]` + /// [WindowsUpdateInstallMode::Quiet] will return `["/S", "/R", "/ARGS"]` pub fn nsis_args(&self) -> &'static [&'static str] { match self { - Self::Passive => &["/P", "/R"], - Self::Quiet => &["/S", "/R"], + Self::Passive => &["/P", "/R", "/ARGS"], + Self::Quiet => &["/S", "/R", "/ARGS"], _ => &[], } } diff --git a/core/tauri-utils/src/platform.rs b/core/tauri-utils/src/platform.rs index 5e2e7719232..2fff3ec6447 100644 --- a/core/tauri-utils/src/platform.rs +++ b/core/tauri-utils/src/platform.rs @@ -83,7 +83,9 @@ mod starting_binary; /// [Hard Link]: https://en.wikipedia.org/wiki/Hard_link /// [See the patch that enabled this]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=800179c9b8a1e796e441674776d11cd4c05d61d7 pub fn current_exe() -> std::io::Result { - self::starting_binary::STARTING_BINARY.cloned() + self::starting_binary::STARTING_BINARY + .cloned() + .map(|p| dunce::simplified(&p).to_path_buf()) } /// Try to determine the current target triple. diff --git a/core/tauri/src/updater/core.rs b/core/tauri/src/updater/core.rs index e487aab37ed..c6d8a4cd955 100644 --- a/core/tauri/src/updater/core.rs +++ b/core/tauri/src/updater/core.rs @@ -734,6 +734,10 @@ fn copy_files_and_run( |p| format!("{p}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"), ); + let current_args = std::env::args_os() + .skip(1) + .collect::>(); + for path in paths { let found_path = path?.path(); // we support 2 type of files exe & msi for now @@ -747,28 +751,35 @@ fn copy_files_and_run( // Run the EXE Command::new(powershell_path) - .args(["-NoProfile", "-WindowStyle", "Hidden"]) - .args(["Start-Process"]) + .args(["-NoProfile", "-WindowStyle", "Hidden", "Start-Process"]) .arg(found_path) .arg("-ArgumentList") .arg( [ - config.tauri.updater.windows.install_mode.nsis_args(), + config + .tauri + .updater + .windows + .install_mode + .nsis_args() + .iter() + .map(Into::into) + .collect(), + current_args, config .tauri .updater .windows .installer_args .iter() - .map(AsRef::as_ref) - .collect::>() - .as_slice(), + .map(Into::into) + .collect::>(), ] .concat() - .join(", "), + .join(std::ffi::OsStr::new(", ")), ) .spawn() - .expect("installer failed to start"); + .expect("Running NSIS installer from powershell has failed to start"); exit(0); } else if found_path.extension() == Some(OsStr::new("msi")) { @@ -848,6 +859,8 @@ fn copy_files_and_run( .arg(format!(", {}, /promptrestart;", msiexec_args.join(", "))) .arg("Start-Process") .arg(current_exe_arg) + .arg("-ArgumentList") + .arg(current_args.join(std::ffi::OsStr::new(", "))) .spawn(); if powershell_install_res.is_err() { // fallback to running msiexec directly - relaunch won't be available diff --git a/tooling/bundler/src/bundle/windows/templates/installer.nsi b/tooling/bundler/src/bundle/windows/templates/installer.nsi index 356fdcde1d7..36676b9a7a3 100644 --- a/tooling/bundler/src/bundle/windows/templates/installer.nsi +++ b/tooling/bundler/src/bundle/windows/templates/installer.nsi @@ -600,7 +600,8 @@ Function .onInstSuccess check_r_flag: ${GetOptions} $CMDLINE "/R" $R0 IfErrors run_done 0 - Exec '"$INSTDIR\${MAINBINARYNAME}.exe"' + ${GetOptions} $CMDLINE "/ARGS" $R0 + Exec '"$INSTDIR\${MAINBINARYNAME}.exe" $R0' run_done: FunctionEnd