FormatChecks #2
@@ -62,4 +62,4 @@ jobs:
|
|||||||
submodules: recursive
|
submodules: recursive
|
||||||
|
|
||||||
- name: Clang Format Checks
|
- name: Clang Format Checks
|
||||||
run: chmod +x format.sh && ./format.sh Bigfoot checker
|
run: ./format.sh --check Bigfoot
|
||||||
86
format.bat
86
format.bat
@@ -1,30 +1,74 @@
|
|||||||
@echo off
|
@echo off
|
||||||
REM Variable that will hold the name of the clang-format command
|
SETLOCAL ENABLEDELAYEDEXPANSION
|
||||||
|
|
||||||
|
REM =========================
|
||||||
|
REM Variables
|
||||||
|
REM =========================
|
||||||
SET FMT=clang-format
|
SET FMT=clang-format
|
||||||
|
SET MODE=
|
||||||
|
SET EXIT_CODE=0
|
||||||
|
|
||||||
REM Function to format files
|
REM =========================
|
||||||
:format
|
REM Parse arguments
|
||||||
for /r %%f in (*.h *.hpp *.m *.mm *.c *.cpp) do (
|
REM =========================
|
||||||
echo %%~nxf | findstr /i "_generated$" >nul
|
:parse_args
|
||||||
if errorlevel 1 (
|
IF "%~1"=="" GOTO end_parse_args
|
||||||
echo format %%f
|
IF "%~1"=="--check" (
|
||||||
%FMT% -i "%%f"
|
SET MODE=check
|
||||||
|
) ELSE IF "%~1"=="--fix" (
|
||||||
|
SET MODE=fix
|
||||||
|
) ELSE (
|
||||||
|
SET DIRS=!DIRS! "%~1"
|
||||||
)
|
)
|
||||||
)
|
SHIFT
|
||||||
echo ~~~ %1 Done ~~~
|
GOTO parse_args
|
||||||
exit /b
|
:end_parse_args
|
||||||
|
|
||||||
REM Check if argument is provided
|
IF "%MODE%"=="" (
|
||||||
if "%1"=="" (
|
ECHO Usage: %~nx0 --check|--fix <directory> [<directory> ...]
|
||||||
echo Please provide a directory as an argument.
|
EXIT /B 1
|
||||||
exit /b
|
|
||||||
)
|
)
|
||||||
|
|
||||||
REM Check if directory exists
|
IF "%DIRS%"=="" (
|
||||||
if not exist "%1" (
|
ECHO Please provide at least one directory.
|
||||||
echo %1 is not a valid directory.
|
EXIT /B 1
|
||||||
exit /b
|
|
||||||
)
|
)
|
||||||
|
|
||||||
cd %1
|
REM =========================
|
||||||
call :format
|
REM Iterate over directories
|
||||||
|
REM =========================
|
||||||
|
FOR %%D IN (%DIRS%) DO (
|
||||||
|
IF NOT EXIST "%%~D" (
|
||||||
|
ECHO %%~D is not a valid directory.
|
||||||
|
SET EXIT_CODE=1
|
||||||
|
GOTO :continue_dirs
|
||||||
|
)
|
||||||
|
|
||||||
|
REM Recursively find source files
|
||||||
|
FOR /R "%%~D" %%F IN (*.h *.hpp *.c *.cpp *.m *.mm) DO (
|
||||||
|
SET FILE=%%F
|
||||||
|
REM Skip *_generated* files
|
||||||
|
ECHO !FILE! | FINDSTR /I "_generated" >nul
|
||||||
|
IF ERRORLEVEL 1 (
|
||||||
|
IF "%MODE%"=="fix" (
|
||||||
|
ECHO Formatting !FILE!
|
||||||
|
%FMT% -i "!FILE!"
|
||||||
|
) ELSE (
|
||||||
|
REM Check mode: run clang-format --dry-run --Werror
|
||||||
|
%FMT% --dry-run --Werror "!FILE!" 2>nul
|
||||||
|
IF ERRORLEVEL 1 (
|
||||||
|
REM clang-format will already print diagnostic
|
||||||
|
ECHO --- Diff for !FILE! ---
|
||||||
|
REM Generate diff using fc
|
||||||
|
%FMT% "!FILE!" > "%TEMP%\fmt.tmp"
|
||||||
|
fc "!FILE!" "%TEMP%\fmt.tmp"
|
||||||
|
SET EXIT_CODE=1
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
:continue_dirs
|
||||||
|
)
|
||||||
|
|
||||||
|
EXIT /B %EXIT_CODE%
|
||||||
|
|||||||
78
format.sh
78
format.sh
@@ -1,31 +1,63 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
# Variable that will hold the name of the clang-format command
|
|
||||||
FMT="clang-format"
|
FMT="clang-format"
|
||||||
|
MODE=""
|
||||||
|
DIRS=()
|
||||||
|
|
||||||
# Function to format files
|
# Parse arguments
|
||||||
format() {
|
for arg in "$@"; do
|
||||||
for f in $(find "$1" \( -name '*.h' -or -name '*.hpp' -or -name '*.m' -or -name '*.mm' -or -name '*.c' -or -name '*.cpp' \)); do
|
case "$arg" in
|
||||||
# Skip *_generated.h files
|
--check)
|
||||||
if [[ "$f" == *_generated* ]]; then
|
MODE="check"
|
||||||
|
;;
|
||||||
|
--fix)
|
||||||
|
MODE="fix"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
DIRS+=("$arg")
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [[ -z "$MODE" ]]; then
|
||||||
|
echo "Usage: $0 --check|--fix <directory> [<directory> ...]"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ ${#DIRS[@]} -eq 0 ]]; then
|
||||||
|
echo "Please provide at least one directory."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
EXIT_CODE=0
|
||||||
|
|
||||||
|
for DIR in "${DIRS[@]}"; do
|
||||||
|
if [[ ! -d "$DIR" ]]; then
|
||||||
|
echo "$DIR is not a valid directory."
|
||||||
|
EXIT_CODE=1
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
echo "format ${f}"
|
|
||||||
${FMT} -i "${f}"
|
while IFS= read -r -d '' file; do
|
||||||
|
[[ "$file" == *_generated* ]] && continue
|
||||||
|
|
||||||
|
if [[ "$MODE" == "fix" ]]; then
|
||||||
|
echo "Formatting $file"
|
||||||
|
$FMT -i "$file"
|
||||||
|
else
|
||||||
|
# Check mode: run clang-format with dry-run and Werror
|
||||||
|
# Capture stdout/stderr for diagnostic
|
||||||
|
if ! $FMT --dry-run --Werror "$file"; then
|
||||||
|
# clang-format will already print:
|
||||||
|
# Bigfoot/Tests/System/Time.cpp:21:46: error: code should be clang-formatted [-Wclang-format-violations]
|
||||||
|
# Now additionally print the diff for clarity
|
||||||
|
diff_output=$(diff -u "$file" <($FMT -style=file "$file") || true)
|
||||||
|
echo "$diff_output"
|
||||||
|
EXIT_CODE=1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done < <(find "$DIR" \( -name '*.h' -o -name '*.hpp' -o -name '*.c' -o -name '*.cpp' -o -name '*.m' -o -name '*.mm' \) -print0)
|
||||||
done
|
done
|
||||||
echo "~~~ $1 Done ~~~"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Check if argument is provided
|
exit $EXIT_CODE
|
||||||
if [ -z "$1" ]; then
|
|
||||||
echo "Please provide a directory as an argument."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Check if directory exists
|
|
||||||
if [ ! -d "$1" ]; then
|
|
||||||
echo "$1 is not a valid directory."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
format "$1"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user