Skip to content

Commit 93e0e13

Browse files
committed
feat(cli/add): support specifying version
closes #9325
1 parent 06833f4 commit 93e0e13

File tree

3 files changed

+45
-23
lines changed

3 files changed

+45
-23
lines changed

.changes/cli-add-@-spec.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": "minor:feat"
3+
"@tauri-apps/cli": "minor:feat"
4+
---
5+
6+
Support specifying a version for `tauri add` subcommand, for example: `tauri add window-state@2.0.0-beta.2`

tooling/cli/src/add.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,24 @@ pub struct Options {
8080
}
8181

8282
pub fn command(options: Options) -> Result<()> {
83-
let plugin = options.plugin;
83+
let (plugin, version) = options
84+
.plugin
85+
.split_once("@")
86+
.map(|(p, v)| (p, Some(v)))
87+
.unwrap_or((&options.plugin, None));
88+
8489
let plugin_snake_case = plugin.replace('-', "_");
8590
let crate_name = format!("tauri-plugin-{plugin}");
8691
let npm_name = format!("@tauri-apps/plugin-{plugin}");
8792

8893
let mut plugins = plugins();
89-
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
94+
let metadata = plugins.remove(plugin).unwrap_or_default();
9095

9196
let tauri_dir = tauri_dir();
9297

9398
cargo::install_one(cargo::CargoInstallOptions {
9499
name: &crate_name,
100+
version,
95101
branch: options.branch.as_deref(),
96102
rev: options.rev.as_deref(),
97103
tag: options.tag.as_deref(),
@@ -108,17 +114,20 @@ pub fn command(options: Options) -> Result<()> {
108114
.map(PackageManager::from_project)
109115
.and_then(|managers| managers.into_iter().next())
110116
{
111-
let npm_spec = match (options.tag, options.rev, options.branch) {
112-
(Some(tag), None, None) => {
117+
let npm_spec = match (version, options.tag, options.rev, options.branch) {
118+
(Some(version), _, _, _) => {
119+
format!("{npm_name}@{version}")
120+
}
121+
(None, Some(tag), None, None) => {
113122
format!("tauri-apps/tauri-plugin-{plugin}#{tag}")
114123
}
115-
(None, Some(rev), None) => {
124+
(None, None, Some(rev), None) => {
116125
format!("tauri-apps/tauri-plugin-{plugin}#{rev}")
117126
}
118-
(None, None, Some(branch)) => {
127+
(None, None, None, Some(branch)) => {
119128
format!("tauri-apps/tauri-plugin-{plugin}#{branch}")
120129
}
121-
(None, None, None) => npm_name,
130+
(None, None, None, None) => npm_name,
122131
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
123132
};
124133
manager.install(&[npm_spec])?;

tooling/cli/src/helpers/cargo.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use anyhow::Context;
99
#[derive(Debug, Default, Clone, Copy)]
1010
pub struct CargoInstallOptions<'a> {
1111
pub name: &'a str,
12+
pub version: Option<&'a str>,
1213
pub rev: Option<&'a str>,
1314
pub tag: Option<&'a str>,
1415
pub branch: Option<&'a str>,
@@ -49,25 +50,31 @@ pub fn install(dependencies: &[String], cwd: Option<&Path>) -> crate::Result<()>
4950

5051
pub fn install_one(options: CargoInstallOptions) -> crate::Result<()> {
5152
let mut cargo = Command::new("cargo");
52-
cargo.args(["add", options.name]);
53+
cargo.arg("add");
5354

54-
if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
55-
cargo.args(["--git", "https://github.com/tauri-apps/plugins-workspace"]);
56-
}
55+
if let Some(version) = options.version {
56+
cargo.arg(format!("{}@{}", options.name, version));
57+
} else {
58+
cargo.arg(options.name);
5759

58-
match (options.tag, options.rev, options.branch) {
59-
(Some(tag), None, None) => {
60-
cargo.args(["--tag", &tag]);
61-
}
62-
(None, Some(rev), None) => {
63-
cargo.args(["--rev", &rev]);
64-
}
65-
(None, None, Some(branch)) => {
66-
cargo.args(["--branch", &branch]);
60+
if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
61+
cargo.args(["--git", "https://github.com/tauri-apps/plugins-workspace"]);
6762
}
68-
(None, None, None) => {}
69-
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
70-
};
63+
64+
match (options.tag, options.rev, options.branch) {
65+
(Some(tag), None, None) => {
66+
cargo.args(["--tag", &tag]);
67+
}
68+
(None, Some(rev), None) => {
69+
cargo.args(["--rev", &rev]);
70+
}
71+
(None, None, Some(branch)) => {
72+
cargo.args(["--branch", &branch]);
73+
}
74+
(None, None, None) => {}
75+
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
76+
};
77+
}
7178

7279
if let Some(target) = options.target {
7380
cargo.args(["--target", target]);

0 commit comments

Comments
 (0)