| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Tests whether a given PowerShell function has a specified attribute applied. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function checks if the provided PowerShell function (CommandInfo) has an attribute that matches |
| | | 6 | | the specified attribute name regex. It first checks the runtime attributes applied to the function's |
| | | 7 | | ScriptBlock, and if not found, it parses the function's definition to look for attributes in the AST. |
| | | 8 | | .PARAMETER Command |
| | | 9 | | The PowerShell function (CommandInfo) to check for the attribute. |
| | | 10 | | .PARAMETER AttributeNameRegex |
| | | 11 | | A regex pattern to match the name of the attribute to look for. |
| | | 12 | | .EXAMPLE |
| | | 13 | | PS> $cmd = Get-Command -Name 'MyFunction' |
| | | 14 | | PS> Test-KrFunctionHasAttribute -Command $cmd -AttributeNameRegex 'MyAttribute' |
| | | 15 | | Returns $true if 'MyFunction' has an attribute matching 'MyAttribute', otherwise $false. |
| | | 16 | | .NOTES |
| | | 17 | | This function is part of the Kestrun PowerShell module. |
| | | 18 | | #> |
| | | 19 | | function Test-KrFunctionHasAttribute { |
| | | 20 | | [CmdletBinding()] |
| | | 21 | | [outputType([bool])] |
| | | 22 | | param( |
| | | 23 | | [Parameter(Mandatory = $true)] |
| | | 24 | | [System.Management.Automation.CommandInfo]$Command, |
| | | 25 | | |
| | | 26 | | [Parameter(Mandatory = $true)] |
| | | 27 | | [ValidateNotNullOrEmpty()] |
| | | 28 | | [string]$AttributeNameRegex |
| | | 29 | | ) |
| | | 30 | | try { |
| | 0 | 31 | | $sb = $Command.ScriptBlock |
| | 0 | 32 | | if (-not $sb) { |
| | 0 | 33 | | return $false |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | # Prefer runtime attributes: this is what PowerShell actually binds |
| | 0 | 37 | | foreach ($a in @($sb.Attributes)) { |
| | 0 | 38 | | $t = $a.GetType() |
| | 0 | 39 | | if ($t.Name -match $AttributeNameRegex -or $t.FullName -match $AttributeNameRegex) { |
| | 0 | 40 | | return $true |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | # Fallback: parse the definition and scan AttributeAst nodes |
| | 0 | 45 | | $def = $Command.Definition |
| | 0 | 46 | | if (-not $def) { |
| | 0 | 47 | | return $false |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | $tokens = $null |
| | 0 | 51 | | $parseErrors = $null |
| | 0 | 52 | | $ast = [System.Management.Automation.Language.Parser]::ParseInput($def, [ref]$tokens, [ref]$parseErrors) |
| | | 53 | | |
| | 0 | 54 | | if ($parseErrors -and $parseErrors.Count -gt 0) { |
| | 0 | 55 | | return $false |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | $found = $ast.FindAll({ |
| | | 59 | | param($n) |
| | 0 | 60 | | $n -is [System.Management.Automation.Language.AttributeAst] -and |
| | 0 | 61 | | ($n.TypeName?.Name -match $AttributeNameRegex) |
| | | 62 | | }, $true) |
| | | 63 | | |
| | 0 | 64 | | return ($found.Count -gt 0) |
| | | 65 | | } catch { |
| | 0 | 66 | | return $false |
| | | 67 | | } |
| | 0 | 68 | | return $false |
| | | 69 | | } |