Skip to content

Commit 29b3801

Browse files
Merge pull request #87 from StartAutomating/Format-JSON-And-Action-Improvement
Format json and action improvement
2 parents 274ac19 + 9c4da75 commit 29b3801

File tree

10 files changed

+497
-80
lines changed

10 files changed

+497
-80
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.9.6:
2+
* Added Format-JSON (Fixes #86)
3+
* Improved GitHub Action (Fixes #85)
4+
---
5+
16
## 1.9.5:
27
* Write-FormatViewExpression:
38
* Adding -DoubleUnderline

EZOut.format.ps1xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-16"?>
2-
<!-- Generated with EZOut 1.9.5: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
2+
<!-- Generated with EZOut 1.9.6: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
33
<Configuration>
44
<SelectionSets>
55
<SelectionSet>

EZOut.psd1

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
@{
22
ModuleToProcess = 'EZOut.psm1'
3-
ModuleVersion = '1.9.5'
3+
ModuleVersion = '1.9.6'
44
GUID = 'cef786f0-8a0b-4a5d-a2c6-b433095354cd'
55
Author = 'James Brundage'
66
CompanyName = 'Start-Automating'
77
Copyright = '2011-2022'
88
Description = 'Easily Author Rich Format Files to Customize PowerShell Output'
99
FunctionsToExport =
10-
'Add-FormatData', 'Clear-FormatData', 'Out-FormatData', 'Remove-FormatData',
10+
# Core format manipulation functions
11+
'Add-FormatData','Clear-FormatData', 'Out-FormatData', 'Remove-FormatData',
1112
'Add-TypeData', 'Clear-TypeData', 'Out-TypeData', 'Remove-TypeData',
13+
'Get-FormatFile', 'Find-FormatView',
14+
# Imports
1215
'Import-FormatView','Import-TypeView',
13-
'Write-FormatControl','Write-FormatView',
14-
'Write-FormatCustomView', 'Write-FormatTableView',
15-
'Write-FormatTreeView','Write-FormatWideView', 'Write-FormatListView',
16+
# Control authoring
17+
'Write-FormatControl',
18+
# Creation of formatting views: splats to
19+
'Write-FormatView',
20+
'Write-FormatCustomView',
21+
'Write-FormatTableView',
22+
'Write-FormatListView',
23+
# doesn't splat to Write-FormatWideView, because it's not useful
24+
'Write-FormatWideView',
25+
# Write-FormatViewExpression is used in custom actions and controls
1626
'Write-FormatViewExpression',
17-
'Write-TypeView','ConvertTo-PropertySet','Write-PropertySet',
18-
'Get-FormatFile', 'Find-FormatView', 'Get-PropertySet', 'Write-EZFormatFile', 'Get-EZOutExtension',
19-
'Format-Object','Format-RichText', 'Format-Markdown', 'Format-YAML','Format-Heatmap','Format-Hashtable'
27+
# Write-FormatTreeView is an advanced formatting control
28+
'Write-FormatTreeView',
29+
# Most other advanced format controls are extensions to Format-Object
30+
'Format-Object',
31+
'Format-Hashtable','Format-Heatmap','Format-JSON',
32+
'Format-Markdown', 'Format-RichText','Format-YAML',
33+
# Don't forget types.ps1xml functions
34+
'Write-TypeView',
35+
'ConvertTo-PropertySet','Write-PropertySet','Get-PropertySet',
36+
# or the code generator for .ezout.ps1 files
37+
'Write-EZFormatFile',
38+
# or Get-EZOutExtension
39+
'Get-EZOutExtension'
40+
2041
AliasesToExport = 'Write-CustomAction'
2142
FormatsToProcess = 'EZOut.format.ps1xml'
2243
PrivateData = @{
@@ -36,13 +57,19 @@
3657
Markdown = "Format-Markdown"
3758
YAML = "Format-YAML"
3859
Hashtable = "Format-Hashtable"
60+
JSON = "Format-JSON"
3961
}
4062
PSData = @{
4163
ProjectURI = 'https://github.com/StartAutomating/EZOut'
4264
LicenseURI = 'https://github.com/StartAutomating/EZOut/blob/master/LICENSE'
4365

4466
Tags = '.ps1xml', 'Format','Output','Types', 'Colorized'
4567
ReleaseNotes = @'
68+
## 1.9.6:
69+
* Added Format-JSON (Fixes #86)
70+
* Improved GitHub Action (Fixes #85)
71+
---
72+
4673
## 1.9.5:
4774
* Write-FormatViewExpression:
4875
* Adding -DoubleUnderline

EZOut.psm1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ Set-Alias ConvertTo-TypePropertySet ConvertTo-PropertySet
6161
. $psScriptRoot\Get-EZOutExtension.ps1
6262

6363
. $psScriptRoot\Format-Object.ps1
64+
65+
. $psScriptRoot\Format-Hashtable.ps1
6466
. $psScriptRoot\Format-Heatmap.ps1
65-
. $psScriptRoot\Format-RichText.ps1
67+
. $psScriptRoot\Format-JSON.ps1
6668
. $psScriptRoot\Format-Markdown.ps1
69+
. $psScriptRoot\Format-RichText.ps1
6770
. $psScriptRoot\Format-YAML.ps1
68-
. $psScriptRoot\Format-Hashtable.ps1
71+
6972

7073
$myInvocation.MyCommand.ScriptBlock.Module.pstypenames.insert(0,'EZOut.RichModuleInfo')
7174

Format-JSON.ps.ps1

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
function Format-JSON
2+
{
3+
<#
4+
.SYNOPSIS
5+
Formats objects as JSON
6+
.DESCRIPTION
7+
Formats an object as JSON.
8+
9+
This is a light wrapper around ConvertTo-Json with a few key differences:
10+
11+
1. It defaults -Depth to 100 (the maximum)
12+
2. It will not encode strings that look like JSON. Format-JSON can have raw JSON as input without it being converted.
13+
3. It allows you to force single values into a list with -AsList
14+
4. If there is nothing to convert, it outputs an empty JSON object.
15+
16+
Using Format-JSON inside of an EZOut command will pack Format-JSON into your exported .ps1xml.
17+
.EXAMPLE
18+
Format-JSON -InputObject @("a", "b", "c")
19+
.EXAMPLE
20+
[Ordered]@{a="b";c="d";e=[Ordered]@{f=@('g')}} | Format-JSON
21+
#>
22+
[inherit("ConvertTo-JSON",Dynamic,Abstract)]
23+
[Management.Automation.Cmdlet("Format", "Object")]
24+
param(
25+
# If set, will always Format-JSON as a list.
26+
[switch]
27+
$AsList
28+
)
29+
30+
begin {
31+
$accumulateInput = [Collections.Queue]::new()
32+
$rawJSON = [Collections.Queue]::new()
33+
}
34+
process {
35+
if ($_ -is [string] -and $_ -match '^\s{0,}[\[\{"]' -and $_ -match '[\]\}"]\s{0,}$') {
36+
$rawJSON.Enqueue($_)
37+
} else {
38+
$accumulateInput.Enqueue($_)
39+
}
40+
}
41+
end {
42+
$joiner = "," + $(
43+
if (-not $PSBoundParameters["Compress"]) {
44+
[System.Environment]::NewLine
45+
}
46+
)
47+
48+
$null = $PSBoundParameters.Remove('AsList')
49+
50+
if ($accumulateInput.Count) {
51+
if (-not $PSBoundParameters["Depth"]) {
52+
$PSBoundParameters["Depth"] = 100
53+
}
54+
$PSBoundParameters['InputObject'] =
55+
if ($accumulateInput.Count -eq 1) {
56+
$accumulateInput[0]
57+
} else {
58+
$accumulateInput.ToArray()
59+
}
60+
61+
$rawJSON.Enqueue((& $baseCommand @PSBoundParameters))
62+
}
63+
64+
if ($rawJSON.Count -gt 1 -or $AsList) {
65+
"[$($rawJSON.ToArray() -join $joiner)]"
66+
} elseif ($rawJSON.Count -eq 1) {
67+
$rawJSON[0]
68+
} else {
69+
"{}"
70+
}
71+
}
72+
}

Format-JSON.ps1

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
function Format-JSON {
2+
<#
3+
.SYNOPSIS
4+
Formats objects as JSON
5+
.DESCRIPTION
6+
Formats an object as JSON.
7+
This is a light wrapper around ConvertTo-Json with a few key differences:
8+
1. It defaults -Depth to 100 (the maximum)
9+
2. It will not encode strings that look like JSON. Format-JSON can have raw JSON as input without it being converted.
10+
3. It allows you to force single values into a list with -AsList
11+
4. If there is nothing to convert, it outputs an empty JSON object.
12+
Using Format-JSON inside of an EZOut command will pack Format-JSON into your exported .ps1xml.
13+
.EXAMPLE
14+
Format-JSON -InputObject @("a", "b", "c")
15+
.EXAMPLE
16+
[Ordered]@{a="b";c="d";e=[Ordered]@{f=@('g')}} | Format-JSON
17+
18+
#>
19+
20+
[Management.Automation.Cmdlet("Format", "Object")]
21+
[CmdletBinding(HelpUri='https://go.microsoft.com/fwlink/?LinkID=2096925', RemotingCapability='None')]
22+
param(
23+
# If set, will always Format-JSON as a list.
24+
[switch]
25+
$AsList
26+
)
27+
dynamicParam {
28+
$baseCommand =
29+
if (-not $script:ConvertToJSON) {
30+
$script:ConvertToJSON =
31+
$executionContext.SessionState.InvokeCommand.GetCommand('ConvertTo-JSON','Cmdlet')
32+
$script:ConvertToJSON
33+
} else {
34+
$script:ConvertToJSON
35+
}
36+
$IncludeParameter = @()
37+
$ExcludeParameter = @()
38+
$DynamicParameters = [Management.Automation.RuntimeDefinedParameterDictionary]::new()
39+
:nextInputParameter foreach ($paramName in ([Management.Automation.CommandMetaData]$baseCommand).Parameters.Keys) {
40+
if ($ExcludeParameter) {
41+
foreach ($exclude in $ExcludeParameter) {
42+
if ($paramName -like $exclude) { continue nextInputParameter}
43+
}
44+
}
45+
if ($IncludeParameter) {
46+
$shouldInclude =
47+
foreach ($include in $IncludeParameter) {
48+
if ($paramName -like $include) { $true;break}
49+
}
50+
if (-not $shouldInclude) { continue nextInputParameter }
51+
}
52+
53+
$DynamicParameters.Add($paramName, [Management.Automation.RuntimeDefinedParameter]::new(
54+
$baseCommand.Parameters[$paramName].Name,
55+
$baseCommand.Parameters[$paramName].ParameterType,
56+
$baseCommand.Parameters[$paramName].Attributes
57+
))
58+
}
59+
$DynamicParameters
60+
}
61+
begin {
62+
$accumulateInput = [Collections.Queue]::new()
63+
$rawJSON = [Collections.Queue]::new()
64+
65+
}
66+
process {
67+
if ($_ -is [string] -and $_ -match '^\s{0,}[\[\{"]' -and $_ -match '[\]\}"]\s{0,}$') {
68+
$rawJSON.Enqueue($_)
69+
} else {
70+
$accumulateInput.Enqueue($_)
71+
}
72+
73+
}
74+
end {
75+
$joiner = "," + $(
76+
if (-not $PSBoundParameters["Compress"]) {
77+
[System.Environment]::NewLine
78+
}
79+
)
80+
$null = $PSBoundParameters.Remove('AsList')
81+
if ($accumulateInput.Count) {
82+
if (-not $PSBoundParameters["Depth"]) {
83+
$PSBoundParameters["Depth"] = 100
84+
}
85+
$PSBoundParameters['InputObject'] =
86+
if ($accumulateInput.Count -eq 1) {
87+
$accumulateInput[0]
88+
} else {
89+
$accumulateInput.ToArray()
90+
}
91+
92+
$rawJSON.Enqueue((& $baseCommand @PSBoundParameters))
93+
}
94+
if ($rawJSON.Count -gt 1 -or $AsList) {
95+
"[$($rawJSON.ToArray() -join $joiner)]"
96+
} elseif ($rawJSON.Count -eq 1) {
97+
$rawJSON[0]
98+
} else {
99+
"{}"
100+
}
101+
102+
}
103+
}
104+

0 commit comments

Comments
 (0)