From ba998dda99dd1aaa4a26c2ed545d6c9a2ffcc197 Mon Sep 17 00:00:00 2001 From: Lucas Peter Date: Wed, 5 Mar 2025 12:09:19 +0100 Subject: [PATCH] Make the script always based on git root directory --- format_code.sh | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/format_code.sh b/format_code.sh index dc7a5fc..a2605ce 100644 --- a/format_code.sh +++ b/format_code.sh @@ -1,7 +1,8 @@ #!/bin/bash # Configuration -CONFIG_FILE="$(dirname "$0")/.clang-format" +ROOT_DIR=$(git rev-parse --show-toplevel) +CONFIG_FILE="$ROOT_DIR/.tools/.clang-format" TARGET_DIRS=("Source" "Private" "Public" "Classes") FILE_EXTS=("*.h" "*.cpp" "*.hpp" "*.c") DRY_RUN=false @@ -20,16 +21,17 @@ done # Find all source files FILES=() -for dir in "../${TARGET_DIRS[@]}"; do - for ext in "${FILE_EXTS[@]}"; do +for dir in "${TARGET_DIRS[@]}"; do + full_dir="$ROOT_DIR/$dir" + if [ -d "$full_dir" ]; then while IFS= read -r -d $'\0' file; do FILES+=("$file") - done < <(find "$(pwd)/$dir" -name "$ext" -print0) - done + done < <(find "$full_dir" -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.c" \) -print0) + fi done # Formatting command -FORMAT_CMD="./clang-format -style=file:$CONFIG_FILE -i" +FORMAT_CMD="$ROOT_DIR/.tools/clang-format -style=file:$CONFIG_FILE -i" $DRY_RUN && FORMAT_CMD+=" --dry-run --Werror" # Execute formatting @@ -47,6 +49,6 @@ if [ $ERRORS -gt 0 ]; then echo "❌ $ERRORS files need formatting!" exit 1 else - echo "✅ All files formatted correctly!" + echo "✅ All ${#FILES[@]} files formatted correctly!" exit 0 fi \ No newline at end of file