|
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 | )
|
|
215 | 225 |
|
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 -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 |
222 | 234 | }
|
223 | 235 | if ($Raw -and $templateData.Scripts.Values) {
|
224 | 236 | $templateData.Scripts.Values | Export-Clixml -Path (Join-Path $OutPath "_PSMD_ParameterScripts.xml")
|
|
227 | 239 | #endregion File
|
228 | 240 |
|
229 | 241 | #region Project
|
230 |
| - "Project" |
231 |
| - { |
| 242 | + "Project" { |
232 | 243 | #region Resolve output folder
|
233 | 244 | if (-not $NoFolder) {
|
234 | 245 | if ($Parameters["Name"]) {
|
|
247 | 258 | #endregion Resolve output folder
|
248 | 259 |
|
249 | 260 | 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 |
251 | 265 | }
|
252 | 266 |
|
253 | 267 | #region Write Config File (Raw)
|
|
287 | 301 | }
|
288 | 302 | }
|
289 | 303 |
|
290 |
| - function Write-TemplateItem { |
| 304 | + function New-TemplateItem { |
291 | 305 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "")]
|
292 | 306 | [CmdletBinding()]
|
293 | 307 | param (
|
|
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 [TemplateResult]@{ |
| 355 | + Filename = $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 [TemplateResult]@{ |
| 364 | + Filename = $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 |
362 |
| - |
| 385 | + $folder = Join-Path -Path (get-item $Path).FullName -ChildPath $folderName |
| 386 | + # $folder = New-Item -Path $Path -Name $folderName -ItemType Directory |
| 387 | + $createdTemplateItems = @() |
363 | 388 | 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 |
365 | 390 | }
|
| 391 | + return $createdTemplateItems |
366 | 392 | }
|
367 | 393 | #endregion Folder
|
368 | 394 | }
|
| 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 | + } |
369 | 476 | #endregion Helper function
|
370 | 477 | }
|
371 | 478 | process {
|
372 | 479 | if (Test-PSFFunctionInterrupt) { return }
|
373 | 480 |
|
374 | 481 | $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 |
381 | 489 | }
|
382 | 490 |
|
383 | 491 | foreach ($item in $Template) {
|
|
0 commit comments