From a44b235f83dee359f0ea406f3da882566e45e3b8 Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 19 Oct 2022 22:43:37 +0200 Subject: [PATCH] Add order field for a task execution order --- configs/crystal/distro.toml | 2 ++ src/distro/distro_config.rs | 12 +++++++++++ src/task/base_task.rs | 21 +++++++++++++++++++ src/task/custom_task.rs | 8 +++++++- src/task/mod.rs | 41 +++++++++++++++++++++++++++++++++++++ src/task/task_executor.rs | 5 ++++- 6 files changed, 87 insertions(+), 2 deletions(-) diff --git a/configs/crystal/distro.toml b/configs/crystal/distro.toml index 251c84d..52f8132 100644 --- a/configs/crystal/distro.toml +++ b/configs/crystal/distro.toml @@ -18,6 +18,8 @@ skip_on_false = true [tasks.install-zramd] config_key = "enable_zramd" skip_on_false = true +order = 10 [tasks.configure-unakite] config_key = "unakite" +order = 0 \ No newline at end of file diff --git a/src/distro/distro_config.rs b/src/distro/distro_config.rs index 7cca61a..384c04e 100644 --- a/src/distro/distro_config.rs +++ b/src/distro/distro_config.rs @@ -45,6 +45,18 @@ pub struct TaskConfig { /// config value of that task is null #[serde(default)] pub skip_on_false: bool, + + /// The execution order of this task + /// Note that custom tasks always get executed after + /// the base tasks + /// If not set this value defaults to [usize::MAX] + #[serde(default = "default_order")] + pub order: usize, +} + +#[inline] +fn default_order() -> usize { + usize::MAX } impl DistroConfig { diff --git a/src/task/base_task.rs b/src/task/base_task.rs index a73707a..429dad8 100644 --- a/src/task/base_task.rs +++ b/src/task/base_task.rs @@ -14,6 +14,7 @@ pub enum BaseTask { InstallBase, InstallBootloader, InstallDesktop, + InstallKernels, InstallExtraPackages, SetupRootUser, SetupUsers, @@ -64,6 +65,10 @@ impl BaseTask { task_name: "setup-users", config_key: Some("users"), }, + BaseTask::InstallKernels => BaseTaskKeydata { + task_name: "install-kernels", + config_key: Some("kernels"), + }, } } } @@ -103,6 +108,21 @@ impl TaskTrait for BaseTask { task_config, })) } + + fn order(&self) -> usize { + match self { + BaseTask::CreatePartitions => 10, + BaseTask::InstallBase => 20, + BaseTask::InstallKernels => 30, + BaseTask::InstallBootloader => 40, + BaseTask::ConfigureLocale => 50, + BaseTask::ConfigureNetwork => 60, + BaseTask::SetupRootUser => 70, + BaseTask::SetupUsers => 80, + BaseTask::InstallDesktop => 90, + BaseTask::InstallExtraPackages => 100, + } + } } lazy_static! { @@ -120,5 +140,6 @@ fn get_all_base_tasks() -> Vec { BaseTask::InstallExtraPackages, BaseTask::SetupRootUser, BaseTask::SetupUsers, + BaseTask::InstallKernels, ] } diff --git a/src/task/custom_task.rs b/src/task/custom_task.rs index 52d1ea9..88310d2 100644 --- a/src/task/custom_task.rs +++ b/src/task/custom_task.rs @@ -10,16 +10,18 @@ pub struct CustomTask { up_script: PathBuf, down_script: PathBuf, skip_on_false: bool, + order: usize, } impl CustomTask { - pub fn from_name_and_key(name: String, config_key: String, skip_on_false: bool) -> Self { + pub fn new(name: String, config_key: String, skip_on_false: bool, order: usize) -> Self { let base_path = PathBuf::from(name); Self { config_key, up_script: base_path.join("up.nu"), down_script: base_path.join("down.nu"), skip_on_false, + order, } } } @@ -54,6 +56,10 @@ impl TaskTrait for CustomTask { })) } } + + fn order(&self) -> usize { + self.order + } } fn config_is_falsy(config: &embed_nu::Value) -> bool { diff --git a/src/task/mod.rs b/src/task/mod.rs index 7677d4b..a1bc012 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -1,3 +1,5 @@ +use std::cmp::Ordering; + use crate::{distro::OSConfig, error::AppResult}; use self::{base_task::BaseTask, custom_task::CustomTask, exec_builder::ExecBuilder}; @@ -9,6 +11,9 @@ pub mod task_executor; pub trait TaskTrait { fn up(&self, config: &OSConfig) -> AppResult>; fn down(&self, config: &OSConfig) -> AppResult>; + /// Used to decide the execution order + /// smaller values mean the task get's executed earlier + fn order(&self) -> usize; } #[derive(Clone, Debug)] @@ -17,6 +22,35 @@ pub enum Task { Custom(CustomTask), } +impl Task { + pub fn is_custom(&self) -> bool { + match self { + Task::Base(_) => false, + Task::Custom(_) => true, + } + } + + pub fn is_base(&self) -> bool { + !self.is_custom() + } + + pub fn compare(&self, other: &Self) -> Ordering { + if self.is_base() && other.is_custom() { + Ordering::Less + } else if self.is_custom() && other.is_base() { + Ordering::Greater + } else { + if self.order() > other.order() { + Ordering::Greater + } else if self.order() < other.order() { + Ordering::Less + } else { + Ordering::Equal + } + } + } +} + impl TaskTrait for Task { #[inline] fn up(&self, config: &OSConfig) -> AppResult> { @@ -33,4 +67,11 @@ impl TaskTrait for Task { Task::Custom(c) => c.down(config), } } + + fn order(&self) -> usize { + match self { + Task::Base(b) => b.order(), + Task::Custom(c) => c.order(), + } + } } diff --git a/src/task/task_executor.rs b/src/task/task_executor.rs index f07f636..ba4a882 100644 --- a/src/task/task_executor.rs +++ b/src/task/task_executor.rs @@ -42,10 +42,11 @@ impl TaskExecutor { .tasks .iter() .map(|(name, task)| { - CustomTask::from_name_and_key( + CustomTask::new( name.to_owned(), task.config_key.to_owned(), task.skip_on_false, + task.order, ) }) .map(Task::Custom) @@ -58,6 +59,8 @@ impl TaskExecutor { /// Executes all tasks #[tracing::instrument(level = "trace", skip_all)] pub async fn execute(&mut self) -> AppResult<()> { + self.tasks.sort_by(Task::compare); + for task in &self.tasks { if let Some(up_task) = task.up(&self.os_config)? { up_task.exec().await?