-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: HelpInfo.ToJson ( Fixes #205 )
- Loading branch information
James Brundage
committed
Oct 13, 2024
1 parent
6ff46cb
commit 7103120
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<# | ||
.SYNOPSIS | ||
Convert HelpInfo to json | ||
.DESCRIPTION | ||
Converts a HelpInfo object to a JSON representation of the object. | ||
.EXAMPLE | ||
(Get-Help Get-Help).ToJson() | ||
#> | ||
|
||
param() | ||
|
||
$helpObject = $this | ||
[Ordered]@{ | ||
Synopsis = $helpObject.Synopsis | ||
Description = $helpObject.Description.text -join ([Environment]::NewLine * 2) | ||
Parameters = @(foreach ($parameter in $helpObject.Parameters) { | ||
[Ordered]@{ | ||
Name = $parameter.Name | ||
Type = $parameter.Type.Name | ||
Description = $parameter.Description.text -join ([Environment]::NewLine * 2) | ||
Required = $parameter.Required -match $true | ||
Position = if ($null -ne ($parameter.Position -as [int])) { | ||
$parameter.Position -as [int] | ||
} else { | ||
-1 | ||
} | ||
Aliases = $parameter.Aliases | ||
DefaultValue = $parameter.DefaultValue | ||
Globbing = $parameter.Globbing -match $true | ||
PipelineInput = $parameter.PipelineInput | ||
variableLength = $parameter.variableLength -match $true | ||
} | ||
}) | ||
Notes = @($helpObject.alertSet.alert.text) | ||
CommandType = $helpObject.Category | ||
Component = @($helpObject.Component) | ||
Inputs = @( | ||
$helpObject.InputTypes.InputType.Type.Name | ||
) | ||
Outputs = @( | ||
$helpObject.ReturnValues.ReturnValue.Type.Name | ||
) | ||
Links = @( | ||
foreach ($relLink in $this.RelatedLinks.navigationLink) { | ||
if ($relLink.uri) { | ||
$relLink.uri | ||
} else { | ||
$relLink.text | ||
} | ||
} | ||
) | ||
Examples = @( | ||
foreach ($example in $helpObject.Examples.Example) { | ||
# Combine the code and remarks | ||
$exampleLines = | ||
@( | ||
$example.Code | ||
foreach ($remark in $example.Remarks.text) { | ||
if (-not $remark) { continue } | ||
$remark | ||
} | ||
) -join ([Environment]::NewLine) -split '(?>\r\n|\n)' # and split into lines | ||
|
||
# Anything until the first non-comment line is a markdown predicate to the example | ||
$nonCommentLine = $false | ||
$markdownLines = @() | ||
|
||
# Go thru each line in the example as part of a loop | ||
$codeBlock = @(foreach ($exampleLine in $exampleLines) { | ||
if ($exampleLine -match '^\#' -and -not $nonCommentLine) { | ||
$markdownLines += $exampleLine -replace '^\#' -replace '^\s+' | ||
} else { | ||
$nonCommentLine = $true | ||
$exampleLine | ||
} | ||
}) -join [Environment]::NewLine | ||
[Ordered]@{ | ||
Title = ($example.Title -replace '^[-\s]+' -replace '[-\s]+$') | ||
Markdown = $markdownLines -join [Environment]::NewLine | ||
Code = $codeBlock | ||
} | ||
} | ||
) | ||
} | ConvertTo-Json -Depth 10 |