From 2b814e9c937489af0acb56051bd01c0d7fca2413 Mon Sep 17 00:00:00 2001 From: crapStone Date: Thu, 13 May 2021 22:01:15 +0200 Subject: [PATCH] added cargo features to tauri config (#1824) Co-authored-by: Lucas Nogueira --- .changes/features-support.md | 5 +++++ tooling/cli.rs/config_definition.rs | 3 +++ tooling/cli.rs/schema.json | 10 ++++++++++ tooling/cli.rs/src/build.rs | 5 ++++- tooling/cli.rs/src/build/rust.rs | 24 +++++++++++++++++------- tooling/cli.rs/src/dev.rs | 27 ++++++++++++++++++++++++--- 6 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 .changes/features-support.md diff --git a/.changes/features-support.md b/.changes/features-support.md new file mode 100644 index 000000000..c891bb3d0 --- /dev/null +++ b/.changes/features-support.md @@ -0,0 +1,5 @@ +--- +"cli.rs": patch +--- + +Read cargo features from `tauri.conf.json > build > features` and propagate them on `dev` and `build`. diff --git a/tooling/cli.rs/config_definition.rs b/tooling/cli.rs/config_definition.rs index ca0856de1..64ad594c4 100644 --- a/tooling/cli.rs/config_definition.rs +++ b/tooling/cli.rs/config_definition.rs @@ -607,6 +607,8 @@ pub struct BuildConfig { pub before_dev_command: Option, /// a shell command to run before `tauri build` kicks in pub before_build_command: Option, + /// features passed to `cargo` commands + pub features: Option>, /// Whether we should inject the Tauri API on `window.__TAURI__` or not. #[serde(default)] pub with_global_tauri: bool, @@ -648,6 +650,7 @@ fn default_build() -> BuildConfig { dist_dir: default_dist_dir(), before_dev_command: None, before_build_command: None, + features: None, with_global_tauri: false, } } diff --git a/tooling/cli.rs/schema.json b/tooling/cli.rs/schema.json index 44be9f9c7..9978dae62 100644 --- a/tooling/cli.rs/schema.json +++ b/tooling/cli.rs/schema.json @@ -234,6 +234,16 @@ "default": "../dist", "type": "string" }, + "features": { + "description": "features passed to `cargo` commands", + "type": [ + "array", + "null" + ], + "items": { + "type": "string" + } + }, "runner": { "description": "The binary used to build and run the application.", "type": [ diff --git a/tooling/cli.rs/src/build.rs b/tooling/cli.rs/src/build.rs index 45bda08e6..f5f25d8be 100644 --- a/tooling/cli.rs/src/build.rs +++ b/tooling/cli.rs/src/build.rs @@ -111,7 +111,10 @@ impl Build { .or(runner_from_config) .unwrap_or_else(|| "cargo".to_string()); - rust::build_project(runner, &self.target, self.debug).with_context(|| "failed to build app")?; + let cargo_features = &config_.build.features; + + rust::build_project(runner, &self.target, cargo_features, self.debug) + .with_context(|| "failed to build app")?; let app_settings = rust::AppSettings::new(&config_)?; diff --git a/tooling/cli.rs/src/build/rust.rs b/tooling/cli.rs/src/build/rust.rs index 6141c068c..c783d9544 100644 --- a/tooling/cli.rs/src/build/rust.rs +++ b/tooling/cli.rs/src/build/rust.rs @@ -90,20 +90,30 @@ struct CargoConfig { build: Option, } -pub fn build_project(runner: String, target: &Option, debug: bool) -> crate::Result<()> { - let mut args = vec!["build", "--features=custom-protocol"]; +pub fn build_project( + runner: String, + target: &Option, + features: &Option>, + debug: bool, +) -> crate::Result<()> { + let mut command = Command::new(&runner); + command.args(["build", "--features=custom-protocol"]); if let Some(target) = target { - args.push("--target"); - args.push(target); + command.arg("--target"); + command.arg(target); + } + + if let Some(features) = features { + command.arg("--features"); + command.arg(features.join(",")); } if !debug { - args.push("--release"); + command.arg("--release"); } - let status = Command::new(&runner) - .args(args) + let status = command .status() .with_context(|| format!("failed to run {}", runner))?; if !status.success() { diff --git a/tooling/cli.rs/src/dev.rs b/tooling/cli.rs/src/dev.rs index ea8ab1af4..4be7c376d 100644 --- a/tooling/cli.rs/src/dev.rs +++ b/tooling/cli.rs/src/dev.rs @@ -145,10 +145,19 @@ impl Dev { } } + let cargo_features = config + .lock() + .unwrap() + .as_ref() + .unwrap() + .build + .features + .clone(); + let (child_wait_tx, child_wait_rx) = channel(); let child_wait_rx = Arc::new(Mutex::new(child_wait_rx)); - process = self.start_app(&runner, child_wait_rx.clone()); + process = self.start_app(&runner, &cargo_features, child_wait_rx.clone()); let (tx, rx) = channel(); @@ -191,22 +200,34 @@ impl Dev { break; } } - process = self.start_app(&runner, child_wait_rx.clone()); + process = self.start_app(&runner, &cargo_features, child_wait_rx.clone()); } } } } } - fn start_app(&self, runner: &str, child_wait_rx: Arc>>) -> Arc { + fn start_app( + &self, + runner: &str, + features: &Option>, + child_wait_rx: Arc>>, + ) -> Arc { let mut command = Command::new(runner); command.args(&["run", "--no-default-features"]); + if let Some(target) = &self.target { command.args(&["--target", target]); } + + if let Some(features) = features { + command.args(&["--features", &features.join(",")]); + } + if !self.args.is_empty() { command.arg("--").args(&self.args); } + let child = SharedChild::spawn(&mut command).unwrap_or_else(|_| panic!("failed to run {}", runner)); let child_arc = Arc::new(child);