-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitPub.format.ps1xml
257 lines (230 loc) · 9.83 KB
/
GitPub.format.ps1xml
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
<?xml version="1.0" encoding="utf-16"?>
<!-- Generated with EZOut 1.9.7: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<ViewDefinitions>
<View>
<Name>YamlHeader</Name>
<ViewSelectedBy>
<TypeName>YamlHeader</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>$moduleName = 'GitPub'
do {
$lm = Get-Module -Name $moduleName -ErrorAction Ignore
if (-not $lm) { continue }
if ($lm.FormatPartsLoaded) { break }
$wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) {
foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") {
$ParentNode = $partNodeName.Node.ParentNode
"$($ParentNode.Name)={
$($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}"
}
}) -join [Environment]::NewLine
New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) |
Import-Module -Global
$onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'")
if (-not $lm.OnRemove) {
$lm.OnRemove = $onRemove
} else {
$lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove)
}
$lm | Add-Member NoteProperty FormatPartsLoaded $true -Force
} while ($false)
@("---"
& ${GitPub_Format-YAML} -InputObject $_
"---") -join [Environment]::NewLine
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
</ViewDefinitions>
<Controls>
<Control>
<Name>${GitPub_Format-YAML}</Name>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
<#
.SYNOPSIS
Formats objects as YAML
.DESCRIPTION
Formats an object as YAML.
.EXAMPLE
Format-Yaml -InputObject @("a", "b", "c")
.EXAMPLE
@{a="b";c="d";e=@{f=@('g')}} | Format-Yaml
#>
[Management.Automation.Cmdlet("Format","Object")]
[ValidateScript({return $true})]
param(
# The InputObject.
[Parameter(ValueFromPipeline)]
[PSObject]
$InputObject,
# If set, will make a YAML header by adding a YAML Document tag above and below output.
[Alias('YAMLDocument')]
[switch]
$YamlHeader,
[int]
$Indent = 0,
# The maximum depth of objects to include.
# Beyond this depth, an empty string will be returned.
[int]
$Depth
)
begin {
$toYaml = {
param(
[Parameter(ValueFromPipeline,Position=0)]$Object,
[Object]$Parent,
[Object]$GrandParent,
[int]$Indent = 0)
begin { $n = 0; $mySelf = $myInvocation.MyCommand.ScriptBlock }
process {
$n++
if ($Object -eq $null) { return }
if ($depth) {
$myDepth = $indent / 2
if ($myDepth -gt $depth) {
return ''
}
}
if ($Parent -and $Parent -is [Collections.IList]) {
if ($Parent.IndexOf($Object) -gt 0) { ' ' * $Indent }
'- '
}
#region Primitives
if ( $Object -is [string] ) { # If it's a string
if ($object -match '\n') { # see if it's a multline string.
"|" # If it is, emit the multiline indicator
$indent +=2
foreach ($l in $object -split '(?>\r\n|\n)') { # and emit each line indented
[Environment]::NewLine
' ' * $indent
$l
}
$indent -=2
} elseif ("$object".Contains('*')) {
"'$($Object -replace "'","''")'"
} else {
$object
}
if ($Parent -is [Collections.IList]) { # If the parent object was a list
[Environment]::NewLine # emit a newline.
}
return # Once the string has been emitted, return.
}
if ( $Object.GetType().IsPrimitive ) { # If it is a primitive type
"$Object".ToLower() # Emit it in lowercase.
if ($Parent -is [Collections.IList]) {
[Environment]::NewLine
}
return
}
#endregion Primitives
#region KVP
if ( $Object -is [Collections.DictionaryEntry] -or $object -is [Management.Automation.PSPropertyInfo]) {
if ($Parent -isnot [Collections.IList] -and
($GrandParent -isnot [Collections.IList] -or $n -gt 1)) {
[Environment]::NewLine + (" " * $Indent)
}
if ($object.Key -and $Object.Key -is [string]) {
$Object.Key +": "
} elseif ($object.Name -and $object.Name -is [string]) {
$Object.Name +": "
}
}
if ( $Object -is [Collections.DictionaryEntry] -or $Object -is [Management.Automation.PSPropertyInfo]) {
& $mySelf -Object $Object.Value -Parent $Object -GrandParent $parent -Indent $Indent
return
}
#endregion KVP
#region Nested
if ($parent -and ($Object -is [Collections.IDictionary] -or $Object -is [PSObject])) {
$Indent += 2
}
elseif ($object -is [Collections.IList]) {
$allPrimitive = 1
foreach ($Obj in $Object) {
$allPrimitive = $allPrimitive -band (
$Obj -is [string] -or
$obj.GetType().IsPrimitive
)
}
if ($parent -and -not $allPrimitive) {
$Indent += 2
}
}
if ( $Object -is [Collections.IDictionary] ) {
$Object.GetEnumerator() |
& $mySelf -Parent $Object -GrandParent $Parent -Indent $Indent
} elseif ($Object -is [Collections.IList]) {
[Environment]::NewLine + (' ' * $Indent)
$Object |
& $mySelf -Parent $Object -GrandParent $Parent -Indent $Indent
}
elseif ($object -is [enum]) {
$object.ToString()
}
elseif ($Object.PSObject.Properties) {
$Object.psobject.properties |
& $mySelf -Parent $Object -GrandParent $Parent -Indent $Indent
}
if ($Object -is [Collections.IDictionary] -or $Object -is [PSCustomObject] -or $Object -is [Collections.IList]) {
if ($Parent -is [Collections.IList]) { [Environment]::NewLine }
$Indent -= 2;
}
#endregion Nested
}
}
function IndentString([string]$String,[int]$Indent) {
@(foreach ($line in @($String -split '(?>\r\n|\n)')) {
(' ' * $indent) + $line
}) -join [Environment]::NewLine
}
$inputWasNotPiped = $PSBoundParameters.InputObject -as [bool]
$allInputObjects = @()
}
process {
if ($inputWasNotPiped) {
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine }) + (
(& $toYaml -object $inputObject) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
} else {
$allInputObjects += $inputObject
}
}
end {
if (-not $allInputObjects) { return }
if ($allInputObjects.Length -eq 1) {
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
(& $toYaml -object $inputObject) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
} else {
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
(& $toYaml -object $allInputObjects) -join '' -replace
"$([Environment]::NewLine * 2)", [Environment]::NewLine
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
}
}
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</Control>
</Controls>
</Configuration>