name: Package

on:
  - push
  # push:
  #   tags:
  #     - v*.*.*

jobs:

  wheel-linux:
    runs-on: ubuntu-latest
    name: Build Linux wheels
    strategy:
      matrix:
        include:
        - python-version: 3.6
          pyo3-python: /opt/python/cp36-cp36m/bin/python
        - python-version: 3.7
          pyo3-python: /opt/python/cp37-cp37m/bin/python
        - python-version: 3.8
          pyo3-python: /opt/python/cp38-cp38/bin/python
        - python-version: 3.9
          pyo3-python: /opt/python/cp39-cp39/bin/python
        - python-version: pypy-3.7
          pyo3-python: /opt/python/pp37-pypy37_pp73/bin/python
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
      with:
        submodules: true
    - name: Build manylinux wheels
      uses: addnab/docker-run-action@v2
      with:
        image: quay.io/pypa/manylinux_2_24_x86_64
        options: -v ${{ github.workspace }}:/io -e TERM=$TERM
        shell: bash
        run: |
          apt update
          apt install -y libboost-math-dev
          ${{ matrix.pyo3-python }} -m pip install -U -r /io/.github/workflows/requirements.txt
          ${{ matrix.pyo3-python }} -m pip wheel -vv /io -w /tmp
          for whl in /tmp/*.whl; do ${{ matrix.pyo3-python }} -m auditwheel repair $whl -w /io/dist; done
    - name: Store built wheels
      uses: actions/upload-artifact@v2
      with:
        name: dist
        path: dist/*

  test-linux:
    runs-on: ubuntu-latest
    name: Test Linux wheels
    needs:
    - wheel-linux
    strategy:
      matrix:
        python-version:
        - 3.5
        - 3.6
        - 3.7
        - 3.8
        - 3.9
        - pypy-3.6
        - pypy-3.7
    steps:
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Download built wheels
      uses: actions/download-artifact@v2
      with:
          name: dist
          path: dist
    - name: Install built wheel
      run: python -m pip install --no-index --find-links=dist pyfastani
    - name: Install numpy
      run: python -m pip install numpy
      if: ${{ !startsWith(matrix.python-version, 'pypy') }}
    - name: Run tests without coverage
      run: python -m unittest pyfastani.tests -vv

  wheel-osx:
    runs-on: macos-latest
    name: Build OSX wheels
    strategy:
      matrix:
        python-version:
        - 3.5
        - 3.6
        - 3.7
        - 3.8
        - 3.9
        - pypy-3.7
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
      with:
        submodules: true
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Python requirements
      run: python -m pip install -r .github/workflows/requirements.txt
    - name: "Install Boost::math"
      run: brew install boost
    - name: Build wheel distribution
      run: python setup.py bdist_wheel
    - name: Store built wheels
      uses: actions/upload-artifact@v2
      with:
        name: dist
        path: dist/*

  test-osx:
    runs-on: macos-latest
    name: Test OSX wheels
    needs:
    - wheel-osx
    strategy:
      matrix:
        python-version:
        - 3.5
        - 3.6
        - 3.7
        - 3.8
        - 3.9
        - pypy-3.6
        - pypy-3.7
    steps:
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Download built wheels
      uses: actions/download-artifact@v2
      with:
          name: dist
          path: dist
    - name: Install built wheel
      run: python -m pip install --no-index --find-links=dist pyfastani
    - name: Install numpy
      run: python -m pip install numpy
      if: ${{ !startsWith(matrix.python-version, 'pypy') }}
    - name: Run tests without coverage
      run: python -m unittest pyfastani.tests -vv

  # wheel-windows:
  #   runs-on: windows-latest
  #   name: Build Windows wheels
  #   strategy:
  #     matrix:
  #       python-version:
  #       - 3.5
  #       - 3.6
  #       - 3.7
  #       - 3.8
  #       - 3.9
  #       - pypy-3.6
  #       - pypy-3.7
  #   steps:
  #   - name: Checkout code
  #     uses: actions/checkout@v2
  #     with:
  #       submodules: true
  #   - name: Set up Python ${{ matrix.python-version }}
  #     uses: actions/setup-python@v2
  #     with:
  #       python-version: ${{ matrix.python-version }}
  #       architecture: x64
  #   - name: Install Python requirements
  #     run: python -m pip install -r .github/workflows/requirements.txt
  #   - name: Build wheel distribution
  #     run: python setup.py bdist_wheel
  #   - name: Store built wheels
  #     uses: actions/upload-artifact@v2
  #     with:
  #       name: dist
  #       path: dist/*
  #
  # test-windows:
  #   runs-on: windows-latest
  #   name: Test Windows wheels
  #   needs:
  #   - wheel-windows
  #   strategy:
  #     matrix:
  #       python-version:
  #       - 3.5
  #       - 3.6
  #       - 3.7
  #       - 3.8
  #       - 3.9
  #       - pypy-3.6
  #       - pypy-3.7
  #   steps:
  #   - name: Setup Python ${{ matrix.python-version }}
  #     uses: actions/setup-python@v2
  #     with:
  #       python-version: ${{ matrix.python-version }}
  #   - name: Download built wheels
  #     uses: actions/download-artifact@v2
  #     with:
  #         name: dist
  #         path: dist
  #   - name: Install built wheel
  #     run: python -m pip install --no-index --find-links=dist pyfastani
  #   - name: Run tests without coverage
  #     run: python -m unittest pyfastani.tests -vv

  sdist:
    runs-on: ubuntu-latest
    name: Build source distribution
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
      with:
        submodules: true
    - name: Set up Python 3.9
      uses: actions/setup-python@v2
      with:
        python-version: 3.9
    - name: Install build requirements
      run: python -m pip install -r .github/workflows/requirements.txt
    - name: Build wheel distribution
      run: python setup.py sdist
    - name: Store built wheels
      uses: actions/upload-artifact@v2
      with:
        name: dist
        path: dist/*

  upload:
    environment: PyPI
    runs-on: ubuntu-latest
    name: Upload
    needs:
    - sdist
    - wheel-linux
    - test-linux
    - wheel-osx
    - test-osx
    # - wheel-windows
    # - test-windows
    steps:
    - name: Download built wheels
      uses: actions/download-artifact@v2
      with:
          name: dist
          path: dist
    - name: Publish distributions to PyPI
      if: startsWith(github.ref, 'refs/tags/v')
      uses: pypa/gh-action-pypi-publish@master
      with:
        user: __token__
        password: ${{ secrets.PYPI_API_TOKEN }}
        skip_existing: false