name: Release

on:
  push:
    tags:
      - "v*.*.*"
      - "v*.*.*-dryrun"

jobs:
  create-release:
    name: Create GitHub Release
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v3
        with:
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-crates:
    name: Publish crates to crates.io
    runs-on: ubuntu-latest
    if: ${{ startsWith(github.ref, 'refs/tags/v') }}
    needs: create-release
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - name: Cargo publish derive (dry run)
        if: ${{ endsWith(github.ref, '-dryrun') }}
        run: cargo publish --manifest-path orion-error-derive/Cargo.toml --dry-run
      - name: Cargo publish root (dry run)
        if: ${{ endsWith(github.ref, '-dryrun') }}
        run: cargo publish --dry-run
      - name: Cargo publish derive
        if: ${{ !endsWith(github.ref, '-dryrun') }}
        run: cargo publish --manifest-path orion-error-derive/Cargo.toml --token ${{ secrets.CRATES_IO_TOKEN }}
      - name: Wait for crates.io index
        if: ${{ !endsWith(github.ref, '-dryrun') }}
        run: sleep 30
      - name: Cargo publish root
        if: ${{ !endsWith(github.ref, '-dryrun') }}
        run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
