name: Release

on:
  push:
    tags: ["v*"]
  release:
    types: [published]

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: false

permissions:
  contents: read

env:
  toolchain: nightly-2026-01-22

jobs:
  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    timeout-minutes: 30
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ env.toolchain }}

      - name: Install dependencies
        run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev

      - name: Publish crates
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          # Publish in dependency order. Crates with no internal deps first,
          # then crates that depend on them, and the root application last.
          crates=(
            jackdaw_geometry
            jackdaw_widgets
            jackdaw_commands
            jackdaw_camera
            jackdaw_terrain
            jackdaw_jsn
            jackdaw_feathers
          )

          for crate in "${crates[@]}"; do
            echo "::group::Publishing $crate"
            cargo publish -p "$crate" --locked
            echo "::endgroup::"
            # Give the crates.io index time to update
            sleep 30
          done

          # Publish the root application crate last
          echo "::group::Publishing jackdaw"
          cargo publish --locked
          echo "::endgroup::"
