unreal-automations/format_code.sh

54 lines
1.3 KiB
Bash
Raw Normal View History

2025-03-05 07:52:49 +00:00
#!/bin/bash
# Configuration
ROOT_DIR=$(git rev-parse --show-toplevel)
CONFIG_FILE="$ROOT_DIR/.tools/.clang-format"
2025-03-05 07:52:49 +00:00
TARGET_DIRS=("Source" "Private" "Public" "Classes")
FILE_EXTS=("*.h" "*.cpp" "*.hpp" "*.c")
DRY_RUN=false
VERBOSE=false
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--dry-run) DRY_RUN=true; shift ;;
--verbose) VERBOSE=true; shift ;;
--config) CONFIG_FILE="$2"; shift 2 ;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
# Find all source files
FILES=()
for dir in "${TARGET_DIRS[@]}"; do
full_dir="$ROOT_DIR/$dir"
if [ -d "$full_dir" ]; then
2025-03-05 07:52:49 +00:00
while IFS= read -r -d $'\0' file; do
FILES+=("$file")
done < <(find "$full_dir" -type f \( -name "*.h" -o -name "*.cpp" -o -name "*.hpp" -o -name "*.c" \) -print0)
fi
2025-03-05 07:52:49 +00:00
done
# Formatting command
FORMAT_CMD="$ROOT_DIR/.tools/clang-format -style=file:$CONFIG_FILE -i"
2025-03-05 07:52:49 +00:00
$DRY_RUN && FORMAT_CMD+=" --dry-run --Werror"
# Execute formatting
ERRORS=0
for file in "${FILES[@]}"; do
if $VERBOSE; then
echo "Checking $file"
fi
if ! $FORMAT_CMD "$file"; then
((ERRORS++))
fi
done
if [ $ERRORS -gt 0 ]; then
echo "$ERRORS files need formatting!"
exit 1
else
echo "✅ All ${#FILES[@]} files formatted correctly!"
2025-03-05 07:52:49 +00:00
exit 0
fi