Skip to content

Commit

Permalink
feat: retain cli args when relaunching after update, closes #7402
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Aug 30, 2023
1 parent f19c7fc commit ecf443b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/tauri-bundler-nsis-args.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changes/tauri-updater-retain-args.md
Original file line number Diff line number Diff line change
@@ -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).
5 changes: 5 additions & 0 deletions .changes/tauri-utils-current-exe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tauri-utils': 'patch:enhance'
---

Simplified the path returned from `platform::current_exe` to remove UNC path prefix on Windows.
5 changes: 5 additions & 0 deletions .changes/tauri-utils-nsis-args.md
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 5 additions & 2 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
_ => &[],
}
}
Expand Down
4 changes: 3 additions & 1 deletion core/tauri-utils/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
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.
Expand Down
29 changes: 21 additions & 8 deletions core/tauri/src/updater/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,10 @@ fn copy_files_and_run<R: Read + Seek>(
|p| format!("{p}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"),
);

let current_args = std::env::args_os()
.skip(1)
.collect::<Vec<std::ffi::OsString>>();

for path in paths {
let found_path = path?.path();
// we support 2 type of files exe & msi for now
Expand All @@ -747,28 +751,35 @@ fn copy_files_and_run<R: Read + Seek>(

// 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::<Vec<_>>()
.as_slice(),
.map(Into::into)
.collect::<Vec<_>>(),
]
.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")) {
Expand Down Expand Up @@ -848,6 +859,8 @@ fn copy_files_and_run<R: Read + Seek>(
.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
Expand Down
3 changes: 2 additions & 1 deletion tooling/bundler/src/bundle/windows/templates/installer.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ecf443b

Please sign in to comment.