name: Release

on:
  release:
    types: [published]

env:
  CARGO_TERM_COLOR: always

concurrency:
  group: release-${{ github.event.release.tag_name }}
  cancel-in-progress: true

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Verify release tag matches Cargo.toml version
        run: |
          set -euo pipefail
          TAG="${{ github.event.release.tag_name }}"
          TAG_VER="${TAG#v}"
          CRATE_VER=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[] | select(.name=="suitecase") | .version')
          echo "Release tag: ${TAG} (expected version: ${TAG_VER})"
          echo "Cargo.toml version: ${CRATE_VER}"
          if [ "${TAG_VER}" != "${CRATE_VER}" ]; then
            echo "::error::Tag version '${TAG_VER}' does not match crate version '${CRATE_VER}'. Bump version in Cargo.toml before tagging."
            exit 1
          fi

      - name: Build
        run: cargo build --all-targets --verbose

      - name: Test
        run: cargo test --all-targets --verbose

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --locked
