-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
Show-Karma.ps1
172 lines (152 loc) · 5.73 KB
/
Show-Karma.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function Show-Karma {
[CmdletBinding(DefaultParameterSetName = 'Default',
HelpUri = 'https://github.com/vexx32/PSKoans/tree/master/docs/Show-Karma.md')]
[OutputType([void])]
[Alias('Invoke-PSKoans', 'Test-Koans', 'Get-Enlightenment', 'Meditate', 'Clear-Path', 'Measure-Karma')]
param(
[Parameter(ParameterSetName = 'ListKoans')]
[Parameter(ParameterSetName = 'ModuleOnly')]
[Parameter(ParameterSetName = 'IncludeModule')]
[Parameter(ParameterSetName = 'OpenFile')]
[Parameter(ParameterSetName = 'Default')]
[Alias('Koan', 'File')]
[SupportsWildcards()]
[string[]]
$Topic,
[Parameter(Mandatory, ParameterSetName = 'ModuleOnly')]
[Parameter(ParameterSetName = 'ListKoans')]
[SupportsWildcards()]
[string[]]
$Module,
[Parameter(Mandatory, ParameterSetName = 'IncludeModule')]
[SupportsWildcards()]
[string[]]
$IncludeModule,
[Parameter(Mandatory, ParameterSetName = 'ListKoans')]
[Alias('ListKoans', 'ListTopics')]
[switch]
$List,
[Parameter(Mandatory, ParameterSetName = 'OpenFile')]
[Alias('Meditate')]
[switch]
$Contemplate,
[Parameter(Mandatory, ParameterSetName = 'OpenFolder')]
[Alias('OpenFolder')]
[switch]
$Library,
[Parameter()]
[Alias()]
[switch]
$ClearScreen,
[Parameter(ParameterSetName = 'ModuleOnly')]
[Parameter(ParameterSetName = 'IncludeModule')]
[Parameter(ParameterSetName = 'Default')]
[Alias()]
[switch]
$Detailed
)
$GetParams = @{ }
switch ($PSCmdlet.ParameterSetName) {
'IncludeModule' { $GetParams['IncludeModule'] = $IncludeModule }
'ModuleOnly' { $GetParams['Module'] = $Module }
{ $PSBoundParameters.ContainsKey('Topic') } { $GetParams['Topic'] = $Topic }
}
switch ($PSCmdlet.ParameterSetName) {
'ListKoans' {
Get-PSKoan @GetParams
}
'OpenFolder' {
$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
}
Write-Verbose "Opening koans folder"
$Editor = Get-PSKoanSetting -Name Editor
if ($Editor -and (Get-Command -Name $Editor -ErrorAction SilentlyContinue)) {
$EditorSplat = @{
FilePath = $Editor
ArgumentList = '"{0}"' -f (Resolve-Path $KoanLocation)
NoNewWindow = $true
}
Start-Process @EditorSplat
}
else {
$KoanLocation | Invoke-Item
}
}
'OpenFile' {
# If there is no cached data, we need to call Get-Karma to populate it
if (-not $script:CurrentTopic -or ($Topic -and $script:CurrentTopic.Name -notlike $Topic)) {
try {
# We can discard this; the results we need are saved in $script:CurrentTopic
$null = Get-Karma @GetParams
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
}
$Editor = Get-PSKoanSetting -Name Editor
$FilePath = (Get-PSKoan -Topic $script:CurrentTopic.Name -Scope User).Path
$LineNumber = $script:CurrentTopic.CurrentLine
$Arguments = switch ($Editor) {
{ $_ -in 'code', 'code-insiders' } {
'--goto'
'"{0}":{1}' -f (Resolve-Path $FilePath), $LineNumber
'--reuse-window'
}
atom {
'"{0}":{1}' -f (Resolve-Path $FilePath), $LineNumber
}
default {
'"{0}"' -f (Resolve-Path $FilePath)
}
}
if ($Editor -and (Get-Command -Name $Editor -ErrorAction SilentlyContinue)) {
Start-Process -FilePath $Editor -ArgumentList $Arguments
}
else {
Invoke-Item -Path $FilePath
}
# Discard the results so we avoid accidentally returning the same result multiple times
$script:CurrentTopic = $null
}
default {
if ($ClearScreen) {
Clear-Host
}
Show-MeditationPrompt -Greeting
try {
$Results = Get-Karma @GetParams
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
if ($Results.Complete) {
$Params = @{
KoansPassed = $Results.KoansPassed
TotalKoans = $Results.TotalKoans
RequestedTopic = $Topic
Complete = $Results.Complete
}
}
else {
$Params = @{
DescribeName = $Results.Describe
ItName = $Results.It
Expectation = $Results.Expectation
Meditation = $Results.Meditation
KoansPassed = $Results.KoansPassed
TotalKoans = $Results.TotalKoans
CurrentTopic = $Results.CurrentTopic
RequestedTopic = $Topic
}
if ($Detailed) {
$Params.Add('Results', $Results.Results)
}
}
Show-MeditationPrompt @Params
}
}
}