| | 1 | | <# |
| | 2 | | .SYNOPSIS |
| | 3 | | Retrieves Kestrun commands based on their context. |
| | 4 | | .DESCRIPTION |
| | 5 | | This function retrieves Kestrun commands based on their context, allowing for filtering by command type and contex |
| | 6 | | It supports both inclusive and exclusive filtering. |
| | 7 | | .PARAMETER AnyOf |
| | 8 | | Specifies the contexts that the commands must match at least one of. |
| | 9 | | .PARAMETER AllOf |
| | 10 | | Specifies the contexts that the commands must match all of. |
| | 11 | | .PARAMETER Not |
| | 12 | | Specifies the contexts that the commands must not match. |
| | 13 | | .PARAMETER Module |
| | 14 | | The name of the module to search for commands. |
| | 15 | | .PARAMETER IncludeNonExported |
| | 16 | | Whether to include non-exported commands in the search. |
| | 17 | | .PARAMETER Exact |
| | 18 | | Whether to match the contexts exactly or allow for partial matches. |
| | 19 | | .PARAMETER Functions |
| | 20 | | An array of functions to filter by context. If not provided, all functions in the specified module are considered. |
| | 21 | | .OUTPUTS |
| | 22 | | [System.Management.Automation.CommandInfo[]] |
| | 23 | | An array of command information objects that match the specified context. |
| | 24 | | #> |
| | 25 | | function Get-KrCommandsByContext { |
| | 26 | | [CmdletBinding(DefaultParameterSetName = 'AnyOf')] |
| | 27 | | param( |
| | 28 | | [Parameter(Mandatory, ParameterSetName = 'AnyOf')] |
| | 29 | | [ValidateSet('Definition', 'Route', 'Schedule', 'ScheduleAndDefinition', 'Runtime', 'Everywhere')] |
| | 30 | | [string[]]$AnyOf, |
| | 31 | |
|
| | 32 | | [Parameter(Mandatory, ParameterSetName = 'AllOf')] |
| | 33 | | [ValidateSet('Definition', 'Route', 'Schedule')] |
| | 34 | | [string[]]$AllOf, |
| | 35 | |
|
| | 36 | | [ValidateSet('Definition', 'Route', 'Schedule', 'ScheduleAndDefinition', 'Runtime', 'Everywhere')] |
| | 37 | | [string[]]$Not, |
| | 38 | |
|
| | 39 | | [string]$Module = 'Kestrun', |
| | 40 | | [switch]$IncludeNonExported, |
| | 41 | | [switch]$Exact, |
| | 42 | |
|
| | 43 | | [object[]]$Functions |
| | 44 | | ) |
| | 45 | | function _KrNameToMask { |
| | 46 | | <# |
| | 47 | | .SYNOPSIS |
| | 48 | | Converts a context name to its corresponding bitmask. |
| | 49 | | .DESCRIPTION |
| | 50 | | This function takes a context name (e.g., Definition, Route, Schedule) and converts it to its |
| | 51 | | corresponding bitmask value. |
| | 52 | | .PARAMETER Name |
| | 53 | | The name of the context to convert. |
| | 54 | | .OUTPUTS |
| | 55 | | [int] |
| | 56 | | The bitmask value for the specified context name. |
| | 57 | | #> |
| | 58 | | param([Parameter(Mandatory)][string]$Name) |
| 0 | 59 | | switch ($Name) { |
| 0 | 60 | | 'Definition' { 1 } |
| 0 | 61 | | 'Route' { 2 } |
| 0 | 62 | | 'Schedule' { 4 } |
| 0 | 63 | | 'ScheduleAndDefinition' { 5 } |
| 0 | 64 | | 'Runtime' { 6 } |
| 0 | 65 | | 'Everywhere' { 7 } |
| 0 | 66 | | default { throw "Unknown context '$Name'." } |
| | 67 | | } |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | $cmds = if ($Functions) { |
| 0 | 71 | | $Functions |
| | 72 | | } else { |
| 0 | 73 | | if ($IncludeNonExported) { Get-Command -Module $Module -All } else { Get-Command -Module $Module } |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | $target = 0 |
| 0 | 77 | | if ($PSCmdlet.ParameterSetName -eq 'AnyOf') { foreach ($n in $AnyOf) { $target = $target -bor (_KrNameToMask $n) } } |
| 0 | 78 | | else { foreach ($n in $AllOf) { $target = $target -bor (_KrNameToMask $n) } } |
| | 79 | |
|
| 0 | 80 | | $notMask = 0 |
| 0 | 81 | | foreach ($n in ($Not | ForEach-Object { $_ })) { $notMask = $notMask -bor (_KrNameToMask $n) } |
| | 82 | |
|
| 0 | 83 | | $match = if ($PSCmdlet.ParameterSetName -eq 'AnyOf') { |
| 0 | 84 | | if ($Exact) { { param($m) $m -eq $target } } else { { param($m) ($m -band $target) -ne 0 } } |
| | 85 | | } else { |
| 0 | 86 | | if ($Exact) { { param($m) $m -eq $target } } else { { param($m) ($m -band $target) -eq $target } } |
| | 87 | | } |
| | 88 | |
|
| 0 | 89 | | foreach ($c in $cmds) { |
| 0 | 90 | | $m = 0 |
| 0 | 91 | | if ($c.CommandType -eq 'Function') { |
| 0 | 92 | | $m = Get-KrFunctionContextMask -Function $c |
| 0 | 93 | | } elseif ($c.CommandType -eq 'Cmdlet' -and $c.ImplementingType) { |
| 0 | 94 | | $a = $c.ImplementingType.GetCustomAttributes($true) | |
| 0 | 95 | | Where-Object { $_.GetType().Name -eq 'KestrunRuntimeApiAttribute' } | |
| 0 | 96 | | Select-Object -First 1 |
| 0 | 97 | | if ($a) { $m = [int]([KestrunApiContext]$a.Contexts) } |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | if ($m -eq 0) { continue } |
| 0 | 101 | | if ($notMask -ne 0 -and ($m -band $notMask) -ne 0) { continue } # exclude forbidden bits |
| 0 | 102 | | if (& $match $m) { $c } |
| | 103 | | } |
| | 104 | | } |
| | 105 | |
|