Skip to content

Commit ebe755a

Browse files
feat: #1528 wix supports custom templates (#1529)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent f35330c commit ebe755a

File tree

9 files changed

+39
-32
lines changed

9 files changed

+39
-32
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-bundler": minor
3+
---
4+
5+
Adds support to custom WiX template.

tooling/bundler/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ tar = "0.4"
3232
termcolor = "1.1.2"
3333
toml = "0.5.8"
3434
walkdir = "2"
35-
lazy_static = { version = "1.4" }
3635
handlebars = { version = "3.5" }
3736
zip = { version = "0.5" }
3837
tempfile = "3.2.0"

tooling/bundler/src/bundle/appimage_bundle.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use super::{common, deb_bundle, path_utils};
66
use crate::Settings;
77

88
use handlebars::Handlebars;
9-
use lazy_static::lazy_static;
109

1110
use std::{
1211
collections::BTreeMap,
@@ -15,18 +14,6 @@ use std::{
1514
process::{Command, Stdio},
1615
};
1716

18-
// Create handlebars template for shell script
19-
lazy_static! {
20-
static ref HANDLEBARS: Handlebars<'static> = {
21-
let mut handlebars = Handlebars::new();
22-
23-
handlebars
24-
.register_template_string("appimage", include_str!("templates/appimage"))
25-
.expect("Failed to register template for handlebars");
26-
handlebars
27-
};
28-
}
29-
3017
/// Bundles the project.
3118
/// Returns a vector of PathBuf that shows where the AppImage was created.
3219
pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
@@ -86,7 +73,11 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
8673
sh_map.insert("icon_path", &larger_icon_path);
8774

8875
// initialize shell script template.
89-
let temp = HANDLEBARS.render("appimage", &sh_map)?;
76+
let mut handlebars = Handlebars::new();
77+
handlebars
78+
.register_template_string("appimage", include_str!("templates/appimage"))
79+
.expect("Failed to register template for handlebars");
80+
let temp = handlebars.render("appimage", &sh_map)?;
9081

9182
// create the shell script file in the target/ folder.
9283
let sh_file = output_path.join("build_appimage.sh");

tooling/bundler/src/bundle/settings.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ pub struct MacOsSettings {
169169
#[derive(Clone, Debug, Deserialize, Default)]
170170
#[serde(rename_all = "camelCase")]
171171
pub struct WixSettings {
172+
/// By default, the bundler uses an internal template.
173+
/// This option allows you to define your own wix file.
174+
pub template: Option<PathBuf>,
172175
#[serde(default)]
173176
pub fragment_paths: Vec<PathBuf>,
174177
#[serde(default)]

tooling/bundler/src/bundle/wix.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use super::{
99
};
1010

1111
use handlebars::{to_json, Handlebars};
12-
use lazy_static::lazy_static;
1312
use regex::Regex;
1413
use serde::Serialize;
1514
use sha2::Digest;
@@ -55,19 +54,6 @@ const UUID_NAMESPACE: [u8; 16] = [
5554
0xfd, 0x85, 0x95, 0xa8, 0x17, 0xa3, 0x47, 0x4e, 0xa6, 0x16, 0x76, 0x14, 0x8d, 0xfa, 0x0c, 0x7b,
5655
];
5756

58-
// setup for the main.wxs template file using handlebars. Dynamically changes the template on compilation based on the application metadata.
59-
lazy_static! {
60-
static ref HANDLEBARS: Handlebars<'static> = {
61-
let mut handlebars = Handlebars::new();
62-
63-
handlebars
64-
.register_template_string("main.wxs", include_str!("templates/main.wxs"))
65-
.map_err(|e| e.to_string())
66-
.expect("Failed to setup handlebar template");
67-
handlebars
68-
};
69-
}
70-
7157
/// Mapper between a resource directory name and its ResourceDirectory descriptor.
7258
type ResourceMap = BTreeMap<String, ResourceDirectory>;
7359

@@ -478,6 +464,8 @@ pub fn build_wix_app_installer(
478464
data.insert("icon_path", to_json(icon_path));
479465

480466
let mut fragment_paths = Vec::new();
467+
let mut handlebars = Handlebars::new();
468+
let mut has_custom_template = false;
481469

482470
if let Some(wix) = &settings.windows().wix {
483471
data.insert("component_group_refs", to_json(&wix.component_group_refs));
@@ -486,9 +474,23 @@ pub fn build_wix_app_installer(
486474
data.insert("feature_refs", to_json(&wix.feature_refs));
487475
data.insert("merge_refs", to_json(&wix.merge_refs));
488476
fragment_paths = wix.fragment_paths.clone();
477+
478+
if let Some(temp_path) = &wix.template {
479+
let template = std::fs::read_to_string(temp_path)?;
480+
handlebars
481+
.register_template_string("main.wxs", &template)
482+
.or_else(|e| Err(e.to_string()))
483+
.expect("Failed to setup custom handlebar template");
484+
has_custom_template = true;
485+
}
489486
}
490487

491-
let temp = HANDLEBARS.render("main.wxs", &data)?;
488+
if !has_custom_template {
489+
handlebars
490+
.register_template_string("main.wxs", include_str!("templates/main.wxs"))
491+
.map_err(|e| e.to_string())
492+
.expect("Failed to setup handlebar template");
493+
};
492494

493495
if output_path.exists() {
494496
remove_dir_all(&output_path)?;
@@ -497,7 +499,7 @@ pub fn build_wix_app_installer(
497499
create_dir_all(&output_path)?;
498500

499501
let main_wxs_path = output_path.join("main.wxs");
500-
write(&main_wxs_path, temp)?;
502+
write(&main_wxs_path, handlebars.render("main.wxs", &data)?)?;
501503

502504
let mut candle_inputs = vec!["main.wxs".into()];
503505

tooling/cli.rs/Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tooling/cli.rs/config_definition.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct MacConfig {
4444
#[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
4545
#[serde(rename_all = "camelCase", deny_unknown_fields)]
4646
pub struct WixConfig {
47+
pub template: Option<PathBuf>,
4748
#[serde(default)]
4849
pub fragment_paths: Vec<PathBuf>,
4950
#[serde(default)]

tooling/cli.rs/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,12 @@
12201220
"items": {
12211221
"type": "string"
12221222
}
1223+
},
1224+
"template": {
1225+
"type": [
1226+
"string",
1227+
"null"
1228+
]
12231229
}
12241230
},
12251231
"additionalProperties": false

tooling/cli.rs/src/helpers/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub use config_definition::*;
1616
impl From<WixConfig> for tauri_bundler::WixSettings {
1717
fn from(config: WixConfig) -> tauri_bundler::WixSettings {
1818
tauri_bundler::WixSettings {
19+
template: config.template,
1920
fragment_paths: config.fragment_paths,
2021
component_group_refs: config.component_group_refs,
2122
component_refs: config.component_refs,

0 commit comments

Comments
 (0)