From dab438d07705847218f2f0ce1a9ccbb760044ae9 Mon Sep 17 00:00:00 2001 From: Mateusz Mazur Date: Sun, 10 Nov 2024 13:42:06 +0100 Subject: [PATCH] Clang format in the CI Signed-off-by: Mateusz Mazur --- .clang-format | 4 ++-- .github/workflows/clang-format.yml | 23 +++++++++++++++++++++++ example/main.cpp | 4 +++- reformat-all.sh | 15 +++++++++++++-- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/clang-format.yml diff --git a/.clang-format b/.clang-format index 7295fd4..2552fc3 100644 --- a/.clang-format +++ b/.clang-format @@ -5,7 +5,7 @@ AccessModifierOffset: -2 AlignAfterOpenBracket: Align AlignArrayOfStructures: None AlignConsecutiveAssignments: - Enabled: true + Enabled: true AcrossEmptyLines: false AcrossComments: false AlignCompound: false @@ -26,7 +26,7 @@ AlignConsecutiveDeclarations: AlignFunctionPointers: false PadOperators: false AlignConsecutiveMacros: - Enabled: false + Enabled: false AcrossEmptyLines: false AcrossComments: false AlignCompound: false diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml new file mode 100644 index 0000000..fe9b92e --- /dev/null +++ b/.github/workflows/clang-format.yml @@ -0,0 +1,23 @@ +name: Clang formatting checker + +on: + push: + branches: ["master"] + pull_request: + branches: ["master"] + +jobs: + build: + runs-on: ubuntu-latest + container: + image: silkeh/clang # https://hub.docker.com/r/silkeh/clang + env: + DRY: "TRUE" + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 1 + + - name: Checking formatting + run: DRY=TRUE bash reformat-all.sh diff --git a/example/main.cpp b/example/main.cpp index 4d5a361..ed540ee 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -12,7 +12,9 @@ void createRequest() { // Create simple request - MB::ModbusRequest request(1, MB::utils::ReadDiscreteOutputCoils, 100, 10); + MB::ModbusRequest request + + (1, MB::utils::ReadDiscreteOutputCoils, 100, 10); std::cout << "Stringed Request: " << request.toString() << std::endl; diff --git a/reformat-all.sh b/reformat-all.sh index bb42286..e6c83eb 100644 --- a/reformat-all.sh +++ b/reformat-all.sh @@ -1,3 +1,14 @@ -#!/bin/sh +#!/usr/bin/env bash -find . -name '*.cpp' -o -name '*.hpp' | xargs clang-format -i -style=file +ALL_FORMATABLE_FILES=$(find src include tests example -name '*.cpp' -o -name '*.hpp') +CLANG_FORMAT_ARGS="-style=file" + +echo "*** Modbus Clang Format Wrapper **" +clang-format --version + +if [[ "$DRY" = "TRUE" ]]; then + diff_count=`clang-format --dry-run $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES 2>&1 | wc -l` + exit $(($diff_count > 0)) +else + exec clang-format -i --verbose $CLANG_FORMAT_ARGS $ALL_FORMATABLE_FILES +fi