Skip to content

Commit

Permalink
Fix up format script to work better when there are obj directories fl…
Browse files Browse the repository at this point in the history
…oating around. Re-run formatting from scratch
  • Loading branch information
dmachaj committed Dec 9, 2024
1 parent 0fb6876 commit 7abe7b3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 26 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ jobs:
if %ERRORLEVEL% neq 0 (
echo ::error::This branch contains changes that have not been formatted with 'clang-format',
echo NOTE: To resolve this issue, you can run 'clang-format' in the following ways:
echo * Run build_test_all.cmd which will run 'clang-format' on _all_ source files. This script is
echo simpler to run, however there's a chance it may touch additional files you never changed due to you having
echo * Run build_test_all.cmd which will run 'git clang-format' on all source files that have been modified
echo in your branch.
echo * Run 'format_all_files.cmd' which will format _all_ source files. This script is
echo simple to run, however there's a chance it may touch additional files you never changed due to you having
echo a mis-matched version of 'clang-format'. This may require you to manually revert changes made by
echo 'clang-format' to the locations where you made no code changes.
echo.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ Before submitting a PR to the cppwinrt repo we ask that you first run `clang-for
There is a CI check in place that will fail the build for your PR if you have not run `clang-format`.
`clang-format` will run automatically as part of `build_test_all.cmd`, so if you use that script this
should happen automatically.

If for some reason you would like to force formatting of every file in the repo then you can run `format_all_files.cmd` to
do so. This will take longer than `git clang-format` that will only format files that you have modified.
26 changes: 2 additions & 24 deletions build_test_all.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,8 @@ if "%target_version%"=="" set target_version=1.2.3.4
call "%~dp0/find_clang_format.cmd"
if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL%

set DIRS=cppwinrt fast_fwd natvis prebuild scratch strings test vsix
set EXTS=.cpp .h
for %%d in (%DIRS%) do call :format_files %~dp0%%d
goto :post_format

:format_files
for %%e in (%EXTS%) do (
for /R %1 %%f in (*%%e) do call :run_clang_format "%%f"
)
goto :eof

:run_clang_format
set filePath=%1
:: The test subfolder has obj directories with many redundant copies of generated cppwinrt headers. The
:: cost of formatting these files is vastly higher than the cost of formatting the code that is checked in
:: to this repo. Skip any file path with "obj" as a substring.
if not !filePath:obj!==!filePath! (
goto :eof
)
echo Formatting !filePath!
"%CLANG_FORMAT%" -style=file -i %1
goto :eof

:post_format
echo Running clang-format on all modified files...
git clang-format origin/master --binary "%CLANG_FORMAT%" --style file -- cppwinrt/*.h cppwinrt/*.cpp fast_fwd/*.h fast_fwd/*.cpp natvis/*.h natvis/*.cpp prebuild/*.h prebuild/*.cpp scratch/*.h scratch/*.cpp strings/*.h strings/*.cpp test/*.h test/*.cpp vsix/*.h vsix/*.cpp

:: NuGet restore all solutions before building
if not exist ".\.nuget" mkdir ".\.nuget"
Expand Down
39 changes: 39 additions & 0 deletions format_all_files.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@echo off
setlocal
setlocal EnableDelayedExpansion

:: This script is a more invasive version of formatting all files in the repo with clang-format. It will format files that have not
:: been modified locally. It is mainly intended to be a "reformat everything" script for when .clang-format is modified.

call "%~dp0/find_clang_format.cmd"
if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL%

if exist ".\test\nuget\obj" (
echo Warning: test\nuget\obj subfolder exists and will have large numbers of header files. It is recommended to delete that folder before running this script.
pause
)

set DIRS=cppwinrt fast_fwd natvis prebuild scratch strings test vsix
set EXTS=.cpp .h
for %%d in (%DIRS%) do call :format_files %~dp0%%d

:format_files
for %%e in (%EXTS%) do (
for /R %1 %%f in (*%%e) do call :run_clang_format "%%f"
)
goto :eof

:run_clang_format
set filePath=%1
:: The test subfolder has obj directories with many redundant copies of generated cppwinrt headers. The
:: cost of formatting these files is vastly higher than the cost of formatting the code that is checked in
:: to this repo. Skip any file path with "obj" as a substring.
set IGNORED=
for /F "tokens=*" %%g in ('git check-ignore %1') do (set IGNORED=%%g)
if not [!IGNORED!]==[] (
echo %1 is ignored by git, skipping
goto :eof
)
echo Formatting !filePath!
"%CLANG_FORMAT%" -style=file -i %1
goto :eof

0 comments on commit 7abe7b3

Please sign in to comment.