From e7bf3e2ce2a6423643ad46957cfd42c72110a5ac Mon Sep 17 00:00:00 2001 From: Sergey Kosukhin Date: Tue, 14 Jan 2025 15:20:41 +0100 Subject: [PATCH] Add pre-commit configuration (#338) Run cmake-format and cmake-lint via pre-commit hooks. Replace the custom CI steps with the pre-commit action. --- .github/workflows/style.yml | 57 +++---------------------------------- .pre-commit-config.yaml | 14 +++++++++ environment-dev.yml | 1 - 3 files changed, 18 insertions(+), 54 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index a156e744e..60205dd0f 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -9,55 +9,8 @@ on: - documentation workflow_dispatch: -env: - FIND_CMAKE_FILES_CMD: "find '${{ github.workspace }}' -name 'CMakeLists.txt' -o -name '*.cmake' -o -name '*.cmake.in'" - jobs: - Format: - runs-on: ubuntu-22.04 - env: - DEFAULT: '\033[0m' - RED: '\033[0;31m' - GREEN: '\033[0;32m' - FORMAT_PATCH: '${{ github.workspace }}/format.patch' - steps: - - name: Check out code - uses: actions/checkout@v4 - - name: Install Python - uses: actions/setup-python@v5 - with: - python-version: '>=3.8' - - name: Install required tools - run: python -m pip install cmake-format - - name: Format CMake - run: cmake-format -i $(eval "${FIND_CMAKE_FILES_CMD}") - - name: Check if patching is required - id: patch-required - run: | - git -C '${{ github.workspace }}' diff --patch-with-raw > "${FORMAT_PATCH}" - test -s "${FORMAT_PATCH}" && { - printf "${RED}The source code does not meet the format requirements. \ - Please, apply the patch (see artifacts).${DEFAULT}\n" - - printf "${RED}Note that the result of the formatting might depend \ - on the versions of the formatting tools. In this project, whatever \ - formatting this CI job produces if the correct one. If it expects \ - you to reformat parts of the source code that you did not modify, do \ - so in a separate commit, which must not be squashed, and list the \ - commit in the '.git-blame-ignore-revs' file.${DEFAULT}\n" - - exit 1 - } || { - printf "${GREEN}The source code meets the format requirements.${DEFAULT}\n" - rm -rf "${FORMAT_PATCH}" - } - - name: Upload the patch file - if: always() && steps.patch-required.outcome == 'failure' - uses: actions/upload-artifact@v4 - with: - name: format-patch - path: ${{ env.FORMAT_PATCH }} - Lint: + pre-commit: runs-on: ubuntu-22.04 steps: - name: Check out code @@ -65,8 +18,6 @@ jobs: - name: Install Python uses: actions/setup-python@v5 with: - python-version: '>=3.8' - - name: Install required tools - run: python -m pip install cmake-format - - name: Lint CMake - run: cmake-lint $(eval "${FIND_CMAKE_FILES_CMD}") + python-version: 3.x + - name: Run pre-commit + uses: pre-commit/action@v3.0.1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..a4f49cf5f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: + - repo: https://github.com/cheshirekow/cmake-format-precommit + rev: v0.6.13 + hooks: + - id: cmake-format + - id: cmake-format + name: cmake-format (templates) + types: [file] + files: \.cmake\.in$ + - id: cmake-lint + - id: cmake-lint + name: cmake-lint (templates) + types: [file] + files: \.cmake\.in$ diff --git a/environment-dev.yml b/environment-dev.yml index affe2b0b2..5b46be5b8 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -20,7 +20,6 @@ dependencies: - gfortran - netcdf-fortran - cmake - - cmake-format - ninja variables: FC: gfortran