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 -k https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-flake8.sh
                . ./prepare-and-run-flake8.sh "$(basename $GITHUB_REPOSITORY)" ./test examples

    pylint:
        name: Pylint
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                sed 's/python=3/python=3.7/' .test-conda-env-py3.yml > .test-conda-env.yml
                USE_CONDA_BUILD=1
                curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/main/prepare-and-run-pylint.sh
                . ./prepare-and-run-pylint.sh "$(basename $GITHUB_REPOSITORY)" test/test_*.py

    pytest:
        name: Conda Pytest
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O -k 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

    pytest_intel:
        name: Conda Pytest with Intel CL
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O https://raw.githubusercontent.com/illinois-scicomp/machine-shop-maintenance/main/install-intel-icd.sh
                sudo bash ./install-intel-icd.sh
                CONDA_ENVIRONMENT=.test-conda-env-py3.yml
                echo "- ocl-icd-system" >> "$CONDA_ENVIRONMENT"
                sed -i "/pocl/ d" "$CONDA_ENVIRONMENT"
                export PYOPENCL_TEST=intel
                source /opt/enable-intel-cl.sh
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env
                test_py_project

    pytest_no_arg_check:
        name: Conda Pytest without arg check
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O -k https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project-within-miniconda.sh
                export _LOOPY_SKIP_ARG_CHECKS=1
                . ./build-and-test-py-project-within-miniconda.sh

    pytest_twice:
        name: Conda Pytest Twice (for cache behavior)
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                curl -L -O -k https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env
                ( test_py_project )
                ( test_py_project )

    examples:
        name: Conda Examples
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -   name: "Main Script"
            run: |
                EXTRA_INSTALL="matplotlib ipykernel nbconvert"
                curl -L -O -k https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env

                curl -L "https://ci.appveyor.com/api/projects/ispc/ispc/artifacts/build%2Fispc-trunk-linux.tar.gz?job=Environment%3A%20APPVEYOR_BUILD_WORKER_IMAGE%3DUbuntu1604%2C%20LLVM_VERSION%3Dlatest" | tar xfz -
                export PATH="$(pwd)/ispc-trunk-linux/bin:$PATH"

                export PYOPENCL_TEST=portable:pthread

                . ./build-py-project-and-run-examples.sh
                run_py_examples
                run_ipynb_examples
                run_floopy_examples

    docs:
        name: Documentation
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v2
        -
            uses: actions/setup-python@v1
            with:
                python-version: '3.x'
        -   name: "Main Script"
            run: |
                PROJECT=loopy
                curl -L -O -k https://tiker.net/ci-support-v0
                . ci-support-v0
                build_py_project_in_conda_env
                build_docs

    downstream_tests:
        strategy:
            matrix:
                downstream_project: [meshmode, grudge, pytential, pytato]
            fail-fast: false
        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 https://tiker.net/ci-support-v0
                . ./ci-support-v0

                # Use "special grudge" for kcv3 and branches targeting it.
                if [[ "$DOWNSTREAM_PROJECT" = "grudge" ]] && [[ "$GITHUB_HEAD_REF" = "kernel_callables_v3-edit2" || "$GITHUB_BASE_REF" = "kernel_callables_v3-edit2" ]]; then
                  with_echo git clone "https://github.com/kaushikcfd/$DOWNSTREAM_PROJECT.git" -b "kcv3-e2-compat"
                else
                  with_echo git clone "https://github.com/inducer/$DOWNSTREAM_PROJECT.git"
                fi
                cd "$DOWNSTREAM_PROJECT"
                echo "*** $DOWNSTREAM_PROJECT version: $(git rev-parse --short HEAD)"

                if [[ "$DOWNSTREAM_PROJECT" = "pytential" ]] && [[ "$GITHUB_HEAD_REF" = "kernel_callables_v3-edit2" || "$GITHUB_BASE_REF" = "kernel_callables_v3-edit2" ]]; then
                  sed -i "/egg=sumpy/ c git+https://github.com/inducer/sumpy.git@loopy-callables#egg=sumpy" requirements.txt
                fi

                transfer_requirements_git_urls ../requirements.txt ./requirements.txt
                sed -i "/egg=loopy/ c git+file://$(readlink -f ..)#egg=loopy" requirements.txt
                sed -i "/mpi4py/ d" requirements.txt

                # Avoid slow or complicated tests in downstream projects
                export PYTEST_ADDOPTS="-k 'not (slowtest or octave or mpi)'"

                build_py_project_in_conda_env
                test_py_project

# vim: sw=4
