Skip to content

Commit 3aa32ca

Browse files
committed
Ensure paramblock usage
1 parent 280dde2 commit 3aa32ca

16 files changed

+174
-71
lines changed

Plaster/Private/Copy-FileWithConflictDetection.ps1

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
# Plaster zen for file handling. All file related operations should use this method
2-
# to actually write/overwrite/modify files in the DestinationPath. This method
3-
# handles detecting conflicts, gives the user a chance to determine how to handle
4-
# conflicts. The user can choose to use the Force parameter to force the overwriting
5-
# of existing files at the destination path.
6-
# File processing (expanding substitution variable, modifying file contents) should always
7-
# be done to a temp file (be sure to always remove temp file when done). That temp file
8-
# is what gets passed to this function as the $SrcPath. This allows Plaster to alert the
9-
# user when the repeated application of a template will modify any existing file.
10-
# NOTE: Plaster keeps track of which files it has "created" (as opposed to overwritten)
11-
# so that any later change to that file doesn't trigger conflict handling.
1+
<#
2+
Plaster zen for file handling. All file related operations should use this
3+
method to actually write/overwrite/modify files in the DestinationPath. This
4+
method handles detecting conflicts, gives the user a chance to determine how to
5+
handle conflicts. The user can choose to use the Force parameter to force the
6+
overwriting of existing files at the destination path. File processing
7+
(expanding substitution variable, modifying file contents) should always be done
8+
to a temp file (be sure to always remove temp file when done). That temp file is
9+
what gets passed to this function as the $SrcPath. This allows Plaster to alert
10+
the user when the repeated application of a template will modify any existing
11+
file.
12+
13+
NOTE: Plaster keeps track of which files it has "created" (as opposed to
14+
overwritten) so that any later change to that file doesn't trigger conflict
15+
handling.
16+
#>
1217
function Copy-FileWithConflictDetection {
1318
[CmdletBinding(SupportsShouldProcess = $true)]
1419
param(
@@ -23,7 +28,7 @@ function Copy-FileWithConflictDetection {
2328

2429
# Check if DstPath file conflicts with an existing SrcPath file.
2530
$operation = $LocalizedData.OpCreate
26-
$opmessage = (ConvertTo-DestinationRelativePath $DstPath)
31+
$opMessage = ConvertTo-DestinationRelativePath $DstPath
2732
if (Test-Path -LiteralPath $DstPath) {
2833
if (Test-FilesIdentical $SrcPath $DstPath) {
2934
$operation = $LocalizedData.OpIdentical
@@ -40,23 +45,31 @@ function Copy-FileWithConflictDetection {
4045

4146
# Copy the file to the destination
4247
if ($PSCmdlet.ShouldProcess($DstPath, $operation)) {
43-
Write-OperationStatus $operation $opmessage
48+
Write-OperationStatus -Operation $operation -Message $opMessage
4449

4550
if ($operation -eq $LocalizedData.OpIdentical) {
4651
# If the files are identical, no need to do anything
4752
return
4853
}
4954

50-
if (($operation -eq $LocalizedData.OpCreate) -or ($operation -eq $LocalizedData.OpUpdate)) {
55+
if (
56+
($operation -eq $LocalizedData.OpCreate) -or
57+
($operation -eq $LocalizedData.OpUpdate)
58+
) {
5159
Copy-Item -LiteralPath $SrcPath -Destination $DstPath
5260
if ($PassThru) {
5361
$InvokePlasterInfo.CreatedFiles += $DstPath
5462
}
5563
$script:templateCreatedFiles[$DstPath] = $null
56-
} elseif ($Force -or $PSCmdlet.ShouldContinue(($LocalizedData.OverwriteFile_F1 -f $DstPath),
64+
} elseif (
65+
$Force -or
66+
$PSCmdlet.ShouldContinue(
67+
($LocalizedData.OverwriteFile_F1 -f $DstPath),
5768
$LocalizedData.FileConflict,
5869
[ref]$script:fileConflictConfirmYesToAll,
59-
[ref]$script:fileConflictConfirmNoToAll)) {
70+
[ref]$script:fileConflictConfirmNoToAll
71+
)
72+
) {
6073
$backupFilename = New-BackupFilename $DstPath
6174
Copy-Item -LiteralPath $DstPath -Destination $backupFilename
6275
Copy-Item -LiteralPath $SrcPath -Destination $DstPath

Plaster/Private/Expand-FileSourceSpec.ps1

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
function Expand-FileSourceSpec([string]$srcRelPath, [string]$dstRelPath) {
2-
$srcPath = Join-Path $templateAbsolutePath $srcRelPath
3-
$dstPath = Join-Path $destinationAbsolutePath $dstRelPath
1+
function Expand-FileSourceSpec {
2+
[CmdletBinding()]
3+
param(
4+
[string]$SourceRelativePath,
5+
[string]$DestinationRelativePath
6+
)
7+
$srcPath = Join-Path $templateAbsolutePath $SourceRelativePath
8+
$dstPath = Join-Path $destinationAbsolutePath $DestinationRelativePath
49

5-
if ($srcRelPath.IndexOfAny([char[]]('*', '?')) -lt 0) {
10+
if ($SourceRelativePath.IndexOfAny([char[]]('*', '?')) -lt 0) {
611
# No wildcard spec in srcRelPath so return info on single file.
712
# Also, if dstRelPath is empty, then use source rel path.
8-
if (!$dstRelPath) {
9-
$dstPath = Join-Path $destinationAbsolutePath $srcRelPath
13+
if (!$DestinationRelativePath) {
14+
$dstPath = Join-Path $destinationAbsolutePath $SourceRelativePath
1015
}
1116

12-
return New-FileSystemCopyInfo $srcPath $dstPath
17+
return (New-FileSystemCopyInfo $srcPath $dstPath)
1318
}
1419

15-
# Prepare parameter values for call to Get-ChildItem to get list of files based on wildcard spec.
20+
# Prepare parameter values for call to Get-ChildItem to get list of files
21+
# based on wildcard spec.
1622
$gciParams = @{}
1723
$parent = Split-Path $srcPath -Parent
1824
$leaf = Split-Path $srcPath -Leaf
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
function Get-MaxOperationLabelLength {
2-
($LocalizedData.OpCreate, $LocalizedData.OpIdentical,
3-
$LocalizedData.OpConflict, $LocalizedData.OpForce,
4-
$LocalizedData.OpMissing, $LocalizedData.OpModify,
5-
$LocalizedData.OpUpdate, $LocalizedData.OpVerify |
6-
Measure-Object -Property Length -Maximum).Maximum
2+
[CmdletBinding()]
3+
[OutputType([int])]
4+
param()
5+
(
6+
$LocalizedData.OpCreate,
7+
$LocalizedData.OpIdentical,
8+
$LocalizedData.OpConflict,
9+
$LocalizedData.OpForce,
10+
$LocalizedData.OpMissing,
11+
$LocalizedData.OpModify,
12+
$LocalizedData.OpUpdate,
13+
$LocalizedData.OpVerify |
14+
Measure-Object -Property Length -Maximum).Maximum
715
}

Plaster/Private/New-BackupFilename.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
function New-BackupFilename([string]$Path) {
1+
function New-BackupFilename {
2+
[CmdletBinding()]
3+
param(
4+
[string]$Path
5+
)
26
$dir = [System.IO.Path]::GetDirectoryName($Path)
37
$filename = [System.IO.Path]::GetFileName($Path)
48
$backupPath = Join-Path -Path $dir -ChildPath "${filename}.bak"

Plaster/Private/New-FileSystemCopyInfo.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
function New-FileSystemCopyInfo([string]$srcPath, [string]$dstPath) {
1+
function New-FileSystemCopyInfo {
2+
[CmdletBinding()]
3+
param(
4+
[string]$srcPath,
5+
[string]$dstPath
6+
)
27
[PSCustomObject]@{
38
SrcFileName = $srcPath
49
DstFileName = $dstPath
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
function Read-PromptForChoice {
22
[CmdletBinding()]
33
param(
4-
[string]$ParameterName,
5-
[ValidateNotNull()]$ChoiceNodes,
6-
[string]$prompt,
7-
[int[]]$defaults,
8-
[switch]$IsMultiChoice
4+
[string]
5+
$ParameterName,
6+
[ValidateNotNull()]
7+
$ChoiceNodes,
8+
[string]
9+
$prompt,
10+
[int[]]
11+
$defaults,
12+
[switch]
13+
$IsMultiChoice
914
)
1015
$choices = New-Object 'System.Collections.ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription]'
1116
$values = New-Object object[] $ChoiceNodes.Count
@@ -21,23 +26,23 @@ function Read-PromptForChoice {
2126
$values[$i++] = $value
2227
}
2328

24-
$retval = [PSCustomObject]@{Values = @(); Indices = @() }
29+
$returnValue = [PSCustomObject]@{Values = @(); Indices = @() }
2530

2631
if ($IsMultiChoice) {
2732
$selections = $Host.UI.PromptForChoice('', $prompt, $choices, $defaults)
2833
foreach ($selection in $selections) {
29-
$retval.Values += $values[$selection]
30-
$retval.Indices += $selection
34+
$returnValue.Values += $values[$selection]
35+
$returnValue.Indices += $selection
3136
}
3237
} else {
3338
if ($defaults.Count -gt 1) {
3439
throw ($LocalizedData.ParameterTypeChoiceMultipleDefault_F1 -f $ChoiceNodes.ParentNode.name)
3540
}
3641

3742
$selection = $Host.UI.PromptForChoice('', $prompt, $choices, $defaults[0])
38-
$retval.Values = $values[$selection]
39-
$retval.Indices = $selection
43+
$returnValue.Values = $values[$selection]
44+
$returnValue.Indices = $selection
4045
}
4146

42-
$retval
47+
$returnValue
4348
}

Plaster/Private/Read-PromptForInput.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
function Read-PromptForInput($prompt, $default, $pattern) {
1+
function Read-PromptForInput {
2+
[CmdletBinding()]
3+
param(
4+
$prompt,
5+
$default,
6+
$pattern
7+
)
28
if (!$pattern) {
39
$patternMatch = $true
410
}

Plaster/Private/Resolve-ProcessMessage.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
function Resolve-ProcessMessage([ValidateNotNull()]$Node) {
1+
function Resolve-ProcessMessage {
2+
[CmdletBinding()]
3+
param(
4+
[ValidateNotNull()]
5+
$Node
6+
)
27
$text = Resolve-AttributeValue $Node.InnerText '<message>'
3-
$nonewline = $Node.nonewline -eq 'true'
8+
$noNewLine = $Node.nonewline -eq 'true'
49

510
# Eliminate whitespace before and after the text that just happens to get inserted because you want
611
# the text on different lines than the start/end element tags.
@@ -14,5 +19,5 @@ function Resolve-ProcessMessage([ValidateNotNull()]$Node) {
1419
return
1520
}
1621

17-
Write-Host $trimmedText -NoNewline:($nonewline -eq 'true')
22+
Write-Host $trimmedText -NoNewline:($noNewLine -eq 'true')
1823
}

Plaster/Private/Set-PlasterVariable.ps1

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1-
# All Plaster variables should be set via this method so that the ConstrainedRunspace can be
2-
# configured to use the new variable. This method will null out the ConstrainedRunspace so that
3-
# later, when we need to evaluate script in that runspace, it will get recreated first with all
4-
# the latest Plaster variables.
51
function Set-PlasterVariable {
2+
<#
3+
.SYNOPSIS
4+
Sets a Plaster variable in the script scope and updates the
5+
ConstrainedRunspace if it exists.
6+
7+
.DESCRIPTION
8+
This function sets a variable in the script scope and updates the
9+
ConstrainedRunspace if it exists. It is used to manage Plaster variables,
10+
which can be parameters or other types of variables.
11+
12+
.PARAMETER Name
13+
The name of the variable to set.
14+
15+
.PARAMETER Value
16+
The value to assign to the variable.
17+
18+
.PARAMETER IsParam
19+
Indicates if the variable is a parameter.
20+
If true, the variable is treated as a Plaster parameter and prefixed with
21+
"PLASTER_PARAM_".
22+
23+
.EXAMPLE
24+
Set-PlasterVariable -Name "MyVariable" -Value "MyValue" -IsParam $true
25+
26+
Sets a Plaster parameter variable named "PLASTER_PARAM_MyVariable" with the
27+
value "MyValue".
28+
.NOTES
29+
All Plaster variables should be set via this method so that the
30+
ConstrainedRunspace can be configured to use the new variable. This method
31+
will null out the ConstrainedRunspace so that later, when we need to
32+
evaluate script in that runspace, it will get recreated first with all
33+
the latest Plaster variables.
34+
#>
635
param(
736
[Parameter(Mandatory = $true)]
837
[ValidateNotNullOrEmpty()]
@@ -17,8 +46,8 @@ function Set-PlasterVariable {
1746
$IsParam = $true
1847
)
1948

20-
# Variables created from a <parameter> in the Plaster manifset are prefixed PLASTER_PARAM all others
21-
# are just PLASTER_.
49+
# Variables created from a <parameter> in the Plaster manifest are prefixed
50+
# PLASTER_PARAM all others are just PLASTER_.
2251
$variableName = if ($IsParam) { "PLASTER_PARAM_$Name" } else { "PLASTER_$Name" }
2352

2453
Set-Variable -Name $variableName -Value $Value -Scope Script -WhatIf:$false

Plaster/Private/Start-ProcessFile.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ function Start-ProcessFile {
3737

3838
# Check if source specifies a wildcard and if so, expand the wildcard
3939
# and then process each file system object (file or empty directory).
40-
$fileSystemCopyInfoObjs = Expand-FileSourceSpec $srcRelPath $dstRelPath
40+
$expandFileSourceSpecSplat = @{
41+
SourceRelativePath = $srcRelPath
42+
DestinationRelativePath = $dstRelPath
43+
}
44+
$fileSystemCopyInfoObjs = Expand-FileSourceSpec @expandFileSourceSpecSplat
4145
foreach ($fileSystemCopyInfo in $fileSystemCopyInfoObjs) {
4246
$srcPath = $fileSystemCopyInfo.SrcFileName
4347
$dstPath = $fileSystemCopyInfo.DstFileName

0 commit comments

Comments
 (0)