forked from StartAutomating/PSDevOps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expand-BuildStep.ps1
320 lines (280 loc) · 13.6 KB
/
Expand-BuildStep.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
function Expand-BuildStep
{
<#
.Synopsis
Expands Build Steps in a single build object
.Description
Component Files are .ps1 or datafiles within a directory that tells you what type they are.
.Example
Expand-BuildStep -StepMap @{Steps='InstallPester','RunPester'}
.Link
Convert-BuildStep
.Link
Import-BuildStep
#>
[OutputType([PSObject])]
param(
# A map of step properties to underlying data.
# Each key is the name of a property the output.
# Each value may contain the name of another Step or StepMap
[Parameter(Mandatory)]
[Collections.IDictionary]
$StepMap,
# The immediate parent object
[PSObject]
$Parent,
# The absolute root object
[PSObject]
$Root,
# If set, the component will be expanded as a singleton (single object)
[switch]
$Singleton,
# A list of item names that automatically become singletons
[string[]]$SingleItemName,
# A list of item names that automatically become plurals
[string[]]$PluralItemName,
# A list of item names that automatically become dictionaries.
[string[]]$DictionaryItemName,
# The build system, either ADO or GitHub.
[ValidateSet('ADOPipeline', 'ADOExtension','GitHubWorkflow','GitHubAction')]
[string]$BuildSystem = 'ADOPipeline',
# The name of parameters that should be supplied from build variables.
# Wildcards accepted.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('VariableParameters')]
[string[]]
$VariableParameter,
# The name of parameters that should be supplied from webhook events.
# Wildcards accepted.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('InputParameters')]
[Collections.IDictionary]
$InputParameter,
# The name of parameters that should be supplied from the environment.
# Wildcards accepted.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('EnvironmentParameters')]
[string[]]
$EnvironmentParameter,
# The name of parameters that should be referred to uniquely.
# For instance, if converting function foo($bar) {} and -UniqueParameter is 'bar'
# The build parameter would be foo_bar.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('UniqueParameters')]
[string[]]
$UniqueParameter,
# The name of parameters that should be excluded.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('ExcludeParameters')]
[string[]]
$ExcludeParameter,
# A collection of default parameters.
[Parameter(ValueFromPipelineByPropertyName)]
[Collections.IDictionary]
$DefaultParameter = @{},
# Options for the build system. The can contain any additional parameters passed to the build system.
[PSObject]
$BuildOption
)
begin {
$convertBuildStepCmd = # Get the command Convert-BuildStep, for we will need it later to splat.
$ExecutionContext.SessionState.InvokeCommand.GetCommand('Convert-BuildStep','Function')
}
process {
$theComponentMetaData = $script:ComponentMetaData.$BuildSystem
$theComponentNames = $script:ComponentNames.$BuildSystem
$outObject = [Ordered]@{}
$splatMe = @{} + $PSBoundParameters
if (-not $Root) {
$Root = $outObject
$splatMe.Root = $outObject
}
$splatMe.Remove('StepMap')
:nextKey foreach ($kv in $stepMap.GetEnumerator()) {
if ($kv.Key.EndsWith('s') -and -not $singleton) { # Already pluralized
$thingType = $kv.Key.Substring(0,$kv.Key.Length -1)
$propName = $kv.Key
} elseif ($parent) {
$thingType = $kv.Key
$propName = $kv.Key
} else {
$thingType = $kv.Key
$propName =
if ($SingleItemName -notcontains $thingType -and
$thingType -notmatch '\W$' -and
$theComponentNames.Keys -contains $thingType) {
$kv.Key.Substring(0,1).ToLower() + $kv.Key.Substring(1) + 's'
} else {
$kv.Key.Substring(0,1).ToLower() + $kv.Key.Substring(1)
$singleton = $true
}
}
# Expand each value
$outValue = :nextValue foreach ($v in $kv.Value) {
$metaData = $theComponentMetaData["$thingType.$v"]
if ($propName -eq $thingType -and -not $singleton) {
if ($v -is [Collections.IDictionary]) {
$splatMe.StepMap = $v
Expand-BuildStep @splatMe
} else {
$v
}
continue nextValue
}
$o =
#region Expand PSD1 Files
if ($metaData.Extension -eq '.psd1') {
$data =
Import-LocalizedData -BaseDirectory (
[IO.Path]::GetDirectoryName($metaData.Path)
) -FileName (
[IO.PATH]::GetFileName($metaData.Path)
)
if (-not $data) { continue nextValue }
$fileText = [IO.File]::ReadAllText($metaData.Path)
$data = & ([ScriptBlock]::Create(($FileText -replace '@{', '[Ordered]@{')))
$splatMe.Parent = $stepMap
if ($data -is [Collections.IDictionary]) {
$splatMe.StepMap = $data
try { Expand-BuildStep @splatMe }
catch {
Write-Debug "Could not Expand $($kv.Id): $_"
}
} else {
$data
}
}
elseif ($metaData.Extension -eq '.json') {
[IO.File]::ReadAllText($metaData.Path) | ConvertFrom-Json
}
#endregion Expand PSD1 Files
elseif ($v -is [Collections.IDictionary])
{
$splatMe.Parent = $stepMap
$splatMe.StepMap = $v
Expand-BuildStep @splatMe
} else
{
$convertedBuildStep =
if ($metaData) {
$convertSplat = @{} + $PsBoundParameters
foreach ($k in @($convertSplat.Keys)) {
if (-not $convertBuildStepCmd.Parameters[$k]) {
$convertSplat.Remove($k)
}
}
$metaData |
Convert-BuildStep @convertSplat
}
if ($convertedBuildStep) {
if ($BuildSystem -eq 'ADOPipeline' -and
$Root -and
$convertedBuildStep.parameters) {
if ($root.parameters) {
:nextKeyValue foreach ($keyValue in $convertedBuildStep.parameters) {
foreach ($item in $root.Parameters) {
if ($item.Name -eq $keyValue.Name) {
if ($item.default -ne $keyValue.default) {
$item.default = ''
}
continue nextKeyValue
}
}
$root.Parameters += $keyValue
}
} else {
$root.parameters = $convertedBuildStep.parameters
}
$convertedBuildStep.Remove('parameters')
}
if ($BuildSystem -in 'GitHubWorkflow','GitHubAction' -and $Root -and # If the BuildSystem was GitHub
$convertedBuildStep.parameters) {
if (
$BuildSystem -eq 'GitHubAction' -or
$convertedBuildStep.env.values -like '*.inputs.*' -and # and we have event inputs
($root.on.workflow_dispatch -is [Collections.IDictionary] -or # and we have a workflow_dispatch trigger.
$root.on -eq 'workflow_dispatch' -or
($root.name -and $root.description)
)
) {
$ComparisonResult = $root.on -eq 'workflow_dispatch'
$workflowDispatch =
$workflowDispatch = [Ordered]@{ # Create an empty workflow_dispatch
inputs = [Ordered]@{} # for inputs.
}
# If the result of root.on -eq 'workflow_dispatch' is a string
if ($ComparisonResult -and $ComparisonResult -is [Object[]]) {
$root.on = @( # on is already a list, so let's keep it that way.
foreach ($o in $root.on) {
if ($o -ne 'workflow_dispatch') { # anything that's not workflow_dispatch
$o # gets put back in order.
} else {
[Ordered]@{ # and workflow_dispatch becomes
workflow_dispatch = $workflowDispatch
}
}
}
)
}
elseif ($ComparisonResult -and # If root.on was 'workflow_dispatch'
$ComparisonResult -is [bool]) { # and the result was a bool.
$root.on = [Ordered]@{ # and workflow_dispatch becomes
workflow_dispatch = $workflowDispatch
}
}
elseif ($BuildSystem -eq 'GitHubAction') {
if (-not $root.inputs) { $root.inputs = [Ordered]@{} }
$workflowDispatch = $root
} else {
# Otherwise, we know that workflow_dispatch is already a dictionary
$workflowDispatch = $root.on.workflow_dispatch
}
if ($workflowDispatch) {
foreach ($convertedParam in $convertedBuildStep.parameters) {
foreach ($keyValue in $convertedParam.GetEnumerator()) {
$workflowDispatch.Inputs[$keyValue.Key] = $keyValue.Value
}
}
}
}
$convertedBuildStep.Remove('parameters')
}
$convertedBuildStep
} else {
$v
}
}
if ($DictionaryItemName -contains $propName) {
if (-not $outObject[$propName]) {
$outObject[$propName] = [Ordered]@{}
}
$outObject[$propName][$v] = $o
continue
}
if ($metaData.Name -and $Option.$($metaData.Name) -is [Collections.IDictionary]) {
$o2 = [Ordered]@{} + $o
foreach ($ov in @($Option.($metaData.Name).GetEnumerator())) {
$o2[$ov.Key] = $ov.Value
}
$o2
} else {
$o
}
}
if ($outValue) {
$outObject[$propName] = $outValue
if ($outObject[$propName] -isnot [Collections.IList] -and
$kv.Value -is [Collections.IList] -and -not $singleton) {
$outObject[$propName] = @($outObject[$propName])
} elseif ($outObject[$propName] -is [Collections.IList] -and $kv.Value -isnot [Collections.IList]) {
$outObject[$propName] = $outObject[$propName][0]
}
if ($PluralItemName -contains $propName -and
$outObject[$propName] -isnot [Collections.IList]) {
$outObject[$propName] = @($outObject[$propName])
}
}
}
$outObject
}
}