-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
Get-PSKoan.ps1
102 lines (88 loc) · 3.28 KB
/
Get-PSKoan.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function Get-PSKoan {
[CmdletBinding(DefaultParameterSetName = 'IncludeModule',
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Get-PSKoan.md')]
[OutputType('PSKoans.KoanInfo')]
param(
[Parameter()]
[SupportsWildcards()]
[string[]]
$Topic,
[Parameter(ParameterSetName = 'ModuleOnly')]
[SupportsWildcards()]
[string[]]
$Module,
[Parameter(ParameterSetName = 'IncludeModule')]
[SupportsWildcards()]
[string[]]
$IncludeModule,
[ValidateSet('User', 'Module')]
[string]
$Scope = 'Module',
[Parameter()]
[switch]
$SkipAttributeParsing,
[Parameter(Mandatory, ParameterSetName = 'ListModules')]
[switch]
$ListModules
)
$ParentPath = switch ($Scope) {
'User' {
$KoanLocation = Get-PSKoanLocation
Write-Verbose "Checking existence of koans folder"
if (-not (Test-Path $KoanLocation)) {
Write-Verbose "Koans folder does not exist. Initiating full reset..."
Update-PSKoan -Confirm:$false
}
$KoanLocation
}
'Module' { Join-Path -Path $script:ModuleRoot -ChildPath 'Koans' }
}
if ($pscmdlet.ParameterSetName -eq 'ListModules') {
$moduleList = Join-Path -Path $ParentPath -ChildPath 'Modules' |
Get-ChildItem -Directory |
Select-Object -ExpandProperty Name
return $moduleList
}
$KoanDirectories = switch ($pscmdlet.ParameterSetName) {
'IncludeModule' {
$Module = $IncludeModule
Get-ChildItem $ParentPath -Exclude Modules -Directory
}
{ $Module } {
$ModuleRegex = ConvertFrom-WildcardPattern -Pattern $Module
Join-Path -Path $ParentPath -ChildPath 'Modules' |
Get-ChildItem -Directory |
Where-Object { $_.Name -match $ModuleRegex }
}
}
$TopicRegex = ConvertFrom-WildcardPattern -Pattern $Topic
$ParentPathPattern = [regex]::Escape($parentPath)
# Declared to avoid scope problems when attribute parsing is not required.
$KoanAttribute = $null
try {
$KoanDirectories |
Get-ChildItem -Recurse -Filter *.Koans.ps1 |
Where-Object { -not $Topic -or $_.BaseName -replace '\.Koans$' -match $TopicRegex } |
Assert-UnblockedFile -PassThru |
ForEach-Object {
if (-not $SkipAttributeParsing) {
$KoanAttribute = Get-KoanAttribute -Path $_.FullName
if (-not $KoanAttribute) {
return
}
}
[PSCustomObject]@{
Topic = $_.BaseName -replace '\.koans$'
Module = $KoanAttribute.Module
Position = $koanAttribute.Position
Path = $_.FullName
RelativePath = $_.Fullname -replace $ParentPathPattern -replace '^\\'
PSTypeName = 'PSKoans.KoanInfo'
}
} |
Sort-Object { $_.Module -ne '_powershell' }, Module, Position, Topic
}
catch {
$pscmdlet.ThrowTerminatingError($_)
}
}