public/functions.ps1

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
$fmgBooleanYes = @("true", "yes", "on", "Y", "y")
$fmgBooleanNo = @("false", "no", "off", "N", "n")


function New-YamlStream() {
    [CmdletBinding()]
    Param(
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [String] $InputObject
    )

    $sr = New-Object System.IO.StringReader $InputObject 
    $ymlStream = New-Object YamlDotNet.RepresentationModel.YamlStream 
    $ymlStream.Load($sr)
    $sr.Dispose()

    return $ymlStream;
}

function ConvertFrom-YamlMappingNode() {
    Param(
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [YamlDotNet.RepresentationModel.YamlMappingNode] $InputObject
    )

    if($null -eq $InputObject) {
        return $null;
    }

    $section = New-Object PsCustomObject
    foreach($key in $InputObject.Children.Keys) {
        $value = $InputObject.Children[$key]
        $value = ConvertFrom-YamlNode $value
        $section | Add-Member NoteProperty -Name $key -Value $value
    }

    return [PsCustomObject]$section;
}

function ConvertFrom-YamlSequenceNode() {
    Param(
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [YamlDotNet.RepresentationModel.YamlSequenceNode] $InputObject
    )

    
    if($null -eq $InputObject) {
        return $null;
    }

    $array = @();
    foreach($child in $InputObject.Children) {
        $array += (ConvertFrom-YamlNode $child)
    }

    return ,$array
}

function ConvertFrom-YamlScalarNode() {
    Param(
        [Parameter(Position = 0, ValueFromPipeline = $True)]
        $InputObject 
    )

    if($null -eq $InputObject) {
        return $Null;
    }

    if($InputObject -is [YamlDotNet.RepresentationModel.YamlScalarNode]) {
        $value = $InputObject.Value;

        if($InputObject.Tag) {
            Write-Host ($InputObject.Tag)
            switch($InputObject.Tag) {
                'tag:yaml.org,2002:str' { return $value.ToString() }
                'tag:yaml.org,2002:int' { return [int]$value}
                'tag:yaml.org,2002:float' { return [double]$value }
                'tag:yaml.org,2002:timestamp' { return [datetime]$value}
                'tag:yaml.org,2002:binary' { return [System.Convert]::FromBase64String($Value)}
                'tag:yaml.org,2002:null' { return $Null }
            }
        }

        # TODO: speed up type testing and parsing.
        if([String]::IsNullOrWhiteSpace($value)) {
            return $value;
        }

        if($fmgBooleanYes.IndexOf($value) -gt -1) {
            return $true;
        }

        if($fmgBooleanNo.IndexOf($value) -gt -1) {
            return $false;
        }

        [System.Int32]$integer = 0; 
        if([Int32]::TryParse($value, [ref] $integer)) {
            return $integer;
        }

        [System.Int64]$long = 0; 
        if([Int64]::TryParse($value, [ref] $long)) {
            return $long;
        }

        [System.Double] $double = $null;
        if([Double]::TryParse($value, [ref] $double)) {
            return $double;
        }

        [System.DateTime] $datetime = [DateTime]::MinValue;
        if([datetime]::TryParse($value, [ref] $datetime)) {
            return $datetime;
        }

        return $value;
    }

    return $InputObject.ToString()
}

function ConvertFrom-YamlNode() {
    Param(
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [Object] $InputObject
    )

    $typeName =$InputObject.GetType().FullName
    switch($typeName) {
        "YamlDotNet.RepresentationModel.YamlSequenceNode" { 
            return ConvertFrom-YamlSequenceNode $InputObject  
        }
        "YamlDotNet.RepresentationModel.YamlMappingNode" { 
            return ConvertFrom-YamlMappingNode $InputObject
        }
        "YamlDotNet.RepresentationModel.YamlScalarNode" { 
            return ConvertFrom-YamlScalarNode $InputObject
        }

    }
    return  $converter.Invoke($InputObject)
}

function ConvertTo-YamlSerializeable() {
    Param(
        [Object] $InputObject
    )

    if($InputObject -is [System.Collections.IDictionary]) {
        return ConvertTo-YamlMappingNodeDictionary $InputObject
    }

    if($InputObject -is [System.Management.Automation.PsObject]) {
        return ConvertTo-YamlMappingNodeDictionary $InputObject
    }

    if($InputObject -is [System.Management.Automation.PSCustomObject]) {
        return ConvertTo-YamlMappingNodeDictionary $InputObject
    }

    if($InputObject -is [System.Collections.IList]) {
        return ConvertTo-YamlSequence 
    }

    return $InputObject;
}

function ConvertTo-YamlMappingNodeDictionary() {
    Param(
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [Object] $InputObject
    )

    $dictionary = New-Object 'System.Collections.Generic.Dictionary[string,object]'

   

    if($InputObject -is [System.Collections.IDictionary]) {
        foreach($key in $InputObject.Keys) {
            $dictionary[$key.ToString()] = $InputObject[$key];
        }

        return $dictionary;
    }

    if($InputObject -is [System.Management.Automation.PsCustomObject]) {
        $InputObject | Get-Member -MemberType NoteProperty | ForEach-Object {
            $name = $_.Name 
            $value = $InputObject.$name 
            $dictionary[$name] = $value
        }
    }

    if($InputObject -is [System.Management.Automation.PsObject]) {
        $InputObject.PsObject.Properties | ForEach-Object {
            $key = $_.Name
            $value = $InputObject.$Name

            $dictionary[$key] = $value;
        }
        return $dictionary;
    }

    return $dictionary;
}

function ConvertTo-YamlSequenceArray() {
    Param(
        [Parameter(Position = 0, ValueFromPipeline = $true)]
        [Object] $InputObject
    )

    $array = @();
    foreach($child in $InputObject) {
        $array += ConvertTo-YamlSerializeable $child 
    }
    
    return ,$array
}

function ConvertFrom-GzYaml() {
<#
.SYNOPSIS
    Converts a YAML string to a PsCustomObject
.DESCRIPTION
    Converts a YAML string to a PsCustomObject
.EXAMPLE
    PS C:\> $obj = yml | ConvertFrom-GzYaml
     
.INPUTS
    String
.OUTPUTS
    PsCustomObject
.NOTES
    This function leverages YamlDotNet
#>

    Param(
        [Parameter(Position = 0, Mandatory=$false, ValueFromPipeline=$true)]
        [string] $InputObject
    )

    if([string]::IsNullOrWhiteSpace($InputObject)) {
        return $Null;
    }

    $ymlStream = $InputObject | New-YamlStream 
    if($ymlStream.Count -lt 1) {
        return $null;
    }

    $documents = @();
    foreach($document in $ymlStream) {
        $documents += (ConvertFrom-YamlNode $document.RootNode)
    }

    return $documents
}

function ConvertTo-GzYaml() {
<#
.SYNOPSIS
    Converts the object to a YAML string
.DESCRIPTION
    Converts a PsCustomObject, PsObject, IDictionary, or IList object
    to a YAML string.
.EXAMPLE
    PS C:\> $yml = $obj | ConvertTo-GzYaml
     
.INPUTS
    Object
.OUTPUTS
    String
.NOTES
    This function leverages YamlDotNet
#>

    Param(
        [Parameter(Position = 0, Mandatory =$true, ValueFromPipeline = $true)]
        [Object] $InputObject
    )

    if($null -eq $InputObject) {
        return $Null;
    }
    $sw = New-Object System.IO.StringWriter 
    $serializer = New-Object YamlDotNet.Serialization.Serializer
    Try {
        $coerced = ConvertTo-YamlSerializeable $InputObject      
        $serializer.Serialize($sw, $coerced)
        return $sw.ToString()
    } finally {
        $sw.Dispose()
    }
}

$gzAliases = @{
    "ConvertTo-Yaml" = "ConvertTo-GzYaml"
    "ConvertFrom-Yaml" = "ConvertFrom-GzYaml"
};

function Remove-GzYamlAlias() {
    <#
    .SYNOPSIS
        Removes the aliases that map to Gz prefixed commands for
        the Gz-Yaml module.
 
    .DESCRIPTION
        Removes the aliases that map to Gz prefixed commands.
        For example, removes the alias ConvertTo-Yaml that points
        to ConvertTo-GzYaml.
    .EXAMPLE
        PS C:\> Remove-GzYamlAlias
    .INPUTS
        None
    .OUTPUTS
        None
    .NOTES
        Aliases are to remove the Gz Prefix
    #>


    foreach($key in $gzAliases.Keys) {
        if($null -ne (Get-Alias $key -EA SilentlyContinue)) {
            Remove-Item alias:\$key 
        }
    }
}

function Add-GzYamlAlias() {
 <#
    .SYNOPSIS
        Adds the aliases that map to Gz prefixed commands for
        the Gz-Yaml module.
 
    .DESCRIPTION
        Adds the aliases that map to Gz prefixed commands.
        For example, adds the alias ConvertTo-Yaml that points
        to ConvertTo-GzYaml.
    .EXAMPLE
        PS C:\> Add-GzYamlAlias
    .INPUTS
        Inputs (if any)
    .OUTPUTS
        Output (if any)
    .NOTES
        Aliases are to remove the Gz Prefix
    #>

    foreach($key in $gzAliases.Keys) {
        if($null -eq (Get-Alias $key -EA SilentlyContinue)) {
            Set-Alias -Name ($key) -Value $gzAliases[$key] -Scope Global 
        }
    }
}