11function Invoke-PSMDTemplate {
2- <#
2+ <#
33 . SYNOPSIS
44 Creates a project/file from a template.
55
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.
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' )]
119123 [switch ]
120124 $Raw ,
121125
126+ [switch ]
127+ $GenerateObjects ,
128+
122129 [switch ]
123130 $Force ,
124131
175182 [bool ]
176183 $Raw ,
177184
185+ [switch ]
186+ $GenerateObjects ,
187+
178188 [bool ]
179189 $Silent
180190 )
212222 }
213223 }
214224 # endregion Scripts
215-
225+ $createdTemplateItems = @ ()
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 - ParameterFlat $Parameters - ParameterScript $scriptParameters - Raw $Raw
231+ # Write-PSFMessage "`$createdTemplateItems=$($createdTemplateItems|convertto-json)"
232+ # Write-TemplateResult -TemplateResult $createdTemplateItems -Encoding $Encoding
222233 }
223234 if ($Raw -and $templateData.Scripts.Values ) {
224235 $templateData.Scripts.Values | Export-Clixml - Path (Join-Path $OutPath " _PSMD_ParameterScripts.xml" )
227238 # endregion File
228239
229240 # region Project
230- " Project"
231- {
241+ " Project" {
232242 # region Resolve output folder
233243 if (-not $NoFolder ) {
234244 if ($Parameters [" Name" ]) {
247257 # endregion Resolve output folder
248258
249259 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
251263 }
252264
253265 # region Write Config File (Raw)
278290 $optionsTemplate = $optionsTemplate -replace " þþþPLACEHOLDER-$ ( $guid ) þþþ" , " "
279291 }
280292
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
283300 }
284301 # endregion Write Config File (Raw)
285302 }
286303 # endregion Project
287304 }
305+ If ($GenerateObjects ){
306+ return $createdTemplateItems
307+ }
308+ Write-TemplateResult - TemplateResult $createdTemplateItems - Encoding $Encoding
288309 }
289310
290- function Write -TemplateItem {
311+ function New -TemplateItem {
291312 [Diagnostics.CodeAnalysis.SuppressMessageAttribute (" PSUseShouldProcessForStateChangingFunctions" , " " )]
292313 [CmdletBinding ()]
293314 param (
297318 [string ]
298319 $Path ,
299320
300- [PSFEncoding ]
301- $Encoding ,
302-
303321 [hashtable ]
304322 $ParameterFlat ,
305323
309327 [bool ]
310328 $Raw
311329 )
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'
314331
315332 $identifier = $Item.Identifier
316333 $isFile = $Item.GetType ().Name -eq ' TemplateItemFile'
320337 $fileName = $Item.Name
321338 if (-not $Raw ) {
322339 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 )
324341 }
325342 foreach ($param in $Item.FileSystemParameterScript ) {
326343 $fileName = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($fileName , " $ ( $identifier ) $ ( $param ) $ ( $identifier ) " , $ParameterScript [$param ], $false )
338355 $text = [PSModuleDevelopment.Utility.UtilityHost ]::Replace($text , " $ ( $identifier ) !$ ( $param ) !$ ( $identifier ) " , $ParameterScript [$param ], $false )
339356 }
340357 }
341- [System.IO.File ]::WriteAllText($destPath , $text , $Encoding )
358+ return [TemplateResult ]@ {
359+ Filename = $fileName
360+ Path = $Path
361+ FullPath = $destPath
362+ Content = $text
363+ }
342364 }
343365 else {
344366 $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+ }
346374 }
347375 }
348376 # endregion File
358386 $folderName = $folderName -replace " $ ( $identifier ) !$ ( [regex ]::Escape($param )) !$ ( $identifier ) " , $ParameterScript [$param ]
359387 }
360388 }
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 = @ ()
363392 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
365394 }
395+ return $createdTemplateItems
366396 }
367397 # endregion Folder
368398 }
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+ }
369426 # endregion Helper function
370427 }
371428 process {
372429 if (Test-PSFFunctionInterrupt ) { return }
373430
374431 $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
381439 }
382440
383441 foreach ($item in $Template ) {
391449 } - EnableException $EnableException - PSCmdlet $PSCmdlet - Continue
392450 }
393451 }
394- }
452+ }
0 commit comments