1
1
function Invoke-PSMDTemplate {
2
- <#
2
+ <#
3
3
. SYNOPSIS
4
4
Creates a project/file from a template.
5
5
49
49
By default, all parameters will be replaced during invocation.
50
50
In Raw mode, this is skipped, reproducing mostly the original template input (dynamic scriptblocks will now be named scriptblocks)).
51
51
52
+ . PARAMETER GenerateObjects
53
+ By default, Invoke-PSMDTemplate generates files.
54
+ In GenerateObjects mode, no file but objects are created.
55
+
52
56
. PARAMETER Force
53
57
If the target path the template should be written to (filename or folder name within $OutPath), then overwrite it.
54
58
By default, this function will fail if an overwrite is required.
78
82
#>
79
83
[Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSPossibleIncorrectUsageOfAssignmentOperator" , " " )]
80
84
[Diagnostics.CodeAnalysis.SuppressMessageAttribute (' PSReviewUnusedParameter' , ' ' )]
81
- [Alias (' imt' )]
85
+ [Alias (' imt' )]
82
86
[CmdletBinding (SupportsShouldProcess = $true )]
83
87
param (
84
88
[Parameter (Mandatory = $true , Position = 0 , ParameterSetName = ' NameStore' )]
119
123
[switch ]
120
124
$Raw ,
121
125
126
+ [switch ]
127
+ $GenerateObjects ,
128
+
122
129
[switch ]
123
130
$Force ,
124
131
175
182
[bool ]
176
183
$Raw ,
177
184
185
+ [switch ]
186
+ $GenerateObjects ,
187
+
178
188
[bool ]
179
189
$Silent
180
190
)
212
222
}
213
223
}
214
224
# endregion Scripts
215
-
225
+ $createdTemplateItems = @ ()
216
226
switch ($templateData.Type.ToString ()) {
217
227
# region File
218
- " File"
219
- {
228
+ " File" {
220
229
foreach ($child in $templateData.Children ) {
221
- Write-TemplateItem - Item $child - Path $OutPath - Encoding $Encoding - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
230
+ $createdTemplateItems += New-TemplateItem - Item $child - Path $OutPath - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
231
+ # Write-PSFMessage "`$createdTemplateItems=$($createdTemplateItems|convertto-json)"
232
+ # Write-TemplateResult -TemplateResult $createdTemplateItems -Encoding $Encoding
222
233
}
223
234
if ($Raw -and $templateData.Scripts.Values ) {
224
235
$templateData.Scripts.Values | Export-Clixml - Path (Join-Path $OutPath " _PSMD_ParameterScripts.xml" )
227
238
# endregion File
228
239
229
240
# region Project
230
- " Project"
231
- {
241
+ " Project" {
232
242
# region Resolve output folder
233
243
if (-not $NoFolder ) {
234
244
if ($Parameters [" Name" ]) {
247
257
# endregion Resolve output folder
248
258
249
259
foreach ($child in $templateData.Children ) {
250
- Write-TemplateItem - Item $child - Path $newFolder.FullName - Encoding $Encoding - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
260
+ $createdTemplateItems += New-TemplateItem - Item $child - Path $newFolder.FullName - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
261
+ # Write-PSFMessage "`$createdTemplateItems=$($createdTemplateItems|convertto-json)"
262
+ # Write-TemplateResult -TemplateResult $createdTemplateItems -Encoding $Encoding
251
263
}
252
264
253
265
# region Write Config File (Raw)
278
290
$optionsTemplate = $optionsTemplate -replace " þþþPLACEHOLDER-$ ( $guid ) þþþ" , " "
279
291
}
280
292
281
- $configFile = Join-Path $newFolder.FullName " PSMDTemplate.ps1"
282
- Set-Content - Path $configFile - Value $optionsTemplate - Encoding ([PSFEncoding ]' utf-8' ).Encoding
293
+ $createdTemplateItems += [TemplateResult ]@ {
294
+ Filename = " PSMDTemplate.ps1"
295
+ Path = $newFolder.FullName
296
+ FullPath = (Join-Path $newFolder.FullName " PSMDTemplate.ps1" )
297
+ Content = $optionsTemplate
298
+ }
299
+ # Set-Content -Path $configFile -Value $optionsTemplate -Encoding ([PSFEncoding]'utf-8').Encoding
283
300
}
284
301
# endregion Write Config File (Raw)
285
302
}
286
303
# endregion Project
287
304
}
305
+ If ($GenerateObjects ){
306
+ return $createdTemplateItems
307
+ }
308
+ Write-TemplateResult - TemplateResult $createdTemplateItems - Encoding $Encoding
288
309
}
289
310
290
- function Write -TemplateItem {
311
+ function New -TemplateItem {
291
312
[Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseShouldProcessForStateChangingFunctions" , " " )]
292
313
[CmdletBinding ()]
293
314
param (
297
318
[string ]
298
319
$Path ,
299
320
300
- [PSFEncoding ]
301
- $Encoding ,
302
-
303
321
[hashtable ]
304
322
$ParameterFlat ,
305
323
309
327
[bool ]
310
328
$Raw
311
329
)
312
-
313
- Write-PSFMessage - Level Verbose - Message " Creating file: $ ( $Item.Name ) ($ ( $Item.RelativePath ) )" - FunctionName Invoke-PSMDTemplate - ModuleName PSModuleDevelopment - Tag ' create' , ' template'
330
+ Write-PSFMessage - Level Verbose - Message " Creating Template-Item: $ ( $Item.Name ) ($ ( $Item.RelativePath ) )" - FunctionName Invoke-PSMDTemplate - ModuleName PSModuleDevelopment - Tag ' create' , ' template'
314
331
315
332
$identifier = $Item.Identifier
316
333
$isFile = $Item.GetType ().Name -eq ' TemplateItemFile'
320
337
$fileName = $Item.Name
321
338
if (-not $Raw ) {
322
339
foreach ($param in $Item.FileSystemParameterFlat ) {
323
- $fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterFlat [$param ], $false )
340
+ $fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterFlat [$param ], $false )
324
341
}
325
342
foreach ($param in $Item.FileSystemParameterScript ) {
326
343
$fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterScript [$param ], $false )
338
355
$text = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($text , " $ ( $identifier ) !$ ( $param ) !$ ( $identifier ) " , $ParameterScript [$param ], $false )
339
356
}
340
357
}
341
- [System.IO.File ]::WriteAllText($destPath , $text , $Encoding )
358
+ return [TemplateResult ]@ {
359
+ Filename = $fileName
360
+ Path = $Path
361
+ FullPath = $destPath
362
+ Content = $text
363
+ }
342
364
}
343
365
else {
344
366
$bytes = [System.Convert ]::FromBase64String($Item.Value )
345
- [System.IO.File ]::WriteAllBytes($destPath , $bytes )
367
+ return [TemplateResult ]@ {
368
+ Filename = $fileName
369
+ Path = $Path
370
+ FullPath = $destPath
371
+ Content = $bytes
372
+ IsText = $false
373
+ }
346
374
}
347
375
}
348
376
# endregion File
358
386
$folderName = $folderName -replace " $ ( $identifier ) !$ ( [regex ]::Escape($param )) !$ ( $identifier ) " , $ParameterScript [$param ]
359
387
}
360
388
}
361
- $folder = New-Item - Path $Path - Name $folderName - ItemType Directory
362
-
389
+ $folder = Join-Path - Path $Path - ChildPath $folderName
390
+ # $folder = New-Item -Path $Path -Name $folderName -ItemType Directory
391
+ $createdTemplateItems = @ ()
363
392
foreach ($child in $Item.Children ) {
364
- Write -TemplateItem - Item $child - Path $folder.FullName - Encoding $Encoding - ParameterFlat $ParameterFlat - ParameterScript $ParameterScript - Raw $Raw
393
+ $createdTemplateItems += New -TemplateItem - Item $child - Path $folder - ParameterFlat $ParameterFlat - ParameterScript $ParameterScript - Raw $Raw
365
394
}
395
+ return $createdTemplateItems
366
396
}
367
397
# endregion Folder
368
398
}
399
+ function Write-TemplateResult {
400
+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseShouldProcessForStateChangingFunctions" , " " )]
401
+ [CmdletBinding ()]
402
+ param (
403
+ [TemplateResult []]
404
+ $TemplateResult ,
405
+
406
+ [PSFEncoding ]
407
+ $Encoding
408
+ )
409
+ foreach ($item in $TemplateResult ) {
410
+ Write-PSFMessage - Level Verbose - Message " Creating file: $ ( $Item.FullPath ) " - FunctionName Invoke-PSMDTemplate - ModuleName PSModuleDevelopment - Tag ' create' , ' template'
411
+ # Write-PSFMessage -Level Verbose -Message "Creating file: $($Item |convertto-json)" -FunctionName Invoke-PSMDTemplate -ModuleName PSModuleDevelopment -Tag 'create', 'template'
412
+ if (-not (Test-Path $Item.Path )) {
413
+ Write-PSFMessage - Level Verbose - Message " Creating Folder $ ( $Item.Path ) "
414
+ New-Item - Path $Item.Path - ItemType Directory | Out-Null
415
+ }
416
+ if ($Item.IsText ) {
417
+ Write-PSFMessage - Level Verbose - Message " Creating as a Text-File"
418
+ [System.IO.File ]::WriteAllText($Item.FullPath , $Item.Content , $Encoding )
419
+ }
420
+ else {
421
+ Write-PSFMessage - Level Verbose - Message " Creating as a Binary-File"
422
+ [System.IO.File ]::WriteAllBytes($Item.FullPath , $Item.Content )
423
+ }
424
+ }
425
+ }
369
426
# endregion Helper function
370
427
}
371
428
process {
372
429
if (Test-PSFFunctionInterrupt ) { return }
373
430
374
431
$invokeParam = @ {
375
- Parameters = $Parameters.Clone ()
376
- OutPath = Resolve-PSFPath - Path $OutPath
377
- NoFolder = $NoFolder
378
- Encoding = $Encoding
379
- Raw = $Raw
380
- Silent = $Silent
432
+ Parameters = $Parameters.Clone ()
433
+ OutPath = Resolve-PSFPath - Path $OutPath
434
+ NoFolder = $NoFolder
435
+ Encoding = $Encoding
436
+ Raw = $Raw
437
+ Silent = $Silent
438
+ GenerateObjects = $GenerateObjects
381
439
}
382
440
383
441
foreach ($item in $Template ) {
391
449
} - EnableException $EnableException - PSCmdlet $PSCmdlet - Continue
392
450
}
393
451
}
394
- }
452
+ }
0 commit comments