Browse Source

feat(core): config for fixed webview2 runtime version path (#27)

Lucas Fernandes Nogueira 3 years ago
parent
commit
85df94f2b0

+ 7 - 0
.changes/fixed-webview2-runtime.md

@@ -0,0 +1,7 @@
+---
+"tauri": patch
+"tauri-utils": patch
+"cli.rs": patch
+---
+
+Allow using a fixed version for the Webview2 runtime via the `tauri > bundle > windows > webviewFixedRuntimePath` config option.

+ 22 - 1
core/tauri-utils/src/config.rs

@@ -181,6 +181,11 @@ pub struct WindowsConfig {
   pub certificate_thumbprint: Option<String>,
   /// Server to use during timestamping.
   pub timestamp_url: Option<String>,
+  /// Path to the webview fixed runtime to use.
+  ///
+  /// The fixed version can be downloaded [on the official website](https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section).
+  /// The `.cab` file must be extracted to a folder and this folder path must be defined on this field.
+  pub webview_fixed_runtime_path: Option<PathBuf>,
   /// Configuration for the MSI generated with WiX.
   pub wix: Option<WixConfig>,
 }
@@ -1931,6 +1936,22 @@ mod build {
     }
   }
 
+  impl ToTokens for WindowsConfig {
+    fn to_tokens(&self, tokens: &mut TokenStream) {
+      let webview_fixed_runtime_path = opt_lit(
+        self
+          .webview_fixed_runtime_path
+          .as_ref()
+          .map(path_buf_lit)
+          .as_ref(),
+      );
+      tokens.append_all(quote! { ::tauri::utils::config::WindowsConfig {
+        webview_fixed_runtime_path: #webview_fixed_runtime_path,
+        ..Default::default()
+      }})
+    }
+  }
+
   impl ToTokens for BundleConfig {
     fn to_tokens(&self, tokens: &mut TokenStream) {
       let identifier = str_lit(&self.identifier);
@@ -1945,7 +1966,7 @@ mod build {
       let deb = quote!(Default::default());
       let macos = quote!(Default::default());
       let external_bin = quote!(None);
-      let windows = quote!(Default::default());
+      let windows = &self.windows;
 
       literal_struct!(
         tokens,

+ 21 - 0
core/tauri/src/app.rs

@@ -1030,6 +1030,27 @@ impl<R: Runtime> Builder<R> {
     });
     app.manage(env);
 
+    #[cfg(windows)]
+    {
+      if let Some(w) = &app
+        .manager
+        .config()
+        .tauri
+        .bundle
+        .windows
+        .webview_fixed_runtime_path
+      {
+        if let Some(resource_dir) = app.path_resolver().resource_dir() {
+          std::env::set_var("WEBVIEW2_BROWSER_EXECUTABLE_FOLDER", resource_dir.join(w));
+        } else {
+          #[cfg(debug_assertions)]
+          eprintln!(
+            "failed to resolve resource directory; fallback to the installed Webview2 runtime."
+          );
+        }
+      }
+    }
+
     #[cfg(feature = "system-tray")]
     if let Some(system_tray) = self.system_tray {
       let mut ids = HashMap::new();

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

@@ -225,6 +225,8 @@ pub struct WindowsSettings {
   pub wix: Option<WixSettings>,
   /// The path to the application icon. Defaults to `./icons/icon.ico`.
   pub icon_path: PathBuf,
+  /// Path to the webview fixed runtime to use.
+  pub webview_fixed_runtime_path: Option<PathBuf>,
 }
 
 impl Default for WindowsSettings {
@@ -235,6 +237,7 @@ impl Default for WindowsSettings {
       timestamp_url: None,
       wix: None,
       icon_path: PathBuf::from("icons/icon.ico"),
+      webview_fixed_runtime_path: None,
     }
   }
 }

+ 4 - 2
tooling/bundler/src/bundle/windows/msi/wix.rs

@@ -518,7 +518,7 @@ pub fn build_wix_app_installer(
   let mut fragment_paths = Vec::new();
   let mut handlebars = Handlebars::new();
   let mut has_custom_template = false;
-  let mut install_webview = true;
+  let mut install_webview = settings.windows().webview_fixed_runtime_path.is_none();
   let mut enable_elevated_update_task = false;
 
   if let Some(wix) = &settings.windows().wix {
@@ -528,7 +528,9 @@ 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();
-    install_webview = !wix.skip_webview_install;
+    if wix.skip_webview_install {
+      install_webview = false;
+    }
     enable_elevated_update_task = wix.enable_elevated_update_task;
 
     if let Some(temp_path) = &wix.template {

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

@@ -150,6 +150,7 @@
             "certificateThumbprint": null,
             "digestAlgorithm": null,
             "timestampUrl": null,
+            "webviewFixedRuntimePath": null,
             "wix": null
           }
         },
@@ -581,6 +582,7 @@
             "certificateThumbprint": null,
             "digestAlgorithm": null,
             "timestampUrl": null,
+            "webviewFixedRuntimePath": null,
             "wix": null
           },
           "allOf": [
@@ -1439,6 +1441,7 @@
               "certificateThumbprint": null,
               "digestAlgorithm": null,
               "timestampUrl": null,
+              "webviewFixedRuntimePath": null,
               "wix": null
             }
           },
@@ -1872,6 +1875,13 @@
             "null"
           ]
         },
+        "webviewFixedRuntimePath": {
+          "description": "Path to the webview fixed runtime to use.\n\nThe fixed version can be downloaded [on the official website](https://developer.microsoft.com/en-us/microsoft-edge/webview2/#download-section). The `.cab` file must be extracted to a folder and this folder path must be defined on this field.",
+          "type": [
+            "string",
+            "null"
+          ]
+        },
         "wix": {
           "description": "Configuration for the MSI generated with WiX.",
           "anyOf": [

+ 8 - 0
tooling/cli.rs/src/interface/rust.rs

@@ -416,6 +416,13 @@ fn tauri_config_to_bundle_settings(
     depends.push("libgtk-3-0".to_string());
   }
 
+  #[cfg(windows)]
+  {
+    if let Some(webview_fixed_runtime_path) = &config.windows.webview_fixed_runtime_path {
+      resources.push(webview_fixed_runtime_path.display().to_string());
+    }
+  }
+
   let signing_identity = match std::env::var_os("APPLE_SIGNING_IDENTITY") {
     Some(signing_identity) => Some(
       signing_identity
@@ -481,6 +488,7 @@ fn tauri_config_to_bundle_settings(
         wix
       }),
       icon_path: windows_icon_path,
+      webview_fixed_runtime_path: config.windows.webview_fixed_runtime_path,
     },
     updater: Some(UpdaterSettings {
       active: updater_config.active,