1
+ <#
2
+ . DESCRIPTION
3
+ Helper script to format all header and source files in the repository.
4
+
5
+ By default, the script will recursively search under the repo root for
6
+ files to format. Users can give explicit parameters indicating how the
7
+ search should include and exclude filetypes.
8
+
9
+ If users don't want the search functionality, they can instead provide
10
+ an explicit list of files to format.
11
+
12
+ . PARAMETER RepoRoot
13
+ Full path to the root of the repository which is the target of the search.
14
+ Will default to the root of the current working directory.
15
+
16
+ . PARAMETER Include
17
+ Array of filetype extensions to target for formatting.
18
+ By default, targets standard extensions for header and source files.
19
+ Follows the same rules as the -Include parameter for Get-ChildItem.
20
+
21
+ . PARAMETER Exclude
22
+ Array of filetype extensions to exclude from formatting.
23
+ By default, excludes generated XAML files.
24
+ Follows the same rules as the -Exclude paramter for Get-ChildItem.
25
+
26
+ . PARAMETER Files
27
+ Array of files to format. The script will exit if one of the provided
28
+ filepaths does not exist.
29
+
30
+ . EXAMPLE
31
+ .\clang-format-all.ps1
32
+
33
+ Formats all header and source files under the repository root.
34
+
35
+ . EXAMPLE
36
+ .\clang-format-all.ps1 -RepoRoot 'S:\repos\calculator' -Include '*.h', '*.cpp' -Exclude '*.g.*'
37
+
38
+ Formats all *.h and *.cpp files under 'S:\repos\calculator', excluding files with an extension
39
+ like *.g.*
40
+
41
+ . EXAMPLE
42
+ .\clang-format-all.ps1 -File 'S:\repos\calculator\src\CalcViewModel\UnitConverterViewModel.h', 'S:\repos\calculator\src\CalcViewModel\MemoryItemViewModel.cpp'
43
+
44
+ Formats the specified files.
45
+ #>
46
+ [CmdletBinding ( DefaultParameterSetName = ' Search' )]
47
+ param (
48
+ [Parameter ( ParameterSetName = ' Search' )]
49
+ [ValidateScript ({ Test-Path - PathType Container - Path $_ })]
50
+ [string ] $RepoRoot = " $ ( git rev- parse -- show-toplevel ) " ,
51
+
52
+ [Parameter ( ParameterSetName = ' Search' )]
53
+ [string []] $Include = ( ' *.h' , ' *.hh' , ' *.hpp' , ' *.c' , ' *.cc' , ' *.cpp' ),
54
+
55
+ [Parameter ( ParameterSetName = ' Search' )]
56
+ [string []] $Exclude = ' *.g.*' ,
57
+
58
+ [Parameter (
59
+ ParameterSetName = ' Explicit' ,
60
+ Mandatory )]
61
+ [ValidateScript ({
62
+ $_ | Where-Object { -not (Test-Path - PathType Leaf - Path $_ ) } |
63
+ ForEach-Object { throw " Could not find file: [$_ ]" }
64
+
65
+ return $true
66
+ })]
67
+ [string []] $Files
68
+ )
69
+
70
+ if ($PSCmdlet.ParameterSetName -eq ' Explicit' )
71
+ {
72
+ # Use the file paths we were given.
73
+ $targetFiles = @ ($Files )
74
+ }
75
+ else
76
+ {
77
+ # Gather the files to be formatted.
78
+ $targetFiles = @ (
79
+ Get-ChildItem - Recurse - Path $RepoRoot - Include $Include - Exclude $Exclude |
80
+ Select-Object - ExpandProperty FullName
81
+ )
82
+ }
83
+
84
+ # Format the files.
85
+ $formatParams = @ (
86
+ ' -i' # In-place
87
+ ' -style=file' # Search for a .clang-format file in the parent directory of the source file.
88
+ ' -verbose'
89
+ )
90
+
91
+ clang- format $formatParams $targetFiles
0 commit comments