Skip to content

Commit 2b814e9

Browse files
added cargo features to tauri config (#1824)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 9a662d2 commit 2b814e9

File tree

6 files changed

+63
-11
lines changed

6 files changed

+63
-11
lines changed

.changes/features-support.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Read cargo features from `tauri.conf.json > build > features` and propagate them on `dev` and `build`.

tooling/cli.rs/config_definition.rs

+3
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,8 @@ pub struct BuildConfig {
607607
pub before_dev_command: Option<String>,
608608
/// a shell command to run before `tauri build` kicks in
609609
pub before_build_command: Option<String>,
610+
/// features passed to `cargo` commands
611+
pub features: Option<Vec<String>>,
610612
/// Whether we should inject the Tauri API on `window.__TAURI__` or not.
611613
#[serde(default)]
612614
pub with_global_tauri: bool,
@@ -648,6 +650,7 @@ fn default_build() -> BuildConfig {
648650
dist_dir: default_dist_dir(),
649651
before_dev_command: None,
650652
before_build_command: None,
653+
features: None,
651654
with_global_tauri: false,
652655
}
653656
}

tooling/cli.rs/schema.json

+10
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@
234234
"default": "../dist",
235235
"type": "string"
236236
},
237+
"features": {
238+
"description": "features passed to `cargo` commands",
239+
"type": [
240+
"array",
241+
"null"
242+
],
243+
"items": {
244+
"type": "string"
245+
}
246+
},
237247
"runner": {
238248
"description": "The binary used to build and run the application.",
239249
"type": [

tooling/cli.rs/src/build.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ impl Build {
111111
.or(runner_from_config)
112112
.unwrap_or_else(|| "cargo".to_string());
113113

114-
rust::build_project(runner, &self.target, self.debug).with_context(|| "failed to build app")?;
114+
let cargo_features = &config_.build.features;
115+
116+
rust::build_project(runner, &self.target, cargo_features, self.debug)
117+
.with_context(|| "failed to build app")?;
115118

116119
let app_settings = rust::AppSettings::new(&config_)?;
117120

tooling/cli.rs/src/build/rust.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,30 @@ struct CargoConfig {
9090
build: Option<CargoBuildConfig>,
9191
}
9292

93-
pub fn build_project(runner: String, target: &Option<String>, debug: bool) -> crate::Result<()> {
94-
let mut args = vec!["build", "--features=custom-protocol"];
93+
pub fn build_project(
94+
runner: String,
95+
target: &Option<String>,
96+
features: &Option<Vec<String>>,
97+
debug: bool,
98+
) -> crate::Result<()> {
99+
let mut command = Command::new(&runner);
100+
command.args(["build", "--features=custom-protocol"]);
95101

96102
if let Some(target) = target {
97-
args.push("--target");
98-
args.push(target);
103+
command.arg("--target");
104+
command.arg(target);
105+
}
106+
107+
if let Some(features) = features {
108+
command.arg("--features");
109+
command.arg(features.join(","));
99110
}
100111

101112
if !debug {
102-
args.push("--release");
113+
command.arg("--release");
103114
}
104115

105-
let status = Command::new(&runner)
106-
.args(args)
116+
let status = command
107117
.status()
108118
.with_context(|| format!("failed to run {}", runner))?;
109119
if !status.success() {

tooling/cli.rs/src/dev.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,19 @@ impl Dev {
145145
}
146146
}
147147

148+
let cargo_features = config
149+
.lock()
150+
.unwrap()
151+
.as_ref()
152+
.unwrap()
153+
.build
154+
.features
155+
.clone();
156+
148157
let (child_wait_tx, child_wait_rx) = channel();
149158
let child_wait_rx = Arc::new(Mutex::new(child_wait_rx));
150159

151-
process = self.start_app(&runner, child_wait_rx.clone());
160+
process = self.start_app(&runner, &cargo_features, child_wait_rx.clone());
152161

153162
let (tx, rx) = channel();
154163

@@ -191,22 +200,34 @@ impl Dev {
191200
break;
192201
}
193202
}
194-
process = self.start_app(&runner, child_wait_rx.clone());
203+
process = self.start_app(&runner, &cargo_features, child_wait_rx.clone());
195204
}
196205
}
197206
}
198207
}
199208
}
200209

201-
fn start_app(&self, runner: &str, child_wait_rx: Arc<Mutex<Receiver<()>>>) -> Arc<SharedChild> {
210+
fn start_app(
211+
&self,
212+
runner: &str,
213+
features: &Option<Vec<String>>,
214+
child_wait_rx: Arc<Mutex<Receiver<()>>>,
215+
) -> Arc<SharedChild> {
202216
let mut command = Command::new(runner);
203217
command.args(&["run", "--no-default-features"]);
218+
204219
if let Some(target) = &self.target {
205220
command.args(&["--target", target]);
206221
}
222+
223+
if let Some(features) = features {
224+
command.args(&["--features", &features.join(",")]);
225+
}
226+
207227
if !self.args.is_empty() {
208228
command.arg("--").args(&self.args);
209229
}
230+
210231
let child =
211232
SharedChild::spawn(&mut command).unwrap_or_else(|_| panic!("failed to run {}", runner));
212233
let child_arc = Arc::new(child);

0 commit comments

Comments
 (0)