# 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

# To test installing wheels without uploading them to PyPI:
#
#   $ mkdir /tmp/pypi
#   $ cp dist/* /tmp/pypi
#   $ python -m pip install piprepo
#   $ piprepo build /tmp/pypi
#   $ python -m pip install -v coverage --index-url=file:///tmp/pypi/simple
#

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

env:
  PIP_DISABLE_PIP_VERSION_CHECK: 1

jobs:
  wheels:
    name: "Build ${{ matrix.os }} ${{ matrix.py }} ${{ matrix.arch }} wheels"
    runs-on: ${{ matrix.os }}-latest
    strategy:
      matrix:
        include:
          # To change the matrix, edit the choices, then process this file with cog:
          #
          # $ python -m pip install cogapp
          # $ python -m cogapp -rP .github/workflows/kit.yml
          #
          #
          # [[[cog
          #   #----- vvv Choices for the matrix vvv -----
          #   oss = ["ubuntu", "macos", "windows"]
          #   pys = ["cp36", "cp37", "cp38", "cp39", "cp310"]
          #   archs = {
          #       "ubuntu": ["x86_64", "i686", "aarch64"],
          #       "macos": ["x86_64"],
          #       "windows": ["x86", "AMD64"],
          #   }
          #   #----- ^^^ ---------------------- ^^^ -----
          #
          #   import json
          #   for the_os in oss:
          #       for the_py in pys:
          #           for the_arch in archs[the_os]:
          #               them = {
          #                   "os": the_os,
          #                   "py": the_py,
          #                   "arch": the_arch,
          #               }
          #               print(f"- {json.dumps(them)}")
          # ]]]
          - {"os": "ubuntu", "py": "cp36", "arch": "x86_64"}
          - {"os": "ubuntu", "py": "cp36", "arch": "i686"}
          - {"os": "ubuntu", "py": "cp36", "arch": "aarch64"}
          - {"os": "ubuntu", "py": "cp37", "arch": "x86_64"}
          - {"os": "ubuntu", "py": "cp37", "arch": "i686"}
          - {"os": "ubuntu", "py": "cp37", "arch": "aarch64"}
          - {"os": "ubuntu", "py": "cp38", "arch": "x86_64"}
          - {"os": "ubuntu", "py": "cp38", "arch": "i686"}
          - {"os": "ubuntu", "py": "cp38", "arch": "aarch64"}
          - {"os": "ubuntu", "py": "cp39", "arch": "x86_64"}
          - {"os": "ubuntu", "py": "cp39", "arch": "i686"}
          - {"os": "ubuntu", "py": "cp39", "arch": "aarch64"}
          - {"os": "ubuntu", "py": "cp310", "arch": "x86_64"}
          - {"os": "ubuntu", "py": "cp310", "arch": "i686"}
          - {"os": "ubuntu", "py": "cp310", "arch": "aarch64"}
          - {"os": "macos", "py": "cp36", "arch": "x86_64"}
          - {"os": "macos", "py": "cp37", "arch": "x86_64"}
          - {"os": "macos", "py": "cp38", "arch": "x86_64"}
          - {"os": "macos", "py": "cp39", "arch": "x86_64"}
          - {"os": "macos", "py": "cp310", "arch": "x86_64"}
          - {"os": "windows", "py": "cp36", "arch": "x86"}
          - {"os": "windows", "py": "cp36", "arch": "AMD64"}
          - {"os": "windows", "py": "cp37", "arch": "x86"}
          - {"os": "windows", "py": "cp37", "arch": "AMD64"}
          - {"os": "windows", "py": "cp38", "arch": "x86"}
          - {"os": "windows", "py": "cp38", "arch": "AMD64"}
          - {"os": "windows", "py": "cp39", "arch": "x86"}
          - {"os": "windows", "py": "cp39", "arch": "AMD64"}
          - {"os": "windows", "py": "cp310", "arch": "x86"}
          - {"os": "windows", "py": "cp310", "arch": "AMD64"}
        # [[[end]]]
      fail-fast: false

    steps:
      - name: Setup QEMU
        if: matrix.os == 'ubuntu'
        uses: docker/setup-qemu-action@v1
        with:
          platforms: arm64

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

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

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

      - name: "Build wheels"
        env:
          CIBW_BUILD: ${{ matrix.py }}-*
          CIBW_ARCHS: ${{ matrix.arch }}
          CIBW_ENVIRONMENT: PIP_DISABLE_PIP_VERSION_CHECK=1
          CIBW_TEST_COMMAND: python -c "from coverage.tracer import CTracer; print('CTracer OK!')"
        run: |
          python -m cibuildwheel --output-dir wheelhouse

      - name: "List wheels"
        run: |
          ls -al wheelhouse/

      - name: "Remove unwanted wheels"
        if: matrix.os == 'ubuntu' && matrix.py == 'cp39'
        run: |
          # Python 3.9 musllinux wheels don't work in some places, so delete them.
          # https://github.com/nedbat/coveragepy/issues/1268
          rm -f wheelhouse/*-cp39-musllinux*.whl

      - 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.8"
        uses: actions/setup-python@v2
        with:
          python-version: "3.8"

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

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

      - name: "List tarballs"
        run: |
          ls -al dist/

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

  pypy:
    name: "Build PyPy wheel"
    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/kit.pip

      - name: "Build wheel"
        run: |
          # One wheel works for all PyPy versions.
          # yes, this is weird syntax: https://github.com/pypa/build/issues/202
          pypy3 -m build -w -C="--global-option=--python-tag" -C="--global-option=pp36.pp37.pp38"

      - name: "List wheels"
        run: |
          ls -al dist/

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

  prerel:
    name: "Build pre-rel ${{ matrix.os }} ${{ matrix.py }} wheels"
    if: ${{ true }}  # true when there are pre-rel, false when not.
    runs-on: "${{ matrix.os }}-latest"
    strategy:
      matrix:
        os:
          - ubuntu
          - windows
          - macos
        py:
          - "3.11.0-alpha.2"
      fail-fast: false

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

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

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

      - name: "Build wheel"
        run: |
          python -m build

      - 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/

      - name: "List wheels"
        run: |
          ls -al dist/

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