forked from SoftServeInc/SitecoreInstallExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneratehelp.ps1
More file actions
289 lines (265 loc) · 10.2 KB
/
generatehelp.ps1
File metadata and controls
289 lines (265 loc) · 10.2 KB
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
# Orginal source from Sitecore Powershell Extension
param(
[Parameter(Mandatory=$true)]
[string]$FunctionName = "Get-Help",
[Parameter(Mandatory=$true)]
[string]$DocFolder
)
Write-Host "Processing $FunctionName to $DocFolder"
$builder = New-Object System.Text.StringBuilder
$carriage = "`r"
function FixString {
param($in = '')
if ($in -eq $null) {
$in = ''
}
return $in.Trim().Trim('\n').Replace('&', '&').Replace('<', '<').Replace('>', '>').Replace("\r","`r")
}
function FixExampleString {
param($in = '')
if ($in -eq $null) {
$in = ''
}
return $in.Trim().Replace('&', '&').Replace('<', '<').Replace('>', '>').Replace("`n","<br/>")
}
$c = Get-Help $FunctionName -Full
$builder.Append("> This file is autogenerated please update doc in Powershell files") | Out-Null
$builder.AppendLine("") | Out-Null
$builder.Append("") | Out-Null
$builder.Append("# $($c.Name)") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("$($c.synopsis)") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Syntax") | Out-Null
$builder.AppendLine(" ") | Out-Null
foreach($syntax in $c.syntax.syntaxItem) {
$builder.AppendLine(" ") | Out-Null
$builder.Append("$($syntax.Name)") | Out-Null
foreach($param in $syntax.parameter){
$builder.Append(" ") | Out-Null
if($param.required -eq "false"){
$builder.Append("[") | Out-Null
}
if($param.position -ne "named"){
$builder.Append("[-$($param.Name)]") | Out-Null
} else {
$builder.Append("-$($param.Name)") | Out-Null
}
if($param.parameterValue -ne $null -and $param.parameterValue -ne "SwitchParameter"){
$builder.Append(" <$($param.parameterValue)>") | Out-Null
}
if($param.required -eq "false"){
$builder.Append("]") | Out-Null
}
}
$builder.AppendLine(" ") | Out-Null
}
if($c.Description) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Detailed Description") | Out-Null
foreach($Description in $c.Description){
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("$(FixString($Description | out-string -width 20000))") | Out-Null
}
}
$aliases = Get-Alias | Where-Object { $_.Definition -eq "$helpFor" } | % { $_.Name }
if ($aliases.Count -gt 0) {
$builder.Append("## Aliases") | Out-Null
$builder.AppendLine($carriage) | Out-Null
$builder.Append("The following abbreviations are aliases for this cmdlet: ") | Out-Null
$builder.AppendLine($carriage) | Out-Null
foreach($alias in $aliases) {
$builder.Append("* $($alias)") | Out-Null
}
}
# Parameters
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Parameters") | Out-Null
foreach ($param in $c.parameters.parameter ) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("### -$($param.Name) <$($param.type.Name)>") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
foreach($Description in $param.Description) {
$builder.Append("$(FixString($Description | out-string -width 2000))") | Out-Null
}
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append(
@"
<table>
<thead></thead>
<tbody>
<tr>
<td>Aliases</td>
<td>$($param.Aliases)</td>
</tr>
<tr>
<td>Required?</td>
<td>$($param.Required)</td>
</tr>
<tr>
<td>Position?</td>
<td>$($param.Position)</td>
</tr>
<tr>
<td>Default Value</td>
<td>$($param.DefaultValue)</td>
</tr>
<tr>
<td>Accept Pipeline Input?</td>
<td>$($param.PipelineInput)</td>
</tr>
<tr>
<td>Accept Wildcard Characters?</td>
<td>$($param.Globbing)</td>
</tr>
</tbody>
</table>
"@) | Out-Null
}
# Input Type
if (($c.inputTypes | Out-String ).Trim().Length -gt 0) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Inputs") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("The input type is the type of the objects that you can pipe to the cmdlet.") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
foreach($inputType in $c.inputTypes.inputType){
$builder.Append("$(FixString(($inputType.description.text -split '\r') | ForEach-Object { if($_.Trim()) { '* ' + $_.Trim() } } | Out-String -width 2000))") | Out-Null
}
foreach($inputType in $c.inputTypes.inputType){
$builder.Append("$(FixString(($inputType.type.name -split '\r') | ForEach-Object { if($_.Trim()) { '* ' + $_.Trim() } } | Out-String -width 2000))") | Out-Null
}
}
# Return Type
if (($c.returnValues | Out-String ).Trim().Length -gt 0) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Outputs") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("The output type is the type of the objects that the cmdlet emits.") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
foreach($returnValue in $c.returnValues.returnValue){
$builder.Append("$(FixString(($returnValue.description.Text -split '\r') | ForEach-Object { if($_.Trim()) { '* ' + $_.Trim() } } | Out-String -width 2000))") | Out-Null
$builder.Append("$(FixString(($returnValue.type.name -split '\r') | ForEach-Object { if($_.Trim()) { '* ' + $_.Trim() } } | Out-String -width 2000))") | Out-Null
}
}
# Notes
if (($c.alertSet | Out-String).Trim().Length -gt 0) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Notes") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
foreach($alert in $c.alertSet.alert){
$builder.Append("$(FixString($alert | out-string))") | Out-Null
}
}
# Examples
if (($c.examples | Out-String).Trim().Length -gt 0) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("## Examples") | Out-Null
foreach ($example in $c.examples.example) {
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("### $(FixString($example.title.Trim(('-',' '))))") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
if( $example.code -like '*json*' )
{
$builder.Append("$(FixString($example.code | out-string -Width 2000).Trim())") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append('```powershell ') | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("$(FixString($example.remarks | out-string -Width 2000).Trim())") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append('```') | Out-Null
}
else
{
$builder.Append("$(FixString($example.remarks | out-string -Width 2000).Trim())") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append('```powershell ') | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append("$($example.code)") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.Append('```') | Out-Null
}
}
}
#Related Topics
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
$links = $c.relatedLinks.navigationLink | Where-Object { $_.linkText }
if($links) {
$builder.Append("## Related Topics") | Out-Null
$builder.AppendLine(" ") | Out-Null
$builder.AppendLine(" ") | Out-Null
foreach ($relatedLink in $links) {
if($relatedLink.linkText.StartsWith('about') -eq $false) {
if($relatedLink.uri){
$builder.Append( "* <a href='$($relatedLink.uri)' target='_blank'>$($relatedLink.uri)</a>") | Out-Null
} else {
if($relatedLink.linkText -match 'http'){
$url = $relatedLink.linkText | select-string -pattern '\b(?:(?:https?|ftp|file)://|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])' | % { $_.Matches } | % { $_.Value } | select -first 1
$builder.Append("`r* <a href='$url' target='_blank'>$($relatedLink.linkText)</a><br/>") | Out-Null
} else {
$internalCommand = $relatedLink.linkText
if ($commandNames -contains $internalCommand) {
$builder.Append("`r* [$internalCommand](/appendix/commands/$internalCommand.md)") | Out-Null
}
else
{
$builder.Append("`r* $internalCommand") | Out-Null
}
}
}
}
}
}
if($hideOutput -eq $null)
{
#Write-Output $builder.ToString()
$doc = Join-Path -Path $DocFolder -ChildPath "$FunctionName.md"
if( Test-Path -Path $doc )
{
# Check if file changed and doc should be updated
$doc1 = Join-Path -Path $DocFolder -ChildPath "$FunctionName.md1"
$builder.ToString() | Out-File -FilePath $doc1
if((Get-FileHash $doc).hash -ne (Get-FileHash $doc1).hash)
{
Write-Host "Update the doc file"
Remove-Item $doc
Copy-Item -Path $doc1 -Destination $doc
}
else
{
Write-Host "Keep the doc file"
Remove-Item $doc1
}
}
else
{
Write-Host "Create a new doc file."
$builder.ToString() | Out-File -FilePath $doc
}
}