Sfoglia il codice sorgente

feat(cli.rs): add `features` arg to dev/build (#1828)

Lucas Fernandes Nogueira 4 anni fa
parent
commit
6ec8e84d91

+ 5 - 0
.changes/cli.rs-features-arg.md

@@ -0,0 +1,5 @@
+---
+"cli.rs": patch
+---
+
+Adds `features` argument to the `dev` and `build` commands.

+ 10 - 1
tooling/cli.rs/src/build.rs

@@ -24,6 +24,7 @@ pub struct Build {
   debug: bool,
   verbose: bool,
   target: Option<String>,
+  features: Option<Vec<String>>,
   bundles: Option<Vec<String>>,
   config: Option<String>,
 }
@@ -53,6 +54,11 @@ impl Build {
     self
   }
 
+  pub fn features(mut self, features: Vec<String>) -> Self {
+    self.features.replace(features);
+    self
+  }
+
   pub fn bundles(mut self, bundles: Vec<String>) -> Self {
     self.bundles.replace(bundles);
     self
@@ -111,7 +117,10 @@ impl Build {
       .or(runner_from_config)
       .unwrap_or_else(|| "cargo".to_string());
 
-    let cargo_features = &config_.build.features;
+    let mut cargo_features = config_.build.features.clone().unwrap_or_default();
+    if let Some(features) = self.features {
+      cargo_features.extend(features);
+    }
 
     rust::build_project(runner, &self.target, cargo_features, self.debug)
       .with_context(|| "failed to build app")?;

+ 3 - 3
tooling/cli.rs/src/build/rust.rs

@@ -93,18 +93,18 @@ struct CargoConfig {
 pub fn build_project(
   runner: String,
   target: &Option<String>,
-  features: &Option<Vec<String>>,
+  features: Vec<String>,
   debug: bool,
 ) -> crate::Result<()> {
   let mut command = Command::new(&runner);
-  command.args(["build", "--features=custom-protocol"]);
+  command.args(&["build", "--features=custom-protocol"]);
 
   if let Some(target) = target {
     command.arg("--target");
     command.arg(target);
   }
 
-  if let Some(features) = features {
+  if !features.is_empty() {
     command.arg("--features");
     command.arg(features.join(","));
   }

+ 10 - 0
tooling/cli.rs/src/cli.yml

@@ -29,6 +29,11 @@ subcommands:
                     long: target
                     about: target triple to build against
                     multiple: true
+                - features:
+                    short: f
+                    long: features
+                    about: list of cargo features to activate
+                    multiple: true
                 - args:
                     about: Args passed to the binary
                     index: 1
@@ -65,6 +70,11 @@ subcommands:
                     long: target
                     about: target triple to build against
                     multiple: true
+                - features:
+                    short: f
+                    long: features
+                    about: list of cargo features to activate
+                    multiple: true
         - sign:
             about: Tauri updates signer.
             args:

+ 14 - 4
tooling/cli.rs/src/dev.rs

@@ -48,6 +48,7 @@ fn kill_before_dev_process() {
 pub struct Dev {
   runner: Option<String>,
   target: Option<String>,
+  features: Option<Vec<String>>,
   exit_on_panic: bool,
   config: Option<String>,
   args: Vec<String>,
@@ -68,6 +69,11 @@ impl Dev {
     self
   }
 
+  pub fn features(mut self, features: Vec<String>) -> Self {
+    self.features.replace(features);
+    self
+  }
+
   pub fn config(mut self, config: String) -> Self {
     self.config.replace(config);
     self
@@ -145,14 +151,18 @@ impl Dev {
       }
     }
 
-    let cargo_features = config
+    let mut cargo_features = config
       .lock()
       .unwrap()
       .as_ref()
       .unwrap()
       .build
       .features
-      .clone();
+      .clone()
+      .unwrap_or_default();
+    if let Some(features) = &self.features {
+      cargo_features.extend(features.clone());
+    }
 
     let (child_wait_tx, child_wait_rx) = channel();
     let child_wait_rx = Arc::new(Mutex::new(child_wait_rx));
@@ -210,7 +220,7 @@ impl Dev {
   fn start_app(
     &self,
     runner: &str,
-    features: &Option<Vec<String>>,
+    features: &[String],
     child_wait_rx: Arc<Mutex<Receiver<()>>>,
   ) -> Arc<SharedChild> {
     let mut command = Command::new(runner);
@@ -220,7 +230,7 @@ impl Dev {
       command.args(&["--target", target]);
     }
 
-    if let Some(features) = features {
+    if !features.is_empty() {
       command.args(&["--features", &features.join(",")]);
     }
 

+ 13 - 2
tooling/cli.rs/src/main.rs

@@ -92,6 +92,10 @@ fn init_command(matches: &ArgMatches) -> Result<()> {
 fn dev_command(matches: &ArgMatches) -> Result<()> {
   let runner = matches.value_of("runner");
   let target = matches.value_of("target");
+  let features: Vec<String> = matches
+    .values_of("features")
+    .map(|a| a.into_iter().map(|v| v.to_string()).collect())
+    .unwrap_or_default();
   let exit_on_panic = matches.is_present("exit-on-panic");
   let config = matches.value_of("config");
   let args: Vec<String> = matches
@@ -99,7 +103,10 @@ fn dev_command(matches: &ArgMatches) -> Result<()> {
     .map(|a| a.into_iter().map(|v| v.to_string()).collect())
     .unwrap_or_default();
 
-  let mut dev_runner = dev::Dev::new().exit_on_panic(exit_on_panic).args(args);
+  let mut dev_runner = dev::Dev::new()
+    .exit_on_panic(exit_on_panic)
+    .args(args)
+    .features(features);
 
   if let Some(runner) = runner {
     dev_runner = dev_runner.runner(runner.to_string());
@@ -117,12 +124,16 @@ fn dev_command(matches: &ArgMatches) -> Result<()> {
 fn build_command(matches: &ArgMatches) -> Result<()> {
   let runner = matches.value_of("runner");
   let target = matches.value_of("target");
+  let features: Vec<String> = matches
+    .values_of("features")
+    .map(|a| a.into_iter().map(|v| v.to_string()).collect())
+    .unwrap_or_default();
   let debug = matches.is_present("debug");
   let verbose = matches.is_present("verbose");
   let bundles = matches.values_of_lossy("bundle");
   let config = matches.value_of("config");
 
-  let mut build_runner = build::Build::new();
+  let mut build_runner = build::Build::new().features(features);
   if let Some(runner) = runner {
     build_runner = build_runner.runner(runner.to_string());
   }