# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/nedbat/coveragepy/blob/master/NOTICE.txt

# Based on:
# https://github.com/joerick/cibuildwheel/blob/master/examples/github-deploy.yml

name: "Kits"

on:
  push:
    branches:
      # Don't build kits all the time, but do if the branch is about kits.
      - "**/*kit*"
  workflow_dispatch:

defaults:
  run:
    shell: bash

jobs:
  wheels:
    name: "Build wheels on ${{ matrix.os }}"
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-latest
          - macos-latest
      fail-fast: false

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@v2

      - name: "Install Python 3.7"
        uses: actions/setup-python@v2
        with:
          python-version: "3.7"

      - name: "Install cibuildwheel"
        run: |
          python -m pip install -c requirements/pins.pip cibuildwheel

      - name: "Install Visual C++ for Python 2.7"
        if: runner.os == 'Windows'
        run: |
          choco install vcpython27 -f -y

      - name: "Build wheels"
        env:
          # Don't build wheels for PyPy.
          CIBW_SKIP: pp*
        run: |
          python -m cibuildwheel --output-dir wheelhouse
          ls -al wheelhouse/

      - name: "Upload wheels"
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: wheelhouse/*.whl

  sdist:
    name: "Build source distribution"
    runs-on: ubuntu-latest
    steps:
      - name: "Check out the repo"
        uses: actions/checkout@v2

      - name: "Install Python 3.7"
        uses: actions/setup-python@v2
        with:
          python-version: "3.7"

      - name: "Build sdist"
        run: |
          python setup.py sdist
          ls -al dist/

      - name: "Upload sdist"
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist/*.tar.gz

  pypy:
    name: "Build PyPy wheels"
    runs-on: ubuntu-latest
    steps:
      - name: "Check out the repo"
        uses: actions/checkout@v2

      - name: "Install PyPy"
        uses: actions/setup-python@v2
        with:
          python-version: "pypy3"

      - name: "Install requirements"
        run: |
          pypy3 -m pip install -r requirements/wheel.pip

      - name: "Build wheels"
        run: |
          pypy3 setup.py bdist_wheel --python-tag pp36
          pypy3 setup.py bdist_wheel --python-tag pp37
          ls -al dist/

      - name: "Upload wheels"
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist/*.whl

  prerel:
    name: "Build ${{ matrix.python-version }} wheels on ${{ matrix.os }}"
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os:
          - ubuntu-latest
          - windows-latest
          - macos-latest
        python-version:
          - "3.10.0-alpha.5"
      fail-fast: false

    steps:
      - name: "Check out the repo"
        uses: actions/checkout@v2

      - name: "Install Python ${{ matrix.pyton-version }}"
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}

      - name: "Install wheel tools"
        run: |
          python -m pip install -c requirements/pins.pip -r requirements/wheel.pip

      - name: "Build wheel"
        run: |
          python setup.py bdist_wheel

      - name: "Convert to manylinux wheel"
        if: runner.os == 'Linux'
        run: |
          ls -la dist/
          auditwheel show dist/*.whl
          auditwheel repair dist/*.whl
          ls -la wheelhouse/
          auditwheel show wheelhouse/*.whl
          rm dist/*.whl
          mv wheelhouse/*.whl dist/
          ls -al dist/

      - name: "Upload wheels"
        uses: actions/upload-artifact@v2
        with:
          name: dist
          path: dist/*.whl
