Skip to content

Commit

Permalink
fix(cli): parse --profile=<profile> syntax (#10136)
Browse files Browse the repository at this point in the history
* fix(cli): parse `--profile=<profile>` syntax

ref: #6255 (comment)

* Update tooling/cli/src/interface/rust.rs

* derive default

* safe check next arg

---------

Co-authored-by: Lucas Nogueira <[email protected]>
  • Loading branch information
amrbashir and lucasfernog authored Jun 27, 2024
1 parent 63da834 commit 08f57ef
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-profile-parse-syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": "patch:bug"
"@tauri-apps/cli": "patch:bug"
---

Fix parsing of cargo profile when using `--profile=<profile>` syntax.
79 changes: 75 additions & 4 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod manifest;
use cargo_config::Config as CargoConfig;
use manifest::{rewrite_manifest, Manifest};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct Options {
pub runner: Option<String>,
pub debug: bool,
Expand Down Expand Up @@ -1022,9 +1022,14 @@ pub fn get_profile(options: &Options) -> &str {
options
.args
.iter()
.position(|a| a == "--profile")
.map(|i| options.args[i + 1].as_str())
.unwrap_or_else(|| if options.debug { "debug" } else { "release" })
.position(|a| a.starts_with("--profile"))
.and_then(|i| {
options.args[i]
.split_once('=')
.map(|(_, p)| Some(p))
.unwrap_or_else(|| options.args.get(i + 1).map(|s| s.as_str()))
})
.unwrap_or(if options.debug { "dev" } else { "release" })
}

pub fn get_profile_dir(options: &Options) -> &str {
Expand Down Expand Up @@ -1247,3 +1252,69 @@ fn tauri_config_to_bundle_settings(
..Default::default()
})
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse_profile_from_opts() {
let options = Options {
args: vec![
"build".into(),
"--".into(),
"--profile".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "testing");

let options = Options {
args: vec![
"build".into(),
"--".into(),
"--profile=customprofile".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "customprofile");

let options = Options {
debug: true,
args: vec![
"build".into(),
"--".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "dev");

let options = Options {
debug: false,
args: vec![
"build".into(),
"--".into(),
"testing".into(),
"--features".into(),
"feat1".into(),
],
..Default::default()
};
assert_eq!(get_profile(&options), "release");

let options = Options {
args: vec!["build".into(), "--".into(), "--profile".into()],
..Default::default()
};
assert_eq!(get_profile(&options), "release");
}
}

0 comments on commit 08f57ef

Please sign in to comment.