diff --git a/Cargo.lock b/Cargo.lock index ebd2a91..c8327bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ checksum = "b03f7fbd470aa8b3ad163c85cce8bccfc11cc9c44ef12da0a4eddd98bd307352" [[package]] name = "spl" -version = "0.1.5" +version = "0.1.6" dependencies = [ "multicall", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index abc24ab..5e48b28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "spl" -version = "0.1.5" +version = "0.1.6" edition = "2021" description = "Stack Pogramming Language: A simple, concise scripting language." license = "MIT" diff --git a/src/main.rs b/src/main.rs index ad9ed51..b1772ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,20 +7,38 @@ fn main() { let arg = &args .next() .unwrap_or_else(|| find_in_splpath("repl.spl").expect("no file to be run")); - if arg == "--build" { + if arg == "--build" || arg == "--run" { let file = args.next().unwrap(); let data = fs::read_to_string(file.clone()).expect("unable to read specified file"); - println!("Building SPL with specified natives file..."); - let mut builder = RustAppBuilder::new(); - if let Some(name) = args.next() { - builder.set_name(name); + let build_only = arg == "--build"; + if build_only { + println!("Building SPL with specified natives file..."); + } + let mut builder = RustAppBuilder::new(); + if build_only { + if let Some(name) = args.next() { + builder.set_name(name); + } + println!("Embedding source..."); + } + builder.add_source(file.to_owned(), data.to_owned()); + if build_only { + println!("Preparing rust code..."); } - println!("Embedding source..."); - builder.add_source(file, data.to_owned()); - println!("Preparing rust code..."); builder.prepare(lex(data.to_owned()).expect("invalid SPL in natives file.")); - println!("Building..."); - println!("Built! Binary is {}", builder.build().unwrap().get_binary()); + if build_only { + println!("Building..."); + } + let app = builder.build(build_only).unwrap(); + if build_only { + println!("Built! Binary is {}", app.get_binary()); + } else { + let mut args: Vec = args.collect(); + args.insert(0, file); + app.execute(args).unwrap(); + app.delete(); + } + return; } if let Err(x) = start_file(arg) { diff --git a/src/oxidizer/mod.rs b/src/oxidizer/mod.rs index abc2d14..1fb2804 100644 --- a/src/oxidizer/mod.rs +++ b/src/oxidizer/mod.rs @@ -4,10 +4,12 @@ use std::{ collections::{hash_map::DefaultHasher, HashMap}, - env, fs, + env, + ffi::OsStr, + fs, hash::{Hash, Hasher}, io, - process::{Child, Command}, + process::{Child, Command, Stdio}, }; use crate::{FuncImplType, Keyword, Word, Words}; @@ -18,6 +20,8 @@ mod splrs; pub struct RustApp { /// The path to the binary binary: String, + /// The path to the project dir + dir: String, } impl RustApp { @@ -27,9 +31,18 @@ impl RustApp { } /// Executes the binary with some args - pub fn execute(&self, args: Vec<&str>) -> Result { + pub fn execute(&self, args: I) -> Result + where + I: IntoIterator, + S: AsRef, + { Command::new(self.binary.clone()).args(args).spawn() } + + /// Deletes the binary and rust project + pub fn delete(self) { + fs::remove_dir_all(self.dir).expect("unable to delete RustApp"); + } } /// A rust function which was embedded in SPL @@ -99,7 +112,7 @@ impl RustAppBuilder { } /// Builds the desired app, including literally building it using cargo. - pub fn build(self) -> Result { + pub fn build(self, output: bool) -> Result { // we need a temp folder! let tmp = "."; // TODO replace? let name = match self.name { @@ -115,16 +128,20 @@ impl RustAppBuilder { .arg("new") .arg(format!("spl-{name}")) .current_dir(tmp) + .stdout(Stdio::null()) + .stderr(Stdio::null()) .spawn() .unwrap() - .wait_with_output(); + .wait(); Command::new("cargo") .arg("add") .arg(format!("spl@{}", env!("CARGO_PKG_VERSION"))) .current_dir(format!("{tmp}/spl-{name}")) + .stdout(Stdio::null()) + .stderr(Stdio::null()) .spawn() .unwrap() - .wait_with_output()?; + .wait()?; let mut runtime_init = String::new(); let mut code = String::new(); for func in self.rust_functions.into_iter().enumerate() { @@ -169,10 +186,21 @@ impl RustAppBuilder { .arg("build") .arg("--release") .current_dir(format!("{tmp}/spl-{name}")) + .stdout(if output { + Stdio::inherit() + } else { + Stdio::null() + }) + .stderr(if output { + Stdio::inherit() + } else { + Stdio::null() + }) .spawn() .unwrap() - .wait_with_output()?; + .wait()?; Ok(RustApp { + dir: format!("{tmp}/spl-{name}"), binary: { // insanity. will have to clean this up at some point. let dir = format!("{tmp}/spl-{name}/target/release/");