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@v3
        -
            uses: actions/setup-python@v4
            with:
                # matches compat target in setup.py
                python-version: '3.8'
        -   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)" ./test examples proto-tests contrib

    pylint:
        name: Pylint
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v3
        -   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 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

    mypy:
        name: Mypy
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v3
        -   name: "Main Script"
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env
                python -m pip install mypy
                ./run-mypy.sh

    pytest:
        name: Conda Pytest
        runs-on: ${{ matrix.os }}
        strategy:
          matrix:
            os: [ubuntu-latest, macos-latest]
        steps:
        -   uses: actions/checkout@v3
        -   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

    pytest_intel:
        name: Conda Pytest with Intel CL
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v3
        -   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@v3
        -   name: "Main Script"
            run: |
                curl -L -O 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@v3
        -   name: "Main Script"
            run: |
                curl -L -O 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@v3
        -   name: "Main Script"
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                build_py_project_in_conda_env

                . ./.ci/examples-funcs.sh
                install_example_prereqs
                run_py_examples
                run_ipynb_examples
                run_floopy_examples

    docs:
        name: Documentation
        runs-on: ubuntu-latest
        steps:
        -   uses: actions/checkout@v3
        -
            uses: actions/setup-python@v4
            with:
                python-version: '3.x'
        -   name: "Main Script"
            run: |
                curl -L -O 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@v3
        -   name: "Main Script"
            env:
                DOWNSTREAM_PROJECT: ${{ matrix.downstream_project }}
            run: |
                curl -L -O https://tiker.net/ci-support-v0
                . ./ci-support-v0
                test_downstream "$DOWNSTREAM_PROJECT"

    downstream_firedrake:
        name: Tests for downstream project Firedrake
        runs-on: ubuntu-latest
        container:
            image: 'firedrakeproject/firedrake'
        steps:
        -   name: "Main script"
            run: |
                cd /home/firedrake
                export HOME="$(pwd)"
                mkdir loopy
                cd loopy
                git init

                git remote add origin "https://github.com/$GITHUB_REPOSITORY.git"
                git fetch origin "$GITHUB_REF"
                git checkout FETCH_HEAD

                git submodule update --init

                . /home/firedrake/firedrake/bin/activate

                pip install --editable .
                pip uninstall -y pytools
                pip uninstall -y pymbolic
                pip install "git+https://github.com/inducer/pytools.git#egg=pytools"
                pip install "git+https://github.com/inducer/pymbolic.git#egg=pymbolic"

                cd /home/firedrake/firedrake/src/firedrake

                # patch so exception messages get shown
                curl -L https://gist.githubusercontent.com/inducer/17d7134ace215f0df1f3627eac4195c7/raw/63edfaf2ec8bf06987896569a4f24264df490e9e/firedrake-debug-patch.diff | patch -p1

                pytest --tb=native -rsxw --durations=10 -m 'not parallel' tests/multigrid/

# vim: sw=4
