name: Builds

on: push

env:
  LIBNAME: example

concurrency:
  group: ci-${{github.actor}}-${{github.head_ref || github.run_number}}-${{github.ref}}-macos
  cancel-in-progress: true

jobs:
  build:
    runs-on:  ${{matrix.os}}
    name: ${{matrix.name}}
    strategy:
      fail-fast: false
      matrix:
        include:
          - identifier: windows-debug
            os: windows-latest
            name: 🏁 Windows Debug
            target: template_debug
            platform: windows
            arch: x86_64
          - identifier: windows-release
            os: windows-latest
            name: 🏁 Windows Release
            target: template_release
            platform: windows
            arch: x86_64
          - identifier: macos-debug
            os: macos-latest
            name: 🍎 macOS (universal) Debug
            target: template_debug
            platform: macos
            arch: universal
          - identifier: macos-release
            os: macos-latest
            name: 🍎 macOS (universal) Release
            target: template_release
            platform: macos
            arch: universal
          - identifier: linux-debug
            os: ubuntu-latest
            name: 🐧 Linux Debug
            runner: ubuntu-20.04
            target: template_debug
            platform: linux
            arch: x86_64
          - identifier: linux-release
            os: ubuntu-latest
            name: 🐧 Linux Release
            runner: ubuntu-20.04
            target: template_release
            platform: linux
            arch: x86_64

    steps:
      - name: Checkout project
        uses: actions/checkout@v3
        with:
          submodules: recursive

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.x'

      - name: Set up SCons
        shell: bash
        run: |
          python -c "import sys; print(sys.version)"
          python -m pip install scons
          scons --version
      - name: Linux dependencies
        if: ${{ matrix.platform == 'linux' }}
        run: |
          sudo apt-get update -qq
          sudo apt-get install -qqq build-essential pkg-config
      - name: Setup MinGW for Windows/MinGW build
        if: ${{ matrix.platform == 'windows' }}
        uses: egor-tensin/setup-mingw@v2

      - name: Compile godot-cpp
        shell: sh
        run: |
          scons target='${{ matrix.target }}' platform='${{ matrix.platform }}' arch='${{ matrix.arch }}'
        working-directory: godot-cpp

      - name: Compile Extension
        shell: sh
        run: |
          scons target='${{ matrix.target }}' platform='${{ matrix.platform }}' arch='${{ matrix.arch }}'
      - name: Delete compilation files
        if: ${{ matrix.platform == 'windows' }}
        run: |
          Remove-Item bin/* -Include *.exp,*.lib,*.pdb -Force
      - name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: ${{ github.event.repository.name }}
          path: |
            ${{ github.workspace }}/bin/*
      - name: Archive Release
        uses: thedoctor0/zip-release@0.7.1
        with:
          type: 'zip'
          filename: '${{ env.LIBNAME }}.${{ matrix.platform }}.${{ matrix.arch }}.zip'
          path: '${{ github.workspace }}/bin/'
        if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')

      - name: Create and upload asset
        if: success() && github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
        uses: ncipollo/release-action@v1
        with:
          allowUpdates: true
          artifacts: "${{ env.LIBNAME }}.${{ matrix.platform }}.${{ matrix.arch }}.zip"
          omitNameDuringUpdate: true
          omitBodyDuringUpdate: true
          token: ${{ secrets.GITHUB_TOKEN }}