verctl-core 0.9.7

Core configuration, manifest, fragment, changelog, and version domain logic for verctl
Documentation
//! Declarative steps, lifecycle attachment points, and declared ecosystems.

use garde::Validate;
use serde::Deserialize;

use super::Config;
use super::validate::non_empty_hook_argv;
use super::validate_steps::inside_the_repo_str;

/// One named extension step declared under `steps` in `.ctl/ver.yaml`.
///
/// Exactly one of `task` or `run` must be set. `task` executes as
/// `mise run --raw <task>`; `run` executes an argv directly without a shell.
/// Inputs reach the command through `VERCTL_PLAN` and, for package-scoped
/// steps, `VERCTL_PACKAGE`, `VERCTL_VERSION`, `VERCTL_PREVIOUS_VERSION`,
/// `VERCTL_TAG`, and `VERCTL_RELEASE_COMMIT`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize, Validate)]
#[serde(deny_unknown_fields)]
#[garde(context(Config as config))]
#[garde(allow_unvalidated)]
#[non_exhaustive]
pub struct StepSpec {
    /// A mise task name, executed as `mise run --raw <task>`. Omit when `run`
    /// is set.
    pub task: Option<String>,
    /// Direct argv to execute without a shell. Omit when `task` is set.
    pub run: Option<Vec<String>>,
    /// Paths relative to the repository root that this step may create,
    /// modify, or remove. Omit for a step that writes nothing.
    #[serde(default)]
    #[garde(inner(custom(inside_the_repo_str)))]
    pub writes: Vec<String>,
    /// Postcondition check argv evaluated before a `released` step runs; exit
    /// zero means the step already completed and is skipped. Omit when not
    /// attached to `released`.
    #[serde(default)]
    #[garde(inner(custom(non_empty_hook_argv)))]
    pub check: Option<Vec<String>>,
}

impl StepSpec {
    /// Command argv executed when this step runs (`mise run --raw <task>` or
    /// its declared `run` argv).
    #[must_use]
    pub fn argv(&self) -> Vec<String> {
        if let Some(task) = &self.task {
            return vec![
                "mise".to_owned(),
                "run".to_owned(),
                "--raw".to_owned(),
                task.clone(),
            ];
        }
        self.run.clone().unwrap_or_default()
    }

    /// Space-joined command label for reports and error messages.
    #[must_use]
    pub fn command(&self) -> String {
        self.argv().join(" ")
    }
}

/// Lifecycle points where named steps attach, either run-wide under top-level
/// `at` or per package under `packages[].at`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct AttachPoints {
    /// Steps run during `verctl version` after manifests and lockfiles, before
    /// served files render. May change only their declared `writes`.
    #[serde(default)]
    pub version: Vec<String>,
    /// Steps run during `verctl release` before any publish, tag, or Release
    /// side effect. May change nothing; a non-zero exit stops the release.
    #[serde(default)]
    pub verify: Vec<String>,
    /// Steps run during `verctl release` after finalization. Every step listed
    /// here must declare `check` so reruns skip completed work.
    #[serde(default)]
    pub released: Vec<String>,
}

impl AttachPoints {
    /// Whether no lifecycle point lists any step.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.version.is_empty() && self.verify.is_empty() && self.released.is_empty()
    }

    /// Every step name attached across `version`, `verify`, and `released`.
    pub fn all_names(&self) -> impl Iterator<Item = &str> {
        self.version
            .iter()
            .chain(&self.verify)
            .chain(&self.released)
            .map(String::as_str)
    }
}

/// A repository-declared ecosystem whose release blocks are implemented by
/// named steps under `steps`.
///
/// Built-in ecosystems (`cargo` and `bun`) are selected by manifest file name
/// unless a package names an ecosystem here. Any other manifest names an
/// ecosystem or configures its own driver and publisher.
#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
#[non_exhaustive]
pub struct EcosystemSpec {
    /// Named step implementing the Manifest block. Omit when a manifest driver
    /// reads and writes the version directly.
    pub manifest: Option<String>,
    /// Named step implementing the Lockfile block. Must list its lockfile path
    /// in `writes`.
    pub lockfile: Option<String>,
    /// Named step implementing the Artifact block. Omit when unused.
    pub artifact: Option<String>,
    /// Named step implementing the publish upload block. Omit when the package
    /// releases on GitHub only or declares its own publish block.
    pub publish: Option<String>,
    /// Named step querying whether a package version is already published;
    /// exit zero means already listed. Omit when unchecked.
    pub registry: Option<String>,
}

impl EcosystemSpec {
    /// Every step name referenced by any block of this ecosystem.
    pub fn step_names(&self) -> impl Iterator<Item = &str> {
        [
            self.manifest.as_deref(),
            self.lockfile.as_deref(),
            self.artifact.as_deref(),
            self.publish.as_deref(),
            self.registry.as_deref(),
        ]
        .into_iter()
        .flatten()
    }
}