use distro::OSConfig; use error::{AppError, AppResult}; use scripting::{ loader::ScriptLoader, script::{NuScript, Script}, }; pub mod error; pub(crate) mod scripting; pub(crate) mod utils; pub use utils::generate_script_files; pub mod distro; pub mod task; #[derive(Clone, Copy, Debug, PartialEq, PartialOrd)] pub enum TaskOperation { Up, Down, } pub struct TaskExecutor { config: Option, loader: ScriptLoader, } macro_rules! task_executors { ($($function:ident => $task:ident),+) => { $( #[tracing::instrument(level = "trace", skip(self))] pub async fn $function(&self, operation: TaskOperation, cfg: <$task as crate::tasks::Task>::Config) -> AppResult<()> { match operation { TaskOperation::Up => self.execute_task::<<$task as crate::tasks::Task>::UpScript>(cfg).await, TaskOperation::Down => self.execute_task::<<$task as crate::tasks::Task>::DownScript>(cfg).await, } } )+ } } impl TaskExecutor { /// Creates a new task executor with a given config pub fn with_config(config: OSConfig) -> Self { Self { config: Some(config), loader: ScriptLoader::new(), } } task_executors!( setup_users => SetupUsersTask, configure_network => ConfigureNetworkTask, configure_unakite => ConfigureUnakiteTask, create_partitions => CreatePartitionsTask, install_base => InstallBaseTask, install_bootloader => InstallBootloaderTask, install_desktop => InstallDesktopTask, install_extra_packages => InstallExtraPackagesTask, install_flatpak => InstallFlatpakTask, install_kernels => InstallKernelsTask, install_timeshift => InstallTimeshiftTask, install_zramd => InstallZRamDTask, setup_root_user => SetupRootUserTask, configure_locale => ConfigureLocaleTask ); /// Installs the system from the given system configuration #[tracing::instrument(level = "trace", skip(self))] pub async fn install_from_config(&self) -> AppResult<()> { let config = self.config.clone().ok_or(AppError::MissingConfig)?; let base_config = config.base; self.create_partitions(TaskOperation::Up, base_config.partitions) .await?; self.install_base(TaskOperation::Up, ()).await?; self.install_kernels(TaskOperation::Up, base_config.kernels) .await?; self.install_bootloader(TaskOperation::Up, base_config.bootloader) .await?; self.configure_locale(TaskOperation::Up, base_config.locale) .await?; self.configure_network(TaskOperation::Up, base_config.network) .await?; if base_config.enable_zramd { self.install_zramd(TaskOperation::Up, ()).await?; } if base_config.enable_timeshift { self.install_timeshift(TaskOperation::Up, ()).await?; } if base_config.enable_flatpak { self.install_flatpak(TaskOperation::Up, ()).await?; } self.setup_users(TaskOperation::Up, base_config.users) .await?; self.setup_root_user(TaskOperation::Up, base_config.root_user) .await?; self.install_desktop(TaskOperation::Up, base_config.desktop) .await?; self.install_extra_packages(TaskOperation::Up, base_config.extra_packages) .await?; if let Some(unakite) = base_config.unakite { self.configure_unakite(TaskOperation::Up, unakite).await?; } Ok(()) } async fn execute_task(&self, args: S::Args) -> AppResult<()> { let script = self.loader.load::()?; self.execute(script, args.clone()).await?; Ok(()) } #[inline] async fn execute(&self, mut script: NuScript, args: S::Args) -> AppResult<()> { if let Some(cfg) = self.config.as_ref() { script.set_global_var("TRM_CONFIG", cfg.to_owned()) } else { script.set_global_var("TRM_CONFIG", OSConfig::empty()) } .set_global_var("TRM_VERSION", env!("CARGO_PKG_VERSION")) .execute(args) .await } }