Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): fix migration from globalShortcut to global-shortcut #9262

Open
wants to merge 10 commits into
base: dev
Choose a base branch
from
9 changes: 9 additions & 0 deletions .changes/fix-cli-multi-run-migrate-and-package-names.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"tauri": patch:bug
---

Fix multiple runs of migration and some package name changes.
- Skip all core plugin migrations.
- Don't exit when `tauri::Builder` is not found.
- Skip overwriting existing migrated capabilities.
- `globalShortcut` now migrates to `global-shortcut`.
9 changes: 8 additions & 1 deletion tooling/cli/src/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,14 @@ pub fn command(options: Options) -> Result<()> {

let re = Regex::new(r"(tauri\s*::\s*Builder\s*::\s*default\(\))(\s*)")?;
for file in [tauri_dir.join("src/main.rs"), tauri_dir.join("src/lib.rs")] {
let contents = std::fs::read_to_string(&file)?;
let contents = std::fs::read_to_string(&file).or_else(|e| {
if e.kind() == std::io::ErrorKind::NotFound {
// It's acceptable to not find the file; should continue to the warning code below
Ok("".into())
} else {
Err(e)
}
})?;

if contents.contains(&plugin_init) {
log::info!(
Expand Down
62 changes: 35 additions & 27 deletions tooling/cli/src/migrate/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,43 @@ pub fn migrate(tauri_dir: &Path) -> Result<MigratedConfig> {
let migrated = migrate_config(&mut config)?;
write(&config_path, serde_json::to_string_pretty(&config)?)?;

let mut permissions: Vec<PermissionEntry> = vec![
"path:default",
"event:default",
"window:default",
"app:default",
"resources:default",
"menu:default",
"tray:default",
]
.into_iter()
.map(|p| PermissionEntry::PermissionRef(p.to_string().try_into().unwrap()))
.collect();
permissions.extend(migrated.permissions.clone());

let capabilities_path = config_path.parent().unwrap().join("capabilities");
create_dir_all(&capabilities_path)?;
write(
capabilities_path.join("migrated.json"),
serde_json::to_string_pretty(&Capability {
identifier: "migrated".to_string(),
description: "permissions that were migrated from v1".into(),
local: true,
remote: None,
windows: vec!["main".into()],
webviews: vec![],
permissions,
platforms: None,
})?,
)?;
let migrated_capabilities = capabilities_path.join("migrated.json");
if !migrated_capabilities.exists() {
let mut permissions: Vec<PermissionEntry> = vec![
"path:default",
"event:default",
"window:default",
"app:default",
"resources:default",
"menu:default",
"tray:default",
]
.into_iter()
.map(|p| PermissionEntry::PermissionRef(p.to_string().try_into().unwrap()))
.collect();
permissions.extend(migrated.permissions.clone());

write(
migrated_capabilities,
serde_json::to_string_pretty(&Capability {
identifier: "migrated".to_string(),
description: "permissions that were migrated from v1".into(),
local: true,
remote: None,
windows: vec!["main".into()],
webviews: vec![],
permissions,
platforms: None,
})?,
)?;
} else {
log::info!(
"Skipping capabilities migration; migrated capabilities found in {}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also print the migrated capability json here? so the user can compare both files (or maybe write to migrates.json.tmp)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The established pattern so far has been to print any manual changes. Rather than a temporary file that the user is expected to act on, would it be better to check if migrated.permissions is empty and, if not, make another file without the *:defaults?

migrated_capabilities.display()
);
}

return Ok(migrated);
}
Expand Down
25 changes: 23 additions & 2 deletions tooling/cli/src/migrate/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ use std::{
path::Path,
};

const CORE_API_MODULES: &[&str] = &["dpi", "event", "path", "core", "window", "mocks"];
const CORE_API_MODULES: &[&str] = &[
"app",
"core",
"dpi",
"event",
"menu",
"mocks",
"path",
"tray",
"webview",
"webviewWindow",
"window",
];
const JS_EXTENSIONS: &[&str] = &["js", "jsx", "ts", "tsx", "mjs"];

pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
Expand Down Expand Up @@ -49,7 +61,14 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
} else if CORE_API_MODULES.contains(&module) {
original.to_string()
} else {
let plugin = format!("@tauri-apps/plugin-{module}");
let plugin = format!(
"@tauri-apps/plugin-{}",
if module == "globalShortcut" {
"global-shortcut"
} else {
module
}
);
log::info!(
"Replacing `{original}` with `{plugin}` on {}",
path.display()
Expand All @@ -60,6 +79,8 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
"tauri-plugin-{}",
if module == "clipboard" {
"clipboard-manager"
} else if module == "globalShortcut" {
"global-shortcut"
} else {
module
}
Expand Down