|
|
@ -5,11 +5,7 @@ use serde::Serialize;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::error::AppResult;
|
|
|
|
use crate::error::AppResult;
|
|
|
|
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
use super::{executor::NuExecutor, record::RecordValue, record_serializer::RecordSerializer};
|
|
|
|
executor::{NuExecutor, VarValue},
|
|
|
|
|
|
|
|
record::RecordValue,
|
|
|
|
|
|
|
|
record_serializer::RecordSerializer,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A trait implemented for a given nu script type to
|
|
|
|
/// A trait implemented for a given nu script type to
|
|
|
|
/// associate arguments
|
|
|
|
/// associate arguments
|
|
|
@ -20,16 +16,24 @@ pub trait Script {
|
|
|
|
/// This function is used by the loader to load the associated file
|
|
|
|
/// This function is used by the loader to load the associated file
|
|
|
|
/// The name needs to include the file extension
|
|
|
|
/// The name needs to include the file extension
|
|
|
|
fn get_name() -> &'static str;
|
|
|
|
fn get_name() -> &'static str;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the name of the script file that get's executed before
|
|
|
|
|
|
|
|
/// the actual script. This has to be the full file name including the extension.
|
|
|
|
|
|
|
|
fn get_pre_hook() -> &'static str;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns the name of the script file that get's executed after
|
|
|
|
|
|
|
|
/// the actual script. This has to be the full file name including the extension.
|
|
|
|
|
|
|
|
fn get_post_hook() -> &'static str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Script arguments that can be collected in a Vec to
|
|
|
|
/// Script arguments that can be collected in a Vec to
|
|
|
|
/// be passed to the script
|
|
|
|
/// be passed to the script
|
|
|
|
pub trait ScriptArgs {
|
|
|
|
pub trait ScriptArgs {
|
|
|
|
fn get_args(self) -> Vec<RecordValue>;
|
|
|
|
fn get_args(&self) -> Vec<RecordValue>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: Serialize> ScriptArgs for T {
|
|
|
|
impl<T: Serialize> ScriptArgs for T {
|
|
|
|
fn get_args(self) -> Vec<RecordValue> {
|
|
|
|
fn get_args(&self) -> Vec<RecordValue> {
|
|
|
|
let mut serializer = RecordSerializer::default();
|
|
|
|
let mut serializer = RecordSerializer::default();
|
|
|
|
let val = self.serialize(&mut serializer).unwrap();
|
|
|
|
let val = self.serialize(&mut serializer).unwrap();
|
|
|
|
match val {
|
|
|
|
match val {
|
|
|
@ -55,19 +59,17 @@ impl<S: Script> NuScript<S> {
|
|
|
|
|
|
|
|
|
|
|
|
/// Executes the script with the given args
|
|
|
|
/// Executes the script with the given args
|
|
|
|
#[tracing::instrument(level = "trace", skip(self))]
|
|
|
|
#[tracing::instrument(level = "trace", skip(self))]
|
|
|
|
pub async fn execute(&self, args: S::Args) -> AppResult<()> {
|
|
|
|
pub async fn execute(&self, args: &S::Args) -> AppResult<()> {
|
|
|
|
NuExecutor::new(&self.path)
|
|
|
|
NuExecutor::new(&self.path)
|
|
|
|
.add_args(args.get_args())
|
|
|
|
.add_args(args.get_args())
|
|
|
|
.add_global_var("BY_TOURMALINE", VarValue::string("Hello from Tourmaline!"))
|
|
|
|
|
|
|
|
.add_global_var(
|
|
|
|
|
|
|
|
"ANOTHER_ONE",
|
|
|
|
|
|
|
|
VarValue::string("This variable was provided by tourmaline"),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.execute()
|
|
|
|
.execute()
|
|
|
|
.await
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Defines a script
|
|
|
|
|
|
|
|
/// This macro doesn't accept a file extension for the script name
|
|
|
|
|
|
|
|
/// as it is reused for the hook name
|
|
|
|
#[macro_export]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! script {
|
|
|
|
macro_rules! script {
|
|
|
|
($script:ident {
|
|
|
|
($script:ident {
|
|
|
@ -80,7 +82,15 @@ macro_rules! script {
|
|
|
|
type Args = $argtype;
|
|
|
|
type Args = $argtype;
|
|
|
|
|
|
|
|
|
|
|
|
fn get_name() -> &'static str {
|
|
|
|
fn get_name() -> &'static str {
|
|
|
|
$name
|
|
|
|
concat!($name, ".nu")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_pre_hook() -> &'static str {
|
|
|
|
|
|
|
|
concat!("name", ".pre.nu")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_post_hook() -> &'static str {
|
|
|
|
|
|
|
|
concat!("name", ".post.nu")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|