# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Sverchok CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      blender_version: "2.93"

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.9
      uses: actions/setup-python@v2
      with:
        python-version: 3.9
    - name: Cache
      uses: actions/cache@v2
      with:
        path: ./installation
        key: blender-${{ env.blender_version }}-${{ hashFiles('.github/workflows/test-sverchok.yml') }}
    - name: Install Blender
      run: |
        # echo "starting run"
        BLENDER_VERSION=2.93
        BLENDER_URL=https://ftp.nluug.nl/pub/graphics/blender/release/Blender2.93/blender-2.93.1-linux-x64.tar.xz
        # BLENDER_URL=https://ftp.nluug.nl/pub/graphics/blender/release/Blender2.93/blender-2.93.0-linux-x64.tar.xz  # OLDER
        SVERCHOK_DIR=scripts/addons/sverchok
        BLENDER_TAR=$(basename $BLENDER_URL)
        BLENDER_DIR=$(basename $BLENDER_URL .tar.xz)
        RELEASE=blender293_LTS

        if [ ! -f installation/blender/blender ]
        then
            mkdir -p installation
            cd installation

            # not verbose, but not quiet.
            wget -nv $BLENDER_URL

            # unpack this tar to a known name
            mkdir $RELEASE
            tar xf $BLENDER_TAR -C $RELEASE --strip-components 1

            rm $BLENDER_TAR
            mv $RELEASE blender

            pushd blender/
            PYTHON=${BLENDER_VERSION}/python/bin/python3.9
            $PYTHON -m ensurepip
            $PYTHON -m pip install --upgrade pip setuptools wheel
            $PYTHON -m pip install --upgrade scipy geomdl scikit-image
            popd

            cd ..
        fi

        #mkdir -p ${PWD}/installation/blender/${BLENDER_VERSION}/${SVERCHOK_DIR}
        ln -s ${PWD} ${PWD}/installation/blender/${BLENDER_VERSION}/${SVERCHOK_DIR}
        mkdir -p ~/.config/blender/${BLENDER_VERSION}/config/
        ln -s ${PWD}/tests/references/userpref.blend ~/.config/blender/${BLENDER_VERSION}/config/
        
    - name: Test it
      run: |
        bash ./run_tests.sh
      env:
        BLENDER: |
          ./installation/blender/blender-softwaregl

    - name: Save logs
      uses: actions/upload-artifact@v2
      if: failure()
      with:
        name: log_files
        retention-days: 5
        path: |
          ./tests/sverchok_tests.log
          ./tests/log_files/
