Skip to content

Commit 21da25c

Browse files
Add progress reporting to Invoke-DbatoolsFormatter
Enhanced Invoke-DbatoolsFormatter to display progress when formatting multiple files, including status updates for each file, error handling, and a summary of processed and updated files. This improves user feedback during batch operations.
1 parent 05eb897 commit 21da25c

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

public/Invoke-DbatoolsFormatter.ps1

Lines changed: 39 additions & 1 deletion
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,22 +66,44 @@ 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
7295
}
7396

7497
# Skip directories
7598
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"
76100
Write-Message -Level Verbose "Skipping directory: $realPath"
77101
continue
78102
}
79103

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+
80107
$originalContent = Get-Content -Path $realPath -Raw -Encoding UTF8
81108
$content = $originalContent
82109

@@ -157,9 +184,20 @@ function Invoke-DbatoolsFormatter {
157184
if ($originalNonEmpty -ne $newNonEmpty) {
158185
[System.IO.File]::WriteAllText($realPath, $newContent, $Utf8NoBomEncoding)
159186
Write-Message -Level Verbose "Updated: $realPath"
187+
$updatedFiles++
160188
} else {
161189
Write-Message -Level Verbose "No changes needed: $realPath"
162190
}
191+
192+
$processedFiles++
163193
}
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"
164202
}
165203
}

0 commit comments

Comments
 (0)