Add order field for a task execution order

config-extension
trivernis 2 years ago
parent d50cbf4ca1
commit a44b235f83
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -18,6 +18,8 @@ skip_on_false = true
[tasks.install-zramd] [tasks.install-zramd]
config_key = "enable_zramd" config_key = "enable_zramd"
skip_on_false = true skip_on_false = true
order = 10
[tasks.configure-unakite] [tasks.configure-unakite]
config_key = "unakite" config_key = "unakite"
order = 0

@ -45,6 +45,18 @@ pub struct TaskConfig {
/// config value of that task is null /// config value of that task is null
#[serde(default)] #[serde(default)]
pub skip_on_false: bool, 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 { impl DistroConfig {

@ -14,6 +14,7 @@ pub enum BaseTask {
InstallBase, InstallBase,
InstallBootloader, InstallBootloader,
InstallDesktop, InstallDesktop,
InstallKernels,
InstallExtraPackages, InstallExtraPackages,
SetupRootUser, SetupRootUser,
SetupUsers, SetupUsers,
@ -64,6 +65,10 @@ impl BaseTask {
task_name: "setup-users", task_name: "setup-users",
config_key: Some("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, 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! { lazy_static! {
@ -120,5 +140,6 @@ fn get_all_base_tasks() -> Vec<BaseTask> {
BaseTask::InstallExtraPackages, BaseTask::InstallExtraPackages,
BaseTask::SetupRootUser, BaseTask::SetupRootUser,
BaseTask::SetupUsers, BaseTask::SetupUsers,
BaseTask::InstallKernels,
] ]
} }

@ -10,16 +10,18 @@ pub struct CustomTask {
up_script: PathBuf, up_script: PathBuf,
down_script: PathBuf, down_script: PathBuf,
skip_on_false: bool, skip_on_false: bool,
order: usize,
} }
impl CustomTask { 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); let base_path = PathBuf::from(name);
Self { Self {
config_key, config_key,
up_script: base_path.join("up.nu"), up_script: base_path.join("up.nu"),
down_script: base_path.join("down.nu"), down_script: base_path.join("down.nu"),
skip_on_false, 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 { fn config_is_falsy(config: &embed_nu::Value) -> bool {

@ -1,3 +1,5 @@
use std::cmp::Ordering;
use crate::{distro::OSConfig, error::AppResult}; use crate::{distro::OSConfig, error::AppResult};
use self::{base_task::BaseTask, custom_task::CustomTask, exec_builder::ExecBuilder}; use self::{base_task::BaseTask, custom_task::CustomTask, exec_builder::ExecBuilder};
@ -9,6 +11,9 @@ pub mod task_executor;
pub trait TaskTrait { pub trait TaskTrait {
fn up(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>>; fn up(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>>;
fn down(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>>; fn down(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>>;
/// Used to decide the execution order
/// smaller values mean the task get's executed earlier
fn order(&self) -> usize;
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -17,6 +22,35 @@ pub enum Task {
Custom(CustomTask), 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 { impl TaskTrait for Task {
#[inline] #[inline]
fn up(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>> { fn up(&self, config: &OSConfig) -> AppResult<Option<ExecBuilder>> {
@ -33,4 +67,11 @@ impl TaskTrait for Task {
Task::Custom(c) => c.down(config), Task::Custom(c) => c.down(config),
} }
} }
fn order(&self) -> usize {
match self {
Task::Base(b) => b.order(),
Task::Custom(c) => c.order(),
}
}
} }

@ -42,10 +42,11 @@ impl TaskExecutor {
.tasks .tasks
.iter() .iter()
.map(|(name, task)| { .map(|(name, task)| {
CustomTask::from_name_and_key( CustomTask::new(
name.to_owned(), name.to_owned(),
task.config_key.to_owned(), task.config_key.to_owned(),
task.skip_on_false, task.skip_on_false,
task.order,
) )
}) })
.map(Task::Custom) .map(Task::Custom)
@ -58,6 +59,8 @@ impl TaskExecutor {
/// Executes all tasks /// Executes all tasks
#[tracing::instrument(level = "trace", skip_all)] #[tracing::instrument(level = "trace", skip_all)]
pub async fn execute(&mut self) -> AppResult<()> { pub async fn execute(&mut self) -> AppResult<()> {
self.tasks.sort_by(Task::compare);
for task in &self.tasks { for task in &self.tasks {
if let Some(up_task) = task.up(&self.os_config)? { if let Some(up_task) = task.up(&self.os_config)? {
up_task.exec().await? up_task.exec().await?

Loading…
Cancel
Save