From e092f799469ff32c7d1595d0f07d06fd2dab5c29 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Fri, 12 May 2023 14:02:07 +0300 Subject: [PATCH] feat(bundler/nsis): allow specifying custom template, closes #6887 (#6922) --- .changes/nsis-custom-template.md | 8 ++++++++ core/tauri-config-schema/schema.json | 7 +++++++ core/tauri-utils/src/config.rs | 2 ++ tooling/bundler/src/bundle/settings.rs | 2 ++ tooling/bundler/src/bundle/windows/msi/wix.rs | 19 ++++++++----------- tooling/bundler/src/bundle/windows/nsis.rs | 17 +++++++++++++---- tooling/cli/schema.json | 7 +++++++ tooling/cli/src/helpers/config.rs | 1 + 8 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 .changes/nsis-custom-template.md diff --git a/.changes/nsis-custom-template.md b/.changes/nsis-custom-template.md new file mode 100644 index 000000000000..d1f5c717a7c9 --- /dev/null +++ b/.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. diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 2fee51980fae..0296f6be7a4e 100644 --- a/core/tauri-config-schema/schema.json +++ b/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": [ diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index ad1b8f145575..82b701c29837 100644 --- a/core/tauri-utils/src/config.rs +++ b/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, /// The path to the license file to render on the installer. pub license: Option, /// The path to a bitmap file to display on the header of installers pages. diff --git a/tooling/bundler/src/bundle/settings.rs b/tooling/bundler/src/bundle/settings.rs index 825cb327f187..6abc05ca257e 100644 --- a/tooling/bundler/src/bundle/settings.rs +++ b/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, /// The path to the license file to render on the installer. pub license: Option, /// The path to a bitmap file to display on the header of installers pages. diff --git a/tooling/bundler/src/bundle/windows/msi/wix.rs b/tooling/bundler/src/bundle/windows/msi/wix.rs index e342ddd66fc1..d57bd587d691 100644 --- a/tooling/bundler/src/bundle/windows/msi/wix.rs +++ b/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()) diff --git a/tooling/bundler/src/bundle/windows/nsis.rs b/tooling/bundler/src/bundle/windows/nsis.rs index c5a88ae4b376..78e80ecdd309 100644 --- a/tooling/bundler/src/bundle/windows/nsis.rs +++ b/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, diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 2fee51980fae..0296f6be7a4e 100644 --- a/tooling/cli/schema.json +++ b/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": [ diff --git a/tooling/cli/src/helpers/config.rs b/tooling/cli/src/helpers/config.rs index 0b2fab01079f..82edb3fbe03b 100644 --- a/tooling/cli/src/helpers/config.rs +++ b/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,