From 59adf7b340d66cf4fa75ccb43db4f0b10b5fbc1c Mon Sep 17 00:00:00 2001 From: amrbashir Date: Mon, 22 Apr 2024 17:24:30 +0200 Subject: [PATCH] feat(cli/migrate): migrate existing v1 plugins closes #9400 --- .changes/cli-migrate-existing-v1-plugins.md | 7 +++ .../webview/autogenerated/reference.md | 4 -- tooling/cli/src/helpers/npm.rs | 4 ++ tooling/cli/src/migrate/frontend.rs | 43 ++++----------- tooling/cli/src/migrate/mod.rs | 23 ++++++-- tooling/cli/src/migrate/v1_plugins.rs | 55 +++++++++++++++++++ 6 files changed, 95 insertions(+), 41 deletions(-) create mode 100644 .changes/cli-migrate-existing-v1-plugins.md create mode 100644 tooling/cli/src/migrate/v1_plugins.rs diff --git a/.changes/cli-migrate-existing-v1-plugins.md b/.changes/cli-migrate-existing-v1-plugins.md new file mode 100644 index 00000000000..055a3c1a254 --- /dev/null +++ b/.changes/cli-migrate-existing-v1-plugins.md @@ -0,0 +1,7 @@ +--- +"tauri-cli": "patch:feat" +"@tauri-apps/cli": "patch:feat" +--- + +`tauri migrate` will migrate v1 plugins if detected. + diff --git a/core/tauri/permissions/webview/autogenerated/reference.md b/core/tauri/permissions/webview/autogenerated/reference.md index 9f858c5c164..7a5e7969c59 100644 --- a/core/tauri/permissions/webview/autogenerated/reference.md +++ b/core/tauri/permissions/webview/autogenerated/reference.md @@ -20,12 +20,8 @@ |`deny-set-webview-zoom`|Denies the set_webview_zoom command without any pre-configured scope.| |`allow-webview-close`|Enables the webview_close command without any pre-configured scope.| |`deny-webview-close`|Denies the webview_close command without any pre-configured scope.| -|`allow-webview-hide`|Enables the webview_hide command without any pre-configured scope.| -|`deny-webview-hide`|Denies the webview_hide command without any pre-configured scope.| |`allow-webview-position`|Enables the webview_position command without any pre-configured scope.| |`deny-webview-position`|Denies the webview_position command without any pre-configured scope.| -|`allow-webview-show`|Enables the webview_show command without any pre-configured scope.| -|`deny-webview-show`|Denies the webview_show command without any pre-configured scope.| |`allow-webview-size`|Enables the webview_size command without any pre-configured scope.| |`deny-webview-size`|Denies the webview_size command without any pre-configured scope.| |`default`|Default permissions for the plugin.| diff --git a/tooling/cli/src/helpers/npm.rs b/tooling/cli/src/helpers/npm.rs index 37e527a31a6..ddba432c183 100644 --- a/tooling/cli/src/helpers/npm.rs +++ b/tooling/cli/src/helpers/npm.rs @@ -96,6 +96,10 @@ impl PackageManager { .join(", ") ); + // because we are using cmd.exe, `>` is a pipe character so we need to escape it + #[cfg(windows)] + let dependencies = dependencies.iter().map(|d| d.replace('>', "^>")); + let status = self .cross_command() .arg("add") diff --git a/tooling/cli/src/migrate/frontend.rs b/tooling/cli/src/migrate/frontend.rs index 23317e01bc5..eb40b67d7af 100644 --- a/tooling/cli/src/migrate/frontend.rs +++ b/tooling/cli/src/migrate/frontend.rs @@ -2,10 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -use crate::{ - helpers::{app_paths::walk_builder, cargo, npm::PackageManager}, - Result, -}; +use crate::{helpers::app_paths::walk_builder, Result}; use anyhow::Context; use std::{fs, path::Path}; @@ -28,14 +25,8 @@ const PLUGINIFIED_MODULES: &[&str] = &[ const JS_EXTENSIONS: &[&str] = &["js", "mjs", "jsx", "ts", "mts", "tsx"]; /// Returns a list of paths that could not be migrated -pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> { - let mut new_npm_packages = Vec::new(); - let mut new_cargo_packages = Vec::new(); - - let pm = PackageManager::from_project(app_dir) - .into_iter() - .next() - .unwrap_or(PackageManager::Npm); +pub fn migrate(app_dir: &Path) -> Result> { + let mut new_plugins = Vec::new(); let tauri_api_import_regex = regex::bytes::Regex::new(r"@tauri-apps/api/(\w+)").unwrap(); @@ -56,18 +47,14 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> { let new = if let Some((_, renamed_to)) = RENAMED_MODULES.iter().find(|(from, _to)| *from == module) { - renamed_to.to_string() + format!("@tauri-apps/api/{renamed_to}") } else if PLUGINIFIED_MODULES.contains(&module.as_str()) { let plugin = format!("@tauri-apps/plugin-{module}"); - new_npm_packages.push(plugin.clone()); - new_cargo_packages.push(format!( - "tauri-plugin-{}", - if module == "clipboard" { - "clipboard-manager" - } else { - &module - } - )); + new_plugins.push(if module == "clipboard" { + "clipboard-manager".to_string() + } else { + module + }); plugin } else { return original; @@ -85,15 +72,5 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> { } } - if !new_npm_packages.is_empty() { - pm.install(&new_npm_packages) - .context("Error installing new npm packages")?; - } - - if !new_cargo_packages.is_empty() { - cargo::install(&new_cargo_packages, Some(tauri_dir)) - .context("Error installing new Cargo packages")?; - } - - Ok(()) + Ok(new_plugins) } diff --git a/tooling/cli/src/migrate/mod.rs b/tooling/cli/src/migrate/mod.rs index 29351319c9f..92da224cca3 100644 --- a/tooling/cli/src/migrate/mod.rs +++ b/tooling/cli/src/migrate/mod.rs @@ -3,7 +3,10 @@ // SPDX-License-Identifier: MIT use crate::{ - helpers::app_paths::{app_dir, tauri_dir}, + helpers::{ + app_paths::{app_dir, tauri_dir}, + npm::PackageManager, + }, Result, }; use anyhow::Context; @@ -11,6 +14,7 @@ use anyhow::Context; mod config; mod frontend; mod manifest; +mod v1_plugins; pub fn command() -> Result<()> { let tauri_dir = tauri_dir(); @@ -18,18 +22,29 @@ pub fn command() -> Result<()> { let migrated = config::migrate(&tauri_dir).context("Could not migrate config")?; manifest::migrate(&tauri_dir).context("Could not migrate manifest")?; - frontend::migrate(app_dir, &tauri_dir)?; + v1_plugins::migrate(&tauri_dir, app_dir).context("Could not migrate v1 plugins")?; + let frontend_plugins = frontend::migrate(app_dir).context("Could not migrate frontend")?; // Add plugins - for plugin in migrated.plugins { + let mut plugins = migrated.plugins; + plugins.extend(frontend_plugins); + for plugin in plugins { crate::add::command(crate::add::Options { plugin: plugin.clone(), branch: None, tag: None, rev: None, }) - .with_context(|| format!("Could not migrate plugin '{plugin}'"))? + .with_context(|| format!("Could not add '{plugin}' plugin"))? } + // Update @tauri-apps/api version + let pm = PackageManager::from_project(app_dir) + .into_iter() + .next() + .unwrap_or(PackageManager::Npm); + pm.install(&["@tauri-apps/api@>=2.0.0-beta.0".into()]) + .context("Failed to update @tauri-apps/api package to v2")?; + Ok(()) } diff --git a/tooling/cli/src/migrate/v1_plugins.rs b/tooling/cli/src/migrate/v1_plugins.rs new file mode 100644 index 00000000000..bae0d17d351 --- /dev/null +++ b/tooling/cli/src/migrate/v1_plugins.rs @@ -0,0 +1,55 @@ +// 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 anyhow::Context; + +use crate::{ + helpers::{cargo, npm::PackageManager}, + Result, +}; + +const PLUGINS: &[&str] = &[ + "authenticator", + "autostart", + "fs-extra", + "fs-watch", + "localhost", + "log", + "persisted-scope", + "positioner", + "single-instance", + "sql", + "store", + "stronghold", + "upload", + "websocket", + "window-state", +]; + +pub fn migrate(tauri_dir: &Path, app_dir: &Path) -> Result<()> { + let manifest_path = tauri_dir.join("Cargo.toml"); + let manifest = fs::read_to_string(manifest_path).context("failed to read Cargo.toml")?; + + let plugins_to_migrate = PLUGINS + .iter() + .filter(|p| manifest.contains(&format!("tauri-plugin-{p}"))); + + let cargo_deps = plugins_to_migrate + .clone() + .map(|p| format!("tauri-plugin-{p}@2.0.0-beta")) + .collect::>(); + cargo::install(&cargo_deps, Some(tauri_dir))?; + + let npm_deps = plugins_to_migrate + .map(|p| format!("@tauri-apps/plugin-{p}@>=2.0.0-beta.0")) + .collect::>(); + let pm = PackageManager::from_project(app_dir) + .into_iter() + .next() + .unwrap_or(PackageManager::Npm); + pm.install(&npm_deps)?; + + Ok(()) +}