Skip to content

Commit 6f49371

Browse files
committed
Seperated Template-Output-Creation from writing to disk
1 parent 99a19c2 commit 6f49371

File tree

1 file changed

+131
-23
lines changed

1 file changed

+131
-23
lines changed

PSModuleDevelopment/functions/templating/Invoke-PSMDTemplate.ps1

Lines changed: 131 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function Invoke-PSMDTemplate {
2-
<#
2+
<#
33
.SYNOPSIS
44
Creates a project/file from a template.
55
@@ -49,6 +49,10 @@
4949
By default, all parameters will be replaced during invocation.
5050
In Raw mode, this is skipped, reproducing mostly the original template input (dynamic scriptblocks will now be named scriptblocks)).
5151
52+
.PARAMETER GenerateObjects
53+
By default, Invoke-PSMDTemplate generates files.
54+
In GenerateObjects mode, no file but objects are created.
55+
5256
.PARAMETER Force
5357
If the target path the template should be written to (filename or folder name within $OutPath), then overwrite it.
5458
By default, this function will fail if an overwrite is required.
@@ -78,7 +82,7 @@
7882
#>
7983
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSPossibleIncorrectUsageOfAssignmentOperator", "")]
8084
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSReviewUnusedParameter', '')]
81-
[Alias('imt')]
85+
[Alias('imt')]
8286
[CmdletBinding(SupportsShouldProcess = $true)]
8387
param (
8488
[Parameter(Mandatory = $true, Position = 0, ParameterSetName = 'NameStore')]
@@ -119,6 +123,9 @@
119123
[switch]
120124
$Raw,
121125

126+
[switch]
127+
$GenerateObjects,
128+
122129
[switch]
123130
$Force,
124131

@@ -175,6 +182,9 @@
175182
[bool]
176183
$Raw,
177184

185+
[switch]
186+
$GenerateObjects,
187+
178188
[bool]
179189
$Silent
180190
)
@@ -215,10 +225,12 @@
215225

216226
switch ($templateData.Type.ToString()) {
217227
#region File
218-
"File"
219-
{
228+
"File" {
220229
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 -Encoding $Encoding -ParameterFlat $Parameters -ParameterScript $scriptParameters -Raw $Raw
231+
Write-PSFMessage "`$createdTemplateItems=$($createdTemplateItems|convertto-json)"
232+
#Todo: Parameter umstellen
233+
Write-TemplateResults -TemplateResult $createdTemplateItems -Encoding $Encoding
222234
}
223235
if ($Raw -and $templateData.Scripts.Values) {
224236
$templateData.Scripts.Values | Export-Clixml -Path (Join-Path $OutPath "_PSMD_ParameterScripts.xml")
@@ -227,8 +239,7 @@
227239
#endregion File
228240

229241
#region Project
230-
"Project"
231-
{
242+
"Project" {
232243
#region Resolve output folder
233244
if (-not $NoFolder) {
234245
if ($Parameters["Name"]) {
@@ -247,7 +258,10 @@
247258
#endregion Resolve output folder
248259

249260
foreach ($child in $templateData.Children) {
250-
Write-TemplateItem -Item $child -Path $newFolder.FullName -Encoding $Encoding -ParameterFlat $Parameters -ParameterScript $scriptParameters -Raw $Raw
261+
$createdTemplateItems = New-TemplateItem -Item $child -Path $newFolder.FullName -Encoding $Encoding -ParameterFlat $Parameters -ParameterScript $scriptParameters -Raw $Raw
262+
Write-PSFMessage "`$createdTemplateItems=$($createdTemplateItems|convertto-json)"
263+
#Todo: Parameter umstellen
264+
Write-TemplateResults -TemplateResult $createdTemplateItems -Encoding $Encoding
251265
}
252266

253267
#region Write Config File (Raw)
@@ -287,7 +301,7 @@
287301
}
288302
}
289303

290-
function Write-TemplateItem {
304+
function New-TemplateItem {
291305
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
292306
[CmdletBinding()]
293307
param (
@@ -309,8 +323,7 @@
309323
[bool]
310324
$Raw
311325
)
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'
314327

315328
$identifier = $Item.Identifier
316329
$isFile = $Item.GetType().Name -eq 'TemplateItemFile'
@@ -320,7 +333,7 @@
320333
$fileName = $Item.Name
321334
if (-not $Raw) {
322335
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)
324337
}
325338
foreach ($param in $Item.FileSystemParameterScript) {
326339
$fileName = [PSModuleDevelopment.Utility.UtilityHost]::Replace($fileName, "$($identifier)$($param)$($identifier)", $ParameterScript[$param], $false)
@@ -338,11 +351,22 @@
338351
$text = [PSModuleDevelopment.Utility.UtilityHost]::Replace($text, "$($identifier)!$($param)!$($identifier)", $ParameterScript[$param], $false)
339352
}
340353
}
341-
[System.IO.File]::WriteAllText($destPath, $text, $Encoding)
354+
return [TemplateResult]@{
355+
Filename = $fileName
356+
Path = $Path
357+
FullPath = $destPath
358+
Content = $text
359+
}
342360
}
343361
else {
344362
$bytes = [System.Convert]::FromBase64String($Item.Value)
345-
[System.IO.File]::WriteAllBytes($destPath, $bytes)
363+
return [TemplateResult]@{
364+
Filename = $fileName
365+
Path = $Path
366+
FullPath = $destPath
367+
Content = $bytes
368+
IsText = $false
369+
}
346370
}
347371
}
348372
#endregion File
@@ -358,26 +382,110 @@
358382
$folderName = $folderName -replace "$($identifier)!$([regex]::Escape($param))!$($identifier)", $ParameterScript[$param]
359383
}
360384
}
361-
$folder = New-Item -Path $Path -Name $folderName -ItemType Directory
362-
385+
$folder = Join-Path -Path (get-item $Path).FullName -ChildPath $folderName
386+
# $folder = New-Item -Path $Path -Name $folderName -ItemType Directory
387+
$createdTemplateItems = @()
363388
foreach ($child in $Item.Children) {
364-
Write-TemplateItem -Item $child -Path $folder.FullName -Encoding $Encoding -ParameterFlat $ParameterFlat -ParameterScript $ParameterScript -Raw $Raw
389+
$createdTemplateItems += New-TemplateItem -Item $child -Path $folder -Encoding $Encoding -ParameterFlat $ParameterFlat -ParameterScript $ParameterScript -Raw $Raw
365390
}
391+
return $createdTemplateItems
366392
}
367393
#endregion Folder
368394
}
395+
function Write-TemplateResults {
396+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
397+
[CmdletBinding()]
398+
param (
399+
[TemplateResult[]]
400+
$TemplateResult,
401+
402+
[PSFEncoding]
403+
$Encoding
404+
)
405+
foreach ($item in $TemplateResult) {
406+
Write-PSFMessage -Level Verbose -Message "Creating file: $($Item.FullPath)" -FunctionName Invoke-PSMDTemplate -ModuleName PSModuleDevelopment -Tag 'create', 'template'
407+
Write-PSFMessage -Level Verbose -Message "Creating file: $($Item |convertto-json)" -FunctionName Invoke-PSMDTemplate -ModuleName PSModuleDevelopment -Tag 'create', 'template'
408+
if (-not (Test-Path $Item.Path)) {
409+
Write-PSFMessage -Level Verbose -Message "Creating Folder $($Item.Path)"
410+
New-Item -Path $Item.Path -ItemType Directory
411+
}
412+
if ($Item.IsText) {
413+
Write-PSFMessage -Level Verbose -Message "Creating as a Text-File"
414+
[System.IO.File]::WriteAllText($Item.FullPath, $Item.Content, $Encoding)
415+
}
416+
else {
417+
Write-PSFMessage -Level Verbose -Message "Creating as a Binary-File"
418+
[System.IO.File]::WriteAllBytes($Item.FullPath, $Item.Content)
419+
}
420+
}
421+
422+
# $identifier = $Item.Identifier
423+
# $isFile = $Item.GetType().Name -eq 'TemplateItemFile'
424+
425+
# #region File
426+
# if ($isFile) {
427+
# $fileName = $Item.Name
428+
# if (-not $Raw) {
429+
# foreach ($param in $Item.FileSystemParameterFlat) {
430+
# $fileName = [PSModuleDevelopment.Utility.UtilityHost]::Replace($fileName, "$($identifier)$($param)$($identifier)", $ParameterFlat[$param], $false)
431+
# }
432+
# foreach ($param in $Item.FileSystemParameterScript) {
433+
# $fileName = [PSModuleDevelopment.Utility.UtilityHost]::Replace($fileName, "$($identifier)$($param)$($identifier)", $ParameterScript[$param], $false)
434+
# }
435+
# }
436+
# $destPath = Join-Path $Path $fileName
437+
438+
# if ($Item.PlainText) {
439+
# $text = $Item.Value
440+
# if (-not $Raw) {
441+
# foreach ($param in $Item.ContentParameterFlat) {
442+
# $text = [PSModuleDevelopment.Utility.UtilityHost]::Replace($text, "$($identifier)$($param)$($identifier)", $ParameterFlat[$param], $false)
443+
# }
444+
# foreach ($param in $Item.ContentParameterScript) {
445+
# $text = [PSModuleDevelopment.Utility.UtilityHost]::Replace($text, "$($identifier)!$($param)!$($identifier)", $ParameterScript[$param], $false)
446+
# }
447+
# }
448+
# [System.IO.File]::WriteAllText($destPath, $text, $Encoding)
449+
# }
450+
# else {
451+
# $bytes = [System.Convert]::FromBase64String($Item.Value)
452+
# [System.IO.File]::WriteAllBytes($destPath, $bytes)
453+
# }
454+
# }
455+
# #endregion File
456+
457+
# #region Folder
458+
# else {
459+
# $folderName = $Item.Name
460+
# if (-not $Raw) {
461+
# foreach ($param in $Item.FileSystemParameterFlat) {
462+
# $folderName = $folderName -replace "$($identifier)$([regex]::Escape($param))$($identifier)", $ParameterFlat[$param]
463+
# }
464+
# foreach ($param in $Item.FileSystemParameterScript) {
465+
# $folderName = $folderName -replace "$($identifier)!$([regex]::Escape($param))!$($identifier)", $ParameterScript[$param]
466+
# }
467+
# }
468+
# $folder = New-Item -Path $Path -Name $folderName -ItemType Directory
469+
470+
# foreach ($child in $Item.Children) {
471+
# Write-TemplateResults -Item $child -Path $folder.FullName -Encoding $Encoding -ParameterFlat $ParameterFlat -ParameterScript $ParameterScript -Raw $Raw
472+
# }
473+
# }
474+
# #endregion Folder
475+
}
369476
#endregion Helper function
370477
}
371478
process {
372479
if (Test-PSFFunctionInterrupt) { return }
373480

374481
$invokeParam = @{
375-
Parameters = $Parameters.Clone()
376-
OutPath = Resolve-PSFPath -Path $OutPath
377-
NoFolder = $NoFolder
378-
Encoding = $Encoding
379-
Raw = $Raw
380-
Silent = $Silent
482+
Parameters = $Parameters.Clone()
483+
OutPath = Resolve-PSFPath -Path $OutPath
484+
NoFolder = $NoFolder
485+
Encoding = $Encoding
486+
Raw = $Raw
487+
Silent = $Silent
488+
GenerateObjects = $GenerateObjects
381489
}
382490

383491
foreach ($item in $Template) {

0 commit comments

Comments
 (0)