| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Retrieves the context mask for a Kestrun function. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function takes a Kestrun function and retrieves its context mask, which indicates the |
| | | 6 | | contexts in which the function is applicable (e.g., Definition, Route, Schedule). |
| | | 7 | | .PARAMETER Function |
| | | 8 | | The Kestrun function for which to retrieve the context mask. |
| | | 9 | | .OUTPUTS |
| | | 10 | | [int] |
| | | 11 | | The context mask for the specified function. |
| | | 12 | | #> |
| | | 13 | | function Get-KrFunctionContextMask { |
| | | 14 | | param([System.Management.Automation.FunctionInfo]$Function) |
| | | 15 | | |
| | 0 | 16 | | if (-not $Function.ScriptBlock) { return 0 } |
| | | 17 | | |
| | 0 | 18 | | $fnAst = $Function.ScriptBlock.Ast. |
| | 0 | 19 | | Find({ param($n) $n -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $n.Name -eq $Function.Nam |
| | 0 | 20 | | if (-not $fnAst) { return 0 } |
| | | 21 | | |
| | 0 | 22 | | $attrs = @() |
| | 0 | 23 | | if ($fnAst.Attributes) { $attrs += $fnAst.Attributes } |
| | 0 | 24 | | if ($fnAst.Body -and $fnAst.Body.ParamBlock -and $fnAst.Body.ParamBlock.Attributes) { |
| | 0 | 25 | | $attrs += $fnAst.Body.ParamBlock.Attributes |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | $kr = $attrs | Where-Object { $_.TypeName.Name -eq 'KestrunRuntimeApi' } | Select-Object -First 1 |
| | 0 | 29 | | if (-not $kr) { return 0 } |
| | | 30 | | |
| | 0 | 31 | | $txt = (($kr.PositionalArguments + $kr.NamedArguments.Expression) | Where-Object { $_ }).Extent.Text |
| | | 32 | | #| ForEach-Object { $_.Extent.Text } -join ' ' |
| | 0 | 33 | | $mask = switch ($txt) { |
| | 0 | 34 | | "'Everywhere'" { 7 } |
| | 0 | 35 | | "'Runtime'" { 6 } |
| | 0 | 36 | | "'ScheduleAndDefinition'" { 5 } |
| | 0 | 37 | | "'Definition'" { 1 } |
| | 0 | 38 | | "'Route'" { 2 } |
| | 0 | 39 | | "'Schedule'" { 4 } |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | return $mask |
| | | 43 | | } |
| | | 44 | | |