From 21e710e1b0637978c1a9b00d5f033d8077159e79 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sat, 29 Feb 2020 16:49:45 -0300 Subject: [PATCH] =?UTF-8?q?feature(bundle)=20merge=20BundleSettings=20with?= =?UTF-8?q?=20config=20from=20tauri.conf=E2=80=A6=20(#471)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(bundler) read tauri config WIP * feat(bundler) merge BundleSettings with tauri.conf.json" * chore(lint) strings must use single quotes * chore(bundler) platform-specific config on separated object on tauri cfg * fix(eslint) unexpected trailing comma --- cli/tauri-bundler/Cargo.toml | 1 + cli/tauri-bundler/src/bundle.rs | 1 + cli/tauri-bundler/src/bundle/settings.rs | 41 +++++++++++- cli/tauri-bundler/src/bundle/tauri_config.rs | 70 ++++++++++++++++++++ cli/tauri-bundler/src/main.rs | 1 + cli/tauri.js/src/template/defaultConfig.ts | 18 ++++- cli/tauri.js/templates/src-tauri/Cargo.toml | 4 -- 7 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 cli/tauri-bundler/src/bundle/tauri_config.rs diff --git a/cli/tauri-bundler/Cargo.toml b/cli/tauri-bundler/Cargo.toml index 193a722c4..890e4b17c 100644 --- a/cli/tauri-bundler/Cargo.toml +++ b/cli/tauri-bundler/Cargo.toml @@ -23,6 +23,7 @@ libflate = "0.1" md5 = "0.7.0" msi = "0.2" +serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } strsim = "0.10.0" tar = "0.4" diff --git a/cli/tauri-bundler/src/bundle.rs b/cli/tauri-bundler/src/bundle.rs index e2535cdea..6d2b77916 100644 --- a/cli/tauri-bundler/src/bundle.rs +++ b/cli/tauri-bundler/src/bundle.rs @@ -14,6 +14,7 @@ mod path_utils; mod platform; mod rpm_bundle; mod settings; +mod tauri_config; #[cfg(target_os = "windows")] mod wix; diff --git a/cli/tauri-bundler/src/bundle/settings.rs b/cli/tauri-bundler/src/bundle/settings.rs index 356153dc8..1d20166cd 100644 --- a/cli/tauri-bundler/src/bundle/settings.rs +++ b/cli/tauri-bundler/src/bundle/settings.rs @@ -235,6 +235,12 @@ impl Settings { let bundle_settings = add_external_bin(bundle_settings)?; + let tauri_config = super::tauri_config::get(); + let merged_bundle_settings = match tauri_config { + Ok(config) => merge_settings(bundle_settings, config.tauri.bundle), + Err(_) => bundle_settings + }; + Ok(Settings { package, package_type, @@ -245,7 +251,7 @@ impl Settings { project_out_directory: target_dir, binary_path, binary_name, - bundle_settings, + bundle_settings: merged_bundle_settings, }) } @@ -584,6 +590,39 @@ fn add_external_bin(bundle_settings: BundleSettings) -> crate::Result(first: Option, second: Option) -> Option { + if first.is_some() { + first + } + else { + second + } +} + +fn merge_settings( + bundle_settings: BundleSettings, + config: crate::bundle::tauri_config::BundleConfig +) -> BundleSettings { + BundleSettings { + name: options_value(config.name, bundle_settings.name), + identifier: options_value(config.identifier, bundle_settings.identifier), + icon: options_value(config.icon, bundle_settings.icon), + version: options_value(config.version, bundle_settings.version), + resources: options_value(config.resources, bundle_settings.resources), + copyright: options_value(config.copyright, bundle_settings.copyright), + category: options_value(config.category, bundle_settings.category), + short_description: options_value(config.short_description, bundle_settings.short_description), + long_description: options_value(config.long_description, bundle_settings.long_description), + script: options_value(config.script, bundle_settings.script), + deb_depends: options_value(config.deb.depends, bundle_settings.deb_depends), + osx_frameworks: options_value(config.osx.frameworks, bundle_settings.osx_frameworks), + osx_minimum_system_version: options_value(config.osx.minimum_system_version, bundle_settings.osx_minimum_system_version), + external_bin: options_value(config.external_bin, bundle_settings.external_bin), + exception_domain: options_value(config.osx.exception_domain, bundle_settings.exception_domain), + ..bundle_settings + } +} + pub struct ResourcePaths<'a> { pattern_iter: std::slice::Iter<'a, String>, glob_iter: Option, diff --git a/cli/tauri-bundler/src/bundle/tauri_config.rs b/cli/tauri-bundler/src/bundle/tauri_config.rs new file mode 100644 index 000000000..28296f745 --- /dev/null +++ b/cli/tauri-bundler/src/bundle/tauri_config.rs @@ -0,0 +1,70 @@ +use serde::Deserialize; +use super::category::AppCategory; +use std::path::PathBuf; + +use std::fs; + +#[derive(PartialEq, Deserialize, Clone, Debug, Default)] +#[serde(tag = "deb", rename_all = "camelCase")] +pub struct DebConfig { + pub depends: Option>, +} + +#[derive(PartialEq, Deserialize, Clone, Debug, Default)] +#[serde(tag = "deb", rename_all = "camelCase")] +pub struct OsxConfig { + pub frameworks: Option>, + pub minimum_system_version: Option, + pub exception_domain: Option, +} + +#[derive(PartialEq, Deserialize, Clone, Debug, Default)] +#[serde(tag = "bundle", rename_all = "camelCase")] +pub struct BundleConfig { + pub name: Option, + pub identifier: Option, + pub icon: Option>, + pub version: Option, + pub resources: Option>, + pub copyright: Option, + pub category: Option, + pub short_description: Option, + pub long_description: Option, + pub script: Option, + #[serde(default)] + pub deb: DebConfig, + #[serde(default)] + pub osx: OsxConfig, + pub external_bin: Option>, +} + +#[derive(PartialEq, Deserialize, Clone, Debug, Default)] +#[serde(tag = "tauri", rename_all = "camelCase")] +pub struct TauriConfig { + #[serde(default)] + pub bundle: BundleConfig, +} + +#[derive(PartialEq, Deserialize, Clone, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Config { + #[serde(default)] + pub tauri: TauriConfig, +} + +pub fn get() -> crate::Result { + match std::env::var_os("TAURI_CONFIG") { + Some(config) => { + let json = &config.into_string().expect("failed to read TAURI_CONFIG"); + Ok(serde_json::from_str(json)?) + }, + None => match std::env::var_os("TAURI_DIR") { + Some(tauri_dir) => { + let tauri_dir_str = tauri_dir.into_string().expect("failed to read TAURI_DIR"); + let json = &fs::read_to_string(format!("{}{}", tauri_dir_str, "/tauri.conf.json"))?; + Ok(serde_json::from_str(json)?) + }, + None => Err(crate::Error::from("Couldn't get tauri config; please specify the TAURI_CONFIG or TAURI_DIR environment variables")) + } + } +} \ No newline at end of file diff --git a/cli/tauri-bundler/src/main.rs b/cli/tauri-bundler/src/main.rs index 54f353ce7..261fb9c8d 100644 --- a/cli/tauri-bundler/src/main.rs +++ b/cli/tauri-bundler/src/main.rs @@ -22,6 +22,7 @@ error_chain! { ConvertError(std::num::TryFromIntError); RegexError(::regex::Error) #[cfg(windows)]; HttpError(::attohttpc::Error) #[cfg(windows)]; + Json(::serde_json::error::Error); } errors {} } diff --git a/cli/tauri.js/src/template/defaultConfig.ts b/cli/tauri.js/src/template/defaultConfig.ts index f85bb9fb8..03643f697 100644 --- a/cli/tauri.js/src/template/defaultConfig.ts +++ b/cli/tauri.js/src/template/defaultConfig.ts @@ -9,7 +9,23 @@ export default { active: true }, bundle: { - active: true + active: true, + identifier: 'com.tauri.dev', + icon: ['icons/32x32.png', 'icons/128x128.png', 'icons/128x128@2x.png', 'icons/icon.icns', 'icons/icon.ico'], + resources: [], + externalBin: [], + copyright: '', + category: '', + shortDescription: '', + longDescription: '', + deb: { + depends: [] + }, + osx: { + frameworks: [], + minimumSystemVersion: '' + }, + exceptionDomain: '' }, whitelist: { all: true diff --git a/cli/tauri.js/templates/src-tauri/Cargo.toml b/cli/tauri.js/templates/src-tauri/Cargo.toml index 3debe2aa5..03b5cef2a 100755 --- a/cli/tauri.js/templates/src-tauri/Cargo.toml +++ b/cli/tauri.js/templates/src-tauri/Cargo.toml @@ -24,10 +24,6 @@ dev-server = [ "tauri/dev-server" ] embedded-server = [ "tauri/embedded-server" ] no-server = [ "tauri/no-server" ] -[package.metadata.bundle] -identifier = "com.tauri.dev" -icon = ["icons/32x32.png", "icons/128x128.png", "icons/128x128@2x.png", "icons/icon.icns", "icons/icon.ico"] - [[bin]] name = "app" path = "src/main.rs"