From 39c0db3d7bb90bca17ab3037cb0656f04d2a5575 Mon Sep 17 00:00:00 2001 From: duckdoom5 Date: Thu, 13 Feb 2025 13:21:22 +0100 Subject: [PATCH] Convert to shell script --- .github/workflows/clang-format-check.yml | 2 +- RunClangFormat.bat | 122 ----------------------- RunClangFormat.sh | 97 ++++++++++++++++++ 3 files changed, 98 insertions(+), 123 deletions(-) delete mode 100644 RunClangFormat.bat create mode 100644 RunClangFormat.sh diff --git a/.github/workflows/clang-format-check.yml b/.github/workflows/clang-format-check.yml index fcb8b8a4..70c1e37b 100644 --- a/.github/workflows/clang-format-check.yml +++ b/.github/workflows/clang-format-check.yml @@ -26,4 +26,4 @@ jobs: with: clang-format-version: 19 check-path: ${{ matrix.path }} - exclude-regex: '(?:(?:Bindings|CLI)\\)|(?:CppParser\\(?:Parse)?(?:Expr|Stmt))' + exclude-regex: '(?:(?:Bindings|CLI)\/)|(?:CppParser\/(?:Parse)?(?:Expr|Stmt))' diff --git a/RunClangFormat.bat b/RunClangFormat.bat deleted file mode 100644 index b083dc8b..00000000 --- a/RunClangFormat.bat +++ /dev/null @@ -1,122 +0,0 @@ -@echo off -setlocal enabledelayedexpansion - -echo Looking for clang-format... - -set "FOLDERS=src examples tests" -set "ALLOWED_EXTENSIONS=*.cpp *.hpp *.c *.h *.inl" - -set CLANG_FORMAT_PATH=VC\Tools\Llvm\bin\clang-format.exe -set VSWHERE_PATH="%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" - -set CLANG_FORMAT_OPTIONS=--fallback-style=none -i - -: ================================================================================= -: Check vswhere -if not exist %VSWHERE_PATH% ( - echo Could not locate vshere.exe at: %VSWHERE_PATH% - pause - exit -) - -: Find visual studio installation path -for /f "usebackq tokens=*" %%i in (`call %VSWHERE_PATH% -latest -products * -property installationPath`) do ( - set VS_INSTALL_DIR=%%i -) - -: Find visual studio installation path -if not exist "%VS_INSTALL_DIR%" ( - echo Visual Studio Installation directory not found. Tried looking at: %VS_INSTALL_DIR% - pause - exit -) - -set CLANG_FORMAT_ABSOLUTE_PATH="%VS_INSTALL_DIR%\%CLANG_FORMAT_PATH%" - -: Verify clang-format actually exists as well -if not exist %CLANG_FORMAT_ABSOLUTE_PATH% ( - echo clang-format.exe could not be located at: %CLANG_FORMAT_ABSOLUTE_PATH% - pause - exit -) -: ================================================================================= - -echo Found clang-format.exe at: %CLANG_FORMAT_ABSOLUTE_PATH% -: Display clang-format version -%CLANG_FORMAT_ABSOLUTE_PATH% --version - -set DRY_RUN=0 -: set /p DRY_RUN="Dry run? (1/0): " - -if %DRY_RUN%==1 ( - set CLANG_FORMAT_OPTIONS=%CLANG_FORMAT_OPTIONS% --dry-run --Werror -) - -: Format all files, in the specified list of directories -: Loop through each file in the directory (and subdirectories) -for %%p in (%FOLDERS%) do ( - echo Formatting files in folder "%%p"... - cd %%p - - for /r %%f in (%ALLOWED_EXTENSIONS%) do ( - : Skip certain folders - call :shouldSkipFile %%f - - if not errorlevel 1 ( - %CLANG_FORMAT_ABSOLUTE_PATH% %CLANG_FORMAT_OPTIONS% %%f - ) - :: else ( - :: echo Skipping file %%f... - :: ) - ) - - cd .. -) - -echo Done! -pause -exit - -:shouldSkipFile -: Skip auto generated files -call :findInString %1 Bindings\ -if errorlevel 1 ( - exit /B 1 -) - -call :findInString %1 CppParser\Expr -if errorlevel 1 ( - exit /B 1 -) -call :findInString %1 CppParser\ParseExpr -if errorlevel 1 ( - exit /B 1 -) -call :findInString %1 CppParser\Stmt -if errorlevel 1 ( - exit /B 1 -) -call :findInString %1 CppParser\ParseStmt -if errorlevel 1 ( - exit /B 1 -) - -: Skip CLI files (clang-format doesn't support them properly) -call :findInString %1 CLI\ -if errorlevel 1 ( - exit /B 1 -) - -exit /B 0 - -:findInString -set searchString=%1 -set key=%2 - -call set keyRemoved=%%searchString:%key%=%% - -if not "x%keyRemoved%"=="x%searchString%" ( - exit /B 1 -) else ( - exit /B 0 -) diff --git a/RunClangFormat.sh b/RunClangFormat.sh new file mode 100644 index 00000000..454f37fd --- /dev/null +++ b/RunClangFormat.sh @@ -0,0 +1,97 @@ +#!/usr/bin/env bash +set -e +solution_dir=$(cd "$(dirname "$0")"; pwd) +FOLDERS="src tests" +ALLOWED_EXTENSIONS_RE=".*\.\(cpp\|hpp\|c\|h\|inl\)" +EXCLUSION_RE="\(Bindings\|CLI\)/\|\(CppParser/\(Parse\)?\(Expr\|Stmt\)\)" +CLANG_FORMAT_OPTIONS="--fallback-style=none -i --verbose" + +clang_format="" +VS_INSTALL_DIR="" + + +format() +{ + find_clang_format + + DRY_RUN=0 + # read -p "Dry run? (1/0): " DRY_RUN + + if [ "$DRY_RUN" -eq 1 ]; then + CLANG_FORMAT_OPTIONS="$CLANG_FORMAT_OPTIONS --dry-run --Werror" + fi + + # Format all files, in the specified list of directories + # Loop through each file in the directory (and subdirectories) + for p in $FOLDERS; do + echo "Formatting files in folder \"$p\"..." + cd "$solution_dir/$p" + + file_list=$(find . -type f -regex $ALLOWED_EXTENSIONS_RE -and -not -regex ".*/\($EXCLUSION_RE\).*" -print) + + "$clang_format" $CLANG_FORMAT_OPTIONS $file_list + + cd .. + done +} + +find_vswhere() +{ + echo "Looking for 'vswhere'..." + VSWHERE_PATH="$(printenv 'ProgramFiles(x86)')/Microsoft Visual Studio/Installer/vswhere.exe" + + if [ -x "$(command -v vswhere.exe)" ]; then + vswhere="vswhere.exe" + elif [ -f "$VSWHERE_PATH" ]; then + vswhere=$VSWHERE_PATH + else + echo -e "Could not locate vswhere.exe at:\n " $VSWHERE_PATH + read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key + exit 1 + fi + + echo -e "Found 'vswhere.exe':\n @" $vswhere "\n" +} + +find_visual_studio() +{ + find_vswhere + + echo "Looking for visual studio..." + + # Find visual studio installation path + VS_INSTALL_DIR=$("$vswhere" -latest -property installationPath) + + # Find visual studio installation path + if [ ! -d "$VS_INSTALL_DIR" ]; then + echo -e "Visual Studio Installation directory not found at vswhere specified path:\n " $VS_INSTALL_DIR + read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key + exit 1 + fi + + echo -e "Found Visual Studio:\n @" $VS_INSTALL_DIR "\n" +} + +find_clang_format() +{ + echo "Looking for clang-format..." + find_visual_studio + + CLANG_FORMAT_PATH="VC/Tools/Llvm/bin/clang-format.exe" + clang_format="$VS_INSTALL_DIR/$CLANG_FORMAT_PATH" + + # Verify clang-format actually exists as well + if ! [ -f "$clang_format" ]; then + echo "clang-format.exe could not be located at: $clang_format" + read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key + exit 1 + fi + + echo -e "Found clang-format.exe:\n" " Using" $("$clang_format" --version) "\n" " @" $clang_format "\n" +} + +format + +echo "Done!" +read -n 1 -s -r -p "Press any key to continue, or Ctrl+C to exit" key +exit 0 \ No newline at end of file