name: Create wheel

on:
  # run when a release has been created
  release:
    types: [created]
  # push:
  #   branches:
  #     - "go_wheel_*"

# env:
#   # comment TWINE_REPOSITORY_URL to use the real pypi. NOTE: change also the secret used in TWINE_PASSWORD
#   TWINE_REPOSITORY_URL: https://test.pypi.org/legacy/

jobs:
  build_wheels:
    name: ${{ matrix.wheel_mode }} wheels ${{ matrix.python }} on ${{ matrix.os }} ${{ matrix.os == 'ubuntu-22.04' && matrix.linux_archs || '' }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # emulated wheels on linux take too much time, split wheels into multiple runs
        python:
          - "cp37-* cp38-*"
          - "cp39-* cp310-*"
          - "cp311-*"
        wheel_mode:
          - compiled
        os:
          - "windows-2022"
          - "macos-12"
          - "ubuntu-22.04"
        linux_archs:
          # this is only meaningful on linux. windows and macos ignore exclude all but one arch
          - "aarch64"
          - "x86_64"

        include:
          # create pure python build
          - os: ubuntu-22.04
            wheel_mode: pure-python
            python: "cp-311*"

        exclude:
          - os: "windows-2022"
            linux_archs: "aarch64"
          - os: "macos-12"
            linux_archs: "aarch64"

      fail-fast: false

    steps:
      - uses: actions/checkout@v3

      - name: Remove tag_build from setup.cfg
        # sqlalchemy has `tag_build` set to `dev` in setup.cfg. We need to remove it before creating the weel
        # otherwise it gets tagged with `dev0`
        shell: pwsh
        # This is equivalent to the sed commands:
        # `sed -i '/tag_build=dev/d' setup.cfg`
        # `sed -i '/tag_build = dev/d' setup.cfg`

        # `-replace` uses a regexp match
        # alternative form: `(get-content setup.cfg) | foreach-object{$_ -replace "tag_build.=.dev",""} | set-content setup.cfg`
        run: |
          (cat setup.cfg) | %{$_ -replace "tag_build.?=.?dev",""} | set-content setup.cfg

      # See details at https://cibuildwheel.readthedocs.io/en/stable/faq/#emulation
      - name: Set up QEMU on linux
        if: ${{ runner.os == 'Linux' }}
        uses: docker/setup-qemu-action@v2
        with:
          platforms: all

      - name: Build compiled wheels
        if: ${{ matrix.wheel_mode == 'compiled' }}
        uses: pypa/cibuildwheel@v2.13.1
        env:
          CIBW_ARCHS_LINUX: ${{ matrix.linux_archs }}
          CIBW_BUILD: ${{ matrix.python }}
          # setting it here does not work on linux
          # PYTHONNOUSERSITE: "1"


      - name: Set up Python for twine and pure-python wheel
        uses: actions/setup-python@v4
        with:
          python-version: "3.10"

      - name: Build pure-python wheel
        if: ${{ matrix.wheel_mode == 'pure-python' && runner.os == 'Linux' }}
        run: |
          python -m pip install --upgrade pip
          pip --version
          pip install build
          pip list
          DISABLE_SQLALCHEMY_CEXT=y python -m build --wheel --outdir ./wheelhouse

      # - uses: actions/upload-artifact@v3
      #   with:
      #     path: ./wheelhouse/*.whl

      - name: Upload wheels to release
        # upload the generated wheels to the github release
        uses: sqlalchemyorg/upload-release-assets@sa
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          files: './wheelhouse/*.whl'

      - name: Publish wheel
        # the action https://github.com/marketplace/actions/pypi-publish runs only on linux and we cannot specify
        # additional options
        env:
          TWINE_USERNAME: __token__
          # replace TWINE_PASSWORD with token for real pypi
          # TWINE_PASSWORD: ${{ secrets.test_pypi_token }}
          TWINE_PASSWORD: ${{ secrets.pypi_token }}
        run: |
          pip install -U twine
          twine upload --skip-existing ./wheelhouse/*
