From efbf236f35a1dd4064c0615b56881e4132a249f4 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 30 Dec 2021 11:28:41 -0300 Subject: [PATCH] refactor(core): make `zstd` optional enabled by default (#3133) --- core/tauri-codegen/Cargo.toml | 6 +++++- core/tauri-codegen/src/embedded_assets.rs | 23 +++++++++++++++++++---- core/tauri-macros/Cargo.toml | 3 ++- core/tauri-runtime-wry/src/lib.rs | 5 +---- core/tauri-utils/Cargo.toml | 3 ++- core/tauri-utils/src/assets.rs | 10 ++++++++++ core/tauri/Cargo.toml | 6 ++++-- 7 files changed, 43 insertions(+), 13 deletions(-) diff --git a/core/tauri-codegen/Cargo.toml b/core/tauri-codegen/Cargo.toml index 57ebec6a9..b378844b3 100644 --- a/core/tauri-codegen/Cargo.toml +++ b/core/tauri-codegen/Cargo.toml @@ -21,6 +21,10 @@ serde_json = "1" tauri-utils = { version = "1.0.0-beta.3", path = "../tauri-utils", features = [ "build" ] } thiserror = "1" walkdir = "2" -zstd = "0.9" +zstd = { version = "0.9", optional = true } kuchiki = "0.8" regex = "1" + +[features] +default = [ "compression" ] +compression = [ "zstd", "tauri-utils/compression" ] diff --git a/core/tauri-codegen/src/embedded_assets.rs b/core/tauri-codegen/src/embedded_assets.rs index 7a3bafb3f..05d7c7b54 100644 --- a/core/tauri-codegen/src/embedded_assets.rs +++ b/core/tauri-codegen/src/embedded_assets.rs @@ -155,6 +155,7 @@ impl EmbeddedAssets { } /// Use highest compression level for release, the fastest one for everything else + #[cfg(feature = "compression")] fn compression_level() -> i32 { let levels = zstd::compression_level_range(); if cfg!(debug_assertions) { @@ -259,11 +260,25 @@ impl EmbeddedAssets { // only compress and write to the file if it doesn't already exist. if !out_path.exists() { - let out_file = File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite { - path: out_path.clone(), - error, - })?; + #[allow(unused_mut)] + let mut out_file = + File::create(&out_path).map_err(|error| EmbeddedAssetsError::AssetWrite { + path: out_path.clone(), + error, + })?; + #[cfg(not(feature = "compression"))] + { + use std::io::Write; + out_file + .write_all(&input) + .map_err(|error| EmbeddedAssetsError::AssetWrite { + path: path.to_owned(), + error, + })?; + } + + #[cfg(feature = "compression")] // entirely write input to the output file path with compression zstd::stream::copy_encode(&*input, out_file, Self::compression_level()).map_err(|error| { EmbeddedAssetsError::AssetWrite { diff --git a/core/tauri-macros/Cargo.toml b/core/tauri-macros/Cargo.toml index 260c9555d..3b32f645f 100644 --- a/core/tauri-macros/Cargo.toml +++ b/core/tauri-macros/Cargo.toml @@ -19,7 +19,8 @@ proc-macro = true proc-macro2 = "1" quote = "1" syn = { version = "1", features = [ "full" ] } -tauri-codegen = { version = "1.0.0-beta.4", path = "../tauri-codegen" } +tauri-codegen = { version = "1.0.0-beta.4", default-features = false, path = "../tauri-codegen" } [features] custom-protocol = [ ] +compression = [ "tauri-codegen/compression" ] diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index fbb4be733..d5b31233e 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -28,10 +28,7 @@ use tauri_runtime::{SystemTray, SystemTrayEvent}; #[cfg(windows)] use webview2_com::FocusChangedEventHandler; #[cfg(windows)] -use windows::Win32::{ - Foundation::HWND, - System::WinRT::EventRegistrationToken, -}; +use windows::Win32::{Foundation::HWND, System::WinRT::EventRegistrationToken}; #[cfg(all(feature = "system-tray", target_os = "macos"))] use wry::application::platform::macos::{SystemTrayBuilderExtMacOS, SystemTrayExtMacOS}; #[cfg(target_os = "linux")] diff --git a/core/tauri-utils/Cargo.toml b/core/tauri-utils/Cargo.toml index 22a3ec9b6..5cf07c7fd 100644 --- a/core/tauri-utils/Cargo.toml +++ b/core/tauri-utils/Cargo.toml @@ -16,7 +16,7 @@ serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" thiserror = "1.0.30" phf = { version = "0.10", features = [ "macros" ] } -zstd = "0.9" +zstd = { version = "0.9", optional = true } url = { version = "2.2", features = [ "serde" ] } kuchiki = "0.8" html5ever = "0.25" @@ -28,3 +28,4 @@ heck = "0.4" [features] build = [ "proc-macro2", "quote" ] +compression = [ "zstd" ] diff --git a/core/tauri-utils/src/assets.rs b/core/tauri-utils/src/assets.rs index 6b784c890..c8755942a 100644 --- a/core/tauri-utils/src/assets.rs +++ b/core/tauri-utils/src/assets.rs @@ -94,6 +94,7 @@ impl EmbeddedAssets { } impl Assets for EmbeddedAssets { + #[cfg(feature = "compression")] fn get(&self, key: &AssetKey) -> Option> { self .0 @@ -103,4 +104,13 @@ impl Assets for EmbeddedAssets { .and_then(Result::ok) .map(Cow::Owned) } + + #[cfg(not(feature = "compression"))] + fn get(&self, key: &AssetKey) -> Option> { + self + .0 + .get(key.as_ref()) + .copied() + .map(|a| Cow::Owned(a.to_vec())) + } } diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 8d0a9c6bb..74d054500 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -19,7 +19,8 @@ exclude = [ readme = "README.md" [package.metadata.docs.rs] -all-features = true +default-features = false +features = ["wry", "custom-protocol", "api-all", "cli", "updater", "system-tray", "dox"] rustdoc-args = [ "--cfg", "doc_cfg" ] default-target = "x86_64-unknown-linux-gnu" targets = [ @@ -95,7 +96,8 @@ tokio = { version = "1.15", features = [ "full" ] } mockito = "0.30" [features] -default = [ "wry" ] +default = [ "wry", "compression" ] +compression = [ "tauri-macros/compression", "tauri-utils/compression" ] dox = [ "tauri-runtime-wry/dox" ] wry = [ "tauri-runtime-wry" ] cli = [ "clap" ]