Skip to content

Commit 1f17830

Browse files
Enhance formatter to preserve alignment and avoid unnecessary writes
Updated Invoke-DbatoolsFormatter to use custom PSSA settings that preserve manually aligned hashtables and assignment operators. The script now only writes files if formatting changes are detected, reducing unnecessary file writes.
1 parent ee4e62c commit 1f17830

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

public/Invoke-DbatoolsFormatter.ps1

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ function Invoke-DbatoolsFormatter {
55
66
.DESCRIPTION
77
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.
810
911
.PARAMETER Path
1012
The path to the ps1 file that needs to be formatted
@@ -57,6 +59,48 @@ function Invoke-DbatoolsFormatter {
5759
$CBHRex = [regex]'(?smi)\s+\<\#[^#]*\#\>'
5860
$CBHStartRex = [regex]'(?<spaces>[ ]+)\<\#'
5961
$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+
60104
$OSEOL = "`n"
61105
if ($psVersionTable.Platform -ne 'Unix') {
62106
$OSEOL = "`r`n"
@@ -71,7 +115,9 @@ function Invoke-DbatoolsFormatter {
71115
Stop-Function -Message "Cannot find or resolve $p" -Continue
72116
}
73117

74-
$content = Get-Content -Path $realPath -Raw -Encoding UTF8
118+
$originalContent = Get-Content -Path $realPath -Raw -Encoding UTF8
119+
$content = $originalContent
120+
75121
if ($OSEOL -eq "`r`n") {
76122
# See #5830, we are in Windows territory here
77123
# Is the file containing at least one `r ?
@@ -85,7 +131,8 @@ function Invoke-DbatoolsFormatter {
85131
#strip ending empty lines
86132
$content = $content -replace "(?s)$OSEOL\s*$"
87133
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
89136
} catch {
90137
Write-Message -Level Warning "Unable to format $p"
91138
}
@@ -118,7 +165,16 @@ function Invoke-DbatoolsFormatter {
118165
#trim whitespace lines
119166
$realContent += $line.Replace("`t", " ").TrimEnd()
120167
}
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+
}
122178
}
123179
}
124180
}

0 commit comments

Comments
 (0)