@@ -68,7 +68,7 @@ function Invoke-DbatoolsFormatter {
68
68
' PSUseConsistentIndentation' ,
69
69
' PSUseConsistentWhitespace'
70
70
)
71
- Rules = @ {
71
+ Rules = @ {
72
72
PSPlaceOpenBrace = @ {
73
73
Enable = $true
74
74
OnSameLine = $true
@@ -150,56 +150,85 @@ function Invoke-DbatoolsFormatter {
150
150
}
151
151
}
152
152
153
- # strip ending empty lines
153
+ # Strip ending empty lines from both original and working content
154
154
$content = $content -replace " (?s)$OSEOL \s*$"
155
+ $originalStripped = $originalContent -replace " (?s)$OSEOL \s*$"
156
+
155
157
try {
156
- # Use custom settings instead of CodeFormattingOTBS
158
+ # Format the content
157
159
$content = Invoke-Formatter - ScriptDefinition $content - Settings $customSettings - ErrorAction Stop
160
+ # Also format the original to compare
161
+ $originalFormatted = Invoke-Formatter - ScriptDefinition $originalStripped - Settings $customSettings - ErrorAction Stop
158
162
} catch {
159
163
Write-Message - Level Warning " Unable to format $realPath : $ ( $_.Exception.Message ) "
160
164
continue
161
165
}
162
166
163
- # Ensure $content is a string before processing
167
+ # Ensure both contents are strings before processing
164
168
if (-not $content -or $content -isnot [string ]) {
165
169
Write-Message - Level Warning " Formatter returned unexpected content type for $realPath "
166
170
continue
167
171
}
168
172
169
- # match the ending indentation of CBH with the starting one, see #4373
173
+ if (-not $originalFormatted -or $originalFormatted -isnot [string ]) {
174
+ Write-Message - Level Warning " Formatter returned unexpected content type for original in $realPath "
175
+ continue
176
+ }
177
+
178
+ # Apply CBH fix to formatted content
170
179
$CBH = $CBHRex.Match ($content ).Value
171
180
if ($CBH ) {
172
- # get starting spaces
173
181
$startSpaces = $CBHStartRex.Match ($CBH ).Groups[' spaces' ]
174
182
if ($startSpaces ) {
175
- # get end
176
183
$newCBH = $CBHEndRex.Replace ($CBH , " $startSpaces #>" )
177
184
if ($newCBH ) {
178
- # replace the CBH
179
185
$content = $content.Replace ($CBH , $newCBH )
180
186
}
181
187
}
182
188
}
189
+
190
+ # Apply CBH fix to original formatted content
191
+ $originalCBH = $CBHRex.Match ($originalFormatted ).Value
192
+ if ($originalCBH ) {
193
+ $startSpaces = $CBHStartRex.Match ($originalCBH ).Groups[' spaces' ]
194
+ if ($startSpaces ) {
195
+ $newOriginalCBH = $CBHEndRex.Replace ($originalCBH , " $startSpaces #>" )
196
+ if ($newOriginalCBH ) {
197
+ $originalFormatted = $originalFormatted.Replace ($originalCBH , $newOriginalCBH )
198
+ }
199
+ }
200
+ }
201
+
183
202
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
184
203
$correctCase = @ (
185
204
' DbaInstanceParameter'
186
205
' PSCredential'
187
206
' PSCustomObject'
188
207
' PSItem'
189
208
)
209
+
210
+ # Process the formatted content
190
211
$realContent = @ ()
191
212
foreach ($line in $content.Split (" `n " )) {
192
213
foreach ($item in $correctCase ) {
193
214
$line = $line -replace $item , $item
194
215
}
195
- # trim whitespace lines
196
216
$realContent += $line.Replace (" `t " , " " ).TrimEnd()
197
217
}
198
-
199
218
$finalContent = $realContent -Join " $OSEOL "
200
219
220
+ # Process the original formatted content the same way
221
+ $originalProcessed = @ ()
222
+ foreach ($line in $originalFormatted.Split (" `n " )) {
223
+ foreach ($item in $correctCase ) {
224
+ $line = $line -replace $item , $item
225
+ }
226
+ $originalProcessed += $line.Replace (" `t " , " " ).TrimEnd()
227
+ }
228
+ $originalFinalContent = $originalProcessed -Join " $OSEOL "
229
+
201
230
# Only write the file if there are actual changes
202
- if ($finalContent -ne $originalContent ) {
231
+ if ($finalContent -ne $originalFinalContent ) {
203
232
try {
204
233
Write-Message - Level Verbose " Formatting changes detected in $realPath "
205
234
[System.IO.File ]::WriteAllText($realPath , $finalContent , $Utf8NoBomEncoding )
0 commit comments