Skip to content

Commit be61d1e

Browse files
StartAutomatingStartAutomating
authored andcommitted
enhancement: CSharp Parser Progress Bars (re #558)
1 parent 1534043 commit be61d1e

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

Commands/Parsers/Parse-CSharp.ps1

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,43 @@ function Parse.CSharp {
3030
[PSObject]
3131
$Source
3232
)
33-
begin {
33+
begin {
3434
if (-not ('Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree' -as [type])) {
3535
Add-Type -AssemblyName Microsoft.CodeAnalysis.CSharp
36-
}
36+
}
37+
$accumulate = [Collections.Queue]::new()
3738
}
3839
process {
40+
$accumulate.Enqueue([Ordered]@{psParameterSet=$psCmdlet.ParameterSetName} + $PSBoundParameters)
41+
}
42+
end {
43+
$count = 0
44+
$total = $accumulate.Count -as [double]
45+
if (-not $script:LastProgressID) { $script:LastProgressID = 1}
46+
$script:LastProgressID++
3947
if (-not ('Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree' -as [type])) {
4048
return
4149
}
42-
if ($Source -is [string]) {
43-
[Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText($Source)
44-
}
45-
elseif ($Source -is [IO.FileInfo]) {
46-
if ($Source.Extension -in '.cs', '.csx') {
47-
[Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText([IO.File]::ReadAllText($Source), $null, $Source.Fullname)
50+
while ($accumulate.Count) {
51+
$dequeue = $accumulate.Dequeue()
52+
if ($total -gt 1) {
53+
Write-Progress "Parsing PowerShell" " " -Id $script:LastProgressID -PercentComplete $(
54+
$count++
55+
[Math]::Min($count / $total, 1) * 100
56+
)
57+
}
58+
foreach ($kv in $dequeue.GetEnumerator()) {
59+
$ExecutionContext.SessionState.PSVariable.Set($kv.Key, $kv.Value)
60+
}
61+
if ($Source -is [string]) {
62+
[Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText($Source)
63+
}
64+
elseif ($Source -is [IO.FileInfo]) {
65+
if ($Source.Extension -in '.cs', '.csx') {
66+
[Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText([IO.File]::ReadAllText($Source), $null, $Source.Fullname)
67+
}
68+
4869
}
49-
5070
}
5171
}
5272
}

0 commit comments

Comments
 (0)