name: CI build
on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master


# Global Settings
env:
  PYTHON_VERSION: "3.7"
  PANDEMONIUM_BINARY_VERSION: "3.2.3"


jobs:


  static-checks:
    name: '📊 Static checks'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@f1d3225b5376a0791fdee5a0e8eac5289355e43a  # pin@v2
        with:
            submodules: true
      - name: Set up Python
        uses: actions/setup-python@0291cefc54fa79cd1986aee8fa5ecb89ad4defea  # pin@v2
        with:
          python-version: ${{ env.PYTHON_VERSION }}
      - name: Bootstrap
        run: |
          set -eux
          python --version
          pip install pre-commit
      - name: Pre-commit hooks check
        run: |
          pre-commit run --all-files --show-diff-on-failure


#################################################################################


  linux-build:
    name: '🐧 Linux build'
    runs-on: ubuntu-latest
    env:
      CC: clang
      PLATFORM: 'x11-64'
    steps:
      - name: 'Checkout'
        uses: actions/checkout@f1d3225b5376a0791fdee5a0e8eac5289355e43a  # pin@v2
        with:
            submodules: true
      - name: 'Set up Python'
        uses: actions/setup-python@0291cefc54fa79cd1986aee8fa5ecb89ad4defea  # pin@v2
        with:
          python-version: ${{ env.PYTHON_VERSION }}
      - name: 'Setup venv'
        run: |
          set -eux
          ${{ env.CC }} --version
          python --version
          pip install -U pip
          pip install -r requirements.txt
          # Configuration for scons
          echo 'pandemonium_binary = "${{ env.PANDEMONIUM_BINARY_VERSION }}"' >> custom.py
          echo 'platform = "${{ env.PLATFORM }}"' >> custom.py
          echo 'CC = "${{ env.CC }}"' >> custom.py
      - name: 'Build project'
        run: |
          set -eux
          scons build -j2
#      - name: 'Start xvfb'
#        run: |
#          /usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
#          echo ">>> Started xvfb"
#      - name: 'Run tests'
#        run: |
#          set -eux
#          scons tests headless=true
#        env:
#          DISPLAY: ':99.0'
      - name: 'Generate artifact archive'
        run: |
          set -eux
          scons release
      - name: 'Export release artifact'
        uses: actions/upload-artifact@11830c9f4d30053679cb8904e3b3ce1b8c00bf40  # pin@v2
        with:
          name: ${{ env.PLATFORM }}-release
          path: 'build/pandemonium-python-*.tar.bz2'


#################################################################################


  windows-build:
    name: '🏁 Windows build'
    runs-on: windows-latest
    strategy:
      matrix:
        include:
          - PLATFORM: 'windows-64'
            PYTHON_ARCH: 'x64'
            VS_ARCH: 'amd64'
          - PLATFORM: 'windows-32'
            PYTHON_ARCH: 'x86'
            VS_ARCH: 'x86'
    steps:
      - name: 'Checkout'
        uses: actions/checkout@f1d3225b5376a0791fdee5a0e8eac5289355e43a  # pin@v2
        with:
            submodules: true
      - name: 'Set up Python'
        uses: actions/setup-python@0291cefc54fa79cd1986aee8fa5ecb89ad4defea  # pin@v2
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          architecture: ${{ matrix.PYTHON_ARCH }}
      - name: 'Setup venv'
        shell: bash
        run: |
          set -eux
          python --version
          python -m pip install --user -U pip
          python -m pip install --user -r requirements.txt
          # Configuration for scons
          echo 'pandemonium_binary = "${{ env.PANDEMONIUM_BINARY_VERSION }}"' >> custom.py
          echo 'platform = "${{ matrix.PLATFORM }}"' >> custom.py
          echo 'MSVC_USE_SCRIPT = True' >> custom.py
          echo 'TARGET_ARCH = "${{ matrix.VS_ARCH }}"' >> custom.py
          echo 'CC = "cl.exe"' >> custom.py
      - name: 'Build project'
        shell: bash
        run: |
          set -eux
          scons build -j2
#      - name: 'Install Mesa3D OpenGL'
#        shell: bash
#        run: |
#          set -eux
#          # Azure pipelines doesn't provide a GPU with an OpenGL driver,
#          # hence we use Mesa3D as software OpenGL driver
#          pushd build/${{ matrix.PLATFORM }}/platforms/
#          if [ "${{ matrix.PLATFORM }}" = "windows-64" ]
#          then
#            curl https://downloads.fdossena.com/Projects/Mesa3D/Builds/MesaForWindows-x64-20.0.7.7z -o mesa.7z
#          else
#            curl https://downloads.fdossena.com/Projects/Mesa3D/Builds/MesaForWindows-20.0.7.7z -o mesa.7z
#          fi
#          # opengl32.dll must be extracted in the same directory than Pandemonium binary
#          7z.exe x mesa.7z
#          ls -lh opengl32.dll  # Sanity check
#          popd
#      - name: 'Run tests'
#        shell: bash
#        run: |
#          set -eux
#          scons tests
      - name: 'Generate artifact archive'
        shell: bash
        run: |
          scons release
      - name: 'Export release artifact'
        uses: actions/upload-artifact@11830c9f4d30053679cb8904e3b3ce1b8c00bf40  # pin@v2
        with:
          name: ${{ matrix.PLATFORM }}-release
          path: 'build/pandemonium-python-*.zip'


#################################################################################


  macos-build:
    name: '🍎 macOS build'
    runs-on: macos-latest
    env:
      CC: clang
      PLATFORM: 'osx-64'
    steps:
      - name: 'Checkout'
        uses: actions/checkout@f1d3225b5376a0791fdee5a0e8eac5289355e43a  # pin@v2
        with:
            submodules: true
      - name: 'Set up Python'
        uses: actions/setup-python@0291cefc54fa79cd1986aee8fa5ecb89ad4defea  # pin@v2
        with:
          python-version: ${{ env.PYTHON_VERSION }}
      - name: 'Setup venv'
        run: |
          set -eux
          ${{ env.CC }} --version
          python --version
          brew update
          brew install zlib openssl
          brew install --cask xquartz
          pip install -U pip
          pip install -r requirements.txt
          # Configuration for scons
          echo 'pandemonium_binary = "${{ env.PANDEMONIUM_BINARY_VERSION }}"' >> custom.py
          echo 'platform = "${{ env.PLATFORM }}"' >> custom.py
          echo 'CC = "${{ env.CC }}"' >> custom.py
      - name: 'Build project'
        run: |
          set -eux
          scons build -j2
#      - name: 'Run tests'
#        run: |
#          set -eux
#          scons tests
      - name: 'Generate artifact archive'
        run: |
          set -eux
          scons release
      - name: 'Export release artifact'
        uses: actions/upload-artifact@11830c9f4d30053679cb8904e3b3ce1b8c00bf40  # pin@v2
        with:
          name: ${{ env.PLATFORM }}-release
          path: 'build/pandemonium-python-*.zip'


#################################################################################


  publish-release:
    name: 'Publish ${{ matrix.PLATFORM }} release'
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    needs:
      - linux-build
      - windows-build
      - macos-build
    strategy:
      matrix:
        include:
          - PLATFORM: x11-64
          - PLATFORM: windows-64
          - PLATFORM: windows-32
          - PLATFORM: osx-64
    steps:
      - uses: actions/download-artifact@0ede0875b5db9a2824878bbbbe3d758a75eb8268  # pin@v2
        name: ${{ matrix.PLATFORM }}-release
      - name: 'Upload release'
        uses: svenstaro/upload-release-action@483c1e56f95e88835747b1c7c60581215016cbf2  # pin@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ github.ref }}
          file: pandemonium-python-*.*
          file_glob: true
          overwrite: true
