Browse Source

feat(cli/migrate): migrate existing v1 plugins

closes #9400
amrbashir 1 year ago
parent
commit
59adf7b340

+ 7 - 0
.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.
+

+ 0 - 4
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.|

+ 4 - 0
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")

+ 10 - 33
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<Vec<String>> {
+  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)
 }

+ 19 - 4
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(())
 }

+ 55 - 0
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::<Vec<_>>();
+  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::<Vec<_>>();
+  let pm = PackageManager::from_project(app_dir)
+    .into_iter()
+    .next()
+    .unwrap_or(PackageManager::Npm);
+  pm.install(&npm_deps)?;
+
+  Ok(())
+}