~cloudbaseit/charms/win2012r2/drupal-iis/trunk

« back to all changes in this revision

Viewing changes to lib/Modules/powershell-yaml/powershell-yaml.psm1

  • Committer: Ionut Balutoiu
  • Date: 2016-12-23 12:55:39 UTC
  • Revision ID: ibalutoiu@cloudbasesolutions.com-20161223125539-j795kbynab3uflha
Added charm code

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2016 Cloudbase Solutions Srl
 
2
#
 
3
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
4
#    not use this file except in compliance with the License. You may obtain
 
5
#    a copy of the License at
 
6
#
 
7
#         http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
#    Unless required by applicable law or agreed to in writing, software
 
10
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
11
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
12
#    License for the specific language governing permissions and limitations
 
13
#    under the License.
 
14
#
 
15
 
 
16
function Get-YamlDocuments {
 
17
    [CmdletBinding()]
 
18
    Param(
 
19
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 
20
        [string]$Yaml
 
21
    )
 
22
    PROCESS {
 
23
        $stringReader = new-object System.IO.StringReader($Yaml)
 
24
        $yamlStream = New-Object "YamlDotNet.RepresentationModel.YamlStream"
 
25
        $yamlStream.Load([System.IO.TextReader] $stringReader)
 
26
        $stringReader.Close()
 
27
        return $yamlStream
 
28
    }
 
29
}
 
30
 
 
31
function Convert-ValueToProperType {
 
32
    [CmdletBinding()]
 
33
    Param(
 
34
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
 
35
        [System.Object]$Value
 
36
    )
 
37
    PROCESS {
 
38
        if (!($Value -is [string])) {
 
39
            return $Value
 
40
        }
 
41
        $types = @([int], [long], [double], [boolean], [datetime])
 
42
        foreach($i in $types){
 
43
            try {
 
44
                return $i::Parse($Value)
 
45
            } catch {
 
46
                continue
 
47
            }
 
48
        }
 
49
        return $Value
 
50
    }
 
51
}
 
52
 
 
53
function Convert-YamlMappingToHashtable {
 
54
    [CmdletBinding()]
 
55
    Param(
 
56
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 
57
        [YamlDotNet.RepresentationModel.YamlMappingNode]$Node
 
58
    )
 
59
    PROCESS {
 
60
        $ret = @{}
 
61
        foreach($i in $Node.Children.Keys) {
 
62
            $ret[$i.Value] = Convert-YamlDocumentToPSObject $Node.Children[$i]
 
63
        }
 
64
        return $ret
 
65
    }
 
66
}
 
67
 
 
68
function Convert-YamlSequenceToArray {
 
69
    [CmdletBinding()]
 
70
    Param(
 
71
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 
72
        [YamlDotNet.RepresentationModel.YamlSequenceNode]$Node
 
73
    )
 
74
    PROCESS {
 
75
        $ret = [System.Collections.Generic.List[object]](New-Object "System.Collections.Generic.List[object]")
 
76
        foreach($i in $Node.Children){
 
77
            $ret.Add((Convert-YamlDocumentToPSObject $i))
 
78
        }
 
79
        return $ret
 
80
    }
 
81
}
 
82
 
 
83
function Convert-YamlDocumentToPSObject {
 
84
    [CmdletBinding()]
 
85
    Param(
 
86
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
 
87
        [System.Object]$Node
 
88
    )
 
89
    PROCESS {
 
90
        switch($Node.GetType().FullName){
 
91
            "YamlDotNet.RepresentationModel.YamlMappingNode"{
 
92
                return Convert-YamlMappingToHashtable $Node
 
93
            }
 
94
            "YamlDotNet.RepresentationModel.YamlSequenceNode" {
 
95
                return Convert-YamlSequenceToArray $Node
 
96
            }
 
97
            "YamlDotNet.RepresentationModel.YamlScalarNode" {
 
98
                return (Convert-ValueToProperType $Node.Value)
 
99
            }
 
100
        }
 
101
    }
 
102
}
 
103
 
 
104
function Convert-HashtableToDictionary {
 
105
    Param(
 
106
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
 
107
        [hashtable]$Data
 
108
    )
 
109
    foreach($i in $($data.Keys)) {
 
110
        $Data[$i] = Convert-PSObjectToGenericObject $Data[$i]
 
111
    }
 
112
    return $Data
 
113
}
 
114
 
 
115
function Convert-ListToGenericList {
 
116
    Param(
 
117
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
 
118
        [array]$Data
 
119
    )
 
120
    for($i=0; $i -lt $Data.Count; $i++) {
 
121
        $Data[$i] = Convert-PSObjectToGenericObject $Data[$i]
 
122
    }
 
123
    return $Data
 
124
}
 
125
 
 
126
function Convert-PSCustomObjectToDictionary {
 
127
    Param(
 
128
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
 
129
        [PSCustomObject]$Data
 
130
    )
 
131
    $ret = [System.Collections.Generic.Dictionary[string,object]](New-Object 'System.Collections.Generic.Dictionary[string,object]')
 
132
    foreach ($i in $Data.psobject.properties) {
 
133
        $ret[$i.Name] = Convert-PSObjectToGenericObject $i.Value
 
134
    }
 
135
    return $ret
 
136
}
 
137
 
 
138
function Convert-PSObjectToGenericObject {
 
139
    Param(
 
140
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
 
141
        [System.Object]$Data
 
142
    )
 
143
    # explicitly cast object to its type. Without this, it gets wrapped inside a powershell object
 
144
    # which causes YamlDotNet to fail
 
145
    $data = $data -as $data.GetType().FullName
 
146
    switch($data.GetType()) {
 
147
        ($_.FullName -eq "System.Management.Automation.PSCustomObject") {
 
148
            return Convert-PSCustomObjectToDictionary
 
149
        }
 
150
        default {
 
151
            if (([System.Collections.IDictionary].IsAssignableFrom($_))){
 
152
                return Convert-HashtableToDictionary $data
 
153
            } elseif (([System.Collections.IList].IsAssignableFrom($_))) {
 
154
                return Convert-ListToGenericList $data
 
155
            }
 
156
            return $data
 
157
        }
 
158
    }
 
159
}
 
160
 
 
161
function ConvertFrom-Yaml {
 
162
    [CmdletBinding()]
 
163
    Param(
 
164
        [Parameter(Mandatory=$false, ValueFromPipeline=$true)]
 
165
        [string]$Yaml,
 
166
        [switch]$AllDocuments=$false
 
167
    )
 
168
    PROCESS {
 
169
        if(!$Yaml){
 
170
            return
 
171
        }
 
172
        $documents = Get-YamlDocuments -Yaml $Yaml
 
173
        if (!$documents.Count) {
 
174
            return
 
175
        }
 
176
        if($documents.Count -eq 1){
 
177
            return Convert-YamlDocumentToPSObject $documents[0].RootNode
 
178
        }
 
179
        if(!$AllDocuments) {
 
180
            return Convert-YamlDocumentToPSObject $documents[0].RootNode
 
181
        }
 
182
        $ret = @()
 
183
        foreach($i in $documents) {
 
184
            $ret += Convert-YamlDocumentToPSObject $i.RootNode
 
185
        }
 
186
        return $ret
 
187
    }
 
188
}
 
189
 
 
190
function ConvertTo-Yaml {
 
191
    [CmdletBinding()]
 
192
    Param(
 
193
        [Parameter(Mandatory=$false,ValueFromPipeline=$true)]
 
194
        [System.Object]$Data,
 
195
        [Parameter(Mandatory=$false)]
 
196
        [string]$OutFile,
 
197
        [switch]$Force=$false
 
198
    )
 
199
    BEGIN {
 
200
        $d = [System.Collections.Generic.List[object]](New-Object "System.Collections.Generic.List[object]")
 
201
    }
 
202
    PROCESS {
 
203
        $d.Add($data)
 
204
    }
 
205
    END {
 
206
        if(!$d){
 
207
            return
 
208
        }
 
209
        $norm = Convert-PSObjectToGenericObject $d
 
210
        if($OutFile) {
 
211
            $parent = Split-Path $OutFile
 
212
            if(!(Test-Path $parent)) {
 
213
                Throw "Parent folder for specified path does not exist"
 
214
            }
 
215
            if((Test-Path $OutFile) -and !$Force){
 
216
                Throw "Target file already exists. Use -Force to overwrite."
 
217
            }
 
218
            $wrt = New-Object "System.IO.StreamWriter" $OutFile
 
219
        } else {
 
220
            $wrt = New-Object "System.IO.StringWriter"
 
221
        }
 
222
        try {
 
223
            $serializer = New-Object "YamlDotNet.Serialization.Serializer" 0
 
224
            $serializer.Serialize($wrt, $norm)
 
225
        } finally {
 
226
            $wrt.Close()
 
227
        }
 
228
        if($OutFile){
 
229
            return
 
230
        }else {
 
231
            return $wrt.ToString()
 
232
        }
 
233
    }
 
234
}
 
235
 
 
236
Export-ModuleMember -Function * -Alias *