---
# This action is centrally managed in https://github.com/<organization>/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in
# the above-mentioned repo.

# Lint c++ source files and cmake files.

name: C++ Lint

on:
  pull_request:
    branches: [master, nightly]
    types: [opened, synchronize, reopened]

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  clang-format:
    name: Clang Format Lint
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Find cpp files
        id: cpp_files
        run: |
          cpp_files=$(find . -type f -iname "*.cpp" -o -iname "*.h" -o -iname "*.m" -o -iname "*.mm")

          echo "found cpp files: ${cpp_files}"

          # do not quote to keep this as a single line
          echo cpp_files=${cpp_files} >> $GITHUB_OUTPUT

      - name: Clang format lint
        if: ${{ steps.cpp_files.outputs.cpp_files }}
        uses: DoozyX/clang-format-lint-action@v0.15
        with:
          source: ${{ steps.cpp_files.outputs.cpp_files }}
          extensions: 'cpp,h,m,mm'
          clangFormatVersion: 15
          style: file
          inplace: false

      - name: Upload Artifacts
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: clang-format-fixes
          path: ${{ steps.cpp_files.outputs.cpp_files }}

  cmake-lint:
    name: CMake Lint
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

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

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip setuptools cmakelang

      - name: Find cmake files
        id: cmake_files
        run: |
          cmake_files=$(find . -type f -iname "CMakeLists.txt" -o -iname "*.cmake")

          echo "found cmake files: ${cmake_files}"

          # do not quote to keep this as a single line
          echo cmake_files=${cmake_files} >> $GITHUB_OUTPUT

      - name: Test with cmake-lint
        run: |
          cmake-lint --line-width 120 --tab-size 4 ${{ steps.cmake_files.outputs.cmake_files }}
