|
1 | 1 | use ::std::collections::HashMap;
|
2 | 2 | use ::std::error::Error;
|
| 3 | +use ::std::ffi::OsStr; |
3 | 4 | use ::std::fs::File;
|
4 | 5 | use ::std::io::BufReader;
|
| 6 | +use ::std::path::{Path, PathBuf}; |
5 | 7 |
|
| 8 | +use duct::cmd; |
| 9 | +use futures::executor::block_on; |
6 | 10 | use serde::Deserialize;
|
7 | 11 | use serde_json::{from_reader, json, to_string, Value};
|
| 12 | +use tempfile::Builder; |
8 | 13 |
|
9 | 14 | use crate::interpreter::ast::{PdlBaseType, PdlBlock, PdlOptionalType, PdlParser, PdlType};
|
| 15 | +use crate::interpreter::pip::pip_install_internal_if_needed; |
10 | 16 |
|
11 | 17 | macro_rules! zip {
|
12 | 18 | ($x: expr) => ($x);
|
@@ -276,18 +282,69 @@ fn tool_imports(object: &String) -> (&str, &str) {
|
276 | 282 | }
|
277 | 283 | }
|
278 | 284 |
|
| 285 | +fn python_source_to_json( |
| 286 | + app_handle: tauri::AppHandle, |
| 287 | + source_file_path: &String, |
| 288 | + debug: &bool, |
| 289 | +) -> Result<PathBuf, Box<dyn Error>> { |
| 290 | + if *debug { |
| 291 | + eprintln!("Compiling from Python source"); |
| 292 | + } |
| 293 | + let bin_path = block_on(pip_install_internal_if_needed( |
| 294 | + app_handle, |
| 295 | + &"interpreter/beeai-requirements.txt", |
| 296 | + ))?; |
| 297 | + |
| 298 | + let dry_run_file_path = Builder::new() |
| 299 | + .prefix(&"pdl-bee") |
| 300 | + .suffix(".json") |
| 301 | + .tempfile()?; |
| 302 | + let (_f, dry_run_file) = dry_run_file_path.keep()?; |
| 303 | + |
| 304 | + let args = vec![source_file_path]; |
| 305 | + |
| 306 | + cmd(bin_path.join("python"), &args) |
| 307 | + .env("DRY_RUN", "True") |
| 308 | + .env("DRY_RUN_FILE", &dry_run_file) |
| 309 | + .run()?; |
| 310 | + |
| 311 | + if *debug { |
| 312 | + eprintln!( |
| 313 | + "Finished generating BeeAi JSON snapshot to {:?}", |
| 314 | + &dry_run_file |
| 315 | + ) |
| 316 | + } |
| 317 | + Ok(dry_run_file) |
| 318 | +} |
| 319 | + |
279 | 320 | pub fn compile(
|
| 321 | + app_handle: tauri::AppHandle, |
280 | 322 | source_file_path: &String,
|
281 | 323 | output_path: &String,
|
282 | 324 | debug: &bool,
|
283 | 325 | ) -> Result<(), Box<dyn Error>> {
|
284 |
| - println!("Compiling beeai {} to {}", source_file_path, output_path); |
| 326 | + if *debug { |
| 327 | + eprintln!("Compiling beeai {} to {}", source_file_path, output_path); |
| 328 | + } |
285 | 329 |
|
286 |
| - // Open the file in read-only mode with buffer. |
287 |
| - let file = File::open(source_file_path)?; |
288 |
| - let reader = BufReader::new(file); |
| 330 | + let file = match Path::new(source_file_path) |
| 331 | + .extension() |
| 332 | + .and_then(OsStr::to_str) |
| 333 | + { |
| 334 | + Some("py") => { |
| 335 | + let json_snapshot_file = python_source_to_json(app_handle, source_file_path, debug)?; |
| 336 | + File::open(json_snapshot_file) |
| 337 | + } |
| 338 | + _ => { |
| 339 | + if *debug { |
| 340 | + eprintln!("Compiling from JSON snapshot"); |
| 341 | + } |
| 342 | + File::open(source_file_path) |
| 343 | + } |
| 344 | + }?; |
289 | 345 |
|
290 | 346 | // Read the JSON contents of the file as an instance of `User`.
|
| 347 | + let reader = BufReader::new(file); |
291 | 348 | let bee: BeeAiProgram = from_reader(reader)?;
|
292 | 349 |
|
293 | 350 | let inputs: Vec<PdlBlock> = bee
|
|
0 commit comments