Skip to content

Commit

Permalink
feat(bundler/nsis): allow specifying custom template, closes #6887 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored May 12, 2023
1 parent c2b0dab commit e092f79
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
8 changes: 8 additions & 0 deletions .changes/nsis-custom-template.md
Original file line number Diff line number Diff line change
@@ -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 changes: 7 additions & 0 deletions core/tauri-config-schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
2 changes: 2 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 8 additions & 11 deletions tooling/bundler/src/bundle/windows/msi/wix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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())
Expand Down
17 changes: 13 additions & 4 deletions tooling/bundler/src/bundle/windows/nsis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/helpers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e092f79

Please sign in to comment.