Parcourir la source

feat(cli.rs): allow passing arguments to the build runner, closes #3398 (#3431)

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
Lucas Fernandes Nogueira il y a 3 ans
Parent
commit
679fe1fedd

+ 5 - 0
.changes/cli-runner-args.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Allow passing arguments to the `build` runner (`tauri build -- <ARGS>...`).

+ 5 - 0
.changes/refactor-cli-dev-args.md

@@ -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 `--`).

+ 30 - 13
tooling/cli/src/build.rs

@@ -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
@@ -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<()> {
@@ -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_)?;
@@ -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))?;
@@ -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)]

+ 2 - 2
tooling/cli/src/dev.rs

@@ -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>,
 }
 
@@ -315,7 +315,7 @@ fn start_app(
   }
 
   if !options.args.is_empty() {
-    command.arg("--").args(&options.args);
+    command.args(&options.args);
   }
 
   command.pipe().unwrap();

+ 4 - 21
tooling/cli/src/interface/rust.rs

@@ -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()?;