oakum 0.12.1

A polyglot release tool that derives dependent version bumps from the dependency graph
Documentation
//! `oakum upgrade`: gated on the install pin like the writers, except that a
//! repository with no pin may still upgrade (ADR-0038). Validates against the
//! old schema, runs migrations on `_config.toml`, regenerates `_schema.json`
//! unless its `#:schema` line opted out of one, refreshes a `README.md` an
//! earlier release wrote, and reports what changed.
//! Owns exactly those files (ADR-0023), and writes nothing when validation or
//! migration fails — a half-migrated config is worse than a stale one.

use super::config::{contain_template_sources, read_config_source, resolve_sibling_write_target};
use super::fs::write_file_via_rename;
use super::install_pin::{self, PinPolicy};
use super::owned_files::{
    refresh_readme, schema_state, write_schema, Records, SchemaOutcome, SchemaState,
};
use super::repository;
use super::CliError;
use super::{say_err, say_out};

pub(super) fn run() -> Result<(), Box<dyn std::error::Error>> {
    let repo = repository::discover()?;
    let Some(source) = read_config_source(&repo)? else {
        return Err(Box::new(CliError::new(
            "nothing to upgrade: `.changeset/_config.toml` does not exist (`oakum init` creates it)",
        )));
    };
    // A missing pin lets upgrade run, but the run must not read as checked against one.
    if let Some(owed) = install_pin::gate(&repo, PinPolicy::Upgrade)? {
        say_err(&format!("unverified: {}", owed.detail()));
    }

    let old = oakum::config::parse(source.text()).map_err(|err| {
        CliError::new(format!(
            "`.changeset/_config.toml` failed validation, so nothing was written: {err}"
        ))
    })?;
    contain_template_sources(&repo, &old).map_err(|err| {
        CliError::new(format!(
            "`.changeset/_config.toml` failed validation, so nothing was written: {err}"
        ))
    })?;

    // Migrations rewrite the raw text in order and go here. One that changes
    // the grammar must run before the strict parse above can accept the old
    // text. A failed migration leaves both files untouched.
    let new_config = oakum::config::remove_tool_version(source.text()).map_err(|err| {
        CliError::new(format!(
            "failed to remove `tool-version`, so nothing was written: {err}"
        ))
    })?;

    let schema_path = resolve_sibling_write_target(&repo, source.changeset_path(), "_schema.json")?;
    let state = schema_state(repo.dir(), &schema_path);
    // Schema first, so an interrupted run never leaves a migrated config beside
    // a stale schema; one opted out for another schema stays out (ADR-0038).
    let schema =
        if state == SchemaState::Absent && oakum::config::names_another_schema(source.text()) {
            None
        } else {
            Some(write_schema(repo.dir(), &schema_path, state)?)
        };
    let regenerated_first = |err: String| match schema {
        Some(SchemaOutcome::Created | SchemaOutcome::Replaced) => {
            format!("{err}; `.changeset/_schema.json` was regenerated first")
        }
        Some(SchemaOutcome::Unchanged) | None => err,
    };
    // A README that cannot be resolved is not one oakum can claim.
    let readme = match resolve_sibling_write_target(&repo, source.changeset_path(), "README.md") {
        Ok(path) => refresh_readme(repo.dir(), &path)
            .map_err(|err| CliError::new(regenerated_first(err.to_string())))?,
        Err(_) => false,
    };
    if new_config.is_none() && !readme {
        match schema {
            Some(SchemaOutcome::Unchanged) => {
                say_out("`.changeset/_config.toml` and `.changeset/_schema.json` are current");
                return Ok(());
            }
            None => {
                say_out("`.changeset/_config.toml` is current; its `#:schema` line names another schema, so no `.changeset/_schema.json` was written");
                return Ok(());
            }
            Some(SchemaOutcome::Created | SchemaOutcome::Replaced) => {}
        }
    }
    if let Some(body) = &new_config {
        write_file_via_rename(repo.dir(), source.config_path(), body)
            .map_err(|err| CliError::new(regenerated_first(err.to_string())))?;
    }

    let mut records = Records::default();
    records.record(if new_config.is_some() {
        "migrations: removed tool-version"
    } else {
        "migrations: none required"
    });
    records.record(&format!(
        "schema: .changeset/_schema.json {}",
        match schema {
            Some(SchemaOutcome::Unchanged) => "unchanged",
            Some(SchemaOutcome::Created | SchemaOutcome::Replaced) => "regenerated",
            None => "not written: the `#:schema` line names another schema",
        }
    ));
    if readme {
        records.record("readme: .changeset/README.md refreshed");
    }
    records.finish()?;
    Ok(())
}