You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tourmaline/src/lib.rs

77 lines
2.0 KiB
Rust

2 years ago
use config::Config;
use error::AppResult;
2 years ago
use scripting::{
loader::{HookType, ScriptLoader},
script::{NuScript, Script, ScriptArgs},
};
use tasks::*;
pub mod config;
pub mod error;
pub(crate) mod scripting;
pub mod tasks;
pub(crate) mod utils;
pub struct TaskExecutor {
2 years ago
config: Option<Config>,
loader: ScriptLoader,
}
macro_rules! tasks {
($($function:ident => $script:ident),+) => {
$(
#[tracing::instrument(level = "trace", skip(self))]
pub async fn $function(&self, cfg: &<$script as crate::scripting::script::Script>::Args) -> AppResult<()> {
2 years ago
self.execute_task::<$script>(cfg).await
}
)+
}
}
impl TaskExecutor {
tasks!(
setup_users => SetupUsersScript,
configure_network => ConfigureNetworkScript,
create_partitions => CreatePartitionsScript,
install_base => InstallBaseScript,
install_bootloader => InstallBootloaderScript,
install_desktop => InstallDesktopScript,
configure_local => ConfigureLocaleScript
);
2 years ago
async fn execute_task<S: Script>(&self, args: &S::Args) -> AppResult<()> {
if let Some(pre_hook) = self.loader.load_hook::<S>(HookType::Pre) {
self.execute(pre_hook, args).await?;
}
let script = self.loader.load::<S>()?;
self.execute(script, args).await?;
if let Some(post_hook) = self.loader.load_hook::<S>(HookType::Post) {
self.execute(post_hook, args).await?;
}
Ok(())
}
#[inline]
2 years ago
async fn execute<S: Script>(&self, mut script: NuScript<S>, args: &S::Args) -> AppResult<()> {
if let Some(cfg) = self.config.as_ref() {
script.set_global_var("TRM_CONFIG", cfg.get_args())
} else {
&mut script
}
.set_global_var("TRM_VERSION", env!("CARGO_PKG_VERSION"))
.execute(args)
.await
}
}
impl Default for TaskExecutor {
fn default() -> Self {
Self {
loader: ScriptLoader::new(),
2 years ago
config: None,
}
}
}