@@ -5,6 +5,8 @@ function Invoke-DbatoolsFormatter {
5
5
6
6
. DESCRIPTION
7
7
Uses PSSA's Invoke-Formatter to format the target files and saves it without the BOM.
8
+ Preserves manually aligned hashtables and assignment operators.
9
+ Only writes files if formatting changes are detected.
8
10
9
11
. PARAMETER Path
10
12
The path to the ps1 file that needs to be formatted
@@ -57,6 +59,48 @@ function Invoke-DbatoolsFormatter {
57
59
$CBHRex = [regex ]' (?smi)\s+\<\#[^#]*\#\>'
58
60
$CBHStartRex = [regex ]' (?<spaces>[ ]+)\<\#'
59
61
$CBHEndRex = [regex ]' (?<spaces>[ ]*)\#\>'
62
+
63
+ # Create custom formatter settings that preserve alignment
64
+ $customSettings = @ {
65
+ IncludeRules = @ (
66
+ ' PSPlaceOpenBrace' ,
67
+ ' PSPlaceCloseBrace' ,
68
+ ' PSUseConsistentIndentation' ,
69
+ ' PSUseConsistentWhitespace'
70
+ )
71
+ Rules = @ {
72
+ PSPlaceOpenBrace = @ {
73
+ Enable = $true
74
+ OnSameLine = $true
75
+ NewLineAfter = $true
76
+ IgnoreOneLineBlock = $true
77
+ }
78
+ PSPlaceCloseBrace = @ {
79
+ Enable = $true
80
+ NewLineAfter = $false
81
+ IgnoreOneLineBlock = $true
82
+ NoEmptyLineBefore = $false
83
+ }
84
+ PSUseConsistentIndentation = @ {
85
+ Enable = $true
86
+ Kind = ' space'
87
+ PipelineIndentation = ' IncreaseIndentationForFirstPipeline'
88
+ IndentationSize = 4
89
+ }
90
+ PSUseConsistentWhitespace = @ {
91
+ Enable = $true
92
+ CheckInnerBrace = $true
93
+ CheckOpenBrace = $true
94
+ CheckOpenParen = $true
95
+ CheckOperator = $false # This is key - don't mess with operator spacing
96
+ CheckPipe = $true
97
+ CheckPipeForRedundantWhitespace = $false
98
+ CheckSeparator = $true
99
+ CheckParameter = $false
100
+ }
101
+ }
102
+ }
103
+
60
104
$OSEOL = " `n "
61
105
if ($psVersionTable.Platform -ne ' Unix' ) {
62
106
$OSEOL = " `r`n "
@@ -71,7 +115,9 @@ function Invoke-DbatoolsFormatter {
71
115
Stop-Function - Message " Cannot find or resolve $p " - Continue
72
116
}
73
117
74
- $content = Get-Content - Path $realPath - Raw - Encoding UTF8
118
+ $originalContent = Get-Content - Path $realPath - Raw - Encoding UTF8
119
+ $content = $originalContent
120
+
75
121
if ($OSEOL -eq " `r`n " ) {
76
122
# See #5830, we are in Windows territory here
77
123
# Is the file containing at least one `r ?
@@ -85,7 +131,8 @@ function Invoke-DbatoolsFormatter {
85
131
# strip ending empty lines
86
132
$content = $content -replace " (?s)$OSEOL \s*$"
87
133
try {
88
- $content = Invoke-Formatter - ScriptDefinition $content - Settings CodeFormattingOTBS - ErrorAction Stop
134
+ # Use custom settings instead of CodeFormattingOTBS
135
+ $content = Invoke-Formatter - ScriptDefinition $content - Settings $customSettings - ErrorAction Stop
89
136
} catch {
90
137
Write-Message - Level Warning " Unable to format $p "
91
138
}
@@ -118,7 +165,16 @@ function Invoke-DbatoolsFormatter {
118
165
# trim whitespace lines
119
166
$realContent += $line.Replace (" `t " , " " ).TrimEnd()
120
167
}
121
- [System.IO.File ]::WriteAllText($realPath , ($realContent -Join " $OSEOL " ), $Utf8NoBomEncoding )
168
+
169
+ $finalContent = $realContent -Join " $OSEOL "
170
+
171
+ # Only write the file if there are actual changes
172
+ if ($finalContent -ne $originalContent ) {
173
+ Write-Message - Level Verbose " Formatting changes detected in $realPath "
174
+ [System.IO.File ]::WriteAllText($realPath , $finalContent , $Utf8NoBomEncoding )
175
+ } else {
176
+ Write-Message - Level Verbose " No formatting changes needed for $realPath "
177
+ }
122
178
}
123
179
}
124
180
}
0 commit comments