-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ShowDemo.types.ps1xml
575 lines (530 loc) · 16.5 KB
/
ShowDemo.types.ps1xml
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?xml version="1.0" encoding="utf-16"?>
<!-- Generated with EZOut 2.0.5: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Types>
<Type>
<Name>Demo</Name>
<Members>
<ScriptMethod>
<Name>Dump</Name>
<Script>
$demoContent =
@(foreach ($chapter in $this.Chapters) {
"# $($chapter.Number) $($chapter.Name)"
$stepIndex = 0
foreach ($step in $chapter.Steps) {
$stepIndex++
if ($this.CurrentChapter -eq $chapter -and $this.CurrentStep -eq $stepIndex) {
" <# *** You Are Here! *** #>"
}
$step
}
}) -join [Environment]::NewLine
$demoContent
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>NextChapter</Name>
<Script>
<#
.SYNOPSIS
Go to the Next Chapter in a Demo
.DESCRIPTION
Advances a demo to the next chapter.
#>
$demo = $this
$chapterIndex = $demo.Chapters.IndexOf($demo.CurrentChapter)
$chapterIndex++
if (-not $demo.Chapters[$chapterIndex]) {
$demo | Add-Member NoteProperty DemoFinished ([datetime]::Now) -Force
$null = New-Event -SourceIdentifier Demo.Complete -Sender $this
} else {
$null = New-Event -SourceIdentifier Demo.NextChapter -Sender $this -EventArguments $demo.Chapters[$chapterIndex].Name -MessageData $demo.Chapters[$chapterIndex]
$demo | Add-Member NoteProperty CurrentChapter $demo.Chapters[$chapterIndex] -Force
$demo | Add-Member NoteProperty CurrentStep 0 -Force
}
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>NextStep</Name>
<Script>
<#
.SYNOPSIS
Go to the next demo step
.DESCRIPTION
Advances a demo to the next step.
#>
$demo = $this
$demo.CurrentStep++
# No more steps in this chapter
if (-not $demo.CurrentChapter.Steps[$demo.CurrentStep - 1]) {
$demo.NextChapter()
}
$null = New-Event -SourceIdentifier Demo.NextStep -Sender $this -EventArguments $args
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>ProcessInput</Name>
<Script>
<#
.SYNOPSIS
Processes demo input
.DESCRIPTION
Processes user input to a demo file.
#>
param($hostInput)
$demo = $this
$null = New-Event -SourceIdentifier Demo.ProcessInput -Sender $this -EventArguments @($hostinput)
switch ($hostInput) {
'?' {
@{
ForegroundColor = 'Cyan'
InputObject = @"
Running demo: $($demo.Name)
(q) Quit
(# ...) Goto Chapter/Step
(f ...) Find cmds using X
(t) Timecheck
(s) Skip
(!) Debug Demo
(d) Dump demo
"@
}
}
'q' {
$demo.Stop()
}
'd' {
$demoDump = $demo.Dump()
$demoDump | Out-Host
if (Get-Command Set-Clipboard -ErrorAction SilentlyContinue) {
$demoDump | Set-Clipboard
}
}
's' {
$demo | Add-Member NoteProperty StepToRun $null -Force
}
't' {
$duration = [Datetime]::now - $demo.DemoStarted
@{
ForegroundColor = 'Warning'
Italic = $true
InputObject =
"{0} {1} [{2}m, {3}s]" -f $demo.Name, $demo.Status, [int]$Duration.TotalMinutes, [int]$Duration.Seconds
}
}
'!' {
Write-Warning "Debugging Demo: Use Resume-Demo to resume."
$host.EnterNestedPrompt()
}
default {
if ($hostInput -match '^\s{0,}\#\s{0,}\d') {
$demo | Add-Member NoteProperty StepToRun $null -Force
$demo.SetChapter($hostInput -replace '^\s{0,}\#\s{0,}')
}
elseif ($hostInput -match '^\s{0,}(?>f|/)\s{0,}\S') {
$toFind = $hostInput -replace '^\s{0,}(?>f|/)\s{0,}'
Select-String -Path $demo.DemoFile -Pattern $toFind | Out-Host
{}
}
}
}
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Reset</Name>
<Script>
<#
.SYNOPSIS
Resets a demo
.DESCRIPTION
Resets a demo file, so that it can be replayed.
#>
$this | Add-Member NoteProperty Status "NotStarted" -Force
$this | Add-Member NoteProperty StepToRun $null -Force
$demo | Add-Member NoteProperty DemoFinished $null -Force
$demo | Add-Member NoteProperty DemoStarted $null -Force
$demo | Add-Member NoteProperty CurrentChapter $null -Force
$demo | Add-Member NoteProperty CurrentStep $null -Force
$null = New-Event -SourceIdentifier Demo.Reset -Sender $this -EventArguments $args
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>SetChapter</Name>
<Script>
param([string]$Chapter)
if (-not $this.Chapters) { throw "No Chapters" }
$chapterParts = @($chapter -split '\.')
$potentialChapterNumbers = @(
if ($chapterParts.Length -gt 1) {
($chapterParts[0..($chapterParts.Length - 2)] -join '\.') + '*'
}
$chapter + '*'
)
$newChapter, $newStep =
:nextChapter foreach ($chap in $this.Chapters) {
foreach ($potential in $potentialChapterNumbers) {
if ($chap.Number -like $potential) {
if ($potential -ne ($Chapter + '*')) {
# Setting chapter and step number
$chap, $chapterParts[-1] -as [int]
break nextChapter
} else {
$chap, 1
}
}
}
}
if (-not $newChapter) {
throw "Could not find chapter '$chapter'"
}
$this | Add-Member NoteProperty CurrentChapter $newChapter -Force
$this | Add-Member NoteProperty CurrentStep ($newStep - 1) -Force
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>SetStatus</Name>
<Script>
<#
.SYNOPSIS
Sets demo status
.DESCRIPTION
Sets the status of a demo.
#>
param([string]$Status)
$this | Add-Member Status $status -Force
$null = New-Event -SourceIdentifier Demo.SetStatus -Sender $this -EventArguments @($Status)
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>ShowStep</Name>
<Script>
param($step)
$null = New-Event -SourceIdentifier Demo.ShowStep -Sender $this -EventArguments @($step)
$stepTokens = [Management.Automation.PSParser]::Tokenize($step, [ref]$null)
$PreviousToken = $null
foreach ($_ in $stepTokens) {
$content = $_.Content
if ($_.Type -in 'Variable', 'String') {
$content = $step.Substring($_.Start, $_.Length)
}
if ($PreviousToken) {
$token = $_
$prevEnd = $PreviousToken.Start + $PreviousToken.Length
$substring = $step.Substring($prevEnd, $token.Start - $prevEnd)
if ($substring) {
@{InputObject=$substring}
}
}
if ($_.Type -eq 'Comment') {
@{
ForegroundColor='Success'
InputObject = $content
}
}
elseif ($_.Type -in 'Keyword', 'String', 'CommandArgument') {
@{
ForegroundColor='Verbose'
InputObject = $Content
}
}
elseif ($_.Type -in 'Variable', 'Command') {
@{
ForegroundColor='Warning'
InputObject = $Content
}
}
elseif ($_.Type -in 'CommandParameter') {
@{
ForegroundColor='Magenta'
InputObject = $Content
}
}
elseif (
$_.Type -in 'Operator','GroupStart', 'GroupEnd'
) {
@{
ForegroundColor='Magenta'
InputObject = $Content
}
}
elseif (
$_.Type -notin 'Comment',
'Keyword', 'String', 'CommandArgument',
'Variable', 'Command',
'CommandParameter',
'Operator','GroupStart', 'GroupEnd'
) {
@{
ForegroundColor='White'
InputObject=$Content
}
} else {
@{
ForegroundColor='White'
InputObject=$Content
}
}
$PreviousToken = $_
}
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Start</Name>
<Script>
<#
.SYNOPSIS
Starts a Demo
.DESCRIPTION
Starts a Demo file.
#>
$this | Add-Member NoteProperty Status Running -Force
$this | Add-Member NoteProperty DemoStarted ([DateTime]::Now) -Force
$null = New-Event -SourceIdentifier Demo.Start -Sender $this -EventArguments $args
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>StartChapter</Name>
<Script>
$this | Add-Member NoteProperty CurrentStep 1 -Force
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Stop</Name>
<Script>
<#
.SYNOPSIS
Stops a demo
.DESCRIPTION
Stops a demo that is currently running
#>
$this | Add-Member NoteProperty Status Stopped -Force
$this | Add-Member NoteProperty StepToRun $null -Force
$this | Add-Member NoteProperty DemoFinished ([datetime]::Now) -Force
$demo | Add-Member NoteProperty CurrentChapter $null -Force
$demo | Add-Member NoteProperty CurrentStep $null -Force
$null = New-Event -SourceIdentifier Demo.Stop -Sender $this -EventArguments $args
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>ToMarkdown</Name>
<Script>
# We don't want to modify this object
$demoCopy = # so create a copy
if ($this.DemoFile) { # by importing the file
Import-Demo -DemoPath $this.DemoFile
} elseif ($this.DemoScript) { # or the script block.
Import-Demo -DemoPath $this.DemoScript
}
# We need Write-Host to be overridden in the same way as Export-Demo does.
# So find Export-Demo's Abstract Syntax Tree
$exportDemoAst = $ExecutionContext.SessionState.InvokeCommand.GetCommand('Export-Demo','Function').ScriptBlock.Ast.Body
# and find our inner function
$writeHost = $exportDemoAst.Find({param($ast)
$ast.Name -eq 'Write-Host' -and $ast.IsFilter -eq $false
}, $false)
# And override it here
${function:Write-Host} = $writeHost.Body.GetScriptBlock()
# Now, modify our demo copy to make it non-interactive
$demoCopy | Add-Member NoteProperty Interactive $false -Force
# and markdown
$demoCopy | Add-Member NoteProperty Markdown $true -Force
# then use the formatter to get the markdown as a string.
$demoCopy |
Format-Custom |
Out-String -Width 1mb
</Script>
</ScriptMethod>
<ScriptProperty>
<Name>TotalSteps</Name>
<GetScriptBlock>
$stepCount = 0
foreach ($chapter in $this.Chapters) {
$stepCount += $chapter.Steps.Length
}
$stepCount
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
<Type>
<Name>Demo.Chapter</Name>
<Members>
<ScriptProperty>
<Name>Steps</Name>
<GetScriptBlock>
$text = $this.Text
$step = @()
$ThisChapterSteps = @()
# We want every step to be able to run independently.
# This would be untrue if the code is unbalanced when a chapter would start
# Thus, while we're primarily looking for comments, we also need to track groups
$groupDepth = 0
for ($tokenNumber =0 ; $tokenNumber -lt $this.Tokens.Length; $tokenNumber++) {
$token = $this.tokens[$tokenNumber]
# If the token is a group start
if ($token.Type -eq 'GroupStart')
{
$groupDepth++ # increment depth.
}
# If the token was a group end
elseif ($token.Type -eq 'GroupEnd')
{
$groupDepth-- # decrement depth.
}
# and
elseif (
(-not $groupDepth) -and # If there was no depth and
$token.StartColumn -le 1 -and # the token was a comment starting in the first column
$token[-1].Type -ne 'Keyword' -and # and it wasn't preceeded by a keyword
$token[0].Type -ne 'Keyword' -and # and it wasn't a keyword
$token[0].Type -ne 'Newline' # and it wasn't a newline
) {
# Then it's the start of a new step
if ($step) {
$stepEnd = $step[-1].Start + $step[-1].Length
$stepStart = $step[0].Start
# Get the content of the last step
$stepScript = $text.Substring($stepStart, $stepEnd - $stepStart) -replace '^\s{0,}$'
if ($stepScript) {
# and make it into a PSObject
$stepScript = [PSObject]::new($stepScript)
# with the PSTypeName 'Demo.Step'
$stepScript.pstypenames.add('Demo.Step')
# and add the .Chapter property, pointing to $this
$stepScript.psobject.properties.add([psnoteproperty]::new(
'Chapter',$this
))
$ThisChapterSteps += $stepScript
}
# then reset the collection of tokens in the current step.
$step = @()
}
}
# Add any token we see into the current step.
$step += $token
}
# If there were any steps remaining
if ($step) {
$stepEnd = $step[-1].Start + $step[-1].Length
$stepStart = $step[0].Start
$stepScript = $text.Substring($stepStart, $stepEnd - $stepStart) -replace '^\s{0,}$'
if ($stepScript) {
# make them into 'Demo.Step' objects
$stepScript = [PSObject]::new($stepScript)
$stepScript.pstypenames.add('Demo.Step')
$stepScript.psobject.properties.add([psnoteproperty]::new(
'Chapter',$this
)) # and add the chapter.
$ThisChapterSteps += $stepScript
}
}
# Force steps to be returned as a list.
,$ThisChapterSteps
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
<Type>
<Name>Demo.Step</Name>
<Members>
<ScriptMethod>
<Name>HidePrompt</Name>
<Script>
<#
.SYNOPSIS
Hides the prompt
.DESCRIPTION
Hides the prompt within a demo.
.EXAMPLE
#.HidePrompt
#>
param(
# Any additional parameters for the step.
# This is ignored when hiding prompts.
$step
)
$this.Chapter.Demo | Add-Member NoteProperty ShowPrompt $false
$null = New-Event -SourceIdentifier Demo.HidePrompt -Sender $this.Chapter.Demo -EventArguments @($step)
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Invoke</Name>
<Script>
<#
.SYNOPSIS
Invokes a demo step
.DESCRIPTION
Invokes a step in a demo file.
#>
$hiddenStep = $this.HiddenStep
$invokeResults =
if (-not $hiddenStep) {
Invoke-Expression $this
} elseif ($this.$($hiddenStep.StepType).Invoke) {
$this.$($hiddenStep.StepType).Invoke($hiddenStep.Arguments)
}
$invokeResults
$null = New-Event -SourceIdentifier Demo.Step.Invoke -Sender $this -EventArguments $args -MessageData $invokeResults
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>ShowPrompt</Name>
<Script>
<#
.SYNOPSIS
Shows the prompt
.DESCRIPTION
Show the prompt within a demo.
.EXAMPLE
#.ShowPrompt
#>
param(
# Any additional parameters for the step.
# This is ignored when showing prompts.
$step
)
$this.Chapter.Demo | Add-Member NoteProperty ShowPrompt $true
$null = New-Event -SourceIdentifier Demo.ShowPrompt -Sender $this.Chapter.Demo -EventArguments @($step)
</Script>
</ScriptMethod>
<ScriptMethod>
<Name>Silent</Name>
<Script>
<#
.SYNOPSIS
Run a silent step
.DESCRIPTION
Run a silent step of a demo.
Silent steps do not display their results.
#>
param($silentStep)
Invoke-Expression $silentStep | Out-Null
$null = New-Event -SourceIdentifier Demo.Step.Silent -Sender $this -EventArguments @($silentStep)
</Script>
</ScriptMethod>
<ScriptProperty>
<Name>HiddenStep</Name>
<GetScriptBlock>
$specialStepNameRegex = '^\s{0,}\<{0,1}\#\s{0,}\.(?<st>[\S]+)'
if ($this -notmatch $specialStepNameRegex) { return $null }
[PSCustomObject]@{
StepType = $matches.st
Arguments = $this -replace $specialStepNameRegex
}
</GetScriptBlock>
</ScriptProperty>
<ScriptProperty>
<Name>IsComment</Name>
<GetScriptBlock>
$stepTokens = [Management.Automation.PSParser]::Tokenize($this, [ref]$null)
foreach ($token in $stepTokens) {
if ($token.Type -notin 'Comment', 'Newline') {
return $false
}
}
return $true
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
</Types>