-
Notifications
You must be signed in to change notification settings - Fork 14
/
Get-Walkthru.ps1
211 lines (191 loc) · 8.64 KB
/
Get-Walkthru.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
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
function Get-Walkthru {
<#
.Synopsis
Gets information from a file as a walkthru
.Description
Parses walkthru steps from a walkthru file.
Walkthru files contain step-by-step examples for using PowerShell.
.Link
Write-WalkthruHTML
.Example
Get-Walkthru -Text {
# Walkthrus are just scripts with comments that start at column 0.
# Step 1:
Get-Process
#Step 2:
Get-Command
}
#>
[CmdletBinding(DefaultParameterSetName="File")]
[OutputType([PSObject])]
param(
# The command used to generate walkthrus
[Parameter(Mandatory=$true,
ParameterSetName="Command",
ValueFromPipeline=$true)]
[Management.Automation.CommandInfo]
$Command,
# The module containing walkthrus
[Parameter(Mandatory=$true,
ParameterSetName="Module",
ValueFromPipeline=$true)]
[Management.Automation.PSModuleInfo]
$Module,
# The file used to generate walkthrus
[Parameter(Mandatory=$true,
ParameterSetName="File",
ValueFromPipelineByPropertyName=$true)]
[Alias('Fullname')]
[string]$File,
# The text used to generate walkthrus
[Parameter(Mandatory=$true,
ParameterSetName="Text")]
[String]$Text,
# The script block used to generate a walkthru
[Parameter(Mandatory=$true,
ParameterSetName="ScriptBlock")]
[ScriptBlock]$ScriptBlock
)
begin {
$err = $null
#region Create walkthru type if it doesn't exist
if (-not ('PSWalkthru.WalkthruData' -as [Type])) {
Add-Type -UsingNamespace System.Management.Automation -Namespace PSWalkthru -Name WalkthruData -MemberDefinition '
public string SourceFile = String.Empty;','
public string Command = String.Empty;','
public string Explanation = String.Empty;','
public string AudioFile = String.Empty;','
public string VideoFile = String.Empty;','
public string Question = String.Empty;','
public string Answer = String.Empty;','
public string Link = String.Empty;','
public string Screenshot = String.Empty;','
public string[] Hint;','
public ScriptBlock Script;
public ScriptBlock Silent;','
public DateTime LastWriteTime;
'
}
#endregion Create walkthru type if it doesn't exist
}
process {
if ($psCmdlet.ParameterSetName -eq "File") {
# If the walkthru's in a file, open it and send it to Get-Walthru -Text
$realItem = Get-Item $file -ErrorAction SilentlyContinue
if (-not $realItem) { return }
$text = [IO.File]::ReadAllText($realItem.FullName)
$Result = Get-Walkthru -Text $text
if ($result) {
# If there was in fact walkthru information, add on the file name and the last write time.
foreach ($r in $result) {
$r.Sourcefile = $realItem.Fullname
$r.LastWriteTime = $realItem.LastWriteTime
$r
}
}
return
} elseif ($psCmdlet.ParameterSetName -eq "Command") {
# If they want to see a command's examples a a walkthru, then pass each example to Get-Walkthru -Text
$help = $command | Get-Help
$c= 1
$help.Examples.Example |
ForEach-Object {
$text = $_.code + ($_.remarks | Out-String)
Get-Walkthru -Text $text |
ForEach-Object {
$_.Command = "$command Walkthru $c"
$_
}
$c++
}
return
} elseif ($psCmdlet.ParameterSetName -eq 'Module') {
# For modules, enumerate all files for the current culture, then pass them down to Get-Walkthru -File
$moduleRoot = Split-Path $module.Path
Get-ChildItem -Path (Join-Path $moduleRoot "$(Get-Culture)") -Filter *.walkthru.help.txt |
Get-Walkthru
return
}
if ($psCmdlet.ParameterSetName -eq 'ScriptBlock') {
$text = "$ScriptBlock"
}
# Tokenize the script
$tokens = [Management.Automation.PSParser]::Tokenize($text, [ref]$err)
if ($err.Count) { return }
$lastToken = $null
$isInContent = $false
$lastResult = New-Object PSWalkthru.WalkthruData
foreach ($token in $tokens) {
if ($token.Type -eq "Newline") { continue }
if ($token.Type -ne "Comment" -or $token.StartColumn -gt 1) {
$isInContent = $true
if (-not $lastToken) { $lastToken = $token }
} else {
if ($lastToken.Type -ne "Comment" -and $lastToken.StartColumn -eq 1) {
$chunk = $text.Substring($lastToken.Start,
$token.Start - 1 - $lastToken.Start)
$lastResult.Script = [ScriptBlock]::Create($chunk)
# mutliparagraph, split up the results if multiparagraph
$paragraphs = @()
$lastResult
$null = $paragraphs
$lastToken = $null
$lastResult = New-Object PSWalkthru.WalkthruData
$isInContent = $false
}
}
if ($isInContent) {
if ($token.Type -eq 'Comment' -and $token.StartColumn -eq 1) {
$chunk = $text.Substring($lastToken.Start,
$token.Start - 1 - $lastToken.Start)
$lastResult.Script = [ScriptBlock]::Create($chunk)
# mutliparagraph, split up the results if multiparagraph
$paragraphs = @()
$lastResult
$null = $paragraphs
$lastToken = $null
$lastResult = New-Object PSWalkthru.WalkthruData
$isInContent = $false
}
}
if (-not $isInContent) {
$lines = $token.Content.Trim("<>#")
$lines = $lines.Split([Environment]::NewLine,
[StringSplitOptions]"RemoveEmptyEntries")
# Handle specialized return data
foreach ($_ in $lines) {
if ($_ -like ".Audio *" ) {
$lastResult.AudioFile = ($_ -ireplace "\.Audio","").Trim()
} elseif ($_ -like ".Video *" ) {
$lastResult.VideoFile = ($_ -ireplace "\.Video","").Trim()
} elseif ($_ -like ".Question *"){
$lastResult.Question = ($_ -ireplace "\.Question","").Trim()
} elseif ($_ -like ".Answer *" ) {
$lastResult.Answer = ($_ -ireplace "\.Answer","").Trim()
} elseif ($_ -like ".Hint *") {
$lastResult.Hint = $_.Substring(".Hint ".Length) -split ','
} elseif ($_ -like ".Link *") {
$lastResult.Link = ($_ -ireplace "\.link","").Trim()
} elseif ($_ -like ".Screenshot *") {
$lastResult.Screenshot = ($_ -ireplace "\.Screenshot","").Trim()
} elseif ($_ -like "*.Silent *") {
$lastResult.Silent = [ScriptBlock]::Create(($_ -ireplace "\.Silent","").Trim())
} else {
if ($_.TrimEnd().EndsWith(".")) {
$lastResult.Explanation += ($_ + [Environment]::NewLine + [Environment]::NewLine + [Environment]::NewLine )
} else {
$lastResult.Explanation += ($_ + [Environment]::NewLine)
}
}
}
}
}
if ($lastToken -and $lastResult) {
$chunk = $text.Substring($lastToken.Start)
$lastResult.Script = [ScriptBlock]::Create($chunk)
$lastResult
} elseif ($lastResult) {
$lastResult
}
}
}