Skip to content

Commit

Permalink
feat(cli/migrate): migrate existing v1 plugins
Browse files Browse the repository at this point in the history
closes #9400
  • Loading branch information
amrbashir committed Apr 22, 2024
1 parent aaa332c commit 59adf7b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .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.

4 changes: 0 additions & 4 deletions core/tauri/permissions/webview/autogenerated/reference.md
Expand Up @@ -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 changes: 4 additions & 0 deletions tooling/cli/src/helpers/npm.rs
Expand Up @@ -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")
Expand Down
43 changes: 10 additions & 33 deletions tooling/cli/src/migrate/frontend.rs
Expand Up @@ -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};
Expand All @@ -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();

Expand All @@ -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;
Expand All @@ -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)
}
23 changes: 19 additions & 4 deletions tooling/cli/src/migrate/mod.rs
Expand Up @@ -3,33 +3,48 @@
// 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;

mod config;
mod frontend;
mod manifest;
mod v1_plugins;

pub fn command() -> Result<()> {
let tauri_dir = tauri_dir();
let app_dir = app_dir();

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 changes: 55 additions & 0 deletions 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(())
}

0 comments on commit 59adf7b

Please sign in to comment.