@@ -6,15 +6,14 @@ use crate::{
66 helpers:: { app_paths:: walk_builder, cargo, npm:: PackageManager } ,
77 Result ,
88} ;
9+ use anyhow:: Context ;
910
10- use std:: {
11- fs:: { read_to_string, write} ,
12- path:: Path ,
13- } ;
11+ use std:: { fs, path:: Path } ;
1412
1513const CORE_API_MODULES : & [ & str ] = & [ "dpi" , "event" , "path" , "core" , "window" , "mocks" ] ;
1614const JS_EXTENSIONS : & [ & str ] = & [ "js" , "jsx" , "ts" , "tsx" , "mjs" ] ;
1715
16+ /// Returns a list of paths that could not be migrated
1817pub fn migrate ( app_dir : & Path , tauri_dir : & Path ) -> Result < ( ) > {
1918 let mut new_npm_packages = Vec :: new ( ) ;
2019 let mut new_cargo_packages = Vec :: new ( ) ;
@@ -24,19 +23,21 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
2423 . next ( )
2524 . unwrap_or ( PackageManager :: Npm ) ;
2625
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 ( ) ;
2827
2928 for entry in walk_builder ( app_dir) . build ( ) . flatten ( ) {
3029 if entry. file_type ( ) . map ( |t| t. is_file ( ) ) . unwrap_or_default ( ) {
3130 let path = entry. path ( ) ;
3231 let ext = path. extension ( ) . unwrap_or_default ( ) ;
3332 if JS_EXTENSIONS . iter ( ) . any ( |e| e == & ext) {
34- let js_contents = read_to_string ( path) ?;
33+ let js_contents = fs :: read ( path) ?;
3534
3635 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 ( ) ;
4041
4142 if module == "tauri" {
4243 let new = "@tauri-apps/api/core" . to_string ( ) ;
@@ -46,8 +47,8 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
4647 let new = "@tauri-apps/api/webviewWindow" . to_string ( ) ;
4748 log:: info!( "Replacing `{original}` with `{new}` on {}" , path. display( ) ) ;
4849 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
5152 } else {
5253 let plugin = format ! ( "@tauri-apps/plugin-{module}" ) ;
5354 log:: info!(
@@ -61,7 +62,7 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
6162 if module == "clipboard" {
6263 "clipboard-manager"
6364 } else {
64- module
65+ & module
6566 }
6667 ) ) ;
6768
@@ -70,18 +71,21 @@ pub fn migrate(app_dir: &Path, tauri_dir: &Path) -> Result<()> {
7071 } ) ;
7172
7273 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( ) ) ) ?;
7476 }
7577 }
7678 }
7779 }
7880
7981 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" ) ?;
8184 }
8285
8386 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" ) ?;
8589 }
8690
8791 Ok ( ( ) )
0 commit comments