@@ -13,25 +13,9 @@ param(
1313
1414Set-StrictMode - Version Latest
1515
16- . $PSScriptRoot \Load- Module.ps1
17- . $PSScriptRoot \CodeFormatterChecks\CheckContainsCurlyQuotes.ps1
18- . $PSScriptRoot \CodeFormatterChecks\CheckFileHasNewlineAtEndOfFile.ps1
19- . $PSScriptRoot \CodeFormatterChecks\CheckMarkdownFileHasNoBOM.ps1
20- . $PSScriptRoot \CodeFormatterChecks\CheckMultipleEmptyLines.ps1
21- . $PSScriptRoot \CodeFormatterChecks\CheckScriptFileHasBOM.ps1
22- . $PSScriptRoot \CodeFormatterChecks\CheckScriptFileHasComplianceHeader.ps1
23- . $PSScriptRoot \CodeFormatterChecks\CheckScriptFormat.ps1
24- . $PSScriptRoot \CodeFormatterChecks\CheckTokenTypeCasing.ps1
16+ . $PSScriptRoot \Invoke-CodeFormatterOnFiles.ps1
2517. $PSScriptRoot \HelpFunctions\Get-CommitFilesOnBranch.ps1
2618
27- if (-not (Load- Module - Name PSScriptAnalyzer - MinimumVersion " 1.24" )) {
28- throw " PSScriptAnalyzer module could not be loaded"
29- }
30-
31- if (-not (Load- Module - Name EncodingAnalyzer)) {
32- throw " EncodingAnalyzer module could not be loaded"
33- }
34-
3519$repoRoot = Get-Item " $PSScriptRoot \.."
3620
3721$optimizeCodeFormatter = [string ]::IsNullOrEmpty($Branch ) -eq $false
@@ -61,61 +45,7 @@ if ($optimizeCodeFormatter) {
6145 }
6246}
6347
64- $errorCount = 0
65-
66- foreach ($fileInfo in $filesToCheck ) {
67- $errorCount += (CheckFileHasNewlineAtEndOfFile $fileInfo $Save ) ? 1 : 0
68- $errorCount += (CheckMarkdownFileHasNoBOM $fileInfo $Save ) ? 1 : 0
69- $errorCount += (CheckScriptFileHasBOM $fileInfo $Save ) ? 1 : 0
70- $errorCount += (CheckScriptFileHasComplianceHeader $fileInfo $Save ) ? 1 : 0
71- $errorCount += (CheckTokenTypeCasing $fileInfo $Save " Keyword" ) ? 1 : 0
72- $errorCount += (CheckTokenTypeCasing $fileInfo $Save " Operator" ) ? 1 : 0
73- $errorCount += (CheckMultipleEmptyLines $fileInfo $Save ) ? 1 : 0
74- $errorCount += (CheckContainsCurlyQuotes $fileInfo $Save ) ? 1 : 0
75-
76- # This one is tricky. It returns $true or $false like the others, but in the case
77- # of an error, we also want to get the diff output. Piping to Out-Host from within
78- # the function loses the colorization, as does redirection. I can't find any way
79- # for the function to output the diff while preserving the color. So we unfortunately
80- # have to handle the output here.
81- $results = @ (CheckScriptFormat $fileInfo $Save )
82- if ($results.Length -gt 0 -and $results [0 ] -eq $true ) {
83- $errorCount ++
84- if ($results.Length -gt 2 ) {
85- git - c color.status= always diff ($ ($results [1 ]) | git hash- object - w -- stdin) ($ ($results [2 ]) | git hash- object - w -- stdin)
86- }
87- }
88- }
89-
90- $maxRetries = 5
91-
92- foreach ($fileInfo in $filesToCheck ) {
93- for ($i = 0 ; $i -lt $maxRetries ; $i ++ ) {
94- try {
95- $params = @ {
96- Path = ($fileInfo.FullName )
97- Settings = " $repoRoot \PSScriptAnalyzerSettings.psd1"
98- CustomRulePath = " $repoRoot \.build\CodeFormatterChecks\CustomRules.psm1"
99- IncludeDefaultRules = $true
100- }
101- $analyzerResults = Invoke-ScriptAnalyzer @params
102- if ($null -ne $analyzerResults ) {
103- $errorCount ++
104- $analyzerResults | Format-Table - AutoSize
105- }
106- break
107- } catch {
108- Write-Warning " Invoke-ScriptAnalyzer failed on $ ( $fileInfo.FullName ) . Error:"
109- $_.Exception | Format-List | Out-Host
110- Write-Warning " Retrying in 5 seconds."
111- Start-Sleep - Seconds 5
112- }
113- }
114-
115- if ($i -eq $maxRetries ) {
116- throw " Invoke-ScriptAnalyzer failed $maxRetries times. Giving up."
117- }
118- }
48+ $errorCount = Invoke-CodeFormatterOnFiles - FilePaths ($filesToCheck | ForEach-Object { $_.FullName }) - Save:$Save
11949
12050if ($errorCount -gt 0 ) {
12151 throw " Failed to match formatting requirements"
0 commit comments