< Summary - Kestrun — Combined Coverage

Information
Class: Private.Assembly.Get-KrCommandByContext
Assembly: Kestrun.PowerShell.Private
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/Assembly/Get-KrCommandByContext.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
0%
Covered lines: 0
Uncovered lines: 31
Coverable lines: 31
Total lines: 105
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Private/Assembly/Get-KrCommandByContext.ps1

#LineLine coverage
 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#>
 25function 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)
 059        switch ($Name) {
 060            'Definition' { 1 }
 061            'Route' { 2 }
 062            'Schedule' { 4 }
 063            'ScheduleAndDefinition' { 5 }
 064            'Runtime' { 6 }
 065            'Everywhere' { 7 }
 066            default { throw "Unknown context '$Name'." }
 67        }
 68    }
 69
 070    $cmds = if ($Functions) {
 071        $Functions
 72    } else {
 073        if ($IncludeNonExported) { Get-Command -Module $Module -All } else { Get-Command -Module $Module }
 74    }
 75
 076    $target = 0
 077    if ($PSCmdlet.ParameterSetName -eq 'AnyOf') { foreach ($n in $AnyOf) { $target = $target -bor (_KrNameToMask $n) } }
 078    else { foreach ($n in $AllOf) { $target = $target -bor (_KrNameToMask $n) } }
 79
 080    $notMask = 0
 081    foreach ($n in ($Not | ForEach-Object { $_ })) { $notMask = $notMask -bor (_KrNameToMask $n) }
 82
 083    $match = if ($PSCmdlet.ParameterSetName -eq 'AnyOf') {
 084        if ($Exact) { { param($m) $m -eq $target } } else { { param($m) ($m -band $target) -ne 0 } }
 85    } else {
 086        if ($Exact) { { param($m) $m -eq $target } } else { { param($m) ($m -band $target) -eq $target } }
 87    }
 88
 089    foreach ($c in $cmds) {
 090        $m = 0
 091        if ($c.CommandType -eq 'Function') {
 092            $m = Get-KrFunctionContextMask -Function $c
 093        } elseif ($c.CommandType -eq 'Cmdlet' -and $c.ImplementingType) {
 094            $a = $c.ImplementingType.GetCustomAttributes($true) |
 095                Where-Object { $_.GetType().Name -eq 'KestrunRuntimeApiAttribute' } |
 096                Select-Object -First 1
 097            if ($a) { $m = [int]([KestrunApiContext]$a.Contexts) }
 98        }
 99
 0100        if ($m -eq 0) { continue }
 0101        if ($notMask -ne 0 -and ($m -band $notMask) -ne 0) { continue }  # exclude forbidden bits
 0102        if (& $match $m) { $c }
 103    }
 104}
 105