unreal-automations/format_code.sh

133 lines
3.5 KiB
Bash
Raw Normal View History

2025-03-05 07:52:49 +00:00
#!/bin/bash
2025-03-05 14:23:04 +00:00
set -eo pipefail
2025-03-05 07:52:49 +00:00
# Configuration
ROOT_DIR=$(git rev-parse --show-toplevel)
2025-03-05 14:23:04 +00:00
CLANG_FORMAT_BIN="${ROOT_DIR}/.tools/clang-format"
CLANG_CONFIG="${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")
2025-03-05 14:23:04 +00:00
# Options
2025-03-05 07:52:49 +00:00
DRY_RUN=false
VERBOSE=false
2025-03-05 14:23:04 +00:00
CHECK_MODIFIED=false
# Help text
show_help() {
cat <<EOF
Usage: $0 [OPTIONS]
Options:
--dry-run Check formatting without modifying files
--verbose Show detailed processing information
--modified-only Check only modified files
--help Show this help message
Exit codes:
0 - Everything formatted correctly
1 - Formatting errors detected
2 - Configuration error
EOF
}
2025-03-05 07:52:49 +00:00
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
2025-03-05 14:23:04 +00:00
--dry-run) DRY_RUN=true; shift ;;
--verbose) VERBOSE=true; shift ;;
--modified-only) CHECK_MODIFIED=true; shift ;;
--help) show_help; exit 0 ;;
*) echo "Unknown option: $1"; exit 2 ;;
2025-03-05 07:52:49 +00:00
esac
done
2025-03-05 14:23:04 +00:00
# Verify dependencies
check_dependencies() {
if [[ ! -x "${CLANG_FORMAT_BIN}" ]]; then
echo "❌ clang-format non trouvé ou non exécutable dans .tools/"
echo " Vérifiez le chemin: ${CLANG_FORMAT_BIN}"
exit 2
fi
if [[ ! -f "${CLANG_CONFIG}" ]]; then
echo "❌ Fichier de configuration .clang-format manquant"
echo " Vérifiez le chemin: ${CLANG_CONFIG}"
exit 2
fi
}
2025-03-05 07:52:49 +00:00
2025-03-05 14:23:04 +00:00
# File discovery
find_source_files() {
local files=()
if [[ "${CHECK_MODIFIED}" == true ]]; then
# Fichiers modifiés uniquement
2025-03-05 17:15:40 +00:00
mapfile -t files < <(git diff --name-only --diff-filter=ACMR HEAD -- $(printf "%s " "${TARGET_DIRS[@]}") | awk 'length($0) && /\.(h|cpp|hpp|c)$/' | grep -v '^$')
2025-03-05 14:23:04 +00:00
else
# Tous les fichiers
for dir in "${TARGET_DIRS[@]}"; do
local full_dir="${ROOT_DIR}/${dir}"
if [[ -d "${full_dir}" ]]; then
while IFS= read -r -d '' file; do
files+=("$file")
done < <(find "${full_dir}" -type f \( \
-name "*.h" -o \
-name "*.cpp" -o \
-name "*.hpp" -o \
-name "*.c" \) -print0)
fi
done
fi
2025-03-05 07:52:49 +00:00
2025-03-05 14:23:04 +00:00
printf "%s\n" "${files[@]}"
}
2025-03-05 07:52:49 +00:00
2025-03-05 14:23:04 +00:00
# Main process
main() {
check_dependencies
local files
mapfile -t files < <(find_source_files)
if [[ ${#files[@]} -eq 0 ]]; then
${VERBOSE} && echo " Aucun fichier trouvé dans ${TARGET_DIRS[*]}"
return
fi
${VERBOSE} && echo "📁 Fichiers à vérifier : ${#files[@]}"
local format_args=("-style=file" "${CLANG_CONFIG}")
local errors=0
if [[ "${DRY_RUN}" == true ]]; then
format_args+=(--dry-run --Werror)
${VERBOSE} && echo "🔍 Mode vérification seulement"
else
format_args+=(-i)
${VERBOSE} && echo "🛠️ Application du formatage"
2025-03-05 07:52:49 +00:00
fi
2025-03-05 14:23:04 +00:00
2025-03-05 17:15:40 +00:00
for file in "${files[@]}"; do
# Si la ligne est vide, passe au suivant
[[ -z "$file" ]] && continue
${VERBOSE} && echo "🔎 Traitement : ${file}"
if ! "${CLANG_FORMAT_BIN}" "${format_args[@]}" "${file}"; then
((errors++))
${DRY_RUN} && echo "❌ Problème détecté : ${file}"
fi
done
2025-03-05 14:23:04 +00:00
if [[ ${errors} -gt 0 ]]; then
echo -e "\n❌ ${errors} fichier(s) nécessitent un formatage"
exit 1
else
echo -e "\n✅ Tous les fichiers sont correctement formatés !"
exit 0
2025-03-05 07:52:49 +00:00
fi
2025-03-05 14:23:04 +00:00
}
2025-03-05 07:52:49 +00:00
2025-03-05 14:23:04 +00:00
# Execution
main