---
# 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@v4

      - name: Find cpp files
        id: find_files
        run: |
          # find files
          found_files=$(find . -type f -iname "*.cpp" -o -iname "*.h" -o -iname "*.m" -o -iname "*.mm")
          ignore_files=$(find . -type f -iname ".clang-format-ignore")

          # Loop through each C++ file
          for file in $found_files; do
            for ignore_file in $ignore_files; do
              ignore_directory=$(dirname "$ignore_file")
              # if directory of ignore_file is beginning of file
              if [[ "$file" == "$ignore_directory"* ]]; then
                echo "ignoring file: ${file}"
                found_files="${found_files//${file}/}"
                break 1
              fi
            done
          done

          # remove empty lines
          found_files=$(echo "$found_files" | sed '/^\s*$/d')

          echo "found cpp files: ${found_files}"

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

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

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

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

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

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

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

      - name: Find cmake files
        id: find_files
        run: |
          # find files
          found_files=$(find . -type f -iname "CMakeLists.txt" -o -iname "*.cmake")
          ignore_files=$(find . -type f -iname ".cmake-lint-ignore")

          # Loop through each C++ file
          for file in $found_files; do
            for ignore_file in $ignore_files; do
              ignore_directory=$(dirname "$ignore_file")
              # if directory of ignore_file is beginning of file
              if [[ "$file" == "$ignore_directory"* ]]; then
                echo "ignoring file: ${file}"
                found_files="${found_files//${file}/}"
                break 1
              fi
            done
          done

          # remove empty lines
          found_files=$(echo "$found_files" | sed '/^\s*$/d')

          echo "found cmake files: ${found_files}"

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

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