From 6ec8e84d9172c090ee1549db56c98c66f12436ff Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 13 May 2021 18:10:48 -0300 Subject: [PATCH] feat(cli.rs): add `features` arg to dev/build (#1828) --- .changes/cli.rs-features-arg.md | 5 +++++ tooling/cli.rs/src/build.rs | 11 ++++++++++- tooling/cli.rs/src/build/rust.rs | 6 +++--- tooling/cli.rs/src/cli.yml | 10 ++++++++++ tooling/cli.rs/src/dev.rs | 18 ++++++++++++++---- tooling/cli.rs/src/main.rs | 15 +++++++++++++-- 6 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 .changes/cli.rs-features-arg.md diff --git a/.changes/cli.rs-features-arg.md b/.changes/cli.rs-features-arg.md new file mode 100644 index 000000000..d078b6ba6 --- /dev/null +++ b/.changes/cli.rs-features-arg.md @@ -0,0 +1,5 @@ +--- +"cli.rs": patch +--- + +Adds `features` argument to the `dev` and `build` commands. diff --git a/tooling/cli.rs/src/build.rs b/tooling/cli.rs/src/build.rs index f5f25d8be..89b35b367 100644 --- a/tooling/cli.rs/src/build.rs +++ b/tooling/cli.rs/src/build.rs @@ -24,6 +24,7 @@ pub struct Build { debug: bool, verbose: bool, target: Option, + features: Option>, bundles: Option>, config: Option, } @@ -53,6 +54,11 @@ impl Build { self } + pub fn features(mut self, features: Vec) -> Self { + self.features.replace(features); + self + } + pub fn bundles(mut self, bundles: Vec) -> 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")?; diff --git a/tooling/cli.rs/src/build/rust.rs b/tooling/cli.rs/src/build/rust.rs index c783d9544..6bbefab3a 100644 --- a/tooling/cli.rs/src/build/rust.rs +++ b/tooling/cli.rs/src/build/rust.rs @@ -93,18 +93,18 @@ struct CargoConfig { pub fn build_project( runner: String, target: &Option, - features: &Option>, + features: Vec, 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(",")); } diff --git a/tooling/cli.rs/src/cli.yml b/tooling/cli.rs/src/cli.yml index 71fcbf416..b0bb0c85f 100644 --- a/tooling/cli.rs/src/cli.yml +++ b/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: diff --git a/tooling/cli.rs/src/dev.rs b/tooling/cli.rs/src/dev.rs index 4be7c376d..84082030e 100644 --- a/tooling/cli.rs/src/dev.rs +++ b/tooling/cli.rs/src/dev.rs @@ -48,6 +48,7 @@ fn kill_before_dev_process() { pub struct Dev { runner: Option, target: Option, + features: Option>, exit_on_panic: bool, config: Option, args: Vec, @@ -68,6 +69,11 @@ impl Dev { self } + pub fn features(mut self, features: Vec) -> 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>, + features: &[String], child_wait_rx: Arc>>, ) -> Arc { 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(",")]); } diff --git a/tooling/cli.rs/src/main.rs b/tooling/cli.rs/src/main.rs index 6b66f715f..815a7b7ea 100644 --- a/tooling/cli.rs/src/main.rs +++ b/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 = 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 = 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 = 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()); }