Skip to content

Commit

Permalink
feat(cli/add): support specifying version
Browse files Browse the repository at this point in the history
closes #9325
  • Loading branch information
amrbashir committed Apr 1, 2024
1 parent 06833f4 commit 93e0e13
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .changes/cli-add-@-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri-cli": "minor:feat"
"@tauri-apps/cli": "minor:feat"
---

Support specifying a version for `tauri add` subcommand, for example: `tauri add window-state@2.0.0-beta.2`
23 changes: 16 additions & 7 deletions tooling/cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,24 @@ pub struct Options {
}

pub fn command(options: Options) -> Result<()> {
let plugin = options.plugin;
let (plugin, version) = options
.plugin
.split_once("@")
.map(|(p, v)| (p, Some(v)))
.unwrap_or((&options.plugin, None));

let plugin_snake_case = plugin.replace('-', "_");
let crate_name = format!("tauri-plugin-{plugin}");
let npm_name = format!("@tauri-apps/plugin-{plugin}");

let mut plugins = plugins();
let metadata = plugins.remove(plugin.as_str()).unwrap_or_default();
let metadata = plugins.remove(plugin).unwrap_or_default();

let tauri_dir = tauri_dir();

cargo::install_one(cargo::CargoInstallOptions {
name: &crate_name,
version,
branch: options.branch.as_deref(),
rev: options.rev.as_deref(),
tag: options.tag.as_deref(),
Expand All @@ -108,17 +114,20 @@ pub fn command(options: Options) -> Result<()> {
.map(PackageManager::from_project)
.and_then(|managers| managers.into_iter().next())
{
let npm_spec = match (options.tag, options.rev, options.branch) {
(Some(tag), None, None) => {
let npm_spec = match (version, options.tag, options.rev, options.branch) {
(Some(version), _, _, _) => {
format!("{npm_name}@{version}")
}
(None, Some(tag), None, None) => {
format!("tauri-apps/tauri-plugin-{plugin}#{tag}")
}
(None, Some(rev), None) => {
(None, None, Some(rev), None) => {
format!("tauri-apps/tauri-plugin-{plugin}#{rev}")
}
(None, None, Some(branch)) => {
(None, None, None, Some(branch)) => {
format!("tauri-apps/tauri-plugin-{plugin}#{branch}")
}
(None, None, None) => npm_name,
(None, None, None, None) => npm_name,
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
};
manager.install(&[npm_spec])?;
Expand Down
39 changes: 23 additions & 16 deletions tooling/cli/src/helpers/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use anyhow::Context;
#[derive(Debug, Default, Clone, Copy)]
pub struct CargoInstallOptions<'a> {
pub name: &'a str,
pub version: Option<&'a str>,
pub rev: Option<&'a str>,
pub tag: Option<&'a str>,
pub branch: Option<&'a str>,
Expand Down Expand Up @@ -49,25 +50,31 @@ pub fn install(dependencies: &[String], cwd: Option<&Path>) -> crate::Result<()>

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

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

match (options.tag, options.rev, options.branch) {
(Some(tag), None, None) => {
cargo.args(["--tag", &tag]);
}
(None, Some(rev), None) => {
cargo.args(["--rev", &rev]);
}
(None, None, Some(branch)) => {
cargo.args(["--branch", &branch]);
if options.tag.is_some() || options.rev.is_some() || options.branch.is_some() {
cargo.args(["--git", "https://github.com/tauri-apps/plugins-workspace"]);
}
(None, None, None) => {}
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
};

match (options.tag, options.rev, options.branch) {
(Some(tag), None, None) => {
cargo.args(["--tag", &tag]);
}
(None, Some(rev), None) => {
cargo.args(["--rev", &rev]);
}
(None, None, Some(branch)) => {
cargo.args(["--branch", &branch]);
}
(None, None, None) => {}
_ => anyhow::bail!("Only one of --tag, --rev and --branch can be specified"),
};
}

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

0 comments on commit 93e0e13

Please sign in to comment.