name: Publish to PyPI and Release

# Releases are driven by the repository-root VERSION file: merging a change
# to it on main validates, builds, publishes to PyPI and creates the GitHub
# Release (which creates the tag and fires the Zenodo webhook). Creating the
# tag from a separate workflow with GITHUB_TOKEN would NOT trigger this one
# (GitHub suppresses recursive workflow triggers), hence this single
# self-contained workflow.

on:
  push:
    branches: ["main"]
    paths: ["VERSION"]
  workflow_dispatch:

permissions:
  contents: write

jobs:
  release:
    runs-on: ubuntu-latest
    # Ref guard: workflow_dispatch could otherwise be run from any branch
    # and publish unreviewed code; only main may release.
    if: github.repository == 'jmrplens/phonometry' && github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Read VERSION and guard existing tag
        id: version
        run: |
          VERSION=$(tr -d ' \n' < VERSION)
          if ! echo "$VERSION" | grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+([.-][0-9A-Za-z.]+)?$'; then
            echo "::error::VERSION file does not contain a valid semver: '$VERSION'"
            exit 1
          fi
          if git rev-parse "v$VERSION" >/dev/null 2>&1; then
            echo "::notice::Tag v$VERSION already exists — nothing to release."
            echo "skip=true" >> "$GITHUB_OUTPUT"
            exit 0
          fi
          echo "skip=false" >> "$GITHUB_OUTPUT"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=v$VERSION" >> "$GITHUB_OUTPUT"

      - name: Set up Python
        if: steps.version.outputs.skip != 'true'
        uses: actions/setup-python@v6
        with:
          python-version: "3.13"

      - name: Install build tooling
        if: steps.version.outputs.skip != 'true'
        run: |
          python -m pip install --upgrade pip
          pip install build twine

      - name: Build package
        if: steps.version.outputs.skip != 'true'
        run: python -m build

      - name: Check package
        if: steps.version.outputs.skip != 'true'
        run: python -m twine check dist/*

      - name: Verify built version matches VERSION
        if: steps.version.outputs.skip != 'true'
        run: |
          ls dist/phonometry-${{ steps.version.outputs.version }}.tar.gz
          ls dist/phonometry-${{ steps.version.outputs.version }}-py3-none-any.whl

      - name: Publish to PyPI
        if: steps.version.outputs.skip != 'true'
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
        run: python -m twine upload dist/*

      - name: Create GitHub Release (creates the tag)
        if: steps.version.outputs.skip != 'true'
        uses: softprops/action-gh-release@v3
        with:
          tag_name: ${{ steps.version.outputs.tag }}
          name: Release ${{ steps.version.outputs.tag }}
          body: |
            See the [changelog](https://github.com/jmrplens/phonometry/blob/main/CHANGELOG.md) for the full details of this release.
          generate_release_notes: true
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
