name: check
on:
  push:
  pull_request:
  schedule:
    - cron: "0 8 * * *"

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-python@v2
      - uses: pre-commit/action@v2.0.0

  test:
    name: test ${{ matrix.py }} - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}-latest
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu
          - windows
          - macos
        py:
          - 3.9
          - 3.8
          - 3.7
          - 3.6
          - 3.5
          - pypy3
          - 2.7
          - pypy2
        include:
          - { os: macos, py: brew@py3 }
          - { os: ubuntu, py: 3.4.10 }
    steps:
      - name: Install OS dependencies
        run: |
          for i in 1 2 3; do
            echo "try $i" && \
            ${{ runner.os == 'Linux' && 'sudo apt-get update -y && sudo apt-get install fish csh' || true}} && \
            ${{ runner.os == 'macOS' && 'brew update -vvv && brew install fish tcsh' || true}} && \
            ${{ runner.os == 'Windows' && ( matrix.py == '2.7' || matrix.py == 'pypy2' ) && 'choco install vcpython27 --yes -f' || true }} && \
            exit 0 || true;
          done
          exit 1
        shell: bash
      - name: Setup python for tox
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install tox
        run: python -m pip install tox
      - uses: actions/checkout@v2
      - name: Use local virtualenv for tox
        run: python -m pip install .
      - name: Setup python for test ${{ matrix.py }}
        if: "!( startsWith(matrix.py,'brew@py') || endsWith(matrix.py, '-dev') )"
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.py }}
      - name: Setup brew python for test ${{ matrix.py }}
        if: startsWith(matrix.py,'brew@py')
        run: |
          import subprocess
          subprocess.check_call(["bash", "-c", f"brew upgrade python@3 || brew install python@3"])
          print(f"::add-path::/usr/local/opt/python@3")
        shell: python
      - name: Pick environment to run
        run: |
          import platform; import os; import sys; import codecs
          cpy = platform.python_implementation() == "CPython"
          base =("{}{}{}" if cpy else "{}{}").format("py" if cpy else "pypy", *sys.version_info[0:2])
          env = "TOXENV={}\n".format(base)
          print("Picked:\n{}for{}".format(env, sys.version))
          with codecs.open(os.environ["GITHUB_ENV"], "a", "utf-8") as file_handler:
               file_handler.write(env)
        shell: python
      - name: Setup test suite
        run: tox -vv --notest
      - name: Run test suite
        run: tox --skip-pkg-install
        env:
          PYTEST_ADDOPTS: "-vv --durations=20"
          CI_RUN: "yes"
          DIFF_AGAINST: HEAD
      - name: Rename coverage report file
        run: |
          import os; os.rename('.tox/coverage.{}.xml'.format(os.environ['TOXENV']), '.tox/coverage.xml')
        shell: python
      - uses: codecov/codecov-action@v1
        with:
          file: ./.tox/coverage.xml
          flags: tests
          name: ${{ matrix.py }} - ${{ matrix.os }}

  check:
    name: ${{ matrix.tox_env }} - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}-latest
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu
          - windows
        tox_env:
          - dev
          - docs
          - readme
          - upgrade
          - zipapp
        exclude:
          - { os: windows, tox_env: readme }
          - { os: windows, tox_env: docs }
    steps:
      - uses: actions/checkout@v2
      - name: Setup Python 3.9
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install tox
        run: python -m pip install tox
      - name: Run check for ${{ matrix.tox_env }}
        run: python -m tox -e ${{ matrix.tox_env }}
        env:
          UPGRADE_ADVISORY: "yes"

  publish:
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
    needs: [check, test, lint]
    runs-on: ubuntu-latest
    steps:
      - name: Setup python to build package
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      - name: Install https://pypi.org/project/build
        run: python -m pip install build
      - uses: actions/checkout@v2
      - name: Build sdist and wheel
        run: python -m build -s -w . -o dist
      - name: Publish to PyPi
        uses: pypa/gh-action-pypi-publish@master
        with:
          skip_existing: true
          user: __token__
          password: ${{ secrets.pypi_password }}
