Skip to content

Commit

Permalink
enhancement: CSharp Parser Progress Bars (re #558)
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Nov 6, 2023
1 parent 350fa19 commit 1534043
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions Commands/Parsers/Parse-CSharp.ps.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

}
}
}

0 comments on commit 1534043

Please sign in to comment.