Skip to content

Commit 319d978

Browse files
committed
feat: embed PDL interpreter into react (tauri) app builds
```shell PDL run <source.pdl> ``` Signed-off-by: Nick Mitchell <[email protected]>
1 parent feab86b commit 319d978

File tree

3 files changed

+99
-54
lines changed

3 files changed

+99
-54
lines changed

pdl-live-react/package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
"scripts": {
77
"dev": "vite",
88
"build": "tsc && vite build",
9-
"build:app": "npm run tauri build",
9+
"build:app": "npm run prep_interpreter && npm run tauri build",
1010
"lint": "eslint .",
1111
"format": "prettier --write 'tests/**/*.ts' 'src/**/*.{ts,tsx,css}' && (cd src-tauri && cargo fmt)",
1212
"tauri": "tauri",
1313
"test:quality": "concurrently -n 'lint,types,formatting' 'npm run lint' 'tsc --build --noEmit' \"prettier --check 'tests/**/*.ts' 'src/**/*.{ts,tsx,css}'\"",
1414
"test:ui": "playwright install --with-deps && playwright test",
1515
"types": "(cd .. && python -m src.pdl.pdl --schema > src/pdl/pdl-schema.json) && json2ts ../src/pdl/pdl-schema.json src/pdl_ast.d.ts --unreachableDefinitions",
1616
"test": "concurrently -n 'quality,playwright' 'npm run test:quality' 'npm run test:ui'",
17-
"view": "./src-tauri/target/debug/PDL view",
18-
"start": "npm run tauri dev"
17+
"pdl": "./src-tauri/target/debug/PDL",
18+
"view": "npm run pdl view",
19+
"prep_interpreter": "if [ ! -d .venv ]; then python3.12 -mvenv .venv; source .venv/bin/activate; pip install -e ..; fi",
20+
"start": "npm run prep_interpreter && npm run tauri dev"
1921
},
2022
"dependencies": {
2123
"@patternfly/react-code-editor": "^6.1.0",
Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
use serde_json::Value;
1+
use std::env::args_os;
22
use 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;
48
use urlencoding::encode;
59

610
use tauri::ipc::Response;
11+
use tauri::path::BaseDirectory;
12+
use tauri::Manager;
713
use 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+
}

pdl-live-react/src-tauri/tauri.conf.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
"args": [
2121
],
2222
"subcommands": {
23+
"run": {
24+
"description": "Run a PDL program",
25+
"args": [
26+
{
27+
"name": "source",
28+
"index": 1,
29+
"takesValue": true
30+
}
31+
]
32+
},
2333
"view": {
2434
"description": "View a trace",
2535
"args": [
@@ -36,6 +46,9 @@
3646
"bundle": {
3747
"active": true,
3848
"targets": "all",
49+
"resources": {
50+
"../.venv/": "interpreter/"
51+
},
3952
"icon": [
4053
"icons/32x32.png",
4154
"icons/128x128.png",

0 commit comments

Comments
 (0)