use anyhow::Result;
use clap::ValueEnum;
use crate::config::Config;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
pub enum Phase {
PostConfig,
Discover,
AddDependencies,
Resolve,
Classify,
Build,
}
impl Phase {
pub fn name(self) -> &'static str {
match self {
Phase::PostConfig => "post-config",
Phase::Discover => "discover",
Phase::AddDependencies => "add-dependencies",
Phase::Resolve => "resolve",
Phase::Classify => "classify",
Phase::Build => "build",
}
}
pub fn description(self) -> &'static str {
match self {
Phase::PostConfig => "Fires after Config::load, before any command uses the config",
Phase::Discover => "Build pipeline: products discovered",
Phase::AddDependencies => "Build pipeline: dependencies added to graph",
Phase::Resolve => "Build pipeline: graph resolved",
Phase::Classify => "Build pipeline: products classified (skip/restore/build)",
Phase::Build => "Build pipeline: build execution finished",
}
}
pub fn all() -> &'static [Phase] {
&[
Phase::PostConfig,
Phase::Discover,
Phase::AddDependencies,
Phase::Resolve,
Phase::Classify,
Phase::Build,
]
}
}
pub struct PhaseHook {
pub name: &'static str,
pub phase: Phase,
pub description: &'static str,
pub function: &'static str,
pub location: &'static str,
pub run: fn(&mut Config) -> Result<()>,
}
inventory::collect!(PhaseHook);
pub fn all_hooks() -> impl Iterator<Item = &'static PhaseHook> {
inventory::iter::<PhaseHook>()
}
pub fn hooks_for(phase: Phase) -> impl Iterator<Item = &'static PhaseHook> {
all_hooks().filter(move |h| h.phase == phase)
}
pub fn run_phase(phase: Phase, config: &mut Config) -> Result<()> {
for hook in hooks_for(phase) {
(hook.run)(config)
.map_err(|e| e.context(format!("phase hook '{}' (phase {})", hook.name, phase.name())))?;
}
Ok(())
}