@@ -26,6 +26,7 @@ use py_lib_pyo3 as py_lib;
2626
2727pub use error:: { Error , Result } ;
2828use models:: * ;
29+ use std:: path:: PathBuf ;
2930
3031#[ cfg( desktop) ]
3132use desktop:: Python ;
@@ -63,21 +64,14 @@ impl<R: Runtime, T: Manager<R>> crate::PythonExt<R> for T {
6364 }
6465}
6566
66- fn read_main_py_from_resources < R : Runtime > ( app : & AppHandle < R > ) -> String {
67- let py_file_path = app
68- . path ( )
69- . resolve ( "src-python/main.py" , BaseDirectory :: Resource )
70- . unwrap_or_default ( ) ;
71- std:: fs:: read_to_string ( & py_file_path) . unwrap_or_default ( )
67+ fn get_resource_dir < R : Runtime > ( app : & AppHandle < R > ) -> PathBuf {
68+ app. path ( )
69+ . resolve ( "src-python" , BaseDirectory :: Resource )
70+ . unwrap_or_default ( )
7271}
7372
74- fn read_main_py_from_current_dir ( ) -> String {
75- let py_file_path = std:: env:: current_dir ( )
76- . unwrap ( )
77- . join ( "src-python" )
78- . join ( "main.py" ) ;
79- std:: fs:: read_to_string ( py_file_path) . unwrap_or_default ( )
80- // include_str!(concat!(env!("PWD"), "/src-tauri/src-python/main.py"))
73+ fn get_current_dir ( ) -> PathBuf {
74+ std:: env:: current_dir ( ) . unwrap ( ) . join ( "src-python" )
8175}
8276
8377/// Initializes the plugin with functions
@@ -101,17 +95,19 @@ pub fn init_and_register<R: Runtime>(python_functions: Vec<&'static str>) -> Tau
10195 let python = desktop:: init ( app, api) ?;
10296 app. manage ( python) ;
10397
104- let mut code = read_main_py_from_resources ( app) ;
98+ let mut dir = get_resource_dir ( app) ;
99+ let mut code = std:: fs:: read_to_string ( & dir. join ( "main.py" ) ) . unwrap_or_default ( ) ;
105100 if code. is_empty ( ) {
106101 println ! (
107102 "Warning: 'src-tauri/main.py' seems not to be registered in 'tauri.conf.json'"
108103 ) ;
109- code = read_main_py_from_current_dir ( ) ;
104+ dir = get_current_dir ( ) ;
105+ code = std:: fs:: read_to_string ( & dir. join ( "main.py" ) ) . unwrap_or_default ( ) ;
110106 }
111107 if code. is_empty ( ) {
112108 println ! ( "ERROR: Error reading 'src-tauri/main.py'" ) ;
113109 }
114- py_lib:: init_python ( code) . unwrap ( ) ;
110+ py_lib:: init_python ( code, dir ) . unwrap ( ) ;
115111 for function_name in python_functions {
116112 py_lib:: register_function_str ( function_name. into ( ) , None ) . unwrap ( ) ;
117113 }
0 commit comments