Skip to content

Commit 1e8e4fe

Browse files
Merge branch 'formatfix' into b4
2 parents 8e40775 + 21da25c commit 1e8e4fe

File tree

1 file changed

+84
-5
lines changed

1 file changed

+84
-5
lines changed

public/Invoke-DbatoolsFormatter.ps1

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ function Invoke-DbatoolsFormatter {
2929
PS C:\> Invoke-DbatoolsFormatter -Path C:\dbatools\public\Get-DbaDatabase.ps1
3030
3131
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
3237
#>
3338
[CmdletBinding()]
3439
param (
@@ -61,17 +66,47 @@ function Invoke-DbatoolsFormatter {
6166
if ($psVersionTable.Platform -ne 'Unix') {
6267
$OSEOL = "`r`n"
6368
}
69+
70+
# Collect all paths for progress tracking
71+
$allPaths = @()
6472
}
6573
process {
6674
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+
6889
try {
6990
$realPath = (Resolve-Path -Path $p -ErrorAction Stop).Path
7091
} catch {
92+
Write-Progress -Activity "Formatting PowerShell files" -Status "Error resolving path: $p" -PercentComplete (($currentFile / $totalFiles) * 100) -CurrentOperation "File $currentFile of $totalFiles"
7193
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
72102
}
73103

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+
75110
if ($OSEOL -eq "`r`n") {
76111
# See #5830, we are in Windows territory here
77112
# Is the file containing at least one `r ?
@@ -84,11 +119,32 @@ function Invoke-DbatoolsFormatter {
84119

85120
#strip ending empty lines
86121
$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+
87134
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+
}
89139
} catch {
90-
Write-Message -Level Warning "Unable to format $p"
140+
# Just silently continue - the formatting might still work partially
91141
}
142+
143+
# Restore the aligned patterns
144+
foreach ($key in $placeholders.Keys) {
145+
$content = $content.Replace($key, $placeholders[$key])
146+
}
147+
92148
#match the ending indentation of CBH with the starting one, see #4373
93149
$CBH = $CBHRex.Match($content).Value
94150
if ($CBH) {
@@ -118,7 +174,30 @@ function Invoke-DbatoolsFormatter {
118174
#trim whitespace lines
119175
$realContent += $line.Replace("`t", " ").TrimEnd()
120176
}
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++
122193
}
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"
123202
}
124203
}

0 commit comments

Comments
 (0)