Skip to content

Commit 679fe1f

Browse files
feat(cli.rs): allow passing arguments to the build runner, closes #3398 (#3431)
Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
1 parent 102a5e9 commit 679fe1f

File tree

5 files changed

+46
-36
lines changed

5 files changed

+46
-36
lines changed

.changes/cli-runner-args.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
Allow passing arguments to the `build` runner (`tauri build -- <ARGS>...`).

.changes/refactor-cli-dev-args.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"cli.rs": patch
3+
---
4+
5+
**Breaking change:** The extra arguments passed to `tauri dev` using `-- <ARGS>...` are now propagated to the runner (defaults to cargo). To pass arguments to your binary using Cargo, you now need to run `tauri dev -- -- <ARGS-TO-YOUR-BINARY>...` (notice the double `--`).

tooling/cli/src/build.rs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use tauri_bundler::bundle::{bundle_project, PackageType};
2121
#[derive(Debug, Parser)]
2222
#[clap(about = "Tauri build")]
2323
pub struct Options {
24-
/// Binary to use to build the application
24+
/// Binary to use to build the application, defaults to `cargo`
2525
#[clap(short, long)]
2626
runner: Option<String>,
2727
/// Builds with the debug flag
@@ -44,6 +44,8 @@ pub struct Options {
4444
/// JSON string or path to JSON file to merge with tauri.conf.json
4545
#[clap(short, long)]
4646
config: Option<String>,
47+
/// Command line arguments passed to the runner
48+
args: Vec<String>,
4749
}
4850

4951
pub fn command(options: Options) -> Result<()> {
@@ -136,9 +138,23 @@ pub fn command(options: Options) -> Result<()> {
136138
.or(runner_from_config)
137139
.unwrap_or_else(|| "cargo".to_string());
138140

139-
let mut cargo_features = config_.build.features.clone().unwrap_or_default();
140-
if let Some(features) = options.features {
141-
cargo_features.extend(features);
141+
let mut features = config_.build.features.clone().unwrap_or_default();
142+
if let Some(list) = options.features {
143+
features.extend(list);
144+
}
145+
146+
let mut args = Vec::new();
147+
if !options.args.is_empty() {
148+
args.extend(options.args);
149+
}
150+
151+
if !features.is_empty() {
152+
args.push("--features".into());
153+
args.push(features.join(","));
154+
}
155+
156+
if !options.debug {
157+
args.push("--release".into());
142158
}
143159

144160
let app_settings = crate::interface::rust::AppSettings::new(config_)?;
@@ -166,13 +182,11 @@ pub fn command(options: Options) -> Result<()> {
166182
.arg("-output")
167183
.arg(out_dir.join(&bin_name));
168184
for triple in ["aarch64-apple-darwin", "x86_64-apple-darwin"] {
169-
crate::interface::rust::build_project(
170-
runner.clone(),
171-
&Some(triple.into()),
172-
cargo_features.clone(),
173-
options.debug,
174-
)
175-
.with_context(|| format!("failed to build {} binary", triple))?;
185+
let mut args_ = args.clone();
186+
args_.push("--target".into());
187+
args_.push(triple.into());
188+
crate::interface::rust::build_project(runner.clone(), args_)
189+
.with_context(|| format!("failed to build {} binary", triple))?;
176190
let triple_out_dir = app_settings
177191
.get_out_dir(Some(triple.into()), options.debug)
178192
.with_context(|| format!("failed to get {} out dir", triple))?;
@@ -187,8 +201,11 @@ pub fn command(options: Options) -> Result<()> {
187201
)));
188202
}
189203
} else {
190-
crate::interface::rust::build_project(runner, &options.target, cargo_features, options.debug)
191-
.with_context(|| "failed to build app")?;
204+
if let Some(target) = &options.target {
205+
args.push("--target".into());
206+
args.push(target.clone());
207+
}
208+
crate::interface::rust::build_project(runner, args).with_context(|| "failed to build app")?;
192209
}
193210

194211
#[cfg(unix)]

tooling/cli/src/dev.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct Options {
5454
/// Run the code in release mode
5555
#[clap(long = "release")]
5656
release_mode: bool,
57-
/// Args passed to the binary
57+
/// Command line arguments passed to the runner
5858
args: Vec<String>,
5959
}
6060

@@ -315,7 +315,7 @@ fn start_app(
315315
}
316316

317317
if !options.args.is_empty() {
318-
command.arg("--").args(&options.args);
318+
command.args(&options.args);
319319
}
320320

321321
command.pipe().unwrap();

tooling/cli/src/interface/rust.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,28 +100,11 @@ struct CargoConfig {
100100
build: Option<CargoBuildConfig>,
101101
}
102102

103-
pub fn build_project(
104-
runner: String,
105-
target: &Option<String>,
106-
features: Vec<String>,
107-
debug: bool,
108-
) -> crate::Result<()> {
103+
pub fn build_project(runner: String, args: Vec<String>) -> crate::Result<()> {
109104
let mut command = Command::new(&runner);
110-
command.args(&["build", "--features=custom-protocol"]);
111-
112-
if let Some(target) = target {
113-
command.arg("--target");
114-
command.arg(target);
115-
}
116-
117-
if !features.is_empty() {
118-
command.arg("--features");
119-
command.arg(features.join(","));
120-
}
121-
122-
if !debug {
123-
command.arg("--release");
124-
}
105+
command
106+
.args(&["build", "--features=custom-protocol"])
107+
.args(args);
125108

126109
command.pipe()?;
127110

0 commit comments

Comments
 (0)