Browse Source

feat(bundler/nsis): allow specifying custom template, closes #6887 (#6922)

Amr Bashir 2 years ago
parent
commit
e092f79946

+ 8 - 0
.changes/nsis-custom-template.md

@@ -0,0 +1,8 @@
+---
+'tauri-utils': 'minor'
+'tauri-bundler': 'minor'
+'cli.rs': 'minor'
+'cli.js': 'minor'
+---
+
+Add `nsis > template` option to specify custom NSIS installer template.

+ 7 - 0
core/tauri-config-schema/schema.json

@@ -1661,6 +1661,13 @@
       "description": "Configuration for the Installer bundle using NSIS.",
       "type": "object",
       "properties": {
+        "template": {
+          "description": "A custom .nsi template to use.",
+          "type": [
+            "string",
+            "null"
+          ]
+        },
         "license": {
           "description": "The path to the license file to render on the installer.",
           "type": [

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

@@ -435,6 +435,8 @@ pub struct WixConfig {
 #[cfg_attr(feature = "schema", derive(JsonSchema))]
 #[serde(rename_all = "camelCase", deny_unknown_fields)]
 pub struct NsisConfig {
+  /// A custom .nsi template to use.
+  pub template: Option<PathBuf>,
   /// The path to the license file to render on the installer.
   pub license: Option<PathBuf>,
   /// The path to a bitmap file to display on the header of installers pages.

+ 2 - 0
tooling/bundler/src/bundle/settings.rs

@@ -252,6 +252,8 @@ pub struct WixSettings {
 /// Settings specific to the NSIS implementation.
 #[derive(Clone, Debug, Default)]
 pub struct NsisSettings {
+  /// A custom .nsi template to use.
+  pub template: Option<PathBuf>,
   /// The path to the license file to render on the installer.
   pub license: Option<PathBuf>,
   /// The path to a bitmap file to display on the header of installers pages.

+ 8 - 11
tooling/bundler/src/bundle/windows/msi/wix.rs

@@ -615,7 +615,7 @@ pub fn build_wix_app_installer(
   let mut fragment_paths = Vec::new();
   let mut handlebars = Handlebars::new();
   handlebars.register_escape_fn(handlebars::no_escape);
-  let mut has_custom_template = false;
+  let mut custom_template_path = None;
   let mut enable_elevated_update_task = false;
 
   if let Some(wix) = &settings.windows().wix {
@@ -626,15 +626,7 @@ pub fn build_wix_app_installer(
     data.insert("merge_refs", to_json(&wix.merge_refs));
     fragment_paths = wix.fragment_paths.clone();
     enable_elevated_update_task = wix.enable_elevated_update_task;
-
-    if let Some(temp_path) = &wix.template {
-      let template = read_to_string(temp_path)?;
-      handlebars
-        .register_template_string("main.wxs", &template)
-        .map_err(|e| e.to_string())
-        .expect("Failed to setup custom handlebar template");
-      has_custom_template = true;
-    }
+    custom_template_path = wix.template.clone();
 
     if let Some(banner_path) = &wix.banner_path {
       let filename = banner_path
@@ -661,7 +653,12 @@ pub fn build_wix_app_installer(
     }
   }
 
-  if !has_custom_template {
+  if let Some(path) = custom_template_path {
+    handlebars
+      .register_template_string("main.wxs", read_to_string(path)?)
+      .map_err(|e| e.to_string())
+      .expect("Failed to setup custom handlebar template");
+  } else {
     handlebars
       .register_template_string("main.wxs", include_str!("../templates/main.wxs"))
       .map_err(|e| e.to_string())

+ 13 - 4
tooling/bundler/src/bundle/windows/nsis.rs

@@ -188,7 +188,9 @@ fn build_nsis_app_installer(
 
   let mut install_mode = NSISInstallerMode::CurrentUser;
   let mut languages = vec!["English".into()];
+  let mut custom_template_path = None;
   if let Some(nsis) = &settings.windows().nsis {
+    custom_template_path = nsis.template.clone();
     install_mode = nsis.install_mode;
     if let Some(langs) = &nsis.languages {
       languages.clear();
@@ -359,10 +361,17 @@ fn build_nsis_app_installer(
     }
     output
   });
-  handlebars
-    .register_template_string("installer.nsi", include_str!("./templates/installer.nsi"))
-    .map_err(|e| e.to_string())
-    .expect("Failed to setup handlebar template");
+  if let Some(path) = custom_template_path {
+    handlebars
+      .register_template_string("installer.nsi", std::fs::read_to_string(path)?)
+      .map_err(|e| e.to_string())
+      .expect("Failed to setup custom handlebar template");
+  } else {
+    handlebars
+      .register_template_string("installer.nsi", include_str!("./templates/installer.nsi"))
+      .map_err(|e| e.to_string())
+      .expect("Failed to setup handlebar template");
+  }
   let installer_nsi_path = output_path.join("installer.nsi");
   write(
     &installer_nsi_path,

+ 7 - 0
tooling/cli/schema.json

@@ -1661,6 +1661,13 @@
       "description": "Configuration for the Installer bundle using NSIS.",
       "type": "object",
       "properties": {
+        "template": {
+          "description": "A custom .nsi template to use.",
+          "type": [
+            "string",
+            "null"
+          ]
+        },
         "license": {
           "description": "The path to the license file to render on the installer.",
           "type": [

+ 1 - 0
tooling/cli/src/helpers/config.rs

@@ -99,6 +99,7 @@ pub fn wix_settings(config: WixConfig) -> tauri_bundler::WixSettings {
 
 pub fn nsis_settings(config: NsisConfig) -> tauri_bundler::NsisSettings {
   tauri_bundler::NsisSettings {
+    template: config.template,
     license: config.license,
     header_image: config.header_image,
     sidebar_image: config.sidebar_image,