name: main

on: [push, pull_request]

jobs:
  test:
    runs-on: ${{ matrix.os }}
    timeout-minutes: 30
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04 , windows-latest, macos-latest]
        qt-lib: [pyqt, pyside]
        python-version: [3.7, 3.8, 3.9]
        include:
          - python-version: "3.7"
            qt-lib: "pyqt"
            qt-version: "PyQt5~=5.12.0"
            numpy-version: "~=1.17.0"
          - python-version: "3.7"
            qt-lib: "pyside"
            qt-version: "PySide2~=5.12.0"
            numpy-version: "~=1.17.0"
          - python-version: "3.8"
            qt-lib: "pyqt"
            qt-version: "PyQt5~=5.15.0"
            numpy-version: "~=1.19.0"
          - python-version: "3.8"
            qt-lib: "pyside"
            qt-version: "PySide2~=5.15.0"
            numpy-version: "~=1.19.0"
          - python-version: "3.9"
            qt-lib: "pyqt"
            qt-version: "PyQt6"
            numpy-version: "~=1.19.0"
          - python-version: "3.9"
            qt-lib: "pyside"
            qt-version: "PySide6"
            numpy-version: "~=1.19.0"
    steps:
    - name: Checkout
      uses: actions/checkout@v2
    - name: Checkout test-data
      uses: actions/checkout@v2
      with:
        repository: pyqtgraph/test-data
        path: .pyqtgraph/test-data
    - name: Setup Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        # Semantic version range syntax or exact version of a Python version
        python-version: ${{ matrix.python-version }}
    - name: "Install Windows-Mesa OpenGL DLL"
      if: runner.os == 'Windows'
      run: |
        curl -LJO https://github.com/pal1000/mesa-dist-win/releases/download/19.2.7/mesa3d-19.2.7-release-msvc.7z
        7z x mesa3d-19.2.7-release-msvc.7z
        cd x64
        xcopy opengl32.dll C:\windows\system32\mesadrv.dll*
        xcopy opengl32.dll C:\windows\syswow64\mesadrv.dll*
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v DLL /t REG_SZ /d "mesadrv.dll" /f
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v DriverVersion /t REG_DWORD /d 1 /f
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v Flags /t REG_DWORD /d 1 /f
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v Version /t REG_DWORD /d 2 /f
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v DLL /t REG_SZ /d "mesadrv.dll" /f
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v DriverVersion /t REG_DWORD /d 1 /f
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v Flags /t REG_DWORD /d 1 /f 
        REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion\OpenGLDrivers\MSOGL" /v Version /t REG_DWORD /d 2 /f 
      shell: cmd
    - name: Install Dependencies
      run: |
        pip install --upgrade pip
        pip install ${{ matrix.qt-version }} numpy${{ matrix.numpy-version }} scipy pyopengl h5py matplotlib
        pip install .
        pip install pytest pytest-cov pytest-xdist coverage
    - name: "Install Linux VirtualDisplay"
      if: runner.os == 'Linux'
      run: |
        sudo apt-get update -y
        sudo apt-get install -y libxkbcommon-x11-0 x11-utils
        sudo apt-get install --no-install-recommends -y libyaml-dev libegl1-mesa libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0
        sudo apt-get install -y libopengl0
        pip install pytest-xvfb
    - name: 'Debug Info'
      run: |
        echo python location: `which python`
        echo python version: `python --version`
        echo pytest location: `which pytest`
        echo installed packages
        pip list
        echo pyqtgraph system info
        python -c "import pyqtgraph as pg; pg.systemInfo()"
      shell: bash
      env:
        QT_DEBUG_PLUGINS: 1
    - name: 'XVFB Display Info'
      run: |
        xvfb-run --server-args="-screen 0, 1920x1200x24 -ac +extension GLX +render -noreset" python -c "from pyqtgraph.opengl.glInfo import GLTest"
        xvfb-run --server-args="-screen 0, 1920x1200x24 -ac +extension GLX +render -noreset" python -m pyqtgraph.util.get_resolution
      if: runner.os == 'Linux'
    - name: 'Display Info'
      run: |
        python -c "from pyqtgraph.opengl.glInfo import GLTest"
        python -m pyqtgraph.util.get_resolution
      if: runner.os != 'Linux'
    - name: Run Tests
      run: |
        mkdir $SCREENSHOT_DIR
        pytest pyqtgraph examples -v \
          -n auto \
          --junitxml pytest.xml \
          --cov pyqtgraph --cov-report=xml --cov-report=html
      shell: bash
    - name: Upload Screenshots
      uses: actions/upload-artifact@v2
      with:
        name: Screenshots (Python ${{ matrix.python-version }} - Qt-Bindings ${{ matrix.qt-lib }} - OS ${{ matrix.os }})
        path: $SCREENSHOT_DIR
        if-no-files-found: ignore
    env:
      SCREENSHOT_DIR: ./screenshots

  build-docs:
    name: build docs
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Setup Python 3.9
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - name: Install Dependencies
        run: |
          cd doc
          python -m pip install -r requirements.txt
      - name: Build Documentation
        run: |
          cd doc
          make html SPHINXOPTS='-W --keep-going -v'

  build-wheel:
    name: build wheel
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Setup Python 3.9
        uses: actions/setup-python@v2
        with:
          python-version: '3.9'
      - name: Build Wheel
        run: |
          python -m pip install setuptools wheel
          python setup.py bdist_wheel
