-
Notifications
You must be signed in to change notification settings - Fork 2
/
NewXmlDocument.ps1
326 lines (256 loc) · 10.1 KB
/
NewXmlDocument.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
<#PSScriptInfo
.VERSION 0.1.0
.GUID 1bb4d587-0d89-4ad0-91e1-38bf9ef1cc84
.AUTHOR Patrick Meinecke
.COMPANYNAME Community
.COPYRIGHT (c) 2017 Patrick Meinecke. All rights reserved.
.TAGS XML, DSL, Document
.LICENSEURI https://github.com/SeeminglyScience/NewXmlDocument/blob/master/LICENSE
.PROJECTURI https://github.com/SeeminglyScience/NewXmlDocument
.ICONURI
.EXTERNALMODULEDEPENDENCIES
.REQUIREDSCRIPTS
.EXTERNALSCRIPTDEPENDENCIES
.RELEASENOTES
#>
#requires -Version 5.1
<#
.SYNOPSIS
Easy DSL for generating XML documents.
.DESCRIPTION
The NewXmlDocument script creates a new XML document using a dynamic DSL (Domain Specific Language).
.PARAMETER ScriptBlock
Specifies a script block that contains XML elements as command names. Any command in the script block
that does not have a command that is loaded into the current session will be treated as an XML element.
To create an element, use the element name as the command name, and pass a script block as a
argument.
For example, this command would create the XML "<Description>description here</Description>".
Description { 'description here' }
You can use any resolvable command, variable, etc to return the value assigned to the element.
To create an attribute, use the same syntax you would in a hashtable definition.
For example, this command
Author {
Name = "Jim"
}
Would create the XML <Author Name="Jim" />
The syntax for these can be combined and nested to form a full XML document (see examples for more
details)
.PARAMETER Namespace
Specifies the namespace for the XML document. This is the only valid way to define the namespace,
if you try to define it with a "xmlns" attribute an exception will be thrown.
.PARAMETER FilePath
Specifies the file path to save the XML document to.
.PARAMETER PassThru
If specified with the "FilePath" parameter, the XElement will be returned to the pipeline as well
as written to XML. This parameter has no effect if "FilePath" is not present.
.INPUTS
None
This script does not accept input from the pipeline
.OUTPUTS
System.Xml.Linq.XElement
The completed XML element will be returned to the pipeline.
.EXAMPLE
PS C:\> $xml = NewXmlDocument.ps1 -FilePath '.\Authors.xml' {
>> Authors {
>> Author {
>> Name = 'John'
>> Age = 30
>> }
>> Author {
>> Name = 'Tim'
>> Age = 10
>> 'Writes about horror'
>> }
>> }
>>}
Produces an XML file with the following content:
<?xml version="1.0" encoding="utf-8"?>
<Authors>
<Author Name="John" Age="30" />
<Author Name="Tim" Age="10">Writes about horror</Author>
</Authors>
.EXAMPLE
PS C:\> $plaster = NewXmlDocument.ps1 -Namespace 'http://www.microsoft.com/schemas/PowerShell/Plaster/v1' {
>> plasterManifest {
>> schemaVersion = '1.0'
>> metadata {
>> name { 'TestManifest' }
>> id { (New-Guid).Guid }
>> version { '0.1.0' }
>> title { 'My Plaster Manifest' }
>> description { 'A plaster manifest created to test this function.' }
>> }
>> parameters {
>> parameter {
>> name = 'ModuleName'
>> type = 'Text'
>> prompt = 'Enter the name of the module'
>> }
>> }
>> content {
>> file {
>> source = '_module.psm1'
>> destination = '${PLASTER_PARAM_ModuleName}.psd1'
>> }
>> requireModule {
>> name = 'Pester'
>> minimumVersion = '3.4.0'
>> message = 'Without Pester, you will not be able to run tests!'
>> }
>> }
>> }
>>}
PS C:\> $xml.Save('.\plasterManifest.xml')
Produces an working plaster manifest XML document with the following content:
<?xml version="1.0" encoding="utf-8"?>
<plasterManifest schemaVersion="1.0" xmlns="http://www.microsoft.com/schemas/PowerShell/Plaster/v1">
<metadata>
<name>TestManifest</name>
<id>3598072f-578d-42f7-b576-101cadc5efce</id>
<version>0.1.0</version>
<title>My Plaster Manifest</title>
<description>A plaster manifest created to test this function.</description>
</metadata>
<parameters>
<parameter name="ModuleName" type="Text" prompt="Enter the name of the module" />
</parameters>
<content>
<file source="_module.psm1" destination="${PLASTER_PARAM_ModuleName}.psd1" />
<requireModule name="Pester" minimumVersion="3.4.0" message="Without Pester, you will not be able to run tests!" />
</content>
</plasterManifest>
.NOTES
The following changes are made to command lookup while the DSL is running:
- Module Autoloading is set to ModuleQualified
- Aliases are disabled
- The "prompt" function is disabled
- Resolving "Get-*" commands with only the noun is disabled
- All command lookup failures are routed to a function in this script
#>
using assembly System.Xml.Linq
[CmdletBinding(PositionalBinding = $false)]
[OutputType('System.Xml.Linq.XElement')]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseOutputTypeCorrectly', '')]
param(
[Parameter(Mandatory, Position = 0)]
[ValidateNotNullOrEmpty()]
[scriptblock]
$ScriptBlock,
[ValidateNotNullOrEmpty()]
[Alias('Path')]
[string]
$FilePath,
$Namespace
)
begin {
#region New-XmlElement
function New-XmlElement {
[CmdletBinding(DefaultParameterSetName='Body')]
[OutputType('System.Xml.Linq.XElement', ParameterSetName='Element')]
[OutputType('System.Xml.Linq.XAttribute', ParameterSetName='Attribute')]
param(
[Parameter(Position = 0, Mandatory, ParameterSetName='Element')]
[scriptblock]
$Body,
[Parameter(Position = 0, Mandatory, ParameterSetName='Attribute')]
[ValidateSet('=')]
[string]
$Operator,
[Parameter(Position = 1, Mandatory, ParameterSetName='Attribute')]
[string]
$Text
)
end {
$elementName = [System.Xml.Linq.XName]($MyInvocation.InvocationName)
if ($Namespace) {
$elementName = [System.Xml.Linq.XNamespace]$Namespace + $elementName
}
switch ($PSCmdlet.ParameterSetName) {
Attribute {
[System.Xml.Linq.XAttribute]::new($MyInvocation.InvocationName, $Text)
}
Element {
if ($Body -and ($output = . $Body)) {
$element = [System.Xml.Linq.XElement]::new($elementName)
foreach ($item in $output) {
$element.Add($item)
}
$element
} else {
[System.Xml.Linq.XElement]::new($elementName)
}
}
}
}
}
#endregion
}
end {
try {
# Disable module autoloading so command lookup doesn't take forever to fail.
$originalPreference = $PSModuleAutoLoadingPreference
$PSModuleAutoLoadingPreference = 'ModuleQualified'
# Add post lookup action to override some command lookup results
$originalPCLA = $ExecutionContext.SessionState.InvokeCommand.PostCommandLookupAction
$ExecutionContext.SessionState.InvokeCommand.PostCommandLookupAction = {
param(
[string]
$commandName,
[System.Management.Automation.CommandLookupEventArgs]
$lookupEventArgs
)
# Command lookup will prepend Get when looking up verbless commands.
$isPrependedGet = $lookupEventArgs.Command.Name -match 'Get-(\w+)' -and
$Matches[1] -eq $commandName
# Skip aliases
$isAlias ='Alias' -eq $lookupEventArgs.Command.CommandType
# Skip the commands that don't fit Noun-Verb format.
$isNonStandardFormat = $lookupEventArgs.Command.Name -notmatch '\w+-\w+'
if ($isPrependedGet -or $isAlias -or $isNonStandardFormat) {
$lookupEventArgs.Command = $ExecutionContext.SessionState.InvokeCommand.GetCommand(
'New-XmlElement',
'Function')
}
}
# Add command not found action that returns New-XmlElement for any command lookup failures
$originalCNFA = $ExecutionContext.SessionState.InvokeCommand.CommandNotFoundAction
$ExecutionContext.SessionState.InvokeCommand.CommandNotFoundAction = {
param(
[string]
$commandName,
[System.Management.Automation.CommandLookupEventArgs]
$lookupEventArgs
)
$lookupEventArgs.Command = $ExecutionContext.SessionState.InvokeCommand.GetCommand(
'New-XmlElement',
'Function')
}
# Any unknown commands in the script block will be treated as XML elements.
$xml = $ScriptBlock.Invoke()
if (-not $xml) { return }
if ($FilePath) {
try {
$resolved = $PSCmdlet.SessionState.Path.
GetUnresolvedProviderPathFromPSPath($FilePath)
$xml.Save($resolved)
} catch {
$exception = $PSItem -as [Exception]
if (-not $exception) { $exception = $PSItem.Exception }
if (-not $exception) { $exception = $PSItem.InnerException }
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
$exception,
'FailureSavingXml',
[System.Management.Automation.ErrorCategory]::WriteError,
$FilePath))
}
}
if (-not $FilePath -or $PassThru.IsPresent) {
$xml # yield
}
} finally {
$PSModuleAutoLoadingPreference = $originalPreference
$ExecutionContext.SessionState.InvokeCommand.CommandNotFoundAction = $originalCNFA
$ExecutionContext.SessionState.InvokeCommand.PostCommandLookupAction = $originalPCLA
}
}