ソースを参照

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

* feat(nsis): support installer hooks

closes #9668

* update change files

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 1 年間 前
コミット
5462e5cadc

+ 6 - 0
.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`.
+

+ 5 - 0
.changes/utils-installer-hooks.md

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

+ 7 - 0
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

+ 35 - 0
core/tauri-utils/src/config.rs

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

+ 34 - 0
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<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.

+ 5 - 0
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",

+ 22 - 1
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

+ 7 - 0
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

+ 1 - 0
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,
   }
 }