Skip to content

Commit 7abe7b3

Browse files
committed
Fix up format script to work better when there are obj directories floating around. Re-run formatting from scratch
1 parent 0fb6876 commit 7abe7b3

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ jobs:
3939
if %ERRORLEVEL% neq 0 (
4040
echo ::error::This branch contains changes that have not been formatted with 'clang-format',
4141
echo NOTE: To resolve this issue, you can run 'clang-format' in the following ways:
42-
echo * Run build_test_all.cmd which will run 'clang-format' on _all_ source files. This script is
43-
echo simpler to run, however there's a chance it may touch additional files you never changed due to you having
42+
echo * Run build_test_all.cmd which will run 'git clang-format' on all source files that have been modified
43+
echo in your branch.
44+
echo * Run 'format_all_files.cmd' which will format _all_ source files. This script is
45+
echo simple to run, however there's a chance it may touch additional files you never changed due to you having
4446
echo a mis-matched version of 'clang-format'. This may require you to manually revert changes made by
4547
echo 'clang-format' to the locations where you made no code changes.
4648
echo.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ Before submitting a PR to the cppwinrt repo we ask that you first run `clang-for
5050
There is a CI check in place that will fail the build for your PR if you have not run `clang-format`.
5151
`clang-format` will run automatically as part of `build_test_all.cmd`, so if you use that script this
5252
should happen automatically.
53+
54+
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
55+
do so. This will take longer than `git clang-format` that will only format files that you have modified.

build_test_all.cmd

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,8 @@ if "%target_version%"=="" set target_version=1.2.3.4
1515
call "%~dp0/find_clang_format.cmd"
1616
if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL%
1717

18-
set DIRS=cppwinrt fast_fwd natvis prebuild scratch strings test vsix
19-
set EXTS=.cpp .h
20-
for %%d in (%DIRS%) do call :format_files %~dp0%%d
21-
goto :post_format
22-
23-
:format_files
24-
for %%e in (%EXTS%) do (
25-
for /R %1 %%f in (*%%e) do call :run_clang_format "%%f"
26-
)
27-
goto :eof
28-
29-
:run_clang_format
30-
set filePath=%1
31-
:: The test subfolder has obj directories with many redundant copies of generated cppwinrt headers. The
32-
:: cost of formatting these files is vastly higher than the cost of formatting the code that is checked in
33-
:: to this repo. Skip any file path with "obj" as a substring.
34-
if not !filePath:obj!==!filePath! (
35-
goto :eof
36-
)
37-
echo Formatting !filePath!
38-
"%CLANG_FORMAT%" -style=file -i %1
39-
goto :eof
40-
41-
:post_format
18+
echo Running clang-format on all modified files...
19+
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
4220

4321
:: NuGet restore all solutions before building
4422
if not exist ".\.nuget" mkdir ".\.nuget"

format_all_files.cmd

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
@echo off
2+
setlocal
3+
setlocal EnableDelayedExpansion
4+
5+
:: This script is a more invasive version of formatting all files in the repo with clang-format. It will format files that have not
6+
:: been modified locally. It is mainly intended to be a "reformat everything" script for when .clang-format is modified.
7+
8+
call "%~dp0/find_clang_format.cmd"
9+
if %ERRORLEVEL% neq 0 exit /b %ERRORLEVEL%
10+
11+
if exist ".\test\nuget\obj" (
12+
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.
13+
pause
14+
)
15+
16+
set DIRS=cppwinrt fast_fwd natvis prebuild scratch strings test vsix
17+
set EXTS=.cpp .h
18+
for %%d in (%DIRS%) do call :format_files %~dp0%%d
19+
20+
:format_files
21+
for %%e in (%EXTS%) do (
22+
for /R %1 %%f in (*%%e) do call :run_clang_format "%%f"
23+
)
24+
goto :eof
25+
26+
:run_clang_format
27+
set filePath=%1
28+
:: The test subfolder has obj directories with many redundant copies of generated cppwinrt headers. The
29+
:: cost of formatting these files is vastly higher than the cost of formatting the code that is checked in
30+
:: to this repo. Skip any file path with "obj" as a substring.
31+
set IGNORED=
32+
for /F "tokens=*" %%g in ('git check-ignore %1') do (set IGNORED=%%g)
33+
if not [!IGNORED!]==[] (
34+
echo %1 is ignored by git, skipping
35+
goto :eof
36+
)
37+
echo Formatting !filePath!
38+
"%CLANG_FORMAT%" -style=file -i %1
39+
goto :eof

0 commit comments

Comments
 (0)