# Based on https://github.com/pypa/cibuildwheel/blob/main/examples/github-deploy.yml
name: Build and deploy to PyPI

# Build on every branch push and pull request
# Also build (and deploy) when a GitHub Release is created
on:
  push:
  pull_request:
  release:
    types:
      - published

jobs:
  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch the entire repo history, required to guarantee setuptools_scm will pick up the tags

      # setuptools_scm workaround for https://github.com/pypa/setuptools_scm/issues/455
      - name: Disable local version identifier on develop CI
        run: |
          echo 'local_scheme = "no-local-version"' >> pyproject.toml
          git diff --color=always
          git update-index --assume-unchanged pyproject.toml
        if: github.event_name == 'push' && github.ref_name == 'develop'

      - name: Build wheels
        uses: pypa/cibuildwheel@v2.19.2
        env:
          # PyPy wheels not allowed because SciPy (build requirement) is not available
          CIBW_BUILD: cp3*-*
          CIBW_SKIP: cp36-* cp37-* cp38-* *-musllinux_*
          CIBW_ARCHS_LINUX: auto64
          CIBW_ARCHS_MACOS: x86_64 arm64
          CIBW_ARCHS_WINDOWS: auto64
          CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014

      - uses: actions/upload-artifact@v4
        with:
          name: artifact-wheels-${{ matrix.os }}
          path: ./wheelhouse/*.whl

  build_sdist:
    name: Build source distribution
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0  # fetch the entire repo history, required to guarantee setuptools_scm will pick up the tags

      # setuptools_scm workaround for https://github.com/pypa/setuptools_scm/issues/455
      - name: Disable local version identifier on develop CI
        run: |
          echo 'local_scheme = "no-local-version"' >> pyproject.toml
          git diff --color=always
          git update-index --assume-unchanged pyproject.toml
        if: github.event_name == 'push' && github.ref_name == 'develop'

      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: '3.9'

      - run: pip install build

      - name: Build sdist
        run: python -m build --sdist

      - uses: actions/upload-artifact@v4
        with:
          name: artifact-source
          path: dist/*.tar.gz

  upload_pypi:
    needs: [build_wheels, build_sdist]
    runs-on: ubuntu-latest
    permissions:
      id-token: write  # IMPORTANT: this permission is mandatory for trusted publishing
    # upload to PyPI when a GitHub Release is created
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: artifact-*
          path: dist
          merge-multiple: true  # download and extract all artifacts in the same directory
      - name: PyPI - Upload Artifacts to PyPI (Production)
        uses: pypa/gh-action-pypi-publish@v1.9.0
        if: github.event_name == 'release' && github.event.action == 'published'
      - name: TestPyPI - Upload Artifacts
        uses: pypa/gh-action-pypi-publish@v1.9.0
        if: github.event_name == 'push' && github.ref_name == 'develop'
        with:
          repository-url: https://test.pypi.org/legacy/
