1- use serde_json :: Value ;
1+ use std :: env :: args_os ;
22use std:: fs:: read;
3- use std:: path:: Path ;
3+ use std:: io:: { BufRead , BufReader } ;
4+ use std:: path:: { Path , PathBuf } ;
5+ use std:: process:: { exit, Command , Stdio } ;
6+
7+ use serde_json:: Value ;
48use urlencoding:: encode;
59
610use tauri:: ipc:: Response ;
11+ use tauri:: path:: BaseDirectory ;
12+ use tauri:: Manager ;
713use tauri_plugin_cli:: CliExt ;
814
915// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
@@ -21,65 +27,55 @@ pub fn run() {
2127 app. handle ( ) . plugin ( tauri_plugin_cli:: init ( ) ) ?;
2228
2329 // Default to GUI if the app was opened with no CLI args.
24- if std :: env :: args_os ( ) . count ( ) <= 1 {
30+ if args_os ( ) . count ( ) <= 1 {
2531 gui ( app. handle ( ) . clone ( ) , "" . to_owned ( ) ) ?;
2632 }
2733 match app. cli ( ) . matches ( ) {
2834 // `matches` here is a Struct with { args, subcommand }.
2935 // `args` is `HashMap<String, ArgData>` where `ArgData` is a struct with { value, occurrences }.
3036 // `subcommand` is `Option<Box<SubcommandMatches>>` where `SubcommandMatches` is a struct with { name, matches }.
31- Ok ( matches) => {
32- match matches. subcommand {
33- Some ( subcommand_matches) => {
34- match subcommand_matches. name . as_str ( ) {
35- "view" => {
36- match subcommand_matches. matches . args . get ( "trace" ) {
37- Some ( trace) => {
38- match & trace. value {
39- Value :: String ( trace_file) => {
40- // app.handle().plugin(tauri_plugin_fs::init())?;
41-
42- //let src = Path::new(trace_file);
43- //let name = src.file_name().unwrap();
44- //let tmp = app.path().app_local_data_dir().join("mytraces").join(name);
45- //fs::copy(src, &tmp)?;
37+ Ok ( matches) => match matches. subcommand {
38+ Some ( subcommand_matches) => match subcommand_matches. name . as_str ( ) {
39+ "run" => {
40+ if let Some ( source) = subcommand_matches. matches . args . get ( "source" ) {
41+ if let Value :: String ( source_file_path) = & source. value {
42+ let interpreter_path = app
43+ . path ( )
44+ . resolve ( "interpreter/" , BaseDirectory :: Resource ) ?;
4645
47- // allowed access to the trace directory
48- // let src = Path::new(&trace_file);
49- //let canon = fs::canonicalize(src)?;
50- //let abs = canon.as_path();
51- //println!("!!!!!!!!!!!! {:?}", abs);
52- //let src = &abs.display().to_string();
53-
54- let encoded = encode ( trace_file) ;
55- gui (
56- app. handle ( ) . clone ( ) ,
57- Path :: new ( "/local" )
58- . join ( encoded. as_ref ( ) )
59- . display ( )
60- . to_string ( ) ,
61- ) ?
62- }
63- _ => {
64- println ! ( "Usage: view <tracefile.json>" ) ;
65- std:: process:: exit ( 1 )
66- }
67- }
68- }
69- _ => {
70- println ! ( "Usage: view <tracefile.json>" ) ;
71- std:: process:: exit ( 1 )
72- }
73- }
46+ eval_pdl_program ( source_file_path. clone ( ) , interpreter_path) ?;
47+ exit ( 0 )
7448 }
75- _ => { }
7649 }
50+ println ! ( "Usage: run <source.pdl>" ) ;
51+ exit ( 1 )
7752 }
78- None => { }
79- }
80- //println!(" {:?}", matches);
81- //gui(app.handle().clone(), "".to_owned())?;
82- }
53+ "view" => match subcommand_matches. matches . args . get ( "trace" ) {
54+ Some ( trace) => match & trace. value {
55+ Value :: String ( trace_file) => {
56+ let encoded = encode ( trace_file) ;
57+ gui (
58+ app. handle ( ) . clone ( ) ,
59+ Path :: new ( "/local" )
60+ . join ( encoded. as_ref ( ) )
61+ . display ( )
62+ . to_string ( ) ,
63+ ) ?
64+ }
65+ _ => {
66+ println ! ( "Usage: view <tracefile.json>" ) ;
67+ exit ( 1 )
68+ }
69+ } ,
70+ _ => {
71+ println ! ( "Usage: view <tracefile.json>" ) ;
72+ exit ( 1 )
73+ }
74+ } ,
75+ _ => { }
76+ } ,
77+ None => { }
78+ } ,
8379 Err ( _) => { }
8480 }
8581 Ok ( ( ) )
@@ -101,3 +97,37 @@ fn gui(app: tauri::AppHandle, path: String) -> Result<(), tauri::Error> {
10197 . build ( ) ?;
10298 Ok ( ( ) )
10399}
100+
101+ #[ cfg( desktop) ]
102+ fn eval_pdl_program (
103+ source_file_path : String ,
104+ interpreter_path : PathBuf ,
105+ ) -> Result < ( ) , tauri:: Error > {
106+ println ! ( "Evaluating {:?}" , source_file_path) ;
107+ //let interp = interpreter_path.display().to_string()
108+ let activate = interpreter_path. join ( "bin/activate" ) . display ( ) . to_string ( ) ;
109+ let mut child = Command :: new ( "sh" )
110+ . args ( [
111+ "-c" ,
112+ & [
113+ "source" ,
114+ activate. as_str ( ) ,
115+ "; pdl" ,
116+ source_file_path. as_str ( ) ,
117+ ]
118+ . join ( " " ) ,
119+ ] )
120+ . stdout ( Stdio :: piped ( ) )
121+ . spawn ( )
122+ . unwrap ( ) ;
123+
124+ let stdout = child. stdout . take ( ) . unwrap ( ) ;
125+
126+ // Stream output.
127+ let lines = BufReader :: new ( stdout) . lines ( ) ;
128+ for line in lines {
129+ println ! ( "{}" , line. unwrap( ) ) ;
130+ }
131+
132+ Ok ( ( ) )
133+ }
0 commit comments