Skip to content

Commit

Permalink
feat(cli.rs): allow passing arguments to the build runner, closes #3398
Browse files Browse the repository at this point in the history
… (#3431)

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
lucasfernog and amrbashir authored Feb 13, 2022
1 parent 102a5e9 commit 679fe1f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changes/cli-runner-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

Allow passing arguments to the `build` runner (`tauri build -- <ARGS>...`).
5 changes: 5 additions & 0 deletions .changes/refactor-cli-dev-args.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cli.rs": patch
---

**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 `--`).
43 changes: 30 additions & 13 deletions tooling/cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tauri_bundler::bundle::{bundle_project, PackageType};
#[derive(Debug, Parser)]
#[clap(about = "Tauri build")]
pub struct Options {
/// Binary to use to build the application
/// Binary to use to build the application, defaults to `cargo`
#[clap(short, long)]
runner: Option<String>,
/// Builds with the debug flag
Expand All @@ -44,6 +44,8 @@ pub struct Options {
/// JSON string or path to JSON file to merge with tauri.conf.json
#[clap(short, long)]
config: Option<String>,
/// Command line arguments passed to the runner
args: Vec<String>,
}

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

let mut cargo_features = config_.build.features.clone().unwrap_or_default();
if let Some(features) = options.features {
cargo_features.extend(features);
let mut features = config_.build.features.clone().unwrap_or_default();
if let Some(list) = options.features {
features.extend(list);
}

let mut args = Vec::new();
if !options.args.is_empty() {
args.extend(options.args);
}

if !features.is_empty() {
args.push("--features".into());
args.push(features.join(","));
}

if !options.debug {
args.push("--release".into());
}

let app_settings = crate::interface::rust::AppSettings::new(config_)?;
Expand Down Expand Up @@ -166,13 +182,11 @@ pub fn command(options: Options) -> Result<()> {
.arg("-output")
.arg(out_dir.join(&bin_name));
for triple in ["aarch64-apple-darwin", "x86_64-apple-darwin"] {
crate::interface::rust::build_project(
runner.clone(),
&Some(triple.into()),
cargo_features.clone(),
options.debug,
)
.with_context(|| format!("failed to build {} binary", triple))?;
let mut args_ = args.clone();
args_.push("--target".into());
args_.push(triple.into());
crate::interface::rust::build_project(runner.clone(), args_)
.with_context(|| format!("failed to build {} binary", triple))?;
let triple_out_dir = app_settings
.get_out_dir(Some(triple.into()), options.debug)
.with_context(|| format!("failed to get {} out dir", triple))?;
Expand All @@ -187,8 +201,11 @@ pub fn command(options: Options) -> Result<()> {
)));
}
} else {
crate::interface::rust::build_project(runner, &options.target, cargo_features, options.debug)
.with_context(|| "failed to build app")?;
if let Some(target) = &options.target {
args.push("--target".into());
args.push(target.clone());
}
crate::interface::rust::build_project(runner, args).with_context(|| "failed to build app")?;
}

#[cfg(unix)]
Expand Down
4 changes: 2 additions & 2 deletions tooling/cli/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub struct Options {
/// Run the code in release mode
#[clap(long = "release")]
release_mode: bool,
/// Args passed to the binary
/// Command line arguments passed to the runner
args: Vec<String>,
}

Expand Down Expand Up @@ -315,7 +315,7 @@ fn start_app(
}

if !options.args.is_empty() {
command.arg("--").args(&options.args);
command.args(&options.args);
}

command.pipe().unwrap();
Expand Down
25 changes: 4 additions & 21 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,11 @@ struct CargoConfig {
build: Option<CargoBuildConfig>,
}

pub fn build_project(
runner: String,
target: &Option<String>,
features: Vec<String>,
debug: bool,
) -> crate::Result<()> {
pub fn build_project(runner: String, args: Vec<String>) -> crate::Result<()> {
let mut command = Command::new(&runner);
command.args(&["build", "--features=custom-protocol"]);

if let Some(target) = target {
command.arg("--target");
command.arg(target);
}

if !features.is_empty() {
command.arg("--features");
command.arg(features.join(","));
}

if !debug {
command.arg("--release");
}
command
.args(&["build", "--features=custom-protocol"])
.args(args);

command.pipe()?;

Expand Down

0 comments on commit 679fe1f

Please sign in to comment.