name: CI

on:
  push:
    branches:
      - master
      - release/*
  pull_request:
    branches:
      - master
      - release/*

jobs:

  # ----------------------------------------
  lint-tests:
    strategy:
      fail-fast: true
      matrix:
        python-version: ['3.10', '3.11', '3.12']
        os: [ubuntu-latest, macos-latest, windows-latest]
    defaults:
      run:
        shell: bash
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v3

    - name: Install Just
      uses: extractions/setup-just@v2

    - name: Install poetry
      run: pipx install poetry!=1.4.1

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
        cache: 'poetry'
    - name: Install project
      run: poetry install -E all
    - name: Lint and static analysis
      if:  matrix.os == 'ubuntu-latest' && matrix.python-version == '3.12'  # no need to run that 9x
      run: |
        poetry run just lint
    # needed for tests to work on ubuntu (libEGL.so.1)
    - name: Install EGL mesa
      if: matrix.os == 'ubuntu-latest'
      run: |
        sudo apt-get update -y -qq
        sudo apt-get install -y -qq libegl1-mesa libegl1-mesa-dev
    # PYTEST STRATEGY
    # macOS is the only runner who has working ModernGL behaviour
    # macOS + 3.11 is used for code coverage
    - name: Pytest (code coverage)
      run: |
        poetry run pytest --cov=./ --cov-report=xml
      if: matrix.os == 'macos-latest' && matrix.python-version == '3.12'
    - name: Pytest
      run: |
        poetry run pytest
      if: matrix.os == 'macos-latest' && matrix.python-version != '3.12'
    - name: Pytest (no image similarity check)
      run: |
        poetry run pytest --skip-image-similarity
      if: matrix.os != 'macos-latest'
    - name: Upload comparison test results
      if: failure()
      uses: actions/upload-artifact@v3
      with:
        name: test_report_${{ runner.os }}_${{ matrix.python-version }}
        path: |
          test_report_img_sim/**/*
          test_report_reference_svg/**/*
        if-no-files-found: ignore
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
        file: ./coverage.xml
      if: matrix.os == 'macos-latest' && matrix.python-version == '3.12'


  # ----------------------------------------
  windows-installer:
    defaults:
      run:
        shell: bash
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v3
    - name: Install poetry
      run: pipx install poetry!=1.4.1
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.12'
        cache: 'poetry'
    - name: Install project
      run: poetry install -E all
    - name: Run PyInstaller
      run: | 
        VERSION="`poetry version --short`-${GITHUB_SHA}"
        echo "version=${VERSION}" >> $GITHUB_ENV
        poetry run ${GITHUB_WORKSPACE}\\scripts\\build.bat
    - name: Create NSIS installer
      uses: joncloud/makensis-action@v4
      with:
        script-file: scripts\\installer_win.nsi
        arguments: /V4 /DVERSION=${{ env.version }}
    - uses: actions/upload-artifact@v3
      with:
        name: vpype-${{ env.version }}-setup.exe
        path: dist/vpype-${{ env.version }}-setup.exe