From 26a444fd8f394d9c497cddea7dc365fd3464901f Mon Sep 17 00:00:00 2001 From: Fabian Sauter Date: Sun, 22 Sep 2024 08:38:24 +0200 Subject: [PATCH] Added scripts to check clang-format --- .github/workflows/clang-format.yml | 9 ++---- scripts/check_clang_format.sh | 44 ++++++++++++++++++++++++++++++ scripts/delete_build_dir.sh | 9 ++++++ scripts/run_clang_format.sh | 30 ++++++++++++++++++++ 4 files changed, 85 insertions(+), 7 deletions(-) create mode 100755 scripts/check_clang_format.sh create mode 100755 scripts/delete_build_dir.sh create mode 100755 scripts/run_clang_format.sh diff --git a/.github/workflows/clang-format.yml b/.github/workflows/clang-format.yml index cbbe1afc8..47bbdcec2 100644 --- a/.github/workflows/clang-format.yml +++ b/.github/workflows/clang-format.yml @@ -9,14 +9,9 @@ jobs: steps: - name: Update package list run: sudo dnf update -y - - name: Install dependencies - run: sudo dnf install -y openssl-devel cmake git gcc clang ninja-build - - name: Install clang-tidy + - name: Install clang-format run: sudo dnf install -y clang-tools-extra - name: Checkout uses: actions/checkout@v3 - name: Check format - uses: RafikFarhad/clang-format-github-action@3.2.0 - with: - sources: "include/**/*.hpp,include/**/*.cpp,cpr/**/*.hpp,cpr/**/*.cpp" - style: "file" \ No newline at end of file + run: bash scripts/check_clang_format.sh \ No newline at end of file diff --git a/scripts/check_clang_format.sh b/scripts/check_clang_format.sh new file mode 100755 index 000000000..488c45e79 --- /dev/null +++ b/scripts/check_clang_format.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Based on: https://gist.github.com/leilee/1d0915a583f8f29414cc21cd86e7151b +# Checks if all files are formatted based on the clang-format formatting rules. +# Execute as follows: +# ./scripts/check_clang_format.sh tests src + +printf "📑 Checking if your code fulfills all clang-format rules...\n" + +RET_CODE=0 + +function format() { + for f in $(find $@ -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do + clang-format -i --dry-run --Werror --style=file ${f}; + ret=$? + if [ $ret -ne 0 ]; then + RET_CODE=$ret + fi + done + + echo "~~~ $@ directory checked ~~~"; +} + +# Check all of the arguments first to make sure they're all directories +for dir in "$@"; do + if [ ! -d "${dir}" ]; then + echo "${dir} is not a directory"; + else + format ${dir}; + fi +done + +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' + +if [ $RET_CODE -eq 0 ]; then + printf "✅ ${GREEN}Everything up to standard :party: ${NC}\n" +else + printf "❌ ${RED}Not up to formatting standard :sad_face: ${NC}\n" + echo "Try running run_clang_format.sh to format all files." +fi + +exit $RET_CODE \ No newline at end of file diff --git a/scripts/delete_build_dir.sh b/scripts/delete_build_dir.sh new file mode 100755 index 000000000..52d021568 --- /dev/null +++ b/scripts/delete_build_dir.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +printf "🗑️ Clearing your build directory...\n" +rm -rf build/* + +GREEN='\033[0;32m' +NC='\033[0m' + +printf "✅ ${GREEN}Done. Build directory deleted.${NC}\n" \ No newline at end of file diff --git a/scripts/run_clang_format.sh b/scripts/run_clang_format.sh new file mode 100755 index 000000000..edf116599 --- /dev/null +++ b/scripts/run_clang_format.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +# Based on: https://gist.github.com/leilee/1d0915a583f8f29414cc21cd86e7151b +# Run from the project root directory as follows: +# ./scripts/run_clang_format.sh tests src + +printf "📝 Running clang-format...\n" + +function format() { + for f in $(find $@ -name '*.h' -or -name '*.hpp' -or -name '*.c' -or -name '*.cpp'); do + echo "format ${f}"; + clang-format -i --style=file ${f}; + done + + echo "~~~ $@ directory formatted ~~~"; +} + +# Check all of the arguments first to make sure they're all directories +for dir in "$@"; do + if [ ! -d "${dir}" ]; then + echo "${dir} is not a directory"; + else + format ${dir}; + fi +done + +GREEN='\033[0;32m' +NC='\033[0m' + +printf "✅ ${GREEN}Done. All files were formatted (if required).${NC}\n"