name: Main

on:
  push:
    paths-ignore:
      - '.gitignore'
      - '*.md'

jobs:
  lint:
    name: 🧹 Lint / 📜 C++
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Run clang-format style check
        uses: jidicula/clang-format-action@v4.9.0
        with:
          clang-format-version: '15'
          exclude-regex: 'extern'

  build:
    strategy:
      fail-fast: false
      matrix:
        build_type: ['Debug', 'Release']
        config:
          - { name: '🍏 macOS Clang', os: macos-latest }
          - { name: '🐧 Linux GCC', os: ubuntu-latest }
          - { name: '🪟 Windows MSVC', os: windows-latest }

    name: 🛠 Build / ${{ matrix.config.name }} (${{ matrix.build_type }})
    runs-on: ${{ matrix.config.os }}

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

      - name: Install Dependencies (macOS)
        if: matrix.config.os == 'macos-latest'
        run: brew install ccache ninja

      - name: Install Dependencies (Linux)
        if: matrix.config.os == 'ubuntu-latest'
        run: |
          sudo apt-get update
          sudo apt-get install -y ccache ninja-build

      - name: Install Dependencies (Windows)
        if: matrix.config.os == 'windows-latest'
        run: |
          choco upgrade ccache ninja

      - name: Setup MSVC (Windows)
        if: matrix.config.os == 'windows-latest'
        uses: ilammy/msvc-dev-cmd@v1

      - name: ccache
        uses: hendrikmuhs/ccache-action@v1.2
        with:
          max-size: '10G'
          key: ${{ matrix.config.os }}-${{ matrix.build_type }}

      - name: Configure
        run: >
          mkdir GDExtension-build

          cmake
          -B GDExtension-build
          -G "Ninja"
          -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
          --install-prefix ${{ github.workspace }}/install-${{ matrix.build_type }}
          .

      - name: Build
        run: cmake --build GDExtension-build

      - name: Install
        run: cmake --install GDExtension-build

      - name: Upload artifact (Debug)
        if: matrix.build_type == 'Debug'
        uses: actions/upload-artifact@v3
        with:
          name: ${{ github.event.repository.name }}-Debug
          path: |
            ${{ github.workspace }}/install-${{ matrix.build_type }}/*

      - name: Upload artifact (Release)
        if: matrix.build_type == 'Release'
        uses: actions/upload-artifact@v3
        with:
          name: ${{ github.event.repository.name }}-Release
          path: |
            ${{ github.workspace }}/install-${{ matrix.build_type }}/*
