name: Release

on:
  workflow_dispatch:

permissions:
  contents: write

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

      - name: Create tag
        id: tag
        run: |
          version=$(cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
          tag="v$version"

          git fetch --tags

          if git tag --list "$tag" | grep -q "$tag"; then
            echo "created=false" >> "$GITHUB_OUTPUT"
            echo "tag=$tag" >> "$GITHUB_OUTPUT"
            exit 0
          fi

          git tag "$tag"
          git push origin "$tag"

          echo "created=true" >> "$GITHUB_OUTPUT"
          echo "tag=$tag" >> "$GITHUB_OUTPUT"

      - name: Create GitHub release
        if: steps.tag.outputs.created == 'true'
        env:
          GH_TOKEN: ${{ github.token }}
        run: gh release create "${{ steps.tag.outputs.tag }}" --generate-notes

      - name: Publish crate
        if: steps.tag.outputs.created == 'true'
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish
