@@ -29,6 +29,11 @@ function Invoke-DbatoolsFormatter {
29
29
PS C:\> Invoke-DbatoolsFormatter -Path C:\dbatools\public\Get-DbaDatabase.ps1
30
30
31
31
Reformats C:\dbatools\public\Get-DbaDatabase.ps1 to dbatools' standards
32
+
33
+ . EXAMPLE
34
+ PS C:\> Get-ChildItem *.ps1 | Invoke-DbatoolsFormatter
35
+
36
+ Reformats all .ps1 files in the current directory, showing progress for the batch operation
32
37
#>
33
38
[CmdletBinding ()]
34
39
param (
@@ -61,17 +66,47 @@ function Invoke-DbatoolsFormatter {
61
66
if ($psVersionTable.Platform -ne ' Unix' ) {
62
67
$OSEOL = " `r`n "
63
68
}
69
+
70
+ # Collect all paths for progress tracking
71
+ $allPaths = @ ()
64
72
}
65
73
process {
66
74
if (Test-FunctionInterrupt ) { return }
67
- foreach ($p in $Path ) {
75
+ # Collect all paths from pipeline
76
+ $allPaths += $Path
77
+ }
78
+ end {
79
+ if (Test-FunctionInterrupt ) { return }
80
+
81
+ $totalFiles = $allPaths.Count
82
+ $currentFile = 0
83
+ $processedFiles = 0
84
+ $updatedFiles = 0
85
+
86
+ foreach ($p in $allPaths ) {
87
+ $currentFile ++
88
+
68
89
try {
69
90
$realPath = (Resolve-Path - Path $p - ErrorAction Stop).Path
70
91
} catch {
92
+ Write-Progress - Activity " Formatting PowerShell files" - Status " Error resolving path: $p " - PercentComplete (($currentFile / $totalFiles ) * 100 ) - CurrentOperation " File $currentFile of $totalFiles "
71
93
Stop-Function - Message " Cannot find or resolve $p " - Continue
94
+ continue
95
+ }
96
+
97
+ # Skip directories
98
+ if (Test-Path - Path $realPath - PathType Container) {
99
+ Write-Progress - Activity " Formatting PowerShell files" - Status " Skipping directory: $realPath " - PercentComplete (($currentFile / $totalFiles ) * 100 ) - CurrentOperation " File $currentFile of $totalFiles "
100
+ Write-Message - Level Verbose " Skipping directory: $realPath "
101
+ continue
72
102
}
73
103
74
- $content = Get-Content - Path $realPath - Raw - Encoding UTF8
104
+ $fileName = Split-Path - Leaf $realPath
105
+ Write-Progress - Activity " Formatting PowerShell files" - Status " Processing: $fileName " - PercentComplete (($currentFile / $totalFiles ) * 100 ) - CurrentOperation " File $currentFile of $totalFiles "
106
+
107
+ $originalContent = Get-Content - Path $realPath - Raw - Encoding UTF8
108
+ $content = $originalContent
109
+
75
110
if ($OSEOL -eq " `r`n " ) {
76
111
# See #5830, we are in Windows territory here
77
112
# Is the file containing at least one `r ?
@@ -84,11 +119,32 @@ function Invoke-DbatoolsFormatter {
84
119
85
120
# strip ending empty lines
86
121
$content = $content -replace " (?s)$OSEOL \s*$"
122
+
123
+ # Preserve aligned assignments before formatting
124
+ # Look for patterns with multiple spaces before OR after the = sign
125
+ $alignedPatterns = [regex ]::Matches($content , ' (?m)^\s*(\$\w+|\w+)\s{2,}=\s*.+$|^\s*(\$\w+|\w+)\s*=\s{2,}.+$' )
126
+ $placeholders = @ {}
127
+
128
+ foreach ($match in $alignedPatterns ) {
129
+ $placeholder = " ___ALIGNMENT_PLACEHOLDER_$ ( $placeholders.Count ) ___"
130
+ $placeholders [$placeholder ] = $match.Value
131
+ $content = $content.Replace ($match.Value , $placeholder )
132
+ }
133
+
87
134
try {
88
- $content = Invoke-Formatter - ScriptDefinition $content - Settings CodeFormattingOTBS - ErrorAction Stop
135
+ $formattedContent = Invoke-Formatter - ScriptDefinition $content - Settings CodeFormattingOTBS - ErrorAction Stop
136
+ if ($formattedContent ) {
137
+ $content = $formattedContent
138
+ }
89
139
} catch {
90
- Write-Message - Level Warning " Unable to format $p "
140
+ # Just silently continue - the formatting might still work partially
91
141
}
142
+
143
+ # Restore the aligned patterns
144
+ foreach ($key in $placeholders.Keys ) {
145
+ $content = $content.Replace ($key , $placeholders [$key ])
146
+ }
147
+
92
148
# match the ending indentation of CBH with the starting one, see #4373
93
149
$CBH = $CBHRex.Match ($content ).Value
94
150
if ($CBH ) {
@@ -118,7 +174,30 @@ function Invoke-DbatoolsFormatter {
118
174
# trim whitespace lines
119
175
$realContent += $line.Replace (" `t " , " " ).TrimEnd()
120
176
}
121
- [System.IO.File ]::WriteAllText($realPath , ($realContent -Join " $OSEOL " ), $Utf8NoBomEncoding )
177
+
178
+ $newContent = $realContent -Join " $OSEOL "
179
+
180
+ # Compare without empty lines to detect real changes
181
+ $originalNonEmpty = ($originalContent -split " [\r\n]+" | Where-Object { $_.Trim () }) -join " "
182
+ $newNonEmpty = ($newContent -split " [\r\n]+" | Where-Object { $_.Trim () }) -join " "
183
+
184
+ if ($originalNonEmpty -ne $newNonEmpty ) {
185
+ [System.IO.File ]::WriteAllText($realPath , $newContent , $Utf8NoBomEncoding )
186
+ Write-Message - Level Verbose " Updated: $realPath "
187
+ $updatedFiles ++
188
+ } else {
189
+ Write-Message - Level Verbose " No changes needed: $realPath "
190
+ }
191
+
192
+ $processedFiles ++
122
193
}
194
+
195
+ # Complete the progress bar
196
+ Write-Progress - Activity " Formatting PowerShell files" - Status " Complete" - PercentComplete 100 - CurrentOperation " Processed $processedFiles files, updated $updatedFiles "
197
+ Start-Sleep - Milliseconds 500 # Brief pause to show completion
198
+ Write-Progress - Activity " Formatting PowerShell files" - Completed
199
+
200
+ # Summary message
201
+ Write-Message - Level Verbose " Formatting complete: Processed $processedFiles files, updated $updatedFiles files"
123
202
}
124
203
}
0 commit comments