Add nu script execution capabilities

Signed-off-by: trivernis <trivernis@protonmail.com>
init-main
trivernis 2 years ago
commit 0c7bbe6034
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

2
.gitignore vendored

@ -0,0 +1,2 @@
/target
test.nu

3871
Cargo.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1,24 @@
[package]
name = "tourmaline"
version = "0.1.0"
edition = "2021"
[lib]
name = "tourmaline"
[[bin]]
name = "tml"
path = "src/main.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "3.2.22"
color-eyre = "0.6.2"
nu-cli = "0.68.1"
nu-command = "0.68.1"
nu-protocol = "0.68.1"
thiserror = "1.0.35"
tokio = { version = "1.21.1", features = ["rt", "io-std", "io-util", "process", "time", "macros", "tracing", "fs"] }
tracing = "0.1.36"
tracing-subscriber = "0.3.15"

@ -0,0 +1,7 @@
use scripting::executor::NuExecutor;
mod scripting;
pub fn test_execute(script: String) {
NuExecutor::new(script).execute();
}

@ -0,0 +1,6 @@
use std::env::args;
fn main() {
let script_path = args().skip(1).next().unwrap();
tourmaline::test_execute(script_path);
}

@ -0,0 +1,48 @@
#![allow(unused)]
use nu_protocol::{PipelineData, Span};
/// An executor for nu scripts
pub struct NuExecutor {
script_path: String,
args: Vec<String>,
}
impl NuExecutor {
pub fn new(script_path: String) -> Self {
Self {
script_path,
args: Vec::new(),
}
}
pub fn add_arg<S: ToString>(&mut self, arg: S) -> &mut Self {
self.args.push(arg.to_string());
self
}
pub fn add_args<S: ToString, I: IntoIterator<Item = S>>(&mut self, args: I) -> &mut Self {
let mut args = args.into_iter().map(|a| a.to_string()).collect::<Vec<_>>();
self.args.append(&mut args);
self
}
pub fn execute(&mut self) {
let mut engine_state = nu_command::create_default_context();
let mut stack = nu_protocol::engine::Stack::new();
let input = PipelineData::new(Span::new(0, 0));
let init_cwd = nu_cli::get_init_cwd();
nu_cli::gather_parent_env_vars(&mut engine_state, &init_cwd);
nu_cli::evaluate_file(
self.script_path.clone(),
&self.args,
&mut engine_state,
&mut stack,
input,
false,
)
.unwrap();
}
}

@ -0,0 +1,2 @@
pub mod executor;
mod loader;
Loading…
Cancel
Save