@@ -6,15 +6,14 @@ use crate::{
6
6
helpers:: { app_paths:: walk_builder, cargo, npm:: PackageManager } ,
7
7
Result ,
8
8
} ;
9
+ use anyhow:: Context ;
9
10
10
- use std:: {
11
- fs:: { read_to_string, write} ,
12
- path:: Path ,
13
- } ;
11
+ use std:: { fs, path:: Path } ;
14
12
15
13
const CORE_API_MODULES : & [ & str ] = & [ "dpi" , "event" , "path" , "core" , "window" , "mocks" ] ;
16
14
const JS_EXTENSIONS : & [ & str ] = & [ "js" , "jsx" , "ts" , "tsx" , "mjs" ] ;
17
15
16
+ /// Returns a list of paths that could not be migrated
18
17
pub fn migrate ( app_dir : & Path , tauri_dir : & Path ) -> Result < ( ) > {
19
18
let mut new_npm_packages = Vec :: new ( ) ;
20
19
let mut new_cargo_packages = Vec :: new ( ) ;
@@ -24,19 +23,21 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
24
23
. next ( )
25
24
. unwrap_or ( PackageManager :: Npm ) ;
26
25
27
- let tauri_api_import_regex = regex:: Regex :: new ( r"@tauri-apps/api/(\w+)" ) . unwrap ( ) ;
26
+ let tauri_api_import_regex = regex:: bytes :: Regex :: new ( r"@tauri-apps/api/(\w+)" ) . unwrap ( ) ;
28
27
29
28
for entry in walk_builder ( app_dir) . build ( ) . flatten ( ) {
30
29
if entry. file_type ( ) . map ( |t| t. is_file ( ) ) . unwrap_or_default ( ) {
31
30
let path = entry. path ( ) ;
32
31
let ext = path. extension ( ) . unwrap_or_default ( ) ;
33
32
if JS_EXTENSIONS . iter ( ) . any ( |e| e == & ext) {
34
- let js_contents = read_to_string ( path) ?;
33
+ let js_contents = fs :: read ( path) ?;
35
34
36
35
let new_contents =
37
- tauri_api_import_regex. replace_all ( & js_contents, |cap : & regex:: Captures < ' _ > | {
38
- let module = cap. get ( 1 ) . unwrap ( ) . as_str ( ) ;
39
- let original = cap. get ( 0 ) . unwrap ( ) . as_str ( ) ;
36
+ tauri_api_import_regex. replace_all ( & js_contents, |cap : & regex:: bytes:: Captures < ' _ > | {
37
+ let module = cap. get ( 1 ) . unwrap ( ) . as_bytes ( ) ;
38
+ let module = String :: from_utf8_lossy ( module) . to_string ( ) ;
39
+ let original = cap. get ( 0 ) . unwrap ( ) . as_bytes ( ) ;
40
+ let original = String :: from_utf8_lossy ( original) . to_string ( ) ;
40
41
41
42
if module == "tauri" {
42
43
let new = "@tauri-apps/api/core" . to_string ( ) ;
@@ -46,8 +47,8 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
46
47
let new = "@tauri-apps/api/webviewWindow" . to_string ( ) ;
47
48
log:: info!( "Replacing `{original}` with `{new}` on {}" , path. display( ) ) ;
48
49
new
49
- } else if CORE_API_MODULES . contains ( & module) {
50
- original. to_string ( )
50
+ } else if CORE_API_MODULES . contains ( & module. as_str ( ) ) {
51
+ original
51
52
} else {
52
53
let plugin = format ! ( "@tauri-apps/plugin-{module}" ) ;
53
54
log:: info!(
@@ -61,7 +62,7 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
61
62
if module == "clipboard" {
62
63
"clipboard-manager"
63
64
} else {
64
- module
65
+ & module
65
66
}
66
67
) ) ;
67
68
@@ -70,18 +71,21 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
70
71
} ) ;
71
72
72
73
if new_contents != js_contents {
73
- write ( path, new_contents. as_bytes ( ) ) ?;
74
+ fs:: write ( path, new_contents)
75
+ . with_context ( || format ! ( "Error writing {}" , path. display( ) ) ) ?;
74
76
}
75
77
}
76
78
}
77
79
}
78
80
79
81
if !new_npm_packages. is_empty ( ) {
80
- pm. install ( & new_npm_packages) ?;
82
+ pm. install ( & new_npm_packages)
83
+ . context ( "Error installing new npm packages" ) ?;
81
84
}
82
85
83
86
if !new_cargo_packages. is_empty ( ) {
84
- cargo:: install ( & new_cargo_packages, Some ( tauri_dir) ) ?;
87
+ cargo:: install ( & new_cargo_packages, Some ( tauri_dir) )
88
+ . context ( "Error installing new Cargo packages" ) ?;
85
89
}
86
90
87
91
Ok ( ( ) )
0 commit comments