name: Build Extension

on:
  workflow_dispatch:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        blender-version: [ 4.4 ]
    env:
      ADDON_NAME: io_scene_psk_psa
    steps:
      - uses: actions/checkout@v3
      - uses: SebRollen/toml-action@v1.2.0
        id: read_manifest
        with:
          file: '${{ env.ADDON_NAME }}/blender_manifest.toml'
          field: 'version'
      - name: Install Blender Dependencies
        run: |
          sudo apt-get update -y
          sudo apt-get install libxxf86vm-dev -y
          sudo apt-get install libxfixes3 -y
          sudo apt-get install libxi-dev -y
          sudo apt-get install libxkbcommon-x11-0 -y
          sudo apt-get install libgl1 -y
          sudo apt-get install libglx-mesa0 -y
          sudo apt-get install python3 -y
      - name: Install Requirements
        run: |
          python3 -m pip install --upgrade pip
          python3 -m pip install virtualenv
          python3 -m virtualenv venv
          source venv/bin/activate
          pip install pytest-blender
          pip install blender-downloader
      - name: Install Blender
        run: |
          source venv/bin/activate
          blender_executable="$(blender-downloader ${{ matrix.blender-version }} --extract --print-blender-executable)"
          echo "BLENDER_EXECUTABLE=${blender_executable}" >> $GITHUB_ENV
          blender_python="$(pytest-blender --blender-executable "$blender_executable")"
          echo "BLENDER_PYTHON=${blender_python}" >> $GITHUB_ENV
          # Write the BLENDER_PYTHON path to the console for debugging
          # Deactivate the virtualenv to avoid conflicts with the system python
          deactivate
          $blender_python -m ensurepip
          $blender_python -m pip install -r tests/requirements.txt
      - name: Build extension
        run: |
          pushd ./${{ env.ADDON_NAME }}
          # Run blender using the environment variable set by the action
          ${{ env.BLENDER_EXECUTABLE }} --command extension build
          mkdir artifact
          unzip -q ${{ env.ADDON_NAME }}-${{ steps.read_manifest.outputs.value }}.zip -d ./artifact
          popd
      - name: Run tests
        run: |
          source venv/bin/activate
          pytest -svv tests --blender-addons-dirs .
      - name: Archive addon
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.ADDON_NAME }}-${{ github.ref_name }}-${{ github.sha }}
          path: |
            ./${{ env.ADDON_NAME }}/artifact/*
