name: Release

on:
  push:
    tags: ["v*"]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  gate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2

      - name: Validate tag matches Cargo.toml version
        run: |
          tag_version="${GITHUB_REF_NAME#v}"
          cargo_version=$(cargo metadata --no-deps --format-version=1 | jq -r '.packages[0].version')
          if [ "$tag_version" != "$cargo_version" ]; then
            echo "::error::Tag version ($tag_version) does not match Cargo.toml version ($cargo_version)"
            exit 1
          fi

      - run: cargo test --locked
      - run: cargo clippy --locked -- -D warnings

  build:
    needs: gate
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            runner: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            runner: ubuntu-24.04-arm
          - target: aarch64-apple-darwin
            runner: macos-latest
    runs-on: ${{ matrix.runner }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Build
        run: cargo build --release --locked

      - name: Package
        run: |
          tar czf dotm-${{ matrix.target }}.tar.gz \
            -C target/release dotm \
            -C "$GITHUB_WORKSPACE" LICENSE

      - uses: actions/upload-artifact@v4
        with:
          name: dotm-${{ matrix.target }}
          path: dotm-${{ matrix.target }}.tar.gz

  publish:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

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

      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/*.tar.gz
