mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-09-11 07:55:34 +03:00
cleanup and dedup logic
This commit is contained in:
parent
bc3b9d811a
commit
28fee61af2
@ -72,7 +72,7 @@ fn plugins() -> HashMap<&'static str, PluginMetadata> {
|
||||
plugins
|
||||
}
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[derive(Debug, Parser, Default)]
|
||||
#[clap(about = "Add a tauri plugin to the project")]
|
||||
pub struct Options {
|
||||
/// The plugin to add.
|
||||
@ -86,6 +86,9 @@ pub struct Options {
|
||||
/// Git branch to use.
|
||||
#[clap(short, long)]
|
||||
pub branch: Option<String>,
|
||||
/// Cargo features to enable.
|
||||
#[clap(short, long)]
|
||||
pub features: Option<String>,
|
||||
}
|
||||
|
||||
pub fn command(options: Options) -> Result<()> {
|
||||
@ -115,13 +118,14 @@ pub fn command(options: Options) -> Result<()> {
|
||||
|
||||
cargo::add_one(
|
||||
&crate_name,
|
||||
cargo::AddOneOptions {
|
||||
cargo::AddOptions {
|
||||
version,
|
||||
branch: options.branch.as_deref(),
|
||||
rev: options.rev.as_deref(),
|
||||
tag: options.tag.as_deref(),
|
||||
cwd: Some(&tauri_dir),
|
||||
target: target_str,
|
||||
features: options.features.as_deref(),
|
||||
},
|
||||
)?;
|
||||
|
||||
|
@ -2,79 +2,22 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{ffi::OsStr, path::Path, process::Command};
|
||||
use std::{path::Path, process::Command};
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct AddOptions<'a, I: IntoIterator<Item = S>, S: AsRef<OsStr>> {
|
||||
pub features: Option<I>,
|
||||
pub cwd: Option<&'a Path>,
|
||||
}
|
||||
|
||||
impl<'a> Default for AddOptions<'a, std::slice::Iter<'a, &'a str>, &'a &'a str> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
features: None,
|
||||
cwd: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add<I, S>(dependencies: &[String], options: AddOptions<I, S>) -> crate::Result<()>
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: AsRef<OsStr>,
|
||||
{
|
||||
let dependencies_str = if dependencies.len() > 1 {
|
||||
"dependencies"
|
||||
} else {
|
||||
"dependency"
|
||||
};
|
||||
|
||||
log::info!(
|
||||
"Installing Cargo {dependencies_str} {}...",
|
||||
dependencies
|
||||
.iter()
|
||||
.map(|d| format!("\"{d}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
);
|
||||
|
||||
let mut cmd = Command::new("cargo");
|
||||
cmd.arg("add").args(dependencies);
|
||||
|
||||
if let Some(features) = options.features {
|
||||
let mut features = features.into_iter().peekable();
|
||||
if features.peek().is_some() {
|
||||
cmd.arg("--features").args(features);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(cwd) = options.cwd {
|
||||
cmd.current_dir(cwd);
|
||||
}
|
||||
|
||||
let status = cmd.status().with_context(|| "failed to run cargo")?;
|
||||
|
||||
if !status.success() {
|
||||
anyhow::bail!("Failed to install Cargo {dependencies_str}");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, Copy)]
|
||||
pub struct AddOneOptions<'a> {
|
||||
pub struct AddOptions<'a> {
|
||||
pub version: Option<&'a str>,
|
||||
pub rev: Option<&'a str>,
|
||||
pub tag: Option<&'a str>,
|
||||
pub branch: Option<&'a str>,
|
||||
pub cwd: Option<&'a Path>,
|
||||
pub target: Option<&'a str>,
|
||||
pub features: Option<&'a str>,
|
||||
}
|
||||
|
||||
pub fn add_one(crate_name: &str, options: AddOneOptions) -> crate::Result<()> {
|
||||
pub fn add_one(crate_name: &str, options: AddOptions) -> crate::Result<()> {
|
||||
let mut cargo = Command::new("cargo");
|
||||
cargo.arg("add");
|
||||
|
||||
@ -106,6 +49,10 @@ pub fn add_one(crate_name: &str, options: AddOneOptions) -> crate::Result<()> {
|
||||
cargo.args(["--target", target]);
|
||||
}
|
||||
|
||||
if let Some(features) = options.features {
|
||||
cargo.args(["--features", features]);
|
||||
}
|
||||
|
||||
if let Some(cwd) = options.cwd {
|
||||
cargo.current_dir(cwd);
|
||||
}
|
||||
|
@ -22,18 +22,23 @@ pub fn command() -> Result<()> {
|
||||
|
||||
let migrated = config::migrate(&tauri_dir).context("Could not migrate config")?;
|
||||
manifest::migrate(&tauri_dir).context("Could not migrate manifest")?;
|
||||
v1_plugins::migrate(&tauri_dir, app_dir).context("Could not migrate v1 plugins")?;
|
||||
let v1_plugins = v1_plugins::migrate(&tauri_dir).context("Could not migrate v1 plugins")?;
|
||||
let frontend_plugins = frontend::migrate(app_dir).context("Could not migrate frontend")?;
|
||||
|
||||
// Add plugins
|
||||
let mut plugins = migrated.plugins;
|
||||
plugins.extend(frontend_plugins);
|
||||
plugins.extend(v1_plugins.plugins);
|
||||
for plugin in plugins {
|
||||
let features = v1_plugins.cargo_features.get(plugin.as_str());
|
||||
|
||||
crate::add::command(crate::add::Options {
|
||||
plugin: plugin.clone(),
|
||||
branch: None,
|
||||
tag: None,
|
||||
rev: None,
|
||||
features: features.map(ToString::to_string),
|
||||
..Default::default()
|
||||
})
|
||||
.with_context(|| format!("Could not add '{plugin}' plugin"))?
|
||||
}
|
||||
|
@ -1,14 +1,11 @@
|
||||
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
use std::{fs, path::Path};
|
||||
use std::{collections::HashMap, fs, path::Path};
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
use crate::{
|
||||
helpers::{cargo, npm::PackageManager},
|
||||
Result,
|
||||
};
|
||||
use crate::Result;
|
||||
|
||||
const PLUGINS: &[&str] = &[
|
||||
"authenticator",
|
||||
@ -28,59 +25,33 @@ const PLUGINS: &[&str] = &[
|
||||
"window-state",
|
||||
];
|
||||
|
||||
pub fn migrate(tauri_dir: &Path, app_dir: &Path) -> Result<()> {
|
||||
pub struct V1Plugins {
|
||||
pub plugins: Vec<String>,
|
||||
pub cargo_features: HashMap<&'static str, &'static str>,
|
||||
}
|
||||
|
||||
pub fn migrate(tauri_dir: &Path) -> Result<V1Plugins> {
|
||||
let manifest_path = tauri_dir.join("Cargo.toml");
|
||||
let manifest = fs::read_to_string(manifest_path).context("failed to read Cargo.toml")?;
|
||||
|
||||
let mut features = Vec::new();
|
||||
let mut cargo_features = HashMap::new();
|
||||
|
||||
let plugins_to_migrate = PLUGINS
|
||||
let plugins = PLUGINS
|
||||
.iter()
|
||||
.filter(|p| manifest.contains(&format!("tauri-plugin-{p}")));
|
||||
|
||||
let mut cargo_deps = plugins_to_migrate
|
||||
.filter(|p| manifest.contains(&format!("tauri-plugin-{p}")))
|
||||
.clone()
|
||||
.map(|p| match *p {
|
||||
"fs-extra" => "fs",
|
||||
"fs-extra" => "fs".to_string(),
|
||||
"fs-watch" => {
|
||||
features.push(format!("tauri-plugin-{p}/watch"));
|
||||
"fs"
|
||||
cargo_features.insert("fs", "tauri-plugin-fs/watch");
|
||||
"fs".to_string()
|
||||
}
|
||||
_ => p,
|
||||
_ => p.to_string(),
|
||||
})
|
||||
.map(|p| format!("tauri-plugin-{p}@2.0.0-beta"))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
cargo_deps.sort();
|
||||
cargo_deps.dedup();
|
||||
|
||||
cargo::add(
|
||||
&cargo_deps,
|
||||
cargo::AddOptions {
|
||||
cwd: Some(tauri_dir),
|
||||
features: Some(features),
|
||||
},
|
||||
)?;
|
||||
|
||||
if app_dir.join("package.json").exists() {
|
||||
let mut npm_deps = plugins_to_migrate
|
||||
.map(|p| match *p {
|
||||
"fs-extra" | "fs-watch" => "fs",
|
||||
_ => p,
|
||||
})
|
||||
.map(|p| format!("@tauri-apps/plugin-{p}@>=2.0.0-beta.0"))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
npm_deps.sort();
|
||||
npm_deps.dedup();
|
||||
|
||||
let pm = PackageManager::from_project(app_dir)
|
||||
.into_iter()
|
||||
.next()
|
||||
.unwrap_or(PackageManager::Npm);
|
||||
|
||||
pm.install(&npm_deps)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(V1Plugins {
|
||||
plugins,
|
||||
cargo_features,
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user