Components/PS.Util.AST/Public/Get-PSUtilASTClass.ps1
|
function Get-PSUtilASTClass { [CmdletBinding( DefaultParameterSetName = 'File' )] Param( [Parameter( ParameterSetName = 'File', Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName )] [Alias('FullName')] [String] $FilePath, [Parameter( ParameterSetName = 'Code', Mandatory, ValueFromPipeline )] [string] $Code, [Switch] $NoRecursion ) BEGIN { $predicate = { param($astObject) $astObject -is [System.Management.Automation.Language.TypeDefinitionAst] -and $astObject.IsClass } } PROCESS { $errors = $null $ast = switch ($PSCmdlet.ParameterSetName) { 'File' { [System.Management.Automation.Language.Parser]::ParseFile( $FilePath, [ref]$null, [ref]$errors) } 'Code' { [System.Management.Automation.Language.Parser]::ParseInput($Code, [ref]$null, [ref]$errors) } } if ($errors) { throw [System.InvalidCastException]::new("Submitted text could not be converted to PowerShell because it contains syntax errors: $($errors | Out-String)") } $ast.FindAll($predicate, !$NoRecursion) | Add-Member -MemberType ScriptProperty -Name Type -Value { $this.GetType().Name } -PassThru -Force } } |