diff --git a/.changes/bunderl-installer-hooks.md b/.changes/bunderl-installer-hooks.md new file mode 100644 index 000000000..02e0faa4e --- /dev/null +++ b/.changes/bunderl-installer-hooks.md @@ -0,0 +1,6 @@ +--- +"tauri-bundler": "patch:feat" +--- + +Add support for NSIS installer hooks providing a path to a `.nsh` file in `bundle > windows > nsis > installer_hooks` key in `tauri.conf.json`. + diff --git a/.changes/utils-installer-hooks.md b/.changes/utils-installer-hooks.md new file mode 100644 index 000000000..80988dcdb --- /dev/null +++ b/.changes/utils-installer-hooks.md @@ -0,0 +1,5 @@ +--- +"tauri-utils": "patch:feat" +--- + +Add `installer_hooks` NSIS configuration field diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 3ac4c5769..a21612215 100644 --- a/core/tauri-config-schema/schema.json +++ b/core/tauri-config-schema/schema.json @@ -2298,6 +2298,13 @@ "type": "null" } ] + }, + "installerHooks": { + "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 ```", + "type": [ + "string", + "null" + ] } }, "additionalProperties": false diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 19eae9ffd..e6bdb630e 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -733,6 +733,41 @@ pub struct NsisConfig { /// /// See pub compression: Option, + /// A path to a `.nsh` file that contains special NSIS macros to be hooked into the + /// main installer.nsi script. + /// + /// Supported 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. + /// + /// + /// ### Example + /// + /// ```nsh + /// !define NSIS_HOOK_PREINSTALL "NSIS_HOOK_PREINSTALL_" + /// !macro NSIS_HOOK_PREINSTALL_ + /// MessageBox MB_OK "PreInstall" + /// !macroend + /// + /// !define NSIS_HOOK_POSTINSTALL "NSIS_HOOK_POSTINSTALL_" + /// !macro NSIS_HOOK_POSTINSTALL_ + /// MessageBox MB_OK "PostInstall" + /// !macroend + /// + /// !define NSIS_HOOK_PREUNINSTALL "NSIS_HOOK_PREUNINSTALL_" + /// !macro NSIS_HOOK_PREUNINSTALL_ + /// MessageBox MB_OK "PreUnInstall" + /// !macroend + /// + /// !define NSIS_HOOK_POSTUNINSTALL "NSIS_HOOK_POSTUNINSTALL_" + /// !macro NSIS_HOOK_POSTUNINSTALL_ + /// MessageBox MB_OK "PostUninstall" + /// !macroend + /// ``` + #[serde(alias = "installer-hooks")] + pub installer_hooks: Option, } /// Install Modes for the NSIS installer. diff --git a/tooling/bundler/src/bundle/settings.rs b/tooling/bundler/src/bundle/settings.rs index 31c06b898..445e50cfe 100644 --- a/tooling/bundler/src/bundle/settings.rs +++ b/tooling/bundler/src/bundle/settings.rs @@ -413,6 +413,40 @@ pub struct NsisSettings { pub display_language_selector: bool, /// Set compression algorithm used to compress files in the installer. pub compression: Option, + /// A path to a `.nsh` file that contains special NSIS macros to be hooked into the + /// main installer.nsi script. + /// + /// Supported 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. + /// + /// + /// ### Example + /// + /// ```nsh + /// !define NSIS_HOOK_PREINSTALL "NSIS_HOOK_PREINSTALL_" + /// !macro NSIS_HOOK_PREINSTALL_ + /// MessageBox MB_OK "PreInstall" + /// !macroend + /// + /// !define NSIS_HOOK_POSTINSTALL "NSIS_HOOK_POSTINSTALL_" + /// !macro NSIS_HOOK_POSTINSTALL_ + /// MessageBox MB_OK "PostInstall" + /// !macroend + /// + /// !define NSIS_HOOK_PREUNINSTALL "NSIS_HOOK_PREUNINSTALL_" + /// !macro NSIS_HOOK_PREUNINSTALL_ + /// MessageBox MB_OK "PreUnInstall" + /// !macroend + /// + /// !define NSIS_HOOK_POSTUNINSTALL "NSIS_HOOK_POSTUNINSTALL_" + /// !macro NSIS_HOOK_POSTUNINSTALL_ + /// MessageBox MB_OK "PostUninstall" + /// !macroend + /// ``` + pub installer_hooks: Option, } /// The Windows bundle settings. diff --git a/tooling/bundler/src/bundle/windows/nsis.rs b/tooling/bundler/src/bundle/windows/nsis.rs index 489a8a623..418d0de0f 100644 --- a/tooling/bundler/src/bundle/windows/nsis.rs +++ b/tooling/bundler/src/bundle/windows/nsis.rs @@ -255,6 +255,11 @@ fn build_nsis_app_installer( "display_language_selector", to_json(nsis.display_language_selector && languages.len() > 1), ); + + if let Some(installer_hooks) = &nsis.installer_hooks { + let installer_hooks = dunce::canonicalize(installer_hooks)?; + data.insert("installer_hooks", to_json(installer_hooks)); + } } data.insert( "install_mode", diff --git a/tooling/bundler/src/bundle/windows/templates/installer.nsi b/tooling/bundler/src/bundle/windows/templates/installer.nsi index 32beaba6a..c052667f5 100644 --- a/tooling/bundler/src/bundle/windows/templates/installer.nsi +++ b/tooling/bundler/src/bundle/windows/templates/installer.nsi @@ -13,12 +13,16 @@ ManifestDPIAware true !include WordFunc.nsh !include "utils.nsh" !include "FileAssociation.nsh" -!include "StrFunc.nsh" !include "Win\COM.nsh" !include "Win\Propkey.nsh" +!include "StrFunc.nsh" ${StrCase} ${StrLoc} +{{#if installer_hooks}} +!include "{{installer_hooks}}" +{{/if}} + !define MANUFACTURER "{{manufacturer}}" !define PRODUCTNAME "{{product_name}}" !define VERSION "{{version}}" @@ -505,6 +509,10 @@ Section Install !insertmacro CheckIfAppIsRunning + !ifdef NSIS_HOOK_PREINSTALL + !insertmacro "${NSIS_HOOK_PREINSTALL}" + !endif + ; Copy main executable File "${MAINBINARYSRCPATH}" @@ -580,6 +588,10 @@ Section Install ${EndIf} shortcuts_done: + !ifdef NSIS_HOOK_POSTINSTALL + !insertmacro "${NSIS_HOOK_POSTINSTALL}" + !endif + ; Auto close this page for passive mode ${IfThen} $PassiveMode == 1 ${|} SetAutoClose true ${|} SectionEnd @@ -615,6 +627,10 @@ Section Uninstall !insertmacro CheckIfAppIsRunning + !ifdef NSIS_HOOK_PREUNINSTALL + !insertmacro "${NSIS_HOOK_PREUNINSTALL}" + !endif + ; Delete the app directory and its content from disk ; Copy main executable Delete "$INSTDIR\${MAINBINARYNAME}.exe" @@ -688,6 +704,11 @@ Section Uninstall RmDir /r "$LOCALAPPDATA\${BUNDLEID}" ${EndIf} + !ifdef NSIS_HOOK_POSTUNINSTALL + !insertmacro "${NSIS_HOOK_POSTUNINSTALL}" + !endif + + ; Auto close if passive mode ${GetOptions} $CMDLINE "/P" $R0 IfErrors +2 0 SetAutoClose true diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 3ac4c5769..a21612215 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -2298,6 +2298,13 @@ "type": "null" } ] + }, + "installerHooks": { + "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 ```", + "type": [ + "string", + "null" + ] } }, "additionalProperties": false diff --git a/tooling/cli/src/helpers/config.rs b/tooling/cli/src/helpers/config.rs index a9c5ef666..525746264 100644 --- a/tooling/cli/src/helpers/config.rs +++ b/tooling/cli/src/helpers/config.rs @@ -105,6 +105,7 @@ pub fn nsis_settings(config: NsisConfig) -> tauri_bundler::NsisSettings { custom_language_files: config.custom_language_files, display_language_selector: config.display_language_selector, compression: config.compression, + installer_hooks: config.installer_hooks, } }