Skip to content

Commit 9642b30

Browse files
authored
fix(cli): honor git options in tauri add for known plugins (#16117)
1 parent e1638c7 commit 9642b30

2 files changed

Lines changed: 61 additions & 26 deletions

File tree

‎.changes/cli-add-git-options.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:bug
3+
"@tauri-apps/cli": patch:bug
4+
---
5+
6+
`tauri add` now honors `--tag`, `--rev` and `--branch` for official plugins instead of silently installing the registry version, and rejects passing more than one of them. With npm, the JS package requirement is now `~<version>` like the other package managers, instead of `>=<version>` which allowed a later major version.

‎crates/tauri-cli/src/add.rs‎

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,21 @@ pub fn run(options: Options, dirs: &Dirs) -> Result<()> {
6161
let crate_name = format!("tauri-plugin-{plugin}");
6262
let npm_name = format!("@tauri-apps/plugin-{plugin}");
6363

64-
if !is_known && (options.tag.is_some() || options.rev.is_some() || options.branch.is_some()) {
65-
crate::error::bail!(
66-
"Git options --tag, --rev and --branch can only be used with official Tauri plugins"
67-
);
64+
let git_ref = git_ref(
65+
options.tag.as_deref(),
66+
options.rev.as_deref(),
67+
options.branch.as_deref(),
68+
)?;
69+
70+
if git_ref.is_some() {
71+
if !is_known {
72+
crate::error::bail!(
73+
"Git options --tag, --rev and --branch can only be used with official Tauri plugins"
74+
);
75+
}
76+
if version.is_some() {
77+
crate::error::bail!("A version cannot be used together with --tag, --rev or --branch");
78+
}
6879
}
6980

7081
let frontend_dir = resolve_frontend_dir();
@@ -78,7 +89,12 @@ pub fn run(options: Options, dirs: &Dirs) -> Result<()> {
7889
.then_some(r#"cfg(any(target_os = "android", target_os = "ios"))"#)
7990
});
8091

81-
let cargo_version_req = version.or(metadata.version_req.as_deref());
92+
// a git dependency cannot also have a registry version requirement
93+
let cargo_version_req = if git_ref.is_some() {
94+
None
95+
} else {
96+
version.or(metadata.version_req.as_deref())
97+
};
8298

8399
cargo::install_one(cargo::CargoInstallOptions {
84100
name: &crate_name,
@@ -98,26 +114,15 @@ pub fn run(options: Options, dirs: &Dirs) -> Result<()> {
98114
"If `{crate_name}` has JavaScript bindings, install them with your package manager, see the plugin's documentation for the package name."
99115
);
100116
} else if let Some(manager) = frontend_dir.map(PackageManager::from_project) {
101-
let npm_version_req = version.map(ToString::to_string).or_else(|| {
102-
metadata.version_req.as_ref().map(|v| match manager {
103-
PackageManager::Npm => format!(">={v}"),
104-
_ => format!("~{v}"),
105-
})
106-
});
107-
108-
let npm_spec = match (npm_version_req, options.tag, options.rev, options.branch) {
109-
(Some(version_req), _, _, _) => format!("{npm_name}@{version_req}"),
110-
(None, Some(tag), None, None) => {
111-
format!("tauri-apps/tauri-plugin-{plugin}#{tag}")
112-
}
113-
(None, None, Some(rev), None) => {
114-
format!("tauri-apps/tauri-plugin-{plugin}#{rev}")
115-
}
116-
(None, None, None, Some(branch)) => {
117-
format!("tauri-apps/tauri-plugin-{plugin}#{branch}")
118-
}
119-
(None, None, None, None) => npm_name,
120-
_ => crate::error::bail!("Only one of --tag, --rev and --branch can be specified"),
117+
let npm_spec = if let Some(git_ref) = git_ref {
118+
format!("tauri-apps/tauri-plugin-{plugin}#{git_ref}")
119+
} else if let Some(version_req) = version
120+
.map(ToString::to_string)
121+
.or_else(|| metadata.version_req.as_ref().map(|v| format!("~{v}")))
122+
{
123+
format!("{npm_name}@{version_req}")
124+
} else {
125+
npm_name
121126
};
122127
manager.install(&[npm_spec], dirs.frontend)?;
123128
}
@@ -218,6 +223,19 @@ pub fn run(options: Options, dirs: &Dirs) -> Result<()> {
218223
Ok(())
219224
}
220225

226+
/// Returns the single git ref given with `--tag`, `--rev` or `--branch`, if any.
227+
fn git_ref<'a>(
228+
tag: Option<&'a str>,
229+
rev: Option<&'a str>,
230+
branch: Option<&'a str>,
231+
) -> Result<Option<&'a str>> {
232+
match (tag, rev, branch) {
233+
(Some(r), None, None) | (None, Some(r), None) | (None, None, Some(r)) => Ok(Some(r)),
234+
(None, None, None) => Ok(None),
235+
_ => crate::error::bail!("Only one of --tag, --rev and --branch can be specified"),
236+
}
237+
}
238+
221239
/// Turns on the updater's signed version check for a project adopting the plugin now.
222240
///
223241
/// An update endpoint response is not signed, so the version it announces does not by itself
@@ -285,7 +303,7 @@ fn set_require_signed_version(config: &mut JsonValue) -> bool {
285303

286304
#[cfg(test)]
287305
mod tests {
288-
use super::set_require_signed_version;
306+
use super::{git_ref, set_require_signed_version};
289307
use serde_json::json;
290308

291309
#[test]
@@ -327,4 +345,15 @@ mod tests {
327345
assert!(!set_require_signed_version(&mut config));
328346
assert_eq!(config["plugins"]["updater"], "not an object");
329347
}
348+
349+
#[test]
350+
fn accepts_at_most_one_git_ref() {
351+
assert_eq!(git_ref(None, None, None).unwrap(), None);
352+
assert_eq!(git_ref(Some("v2"), None, None).unwrap(), Some("v2"));
353+
assert_eq!(git_ref(None, Some("abc"), None).unwrap(), Some("abc"));
354+
assert_eq!(git_ref(None, None, Some("dev")).unwrap(), Some("dev"));
355+
assert!(git_ref(Some("v2"), Some("abc"), None).is_err());
356+
assert!(git_ref(Some("v2"), None, Some("dev")).is_err());
357+
assert!(git_ref(None, Some("abc"), Some("dev")).is_err());
358+
}
330359
}

0 commit comments

Comments
 (0)