浏览代码

feat: #1528 wix supports custom templates (#1529)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Downtime 4 年之前
父节点
当前提交
ebe755ac5c

+ 5 - 0
.changes/supports-custom-template.md

@@ -0,0 +1,5 @@
+---
+"tauri-bundler": minor
+---
+
+Adds support to custom WiX template.

+ 0 - 1
tooling/bundler/Cargo.toml

@@ -32,7 +32,6 @@ tar = "0.4"
 termcolor = "1.1.2"
 toml = "0.5.8"
 walkdir = "2"
-lazy_static = { version = "1.4" }
 handlebars = { version = "3.5" }
 zip = { version = "0.5" }
 tempfile = "3.2.0"

+ 5 - 14
tooling/bundler/src/bundle/appimage_bundle.rs

@@ -6,7 +6,6 @@ use super::{common, deb_bundle, path_utils};
 use crate::Settings;
 
 use handlebars::Handlebars;
-use lazy_static::lazy_static;
 
 use std::{
   collections::BTreeMap,
@@ -15,18 +14,6 @@ use std::{
   process::{Command, Stdio},
 };
 
-// Create handlebars template for shell script
-lazy_static! {
-  static ref HANDLEBARS: Handlebars<'static> = {
-    let mut handlebars = Handlebars::new();
-
-    handlebars
-      .register_template_string("appimage", include_str!("templates/appimage"))
-      .expect("Failed to register template for handlebars");
-    handlebars
-  };
-}
-
 /// Bundles the project.
 /// Returns a vector of PathBuf that shows where the AppImage was created.
 pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
@@ -86,7 +73,11 @@ pub fn bundle_project(settings: &Settings) -> crate::Result<Vec<PathBuf>> {
   sh_map.insert("icon_path", &larger_icon_path);
 
   // initialize shell script template.
-  let temp = HANDLEBARS.render("appimage", &sh_map)?;
+  let mut handlebars = Handlebars::new();
+  handlebars
+    .register_template_string("appimage", include_str!("templates/appimage"))
+    .expect("Failed to register template for handlebars");
+  let temp = handlebars.render("appimage", &sh_map)?;
 
   // create the shell script file in the target/ folder.
   let sh_file = output_path.join("build_appimage.sh");

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

@@ -169,6 +169,9 @@ pub struct MacOsSettings {
 #[derive(Clone, Debug, Deserialize, Default)]
 #[serde(rename_all = "camelCase")]
 pub struct WixSettings {
+  /// By default, the bundler uses an internal template.
+  /// This option allows you to define your own wix file.
+  pub template: Option<PathBuf>,
   #[serde(default)]
   pub fragment_paths: Vec<PathBuf>,
   #[serde(default)]

+ 18 - 16
tooling/bundler/src/bundle/wix.rs

@@ -9,7 +9,6 @@ use super::{
 };
 
 use handlebars::{to_json, Handlebars};
-use lazy_static::lazy_static;
 use regex::Regex;
 use serde::Serialize;
 use sha2::Digest;
@@ -55,19 +54,6 @@ const UUID_NAMESPACE: [u8; 16] = [
   0xfd, 0x85, 0x95, 0xa8, 0x17, 0xa3, 0x47, 0x4e, 0xa6, 0x16, 0x76, 0x14, 0x8d, 0xfa, 0x0c, 0x7b,
 ];
 
-// setup for the main.wxs template file using handlebars. Dynamically changes the template on compilation based on the application metadata.
-lazy_static! {
-  static ref HANDLEBARS: Handlebars<'static> = {
-    let mut handlebars = Handlebars::new();
-
-    handlebars
-      .register_template_string("main.wxs", include_str!("templates/main.wxs"))
-      .map_err(|e| e.to_string())
-      .expect("Failed to setup handlebar template");
-    handlebars
-  };
-}
-
 /// Mapper between a resource directory name and its ResourceDirectory descriptor.
 type ResourceMap = BTreeMap<String, ResourceDirectory>;
 
@@ -478,6 +464,8 @@ pub fn build_wix_app_installer(
   data.insert("icon_path", to_json(icon_path));
 
   let mut fragment_paths = Vec::new();
+  let mut handlebars = Handlebars::new();
+  let mut has_custom_template = false;
 
   if let Some(wix) = &settings.windows().wix {
     data.insert("component_group_refs", to_json(&wix.component_group_refs));
@@ -486,9 +474,23 @@ pub fn build_wix_app_installer(
     data.insert("feature_refs", to_json(&wix.feature_refs));
     data.insert("merge_refs", to_json(&wix.merge_refs));
     fragment_paths = wix.fragment_paths.clone();
+
+    if let Some(temp_path) = &wix.template {
+      let template = std::fs::read_to_string(temp_path)?;
+      handlebars
+        .register_template_string("main.wxs", &template)
+        .or_else(|e| Err(e.to_string()))
+        .expect("Failed to setup custom handlebar template");
+      has_custom_template = true;
+    }
   }
 
-  let temp = HANDLEBARS.render("main.wxs", &data)?;
+  if !has_custom_template {
+    handlebars
+      .register_template_string("main.wxs", include_str!("templates/main.wxs"))
+      .map_err(|e| e.to_string())
+      .expect("Failed to setup handlebar template");
+  };
 
   if output_path.exists() {
     remove_dir_all(&output_path)?;
@@ -497,7 +499,7 @@ pub fn build_wix_app_installer(
   create_dir_all(&output_path)?;
 
   let main_wxs_path = output_path.join("main.wxs");
-  write(&main_wxs_path, temp)?;
+  write(&main_wxs_path, handlebars.render("main.wxs", &data)?)?;
 
   let mut candle_inputs = vec!["main.wxs".into()];
 

+ 0 - 1
tooling/cli.rs/Cargo.lock

@@ -2005,7 +2005,6 @@ dependencies = [
  "hex",
  "icns",
  "image",
- "lazy_static",
  "libflate",
  "md5",
  "regex",

+ 1 - 0
tooling/cli.rs/config_definition.rs

@@ -44,6 +44,7 @@ pub struct MacConfig {
 #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize, JsonSchema)]
 #[serde(rename_all = "camelCase", deny_unknown_fields)]
 pub struct WixConfig {
+  pub template: Option<PathBuf>,
   #[serde(default)]
   pub fragment_paths: Vec<PathBuf>,
   #[serde(default)]

+ 6 - 0
tooling/cli.rs/schema.json

@@ -1220,6 +1220,12 @@
           "items": {
             "type": "string"
           }
+        },
+        "template": {
+          "type": [
+            "string",
+            "null"
+          ]
         }
       },
       "additionalProperties": false

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

@@ -16,6 +16,7 @@ pub use config_definition::*;
 impl From<WixConfig> for tauri_bundler::WixSettings {
   fn from(config: WixConfig) -> tauri_bundler::WixSettings {
     tauri_bundler::WixSettings {
+      template: config.template,
       fragment_paths: config.fragment_paths,
       component_group_refs: config.component_group_refs,
       component_refs: config.component_refs,