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
+ [OutputType ([PSModuleDevelopment.Template.TemplateResult ])]
86
+ [Alias (' imt' )]
82
87
[CmdletBinding (SupportsShouldProcess = $true )]
83
88
param (
84
89
[Parameter (Mandatory = $true , Position = 0 , ParameterSetName = ' NameStore' )]
119
124
[switch ]
120
125
$Raw ,
121
126
127
+ [switch ]
128
+ $GenerateObjects ,
129
+
122
130
[switch ]
123
131
$Force ,
124
132
175
183
[bool ]
176
184
$Raw ,
177
185
186
+ [switch ]
187
+ $GenerateObjects ,
188
+
178
189
[bool ]
179
190
$Silent
180
191
)
212
223
}
213
224
}
214
225
# endregion Scripts
215
-
216
- switch ($templateData.Type.ToString ()) {
226
+ $createdTemplateItems = 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
+ New -TemplateItem - Item $child - Path $OutPath - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
222
231
}
223
232
if ($Raw -and $templateData.Scripts.Values ) {
224
233
$templateData.Scripts.Values | Export-Clixml - Path (Join-Path $OutPath " _PSMD_ParameterScripts.xml" )
227
236
# endregion File
228
237
229
238
# region Project
230
- " Project"
231
- {
239
+ " Project" {
232
240
# region Resolve output folder
233
241
if (-not $NoFolder ) {
234
242
if ($Parameters [" Name" ]) {
247
255
# endregion Resolve output folder
248
256
249
257
foreach ($child in $templateData.Children ) {
250
- Write -TemplateItem - Item $child - Path $newFolder.FullName - Encoding $Encoding - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
258
+ New -TemplateItem - Item $child - Path $newFolder.FullName - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
251
259
}
252
260
253
261
# region Write Config File (Raw)
278
286
$optionsTemplate = $optionsTemplate -replace " þþþPLACEHOLDER-$ ( $guid ) þþþ" , " "
279
287
}
280
288
281
- $configFile = Join-Path $newFolder.FullName " PSMDTemplate.ps1"
282
- Set-Content - Path $configFile - Value $optionsTemplate - Encoding ([PSFEncoding ]' utf-8' ).Encoding
289
+ [PSModuleDevelopment.Template.TemplateResult ]@ {
290
+ Name = " PSMDTemplate.ps1"
291
+ Path = $newFolder.FullName
292
+ FullPath = (Join-Path $newFolder.FullName " PSMDTemplate.ps1" )
293
+ Content = $optionsTemplate
294
+ }
283
295
}
284
296
# endregion Write Config File (Raw)
285
297
}
286
298
# endregion Project
287
299
}
300
+ If ($GenerateObjects ) {
301
+ return $createdTemplateItems
302
+ }
303
+ Write-TemplateResult - TemplateResult $createdTemplateItems - Encoding $Encoding
288
304
}
289
305
290
- function Write -TemplateItem {
306
+ function New -TemplateItem {
291
307
[Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseShouldProcessForStateChangingFunctions" , " " )]
308
+ [OutputType ([PSModuleDevelopment.Template.TemplateResult ])]
292
309
[CmdletBinding ()]
293
310
param (
294
311
[PSModuleDevelopment.Template.TemplateItemBase ]
297
314
[string ]
298
315
$Path ,
299
316
300
- [PSFEncoding ]
301
- $Encoding ,
302
-
303
317
[hashtable ]
304
318
$ParameterFlat ,
305
319
309
323
[bool ]
310
324
$Raw
311
325
)
312
-
313
- Write-PSFMessage - Level Verbose - Message " Creating file: $ ( $Item.Name ) ($ ( $Item.RelativePath ) )" - FunctionName Invoke-PSMDTemplate - ModuleName PSModuleDevelopment - Tag ' create' , ' template'
326
+ Write-PSFMessage - Level Verbose - Message " Creating Template-Item: $ ( $Item.Name ) ($ ( $Item.RelativePath ) )" - FunctionName Invoke-PSMDTemplate - ModuleName PSModuleDevelopment - Tag ' create' , ' template'
314
327
315
328
$identifier = $Item.Identifier
316
329
$isFile = $Item.GetType ().Name -eq ' TemplateItemFile'
320
333
$fileName = $Item.Name
321
334
if (-not $Raw ) {
322
335
foreach ($param in $Item.FileSystemParameterFlat ) {
323
- $fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterFlat [$param ], $false )
336
+ $fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterFlat [$param ], $false )
324
337
}
325
338
foreach ($param in $Item.FileSystemParameterScript ) {
326
339
$fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterScript [$param ], $false )
338
351
$text = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($text , " $ ( $identifier ) !$ ( $param ) !$ ( $identifier ) " , $ParameterScript [$param ], $false )
339
352
}
340
353
}
341
- [System.IO.File ]::WriteAllText($destPath , $text , $Encoding )
354
+ return [PSModuleDevelopment.Template.TemplateResult ]@ {
355
+ Name = $fileName
356
+ Path = $Path
357
+ FullPath = $destPath
358
+ Content = $text
359
+ }
342
360
}
343
361
else {
344
362
$bytes = [System.Convert ]::FromBase64String($Item.Value )
345
- [System.IO.File ]::WriteAllBytes($destPath , $bytes )
363
+ return [PSModuleDevelopment.Template.TemplateResult ]@ {
364
+ Name = $fileName
365
+ Path = $Path
366
+ FullPath = $destPath
367
+ Content = $bytes
368
+ IsText = $false
369
+ }
346
370
}
347
371
}
348
372
# endregion File
358
382
$folderName = $folderName -replace " $ ( $identifier ) !$ ( [regex ]::Escape($param )) !$ ( $identifier ) " , $ParameterScript [$param ]
359
383
}
360
384
}
361
- $folder = New-Item - Path $Path - Name $folderName - ItemType Directory
385
+ $folder = Join-Path - Path $Path - ChildPath $folderName
386
+
387
+ # Return a folder object to make sure empty folders are not excluded
388
+ [PSModuleDevelopment.Template.TemplateResult ]@ {
389
+ Name = $folderName
390
+ Path = $Path
391
+ FullPath = $folder
392
+ IsFolder = $true
393
+ IsText = $false
394
+ }
362
395
363
396
foreach ($child in $Item.Children ) {
364
- Write -TemplateItem - Item $child - Path $folder.FullName - Encoding $Encoding - ParameterFlat $ParameterFlat - ParameterScript $ParameterScript - Raw $Raw
397
+ New -TemplateItem - Item $child - Path $folder - ParameterFlat $ParameterFlat - ParameterScript $ParameterScript - Raw $Raw
365
398
}
366
399
}
367
400
# endregion Folder
368
401
}
402
+
403
+ function Write-TemplateResult {
404
+ [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseShouldProcessForStateChangingFunctions" , " " )]
405
+ [CmdletBinding ()]
406
+ param (
407
+ [PSModuleDevelopment.Template.TemplateResult []]
408
+ $TemplateResult ,
409
+
410
+ [PSFEncoding ]
411
+ $Encoding
412
+ )
413
+ $msgParam = @ { Level = ' Verbose' ; FunctionName = ' Invoke-PSMDTemplate' }
414
+ foreach ($item in $TemplateResult | Sort-Object { $_.FullPath.Length }) {
415
+ Write-PSFMessage @msgParam - Message " Creating file: $ ( $item.FullPath ) " - FunctionName Invoke-PSMDTemplate - ModuleName PSModuleDevelopment - Tag ' create' , ' template'
416
+ if (-not (Test-Path $item.Path )) {
417
+ Write-PSFMessage - Level Verbose - Message " Creating Folder $ ( $item.Path ) "
418
+ $null = New-Item - Path $item.Path - ItemType Directory
419
+ }
420
+ if ($item.IsFolder ) {
421
+ if (-not (Test-Path $item.FullPath )) {
422
+ Write-PSFMessage @msgParam - Message " Creating Folder $ ( $item.FullPath ) "
423
+ $null = New-Item - Path $item.FullPath - ItemType Directory
424
+ }
425
+ continue
426
+ }
427
+ if ($item.IsText ) {
428
+ Write-PSFMessage @msgParam - Message " Creating as a Text-File"
429
+ [System.IO.File ]::WriteAllText($item.FullPath , $item.Content , $Encoding )
430
+ }
431
+ else {
432
+ Write-PSFMessage @msgParam - Message " Creating as a Binary-File"
433
+ [System.IO.File ]::WriteAllBytes($item.FullPath , $item.Content )
434
+ }
435
+ }
436
+ }
369
437
# endregion Helper function
370
438
}
371
439
process {
372
440
if (Test-PSFFunctionInterrupt ) { return }
373
441
374
442
$invokeParam = @ {
375
- Parameters = $Parameters.Clone ()
376
- OutPath = Resolve-PSFPath - Path $OutPath
377
- NoFolder = $NoFolder
378
- Encoding = $Encoding
379
- Raw = $Raw
380
- Silent = $Silent
443
+ Parameters = $Parameters.Clone ()
444
+ OutPath = Resolve-PSFPath - Path $OutPath
445
+ NoFolder = $NoFolder
446
+ Encoding = $Encoding
447
+ Raw = $Raw
448
+ Silent = $Silent
449
+ GenerateObjects = $GenerateObjects
381
450
}
382
451
383
452
foreach ($item in $Template ) {
391
460
} - EnableException $EnableException - PSCmdlet $PSCmdlet - Continue
392
461
}
393
462
}
394
- }
463
+ }
0 commit comments