name: CI
on:
    push:
        branches:
        - main
    pull_request:
    schedule:
        - cron:  '17 3 * * 0'

jobs:
    flake8:
        name: Flake8
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -
            uses: actions/setup-python@v1
            with:
                # matches compat target in setup.py
                python-version: '3.6'
        -   name: "Main Script"
            run: |
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
                . ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)" examples test

    pylint:
        name: Pylint
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                echo "- matplotlib" >> .test-conda-env-py3.yml
                echo "- scipy" >> .test-conda-env-py3.yml
                echo "-------------------------------------------"
                cat $CONDA_ENVIRONMENT
                echo "-------------------------------------------"
                USE_CONDA_BUILD=1

                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/master/prepare-and-run-pylint.sh

                . ./prepare-and-run-pylint.sh "$(basename $GITHUB_REPOSITORY)" test/*.py \
                    $(find examples -name '*.py')

    pytest3:
        name: Pytest on Py3
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project-within-miniconda.sh
                . ./build-and-test-py-project-within-miniconda.sh

    pyexamples3:
        name: Examples on Py3
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ci-support-v0
                build_py_project_in_conda_env
                run_examples

                python wave/wave-op-mpi.py --lazy
                python wave/wave-op-mpi.py --lazy --quad --nonaffine
                python euler/acoustic_pulse.py --lazy
                python euler/vortex.py --oi --lazy

                mpiexec -np 2 --oversubscribe python wave/wave-op-mpi.py --lazy

    docs:
        name: Documentation
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                echo "- matplotlib" >> .test-conda-env-py3.yml
                curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/ci-support.sh
                . ci-support.sh
                build_py_project_in_conda_env
                build_docs

    downstream_tests:
        strategy:
            matrix:
                downstream_project: [mirgecom]
        name: Tests for downstream project ${{ matrix.downstream_project }}
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            env:
                DOWNSTREAM_PROJECT: ${{ matrix.downstream_project }}
            run: |
                curl -L -O -k https://tiker.net/ci-support-v0
                . ./ci-support-v0

                if test "$DOWNSTREAM_PROJECT" = "mirgecom"; then
                    if [[ "$GITHUB_HEAD_REF" = "nodal-reduction-device-scalar" ]]; then
                        git clone "https://github.com/majosm/$DOWNSTREAM_PROJECT.git" -b "nodal-reduction-device-scalar"
                    else
                        git clone "https://github.com/illinois-ceesd/$DOWNSTREAM_PROJECT.git"
                    fi
                else
                    git clone "https://github.com/inducer/$DOWNSTREAM_PROJECT.git"
                fi
                cd "$DOWNSTREAM_PROJECT"
                echo "*** $DOWNSTREAM_PROJECT version: $(git rev-parse --short HEAD)"

                transfer_requirements_git_urls ../requirements.txt ./requirements.txt
                edit_requirements_txt_for_downstream_in_subdir

                # Avoid slow or complicated tests in downstream projects
                export PYTEST_ADDOPTS="-k 'not (slowtest or octave or mpi)'"
                if test "$DOWNSTREAM_PROJECT" = "mirgecom"; then
                    # can't turn off MPI in mirgecom
                    export CONDA_ENVIRONMENT=conda-env.yml
                    echo "- mpi4py" >> "$CONDA_ENVIRONMENT"

                    # https://github.com/inducer/grudge/issues/211
                    export CISUPPORT_PARALLEL_PYTEST=no
                else
                    sed -i "/mpi4py/ d" requirements.txt
                fi
                build_py_project_in_conda_env
                test_py_project

# vim: sw=4
