-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertToXmlHelp.ps1
More file actions
554 lines (459 loc) · 18.7 KB
/
Copy pathConvertToXmlHelp.ps1
File metadata and controls
554 lines (459 loc) · 18.7 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
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
<#
.SYNOPSIS
Generates PowerShell XML help files (MAML format) from OpenAPI specifications.
.DESCRIPTION
This script processes OpenAPI JSON files to generate MAML-formatted XML help files that can be used
with PowerShell's Get-Help cmdlet. It extracts summaries, descriptions, parameters, and examples
from the OpenAPI specification and formats them according to PowerShell help standards.
Requires PowerShell 7.0 or later.
.PARAMETER ProjectName
MANDATORY: The name of the project or API. This is used to locate the OpenAPI JSON file.
.PARAMETER MultipleFiles
Indicates whether multiple JSON files should be processed (files named MyModule_1, MyModule_2, etc.)
.PARAMETER OutPath
Specifies the output path for the generated XML help file.
.PARAMETER LastNounToVern
A hashtable mapping the last noun in a cmdlet name to a specific verb (must match ConvertProject.ps1 settings).
.PARAMETER FunctionRename
A hashtable for renaming generated functions (must match ConvertProject.ps1 settings).
.PARAMETER FunctionRenamePattern
A hashtable for renaming functions using patterns (must match ConvertProject.ps1 settings).
.PARAMETER ModuleVersion
Version number for the module. Default: "1.0.0"
.PARAMETER Culture
The culture/language code for the help file. Default: "en-US"
.PARAMETER AdditionalParameters
A hashtable of additional parameters included in generated cmdlets (from ConvertProject.ps1).
These won't have descriptions from OpenAPI. Use AdditionalParameterDescriptions for help text.
.PARAMETER AdditionalParameterDescriptions
A hashtable providing descriptions for additional parameters that aren't in the OpenAPI spec.
Example: @{ GetAll = "Retrieves all pages of results automatically" }
.EXAMPLE
$Variables = @{
ProjectName = "GitHub"
LastNounToVern = @{
disable = "Disable"
enable = "Enable"
}
FunctionRename = @{
"New-GitHubReposActionsWorkflowsDispatches" = "Start-GitHubReposActionsWorkflows"
}
}
.\ConvertToXmlHelp.ps1 @Variables
.NOTES
Author: OpenApi-To-PowerShell
Version: 1.0
Generates MAML (Microsoft Assistance Markup Language) XML format
#>
#Requires -Version 7.0
[CmdletBinding()]
param(
[Parameter(Mandatory, Position=0)]
[string]$ProjectName,
[switch]$MultipleFiles,
[hashtable]$LastNounToVern = @{},
[hashtable]$FunctionRename = @{},
[System.Collections.Specialized.OrderedDictionary]$FunctionRenamePattern = @{},
[hashtable]$DescriptionToVerb = @{},
[string]$OutPath,
[string]$ModuleVersion = "1.0.0",
[string]$Culture = "en-US",
[hashtable]$AdditionalParameters = @{},
[hashtable]$AdditionalParameterDescriptions = @{}
)
$ErrorActionPreference = "Stop"
$error.clear()
###############
# Initialize Paths
###############
$ScriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
$Files = @{
JsonPath = "$ScriptPath\Projects\$ProjectName.json"
Output = "$ScriptPath\Output\$Culture\$ProjectName-Help.xml"
}
if ($MultipleFiles) {
$Files.JsonPath = "$ScriptPath\Projects\$ProjectName*.json"
}
if ($OutPath) {
$Files.Output = ($OutPath + "\$Culture\$ProjectName-Help.xml") -replace "\\\\", "\"
}
# Ensure output directory exists
$outputDir = Split-Path -Parent $Files.Output
if (!(Test-Path $outputDir)) {
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
}
###############
# Load OpenAPI JSON
###############
if (!(Test-Path $Files.JsonPath)) {
Write-Error "The file $($Files.JsonPath) does not exist"
return
}
$AllOpen = Get-Item $Files.JsonPath | ForEach-Object {
$_ | Get-Content | ConvertFrom-Json -Depth 50 -AsHashtable
}
if (!$AllOpen) {
Write-Error "Unable to ConvertFrom-Json the file $($Files.JsonPath)"
return
}
###############
# Mapping Variables (same as ConvertProject.ps1)
###############
$MethodToVerb = @{
"get" = "Get"
"post" = "New"
"put" = "Set"
"delete" = "Remove"
"patch" = "Update"
}
$DescriptionToVerb = @{
get = @{
search = "Find"
}
post = @{
Check = "Test"
search = "Search"
delete = "Remove"
}
}
###############
# Helper Functions
###############
Function Get-LongestCommonString {
param([string[]]$Strings)
$Ans = $Strings | Select-Object -First 1
$Strings | Select-Object -Skip 1 | ForEach-Object {
for ($l = [math]::Min($Ans.length, $_.length); $l -ge 1; $l--) {
if ($Ans.substring(0, $l) -like $_.substring(0, $l)) {
$Ans = $Ans.substring(0, $l)
break
}
}
}
return $Ans
}
Function Convert-Type {
param([string]$Type)
switch -regex ($Type) {
"^map" { return "Hashtable" }
"^string$" { return "String" }
"^integer$" { return "Int32" }
"^number$" { return "Int32" }
"^boolean$" { return "Boolean" }
"^file$" { return "String (FilePath)" }
"^object$" { return "Hashtable" }
"^date-time$" { return "DateTime" }
"^array$" { return "Array" }
default { return "Object" }
}
}
Function New-XmlElement {
param(
[string]$Name,
[string]$Content,
[hashtable]$Attributes = @{}
)
$attrStr = ""
foreach ($key in $Attributes.Keys) {
$attrStr += " $key=`"$($Attributes[$key])`""
}
if ($Content) {
$escapedContent = [System.Security.SecurityElement]::Escape($Content)
return " <$Name$attrStr>$escapedContent</$Name>"
} else {
return " <$Name$attrStr />"
}
}
Function Get-ParameterType {
param($ParamDetails)
if ($ParamDetails.type -eq "array") {
if ($ParamDetails.items.type) {
return (Convert-Type $ParamDetails.items.type) + "[]"
}
return "Array"
}
if ($ParamDetails.type) {
return Convert-Type $ParamDetails.type
}
return "Object"
}
###############
# Build Function Name (same logic as ConvertProject.ps1)
###############
Function Get-FunctionName {
param(
[string]$Path,
[string]$Method,
[hashtable]$PathData
)
# Build Noun
$Noun = $Path -replace "^/|/\{.*?\}|_|-|/$"
$Noun = ($Noun -split "/" | Where-Object { $_ } | ForEach-Object {
$_.substring(0,1).toupper() + $_.substring(1).tolower()
})
$LastNoun = $Noun | Select-Object -Last 1
$OriginalNoun = $Noun -join ""
if ($LastNoun -and $LastNounToVern.containskey($LastNoun)) {
$Noun = $Noun | Select-Object -SkipLast 1
}
$Noun = $Noun -join ""
# Build Verb
$LocalNoun = $Noun
$Verb = $MethodToVerb[$Method]
if ($LastNoun -and $LastNounToVern.containskey($LastNoun)) {
if ($LastNounToVern[$LastNoun] -is [hashtable]) {
if ($Method -like $LastNounToVern[$LastNoun]["Method"]) {
$Verb = $LastNounToVern[$LastNoun]["Verb"]
} else {
$LocalNoun = $OriginalNoun
}
} else {
$Verb = $LastNounToVern[$LastNoun]
}
} else {
$LocalNoun = $OriginalNoun
}
if ($Method -and $DescriptionToVerb.containskey($Method)) {
$FirstWord = $PathData[$Method].summary -split "\s" | Select-Object -First 1
if ($FirstWord -and $DescriptionToVerb[$Method].containskey($FirstWord)) {
$Verb = $DescriptionToVerb[$Method][$FirstWord]
$LocalNoun = $LocalNoun -replace ("$Verb" + '$')
}
}
$FunctionName = "$Verb-$ProjectName$LocalNoun"
# Apply renames
if ($FunctionRename.containskey($FunctionName)) {
$FunctionName = $FunctionRename[$FunctionName]
}
if ($FunctionRenamePattern) {
@($FunctionRenamePattern.psbase.keys) | ForEach-Object {
$FunctionName = $FunctionName -replace "$_", ($FunctionRenamePattern[$_])
}
}
return $FunctionName
}
###############
# Generate MAML XML Content
###############
Write-Host "Phase 1: Parsing OpenAPI specifications..." -ForegroundColor Green
$AllCommands = @{}
foreach ($Open in $AllOpen) {
foreach ($PathKey in (@($open.paths.psbase.keys) | Sort-Object)) {
$PathData = $open.paths[$PathKey]
foreach ($Method in (@($PathData.psbase.keys) | Where-Object { $_ -in @($MethodToVerb.keys) } | Sort-Object)) {
$MethodData = $PathData[$Method]
# Get function name using same logic as ConvertProject.ps1
$FunctionName = Get-FunctionName -Path $PathKey -Method $Method -PathData $PathData
# Build command info
$CommandInfo = @{
Name = $FunctionName
Synopsis = $MethodData.summary
Description = $MethodData.description
Parameters = @()
Examples = @()
Path = $PathKey
Method = $Method.ToUpper()
Tags = $MethodData.tags
}
# Process parameters from OpenAPI
if ($MethodData.parameters) {
foreach ($param in $MethodData.parameters) {
# Skip parameters with no name
if (-not $param.name) {
continue
}
$paramInfo = @{
Name = $param.name
Type = Get-ParameterType ($param.schema ? $param.schema : $param)
Required = $param.required ? $param.required : $false
Description = $param.description
Position = "Named"
PipelineInput = $false
}
$CommandInfo.Parameters += $paramInfo
}
}
# Add additional parameters if this command has any qualifying base parameters
if ($AdditionalParameters.Count -gt 0 -and $MethodData.parameters) {
foreach ($baseParam in $MethodData.parameters) {
if ($baseParam.name -and $AdditionalParameters.ContainsKey($baseParam.name)) {
foreach ($additionalParamName in $AdditionalParameters[$baseParam.name].Keys) {
$additionalParam = $AdditionalParameters[$baseParam.name][$additionalParamName]
$description = "Additional parameter"
if ($additionalParamName -and $AdditionalParameterDescriptions.ContainsKey($additionalParamName)) {
$description = $AdditionalParameterDescriptions[$additionalParamName]
}
$paramInfo = @{
Name = $additionalParamName
Type = $additionalParam.Type ? $additionalParam.Type : "Object"
Required = $false
Description = $description
Position = "Named"
PipelineInput = $false
}
$CommandInfo.Parameters += $paramInfo
}
}
}
}
# Extract examples from externalDocs if available
if ($MethodData.externalDocs) {
$CommandInfo.Examples += @{
Title = "Example 1"
Code = "$FunctionName"
Remarks = "See documentation: $($MethodData.externalDocs.url)"
}
}
$AllCommands[$FunctionName] = $CommandInfo
}
}
}
Write-Host "Phase 2: Generating MAML XML..." -ForegroundColor Green
###############
# Generate MAML XML
###############
$xml = @()
$xml += '<?xml version="1.0" encoding="utf-8"?>'
$xml += '<helpItems schema="maml" xmlns="http://msh">'
foreach ($cmdName in ($AllCommands.Keys | Sort-Object)) {
$cmd = $AllCommands[$cmdName]
$xml += ''
$xml += ' <command:command xmlns:maml="http://schemas.microsoft.com/maml/2004/10" xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10" xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">'
# Command details
$xml += ' <command:details>'
$xml += " <command:name>$cmdName</command:name>"
$xml += " <command:verb>$($cmdName -replace '-.*')</command:verb>"
$xml += " <command:noun>$($cmdName -replace '.*?-')</command:noun>"
$xml += ' <maml:description>'
if ($cmd.Synopsis) {
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($cmd.Synopsis))</maml:para>"
}
$xml += ' </maml:description>'
$xml += ' </command:details>'
# Description
$xml += ' <maml:description>'
if ($cmd.Description) {
# Keep newlines as separate paragraphs but skip empty lines
$descLines = $cmd.Description -split "`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ }
foreach ($line in $descLines) {
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($line))</maml:para>"
}
} else {
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($cmd.Synopsis))</maml:para>"
}
$xml += " <maml:para></maml:para>"
$xml += " <maml:para>API Endpoint: $($cmd.Method) $($cmd.Path)</maml:para>"
$xml += ' </maml:description>'
# Syntax
$xml += ' <command:syntax>'
$xml += " <command:syntaxItem>"
$xml += " <maml:name>$cmdName</maml:name>"
foreach ($param in $cmd.Parameters) {
$xml += ' <command:parameter required="' + ($param.Required ? 'true' : 'false') + '" position="' + $param.Position + '">'
$xml += " <maml:name>$($param.Name)</maml:name>"
$xml += ' <maml:description>'
if ($param.Description) {
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($param.Description))</maml:para>"
}
$xml += ' </maml:description>'
$xml += " <command:parameterValue required='true'>$($param.Type)</command:parameterValue>"
$xml += ' </command:parameter>'
}
$xml += ' </command:syntaxItem>'
$xml += ' </command:syntax>'
# Parameters (detailed)
$xml += ' <command:parameters>'
foreach ($param in $cmd.Parameters) {
$xml += ' <command:parameter required="' + ($param.Required ? 'true' : 'false') + '" position="' + $param.Position + '">'
$xml += " <maml:name>$($param.Name)</maml:name>"
$xml += ' <maml:description>'
if ($param.Description) {
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($param.Description))</maml:para>"
}
$xml += ' </maml:description>'
$xml += " <command:parameterValue required='true'>$($param.Type)</command:parameterValue>"
$xml += ' <dev:type>'
$xml += " <maml:name>$($param.Type)</maml:name>"
$xml += ' <maml:uri />'
$xml += ' </dev:type>'
$xml += ' <dev:defaultValue />'
$xml += ' </command:parameter>'
}
$xml += ' </command:parameters>'
# Input types
$xml += ' <command:inputTypes>'
$xml += ' <command:inputType>'
$xml += ' <dev:type>'
$xml += ' <maml:name>None</maml:name>'
$xml += ' </dev:type>'
$xml += ' <maml:description>'
$xml += ' <maml:para>This cmdlet does not accept pipeline input.</maml:para>'
$xml += ' </maml:description>'
$xml += ' </command:inputType>'
$xml += ' </command:inputTypes>'
# Return types
$xml += ' <command:returnValues>'
$xml += ' <command:returnValue>'
$xml += ' <dev:type>'
$xml += ' <maml:name>Object</maml:name>'
$xml += ' </dev:type>'
$xml += ' <maml:description>'
$xml += ' <maml:para>Returns API response as object.</maml:para>'
$xml += ' </maml:description>'
$xml += ' </command:returnValue>'
$xml += ' </command:returnValues>'
# Examples
$xml += ' <command:examples>'
if ($cmd.Examples.Count -gt 0) {
$exampleNum = 1
foreach ($example in $cmd.Examples) {
$xml += ' <command:example>'
$xml += " <maml:title>Example $exampleNum</maml:title>"
$xml += ' <dev:code>' + [System.Security.SecurityElement]::Escape($example.Code) + '</dev:code>'
$xml += ' <dev:remarks>'
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($example.Remarks))</maml:para>"
$xml += ' </dev:remarks>'
$xml += ' </command:example>'
$exampleNum++
}
} else {
$xml += ' <command:example>'
$xml += ' <maml:title>Example 1</maml:title>'
$xml += " <dev:code>$cmdName</dev:code>"
$xml += ' <dev:remarks>'
$xml += " <maml:para>$([System.Security.SecurityElement]::Escape($cmd.Synopsis))</maml:para>"
$xml += ' </dev:remarks>'
$xml += ' </command:example>'
}
$xml += ' </command:examples>'
# Related links
$xml += ' <command:relatedLinks>'
if ($cmd.Tags) {
$xml += ' <maml:navigationLink>'
$xml += " <maml:linkText>Tags: $($cmd.Tags -join ', ')</maml:linkText>"
$xml += ' <maml:uri />'
$xml += ' </maml:navigationLink>'
}
$xml += ' </command:relatedLinks>'
$xml += ' </command:command>'
}
$xml += '</helpItems>'
###############
# Write XML File
###############
Write-Host "Phase 3: Writing XML help file to $($Files.Output)..." -ForegroundColor Green
$xml -join "`n" | Out-File -FilePath $Files.Output -Encoding UTF8 -Force
Write-Host ""
Write-Host "Successfully generated XML help file!" -ForegroundColor Green
Write-Host " File: $($Files.Output)" -ForegroundColor Cyan
Write-Host " Commands: $($AllCommands.Count)" -ForegroundColor Cyan
Write-Host ""
Write-Host "To use this help file:" -ForegroundColor Yellow
Write-Host " 1. Copy to module directory: <ModulePath>\$Culture\" -ForegroundColor Gray
Write-Host " 2. Test with: Get-Help <CommandName> -Full" -ForegroundColor Gray
if ($error.Count -gt 0) {
Write-Host "`nWarnings/Errors encountered:" -ForegroundColor Yellow
$error | ForEach-Object { Write-Host " $_" -ForegroundColor Gray }
}
# Exit successfully
exit 0