feat(nsis): support installer hooks (#9731)

* feat(nsis): support installer hooks

closes #9668

* update change files

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2024-05-25 16:23:08 +03:00 committed by GitHub
parent 418d72d72d
commit 5462e5cadc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 122 additions and 1 deletions

View File

@ -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`.

View File

@ -0,0 +1,5 @@
---
"tauri-utils": "patch:feat"
---
Add `installer_hooks` NSIS configuration field

View File

@ -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

View File

@ -733,6 +733,41 @@ pub struct NsisConfig {
///
/// See <https://nsis.sourceforge.io/Reference/SetCompressor>
pub compression: Option<NsisCompression>,
/// 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<PathBuf>,
}
/// Install Modes for the NSIS installer.

View File

@ -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<NsisCompression>,
/// 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<PathBuf>,
}
/// The Windows bundle settings.

View File

@ -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",

View File

@ -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

View File

@ -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

View File

@ -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,
}
}