-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathConvertFrom-JsonContentAction.ps1
More file actions
128 lines (110 loc) · 4.96 KB
/
ConvertFrom-JsonContentAction.ps1
File metadata and controls
128 lines (110 loc) · 4.96 KB
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
function ConvertFrom-JsonContentAction {
[CmdletBinding()]
[OutputType([System.Xml.XmlElement])]
param(
[Parameter(Mandatory)]
[object]$Action,
[Parameter(Mandatory)]
[System.Xml.XmlDocument]$XmlDocument
)
switch ($Action.type) {
'message' {
$element = $XmlDocument.CreateElement('message', $TargetNamespace)
$element.InnerText = $Action.text
if ($Action.noNewline) {
$element.SetAttribute('nonewline', 'true')
}
}
'file' {
$element = $XmlDocument.CreateElement('file', $TargetNamespace)
$element.SetAttribute('source', $Action.source)
$element.SetAttribute('destination', $Action.destination)
if ($Action.encoding) {
$element.SetAttribute('encoding', $Action.encoding)
}
if ($Action.openInEditor) {
$element.SetAttribute('openInEditor', 'true')
}
}
'templateFile' {
$element = $XmlDocument.CreateElement('templateFile', $TargetNamespace)
$element.SetAttribute('source', $Action.source)
$element.SetAttribute('destination', $Action.destination)
if ($Action.encoding) {
$element.SetAttribute('encoding', $Action.encoding)
}
if ($Action.openInEditor) {
$element.SetAttribute('openInEditor', 'true')
}
}
'directory' {
$element = $XmlDocument.CreateElement('file', $TargetNamespace)
$element.SetAttribute('source', '')
$element.SetAttribute('destination', $Action.destination)
}
'newModuleManifest' {
$element = $XmlDocument.CreateElement('newModuleManifest', $TargetNamespace)
$element.SetAttribute('destination', $Action.destination)
$manifestProperties = @('moduleVersion', 'rootModule', 'author', 'companyName', 'description', 'powerShellVersion', 'copyright', 'encoding')
foreach ($property in $manifestProperties) {
if ($Action.PSObject.Properties[$property]) {
$element.SetAttribute($property, $Action.$property)
}
}
if ($Action.openInEditor) {
$element.SetAttribute('openInEditor', 'true')
}
}
'modify' {
$element = $XmlDocument.CreateElement('modify', $TargetNamespace)
$element.SetAttribute('path', $Action.path)
if ($Action.encoding) {
$element.SetAttribute('encoding', $Action.encoding)
}
# Add modifications
foreach ($modification in $Action.modifications) {
if ($modification.type -eq 'replace') {
$replaceElement = $XmlDocument.CreateElement('replace', $TargetNamespace)
$originalElement = $XmlDocument.CreateElement('original', $TargetNamespace)
$originalElement.InnerText = $modification.search
if ($modification.isRegex) {
$originalElement.SetAttribute('expand', 'true')
}
$replaceElement.AppendChild($originalElement)
$substituteElement = $XmlDocument.CreateElement('substitute', $TargetNamespace)
$substituteElement.InnerText = $modification.replace
$substituteElement.SetAttribute('expand', 'true')
$replaceElement.AppendChild($substituteElement)
if ($modification.condition) {
$replaceElement.SetAttribute('condition', $modification.condition)
}
$element.AppendChild($replaceElement)
}
}
}
'requireModule' {
$element = $XmlDocument.CreateElement('requireModule', $TargetNamespace)
$element.SetAttribute('name', $Action.name)
$moduleProperties = @('minimumVersion', 'maximumVersion', 'requiredVersion', 'message')
foreach ($property in $moduleProperties) {
if ($Action.PSObject.Properties[$property]) {
$element.SetAttribute($property, $Action.$property)
}
}
}
'execute' {
# Execute action doesn't have direct XML equivalent, convert to message with warning
$element = $XmlDocument.CreateElement('message', $TargetNamespace)
$element.InnerText = "Warning: Execute action not supported in XML format. Script: $($Action.script)"
Write-PlasterLog -Level Warning -Message "Execute action converted to message - not supported in XML format"
}
default {
throw "Unknown action type: $($Action.type)"
}
}
# Add condition if present
if ($Action.condition) {
$element.SetAttribute('condition', $Action.condition)
}
return $element
}