name: release

on:
  push:
    tags:
      - 'v*'

jobs:
  publish-crate:
    name: publish to crates.io
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}

  github-release:
    name: github release
    runs-on: ubuntu-latest
    needs: publish-crate
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
      - name: extract changelog
        id: changelog
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          if [ -f CHANGELOG.md ]; then
            awk -v ver="$VERSION" '
              /^## / { if (p) exit; if (index($0, ver)) p=1; next }
              p { print }
            ' CHANGELOG.md > .release-notes.md
          else
            echo "Release $GITHUB_REF_NAME" > .release-notes.md
          fi
      - uses: softprops/action-gh-release@v3
        with:
          body_path: .release-notes.md
