version: v1

tasks:
  build:
    description: "Build the project (debug)"
    cmds:
      - cargo build

  build-release:
    description: "Build an optimized release binary"
    cmds:
      - cargo build --release

  test:
    description: "Run all tests"
    cmds:
      - cargo test

  test-one:
    description: "Run a single test by name or module path"
    cmds:
      - cargo test ${TEST_NAME}
    env:
      TEST_NAME:
        type: string
        required: true

  lint:
    description: "Run clippy with warnings as errors"
    cmds:
      - cargo clippy -- -D warnings

  fmt:
    description: "Format all code in place"
    cmds:
      - cargo fmt

  fmt-check:
    description: "Check formatting without modifying files"
    cmds:
      - cargo fmt --check

  publish-dry-run:
    description: "Verify the crate is publish-ready without uploading to crates.io"
    agent:
      enabled: false
      risk_level: medium
    cmds:
      - cargo publish --dry-run

  bump-version:
    description: "Create a release branch, bump version in Cargo.toml and Cargo.lock, commit"
    agent:
      enabled: false
      risk_level: high
    env:
      VERSION:
        type: string
        required: true
    cmds:
      - git checkout -b release/v${VERSION}
      - sed -i.bak "s/^version = \".*\"/version = \"${VERSION}\"/" Cargo.toml && rm Cargo.toml.bak
      - cargo update -p meriadoc
      - git add Cargo.toml Cargo.lock
      - git commit -m "Release v${VERSION}"

  open-release-pr:
    description: "Push release branch, open PR, merge it, and return to main"
    agent:
      enabled: false
      risk_level: high
    env:
      VERSION:
        type: string
        required: true
    cmds:
      - git push origin release/v${VERSION}
      - gh pr create --title "Release v${VERSION}" --body "Automated version bump for v${VERSION}" --base main --head release/v${VERSION}
      - gh pr merge --squash --admin --delete-branch
      - git checkout main
      - git pull origin main

  push-tag:
    description: "Push the release tag to origin (triggers GitHub Actions release CI)"
    agent:
      enabled: false
      risk_level: high
    env:
      VERSION:
        type: string
        required: true
    cmds:
      - git push origin v${VERSION}

  publish:
    description: "Publish crate to crates.io"
    agent:
      enabled: false
      risk_level: high
    cmds:
      - cargo publish

  tag:
    description: "Create an annotated git release tag"
    agent:
      enabled: false
      risk_level: high
    env:
      VERSION:
        type: string
        required: true
    preconditions:
      - cmds:
          - git diff --quiet HEAD
        on_failure:
          continue: false
          cmds:
            - echo "Working tree has uncommitted changes — commit everything before tagging."
    cmds:
      - git tag -a v${VERSION} -m "Release v${VERSION}"

jobs:
  ci:
    description: "Full CI pipeline: lint, format check, and test"
    tasks:
      - lint
      - fmt-check
      - test

  release-check:
    description: "Everything CI checks plus a release build and publish dry-run"
    tasks:
      - lint
      - fmt-check
      - test
      - build-release
      - publish-dry-run

  release:
    description: "Full release: CI checks, version bump PR, tag, publish to crates.io"
    env:
      VERSION:
        type: string
        required: true
    tasks:
      - lint
      - fmt-check
      - test
      - bump-version
      - open-release-pr
      - tag
      - push-tag
      - publish

shells:
  dev:
    description: "Development shell with verbose logging and backtraces enabled"
    env:
      RUST_LOG:
        type: choice
        options:
          - debug
          - info
          - warn
          - error
        default: debug
      RUST_BACKTRACE:
        type: choice
        options:
          - "0"
          - "1"
          - full
        default: "1"
    init_cmds:
      - echo "Meriadoc dev shell ready — run 'meriadoc ls tasks' to see available tasks"
