From 1534043663f710f6129b3b0f5626a97e27bc0bbe Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Sun, 5 Nov 2023 16:14:10 -0800 Subject: [PATCH] enhancement: CSharp Parser Progress Bars (re #558) --- Commands/Parsers/Parse-CSharp.ps.ps1 | 42 ++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/Commands/Parsers/Parse-CSharp.ps.ps1 b/Commands/Parsers/Parse-CSharp.ps.ps1 index 97684653b..a79d7047c 100644 --- a/Commands/Parsers/Parse-CSharp.ps.ps1 +++ b/Commands/Parsers/Parse-CSharp.ps.ps1 @@ -16,25 +16,49 @@ Parse function CSharp { $Source ) - begin { + begin { if (-not ('Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree' -as [type])) { Add-Type -AssemblyName Microsoft.CodeAnalysis.CSharp - } + } + $accumulate = [Collections.Queue]::new() } process { + $accumulate.Enqueue([Ordered]@{psParameterSet=$psCmdlet.ParameterSetName} + $PSBoundParameters) + } + + end { + + $count = 0 + $total = $accumulate.Count -as [double] + if (-not $script:LastProgressID) { $script:LastProgressID = 1} + $script:LastProgressID++ if (-not ('Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree' -as [type])) { return } - if ($Source -is [string]) { - [Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText($Source) - } - elseif ($Source -is [IO.FileInfo]) { - if ($Source.Extension -in '.cs', '.csx') { - [Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText([IO.File]::ReadAllText($Source), $null, $Source.Fullname) + while ($accumulate.Count) { + $dequeue = $accumulate.Dequeue() + if ($total -gt 1) { + Write-Progress "Parsing PowerShell" " " -Id $script:LastProgressID -PercentComplete $( + $count++ + [Math]::Min($count / $total, 1) * 100 + ) + } + + foreach ($kv in $dequeue.GetEnumerator()) { + $ExecutionContext.SessionState.PSVariable.Set($kv.Key, $kv.Value) + } + + if ($Source -is [string]) { + [Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText($Source) + } + elseif ($Source -is [IO.FileInfo]) { + if ($Source.Extension -in '.cs', '.csx') { + [Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree]::ParseText([IO.File]::ReadAllText($Source), $null, $Source.Fullname) + } + } - } } }