11use :: std:: fs:: { copy, create_dir_all} ;
2- use :: std:: path:: PathBuf ;
2+ use :: std:: path:: { Path , PathBuf } ;
33
44use duct:: cmd;
5- use file_diff:: diff;
65use tauri:: path:: BaseDirectory ;
76use tauri:: Manager ;
87
9- #[ cfg( desktop) ]
10- pub async fn pip_install_if_needed ( app_handle : tauri:: AppHandle ) -> Result < PathBuf , tauri:: Error > {
11- let cache_path = app_handle. path ( ) . cache_dir ( ) ?. join ( "pdl" ) ;
8+ use crate :: interpreter:: shasum;
129
10+ #[ cfg( desktop) ]
11+ pub async fn pip_install_if_needed (
12+ cache_path : & Path ,
13+ requirements_path : & Path ,
14+ ) -> Result < PathBuf , tauri:: Error > {
1315 create_dir_all ( & cache_path) ?;
14- let venv_path = cache_path. join ( "interpreter-python" ) ;
15- let activate_path = if cfg ! ( windows) {
16- venv_path. join ( "Scripts" ) . join ( "Activate.ps1" )
17- } else {
18- venv_path. join ( "bin/activate" )
19- } ;
20- let cached_requirements_path = venv_path
21- . join ( "requirements.txt" )
22- . into_os_string ( )
23- . into_string ( )
24- . unwrap ( ) ;
25- /* println!(
26- "RUN PATHS activate={:?} cached_reqs={:?}",
27- activate_path, cached_requirements_path
28- ); */
16+
17+ let hash = shasum:: sha256sum ( & requirements_path) ?;
18+ let venv_path = cache_path. join ( hash) ;
19+ let bin_path = venv_path. join ( if cfg ! ( windows) { "Scripts" } else { "bin" } ) ;
2920
3021 if !venv_path. exists ( ) {
3122 println ! ( "Creating virtual environment..." ) ;
@@ -35,34 +26,26 @@ pub async fn pip_install_if_needed(app_handle: tauri::AppHandle) -> Result<PathB
3526 "python3"
3627 } ;
3728 cmd ! ( python, "-mvenv" , & venv_path) . run ( ) ?;
29+
30+ cmd ! ( bin_path. join( "pip" ) , "install" , "-r" , & requirements_path, ) . run ( ) ?;
31+
32+ let cached_requirements_path = venv_path. join ( "requirements.txt" ) ;
33+ copy ( requirements_path, cached_requirements_path) ?;
3834 }
3935
36+ Ok ( bin_path. to_path_buf ( ) )
37+ }
38+
39+ #[ cfg( desktop) ]
40+ pub async fn pip_install_interpreter_if_needed (
41+ app_handle : tauri:: AppHandle ,
42+ ) -> Result < PathBuf , tauri:: Error > {
43+ // the interpreter requirements.txt
4044 let requirements_path = app_handle
4145 . path ( )
42- . resolve ( "interpreter/requirements.txt" , BaseDirectory :: Resource ) ?
43- . into_os_string ( )
44- . into_string ( )
45- . unwrap ( ) ;
46+ . resolve ( "interpreter/requirements.txt" , BaseDirectory :: Resource ) ?;
4647
47- if !diff (
48- requirements_path. as_str ( ) ,
49- cached_requirements_path. as_str ( ) ,
50- ) {
51- cmd ! (
52- venv_path
53- . join( if cfg!( windows) { "Scripts" } else { "bin" } )
54- . join( "pip" ) ,
55- "install" ,
56- "-r" ,
57- & requirements_path,
58- )
59- . run ( ) ?;
60-
61- copy ( requirements_path, cached_requirements_path) ?;
62- }
48+ let cache_path = app_handle. path ( ) . cache_dir ( ) ?. join ( "pdl" ) ;
6349
64- match activate_path. parent ( ) {
65- Some ( parent) => Ok ( parent. to_path_buf ( ) ) ,
66- _ => Err ( tauri:: Error :: UnknownPath ) ,
67- }
50+ pip_install_if_needed ( & cache_path, & requirements_path) . await
6851}
0 commit comments