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
StartAutomating authored and StartAutomating committed Nov 6, 2023
1 parent 1534043 commit be61d1e
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions Commands/Parsers/Parse-CSharp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,43 @@ function Parse.CSharp {
[PSObject]
$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)
}

}

}
}
}
Expand Down

0 comments on commit be61d1e

Please sign in to comment.