Skip to content

Commit 5462e5c

Browse files
feat(nsis): support installer hooks (#9731)
* feat(nsis): support installer hooks closes #9668 * update change files --------- Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 418d72d commit 5462e5c

File tree

9 files changed

+122
-1
lines changed

9 files changed

+122
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-bundler": "patch:feat"
3+
---
4+
5+
Add support for NSIS installer hooks providing a path to a `.nsh` file in `bundle > windows > nsis > installer_hooks` key in `tauri.conf.json`.
6+

.changes/utils-installer-hooks.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": "patch:feat"
3+
---
4+
5+
Add `installer_hooks` NSIS configuration field

core/tauri-config-schema/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,13 @@
22982298
"type": "null"
22992299
}
23002300
]
2301+
},
2302+
"installerHooks": {
2303+
"description": "A path to a `.nsh` file that contains special NSIS macros to be hooked into the main installer.nsi script.\n\nSupported hooks are: - `NSIS_HOOK_PREINSTALL`: This hook runs before copying files, setting registry key values and creating shortcuts. - `NSIS_HOOK_POSTINSTALL`: This hook runs after the installer has finished copying all files, setting the registry keys and created shortcuts. - `NSIS_HOOK_PREUNINSTALL`: This hook runs before removing any files, registry keys and shortcuts. - `NSIS_HOOK_POSTUNINSTALL`: This hook runs after files, registry keys and shortcuts have been removed.\n\n### Example\n\n```nsh !define NSIS_HOOK_PREINSTALL \"NSIS_HOOK_PREINSTALL_\" !macro NSIS_HOOK_PREINSTALL_ MessageBox MB_OK \"PreInstall\" !macroend\n\n!define NSIS_HOOK_POSTINSTALL \"NSIS_HOOK_POSTINSTALL_\" !macro NSIS_HOOK_POSTINSTALL_ MessageBox MB_OK \"PostInstall\" !macroend\n\n!define NSIS_HOOK_PREUNINSTALL \"NSIS_HOOK_PREUNINSTALL_\" !macro NSIS_HOOK_PREUNINSTALL_ MessageBox MB_OK \"PreUnInstall\" !macroend\n\n!define NSIS_HOOK_POSTUNINSTALL \"NSIS_HOOK_POSTUNINSTALL_\" !macro NSIS_HOOK_POSTUNINSTALL_ MessageBox MB_OK \"PostUninstall\" !macroend ```",
2304+
"type": [
2305+
"string",
2306+
"null"
2307+
]
23012308
}
23022309
},
23032310
"additionalProperties": false

core/tauri-utils/src/config.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,41 @@ pub struct NsisConfig {
733733
///
734734
/// See <https://nsis.sourceforge.io/Reference/SetCompressor>
735735
pub compression: Option<NsisCompression>,
736+
/// A path to a `.nsh` file that contains special NSIS macros to be hooked into the
737+
/// main installer.nsi script.
738+
///
739+
/// Supported hooks are:
740+
/// - `NSIS_HOOK_PREINSTALL`: This hook runs before copying files, setting registry key values and creating shortcuts.
741+
/// - `NSIS_HOOK_POSTINSTALL`: This hook runs after the installer has finished copying all files, setting the registry keys and created shortcuts.
742+
/// - `NSIS_HOOK_PREUNINSTALL`: This hook runs before removing any files, registry keys and shortcuts.
743+
/// - `NSIS_HOOK_POSTUNINSTALL`: This hook runs after files, registry keys and shortcuts have been removed.
744+
///
745+
///
746+
/// ### Example
747+
///
748+
/// ```nsh
749+
/// !define NSIS_HOOK_PREINSTALL "NSIS_HOOK_PREINSTALL_"
750+
/// !macro NSIS_HOOK_PREINSTALL_
751+
/// MessageBox MB_OK "PreInstall"
752+
/// !macroend
753+
///
754+
/// !define NSIS_HOOK_POSTINSTALL "NSIS_HOOK_POSTINSTALL_"
755+
/// !macro NSIS_HOOK_POSTINSTALL_
756+
/// MessageBox MB_OK "PostInstall"
757+
/// !macroend
758+
///
759+
/// !define NSIS_HOOK_PREUNINSTALL "NSIS_HOOK_PREUNINSTALL_"
760+
/// !macro NSIS_HOOK_PREUNINSTALL_
761+
/// MessageBox MB_OK "PreUnInstall"
762+
/// !macroend
763+
///
764+
/// !define NSIS_HOOK_POSTUNINSTALL "NSIS_HOOK_POSTUNINSTALL_"
765+
/// !macro NSIS_HOOK_POSTUNINSTALL_
766+
/// MessageBox MB_OK "PostUninstall"
767+
/// !macroend
768+
/// ```
769+
#[serde(alias = "installer-hooks")]
770+
pub installer_hooks: Option<PathBuf>,
736771
}
737772

738773
/// Install Modes for the NSIS installer.

tooling/bundler/src/bundle/settings.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,40 @@ pub struct NsisSettings {
413413
pub display_language_selector: bool,
414414
/// Set compression algorithm used to compress files in the installer.
415415
pub compression: Option<NsisCompression>,
416+
/// A path to a `.nsh` file that contains special NSIS macros to be hooked into the
417+
/// main installer.nsi script.
418+
///
419+
/// Supported hooks are:
420+
/// - `NSIS_HOOK_PREINSTALL`: This hook runs before copying files, setting registry key values and creating shortcuts.
421+
/// - `NSIS_HOOK_POSTINSTALL`: This hook runs after the installer has finished copying all files, setting the registry keys and created shortcuts.
422+
/// - `NSIS_HOOK_PREUNINSTALL`: This hook runs before removing any files, registry keys and shortcuts.
423+
/// - `NSIS_HOOK_POSTUNINSTALL`: This hook runs after files, registry keys and shortcuts have been removed.
424+
///
425+
///
426+
/// ### Example
427+
///
428+
/// ```nsh
429+
/// !define NSIS_HOOK_PREINSTALL "NSIS_HOOK_PREINSTALL_"
430+
/// !macro NSIS_HOOK_PREINSTALL_
431+
/// MessageBox MB_OK "PreInstall"
432+
/// !macroend
433+
///
434+
/// !define NSIS_HOOK_POSTINSTALL "NSIS_HOOK_POSTINSTALL_"
435+
/// !macro NSIS_HOOK_POSTINSTALL_
436+
/// MessageBox MB_OK "PostInstall"
437+
/// !macroend
438+
///
439+
/// !define NSIS_HOOK_PREUNINSTALL "NSIS_HOOK_PREUNINSTALL_"
440+
/// !macro NSIS_HOOK_PREUNINSTALL_
441+
/// MessageBox MB_OK "PreUnInstall"
442+
/// !macroend
443+
///
444+
/// !define NSIS_HOOK_POSTUNINSTALL "NSIS_HOOK_POSTUNINSTALL_"
445+
/// !macro NSIS_HOOK_POSTUNINSTALL_
446+
/// MessageBox MB_OK "PostUninstall"
447+
/// !macroend
448+
/// ```
449+
pub installer_hooks: Option<PathBuf>,
416450
}
417451

418452
/// The Windows bundle settings.

tooling/bundler/src/bundle/windows/nsis.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ fn build_nsis_app_installer(
255255
"display_language_selector",
256256
to_json(nsis.display_language_selector && languages.len() > 1),
257257
);
258+
259+
if let Some(installer_hooks) = &nsis.installer_hooks {
260+
let installer_hooks = dunce::canonicalize(installer_hooks)?;
261+
data.insert("installer_hooks", to_json(installer_hooks));
262+
}
258263
}
259264
data.insert(
260265
"install_mode",

tooling/bundler/src/bundle/windows/templates/installer.nsi

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ ManifestDPIAware true
1313
!include WordFunc.nsh
1414
!include "utils.nsh"
1515
!include "FileAssociation.nsh"
16-
!include "StrFunc.nsh"
1716
!include "Win\COM.nsh"
1817
!include "Win\Propkey.nsh"
18+
!include "StrFunc.nsh"
1919
${StrCase}
2020
${StrLoc}
2121

22+
{{#if installer_hooks}}
23+
!include "{{installer_hooks}}"
24+
{{/if}}
25+
2226
!define MANUFACTURER "{{manufacturer}}"
2327
!define PRODUCTNAME "{{product_name}}"
2428
!define VERSION "{{version}}"
@@ -505,6 +509,10 @@ Section Install
505509

506510
!insertmacro CheckIfAppIsRunning
507511

512+
!ifdef NSIS_HOOK_PREINSTALL
513+
!insertmacro "${NSIS_HOOK_PREINSTALL}"
514+
!endif
515+
508516
; Copy main executable
509517
File "${MAINBINARYSRCPATH}"
510518

@@ -580,6 +588,10 @@ Section Install
580588
${EndIf}
581589
shortcuts_done:
582590

591+
!ifdef NSIS_HOOK_POSTINSTALL
592+
!insertmacro "${NSIS_HOOK_POSTINSTALL}"
593+
!endif
594+
583595
; Auto close this page for passive mode
584596
${IfThen} $PassiveMode == 1 ${|} SetAutoClose true ${|}
585597
SectionEnd
@@ -615,6 +627,10 @@ Section Uninstall
615627

616628
!insertmacro CheckIfAppIsRunning
617629

630+
!ifdef NSIS_HOOK_PREUNINSTALL
631+
!insertmacro "${NSIS_HOOK_PREUNINSTALL}"
632+
!endif
633+
618634
; Delete the app directory and its content from disk
619635
; Copy main executable
620636
Delete "$INSTDIR\${MAINBINARYNAME}.exe"
@@ -688,6 +704,11 @@ Section Uninstall
688704
RmDir /r "$LOCALAPPDATA\${BUNDLEID}"
689705
${EndIf}
690706

707+
!ifdef NSIS_HOOK_POSTUNINSTALL
708+
!insertmacro "${NSIS_HOOK_POSTUNINSTALL}"
709+
!endif
710+
711+
; Auto close if passive mode
691712
${GetOptions} $CMDLINE "/P" $R0
692713
IfErrors +2 0
693714
SetAutoClose true

tooling/cli/schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,13 @@
22982298
"type": "null"
22992299
}
23002300
]
2301+
},
2302+
"installerHooks": {
2303+
"description": "A path to a `.nsh` file that contains special NSIS macros to be hooked into the main installer.nsi script.\n\nSupported hooks are: - `NSIS_HOOK_PREINSTALL`: This hook runs before copying files, setting registry key values and creating shortcuts. - `NSIS_HOOK_POSTINSTALL`: This hook runs after the installer has finished copying all files, setting the registry keys and created shortcuts. - `NSIS_HOOK_PREUNINSTALL`: This hook runs before removing any files, registry keys and shortcuts. - `NSIS_HOOK_POSTUNINSTALL`: This hook runs after files, registry keys and shortcuts have been removed.\n\n### Example\n\n```nsh !define NSIS_HOOK_PREINSTALL \"NSIS_HOOK_PREINSTALL_\" !macro NSIS_HOOK_PREINSTALL_ MessageBox MB_OK \"PreInstall\" !macroend\n\n!define NSIS_HOOK_POSTINSTALL \"NSIS_HOOK_POSTINSTALL_\" !macro NSIS_HOOK_POSTINSTALL_ MessageBox MB_OK \"PostInstall\" !macroend\n\n!define NSIS_HOOK_PREUNINSTALL \"NSIS_HOOK_PREUNINSTALL_\" !macro NSIS_HOOK_PREUNINSTALL_ MessageBox MB_OK \"PreUnInstall\" !macroend\n\n!define NSIS_HOOK_POSTUNINSTALL \"NSIS_HOOK_POSTUNINSTALL_\" !macro NSIS_HOOK_POSTUNINSTALL_ MessageBox MB_OK \"PostUninstall\" !macroend ```",
2304+
"type": [
2305+
"string",
2306+
"null"
2307+
]
23012308
}
23022309
},
23032310
"additionalProperties": false

tooling/cli/src/helpers/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ pub fn nsis_settings(config: NsisConfig) -> tauri_bundler::NsisSettings {
105105
custom_language_files: config.custom_language_files,
106106
display_language_selector: config.display_language_selector,
107107
compression: config.compression,
108+
installer_hooks: config.installer_hooks,
108109
}
109110
}
110111

0 commit comments

Comments
 (0)