fix(cli): parse --profile=<profile> syntax (#10135)

* fix(cli): parse `--profile=<profile>` syntax

ref: https://github.com/tauri-apps/tauri/issues/6255#issuecomment-2192466839

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

* safe check next arg

* add test

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2024-06-27 12:53:26 +03:00 committed by GitHub
parent 167b51a8de
commit 27838365a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 80 additions and 3 deletions

View File

@ -0,0 +1,6 @@
---
"tauri-cli": "patch:bug"
"@tauri-apps/cli": "patch:bug"
---
Fix parsing of cargo profile when using `--profile=<profile>` syntax.

View File

@ -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 {
@ -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");
}
}