Skip to content

Commit

Permalink
fix(cli): parse --profile=<profile> syntax (#10135)
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

* safe check next arg

* add test

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog authored Jun 27, 2024
1 parent 167b51a commit 2783836
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 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.
77 changes: 74 additions & 3 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,9 +1130,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 @@ -1457,3 +1462,69 @@ mod pkgconfig_utils {
}
}
}

#[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 2783836

Please sign in to comment.