Skip to content

Commit 437ca04

Browse files
authored
Migrate Private Functions out of Invoke-Plaster (#438)
2 parents c810135 + 3aa32ca commit 437ca04

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1522
-1235
lines changed

.github/workflows/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Publish Module
22
on:
3-
pull_request:
3+
push:
44
branches: [ master ]
55
workflow_dispatch:
66
jobs:

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// -------- Search configuration --------
99
// Exclude the Output folder from search results.
1010
"search.exclude": {
11-
"Output": true,
11+
"Output/**": true
1212
},
1313
//-------- PowerShell Configuration --------
1414
// Use a custom PowerShell Script Analyzer settings file for this workspace.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function ConvertTo-DestinationRelativePath {
2+
param(
3+
[ValidateNotNullOrEmpty()]
4+
[string]$Path
5+
)
6+
$fullDestPath = $DestinationPath
7+
if (![System.IO.Path]::IsPathRooted($fullDestPath)) {
8+
$fullDestPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($DestinationPath)
9+
}
10+
11+
$fullPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
12+
if (!$fullPath.StartsWith($fullDestPath, 'OrdinalIgnoreCase')) {
13+
throw ($LocalizedData.ErrorPathMustBeUnderDestPath_F2 -f $fullPath, $fullDestPath)
14+
}
15+
16+
$fullPath.Substring($fullDestPath.Length).TrimStart('\', '/')
17+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
#>
17+
function Copy-FileWithConflictDetection {
18+
[CmdletBinding(SupportsShouldProcess = $true)]
19+
param(
20+
[string]$SrcPath,
21+
[string]$DstPath
22+
)
23+
# Just double-checking that DstPath parameter is an absolute path otherwise
24+
# it could fail the check that the DstPath is under the overall DestinationPath.
25+
if (![System.IO.Path]::IsPathRooted($DstPath)) {
26+
$DstPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($DstPath)
27+
}
28+
29+
# Check if DstPath file conflicts with an existing SrcPath file.
30+
$operation = $LocalizedData.OpCreate
31+
$opMessage = ConvertTo-DestinationRelativePath $DstPath
32+
if (Test-Path -LiteralPath $DstPath) {
33+
if (Test-FilesIdentical $SrcPath $DstPath) {
34+
$operation = $LocalizedData.OpIdentical
35+
} elseif ($script:templateCreatedFiles.ContainsKey($DstPath)) {
36+
# Plaster created this file previously during template invocation
37+
# therefore, there is no conflict. We're simply updating the file.
38+
$operation = $LocalizedData.OpUpdate
39+
} elseif ($Force) {
40+
$operation = $LocalizedData.OpForce
41+
} else {
42+
$operation = $LocalizedData.OpConflict
43+
}
44+
}
45+
46+
# Copy the file to the destination
47+
if ($PSCmdlet.ShouldProcess($DstPath, $operation)) {
48+
Write-OperationStatus -Operation $operation -Message $opMessage
49+
50+
if ($operation -eq $LocalizedData.OpIdentical) {
51+
# If the files are identical, no need to do anything
52+
return
53+
}
54+
55+
if (
56+
($operation -eq $LocalizedData.OpCreate) -or
57+
($operation -eq $LocalizedData.OpUpdate)
58+
) {
59+
Copy-Item -LiteralPath $SrcPath -Destination $DstPath
60+
if ($PassThru) {
61+
$InvokePlasterInfo.CreatedFiles += $DstPath
62+
}
63+
$script:templateCreatedFiles[$DstPath] = $null
64+
} elseif (
65+
$Force -or
66+
$PSCmdlet.ShouldContinue(
67+
($LocalizedData.OverwriteFile_F1 -f $DstPath),
68+
$LocalizedData.FileConflict,
69+
[ref]$script:fileConflictConfirmYesToAll,
70+
[ref]$script:fileConflictConfirmNoToAll
71+
)
72+
) {
73+
$backupFilename = New-BackupFilename $DstPath
74+
Copy-Item -LiteralPath $DstPath -Destination $backupFilename
75+
Copy-Item -LiteralPath $SrcPath -Destination $DstPath
76+
if ($PassThru) {
77+
$InvokePlasterInfo.UpdatedFiles += $DstPath
78+
}
79+
$script:templateCreatedFiles[$DstPath] = $null
80+
}
81+
}
82+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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
9+
10+
if ($SourceRelativePath.IndexOfAny([char[]]('*', '?')) -lt 0) {
11+
# No wildcard spec in srcRelPath so return info on single file.
12+
# Also, if dstRelPath is empty, then use source rel path.
13+
if (!$DestinationRelativePath) {
14+
$dstPath = Join-Path $destinationAbsolutePath $SourceRelativePath
15+
}
16+
17+
return (New-FileSystemCopyInfo $srcPath $dstPath)
18+
}
19+
20+
# Prepare parameter values for call to Get-ChildItem to get list of files
21+
# based on wildcard spec.
22+
$gciParams = @{}
23+
$parent = Split-Path $srcPath -Parent
24+
$leaf = Split-Path $srcPath -Leaf
25+
$gciParams['LiteralPath'] = $parent
26+
$gciParams['File'] = $true
27+
28+
if ($leaf -eq '**') {
29+
$gciParams['Recurse'] = $true
30+
} else {
31+
if ($leaf.IndexOfAny([char[]]('*', '?')) -ge 0) {
32+
$gciParams['Filter'] = $leaf
33+
}
34+
35+
$leaf = Split-Path $parent -Leaf
36+
if ($leaf -eq '**') {
37+
$parent = Split-Path $parent -Parent
38+
$gciParams['LiteralPath'] = $parent
39+
$gciParams['Recurse'] = $true
40+
}
41+
}
42+
43+
$srcRelRootPathLength = $gciParams['LiteralPath'].Length
44+
45+
# Generate a FileCopyInfo object for every file expanded by the wildcard spec.
46+
$files = @(Microsoft.PowerShell.Management\Get-ChildItem @gciParams)
47+
foreach ($file in $files) {
48+
$fileSrcPath = $file.FullName
49+
$relPath = $fileSrcPath.Substring($srcRelRootPathLength)
50+
$fileDstPath = Join-Path $dstPath $relPath
51+
New-FileSystemCopyInfo $fileSrcPath $fileDstPath
52+
}
53+
54+
# Copy over empty directories - if any.
55+
$gciParams.Remove('File')
56+
$gciParams['Directory'] = $true
57+
$dirs = @(Microsoft.PowerShell.Management\Get-ChildItem @gciParams |
58+
Where-Object { $_.GetFileSystemInfos().Length -eq 0 })
59+
foreach ($dir in $dirs) {
60+
$dirSrcPath = $dir.FullName
61+
$relPath = $dirSrcPath.Substring($srcRelRootPathLength)
62+
$dirDstPath = Join-Path $dstPath $relPath
63+
New-FileSystemCopyInfo $dirSrcPath $dirDstPath
64+
}
65+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function Get-ColorForOperation {
2+
param(
3+
$operation
4+
)
5+
switch ($operation) {
6+
$LocalizedData.OpConflict { 'Red' }
7+
$LocalizedData.OpCreate { 'Green' }
8+
$LocalizedData.OpForce { 'Yellow' }
9+
$LocalizedData.OpIdentical { 'Cyan' }
10+
$LocalizedData.OpModify { 'Magenta' }
11+
$LocalizedData.OpUpdate { 'Green' }
12+
$LocalizedData.OpMissing { 'Red' }
13+
$LocalizedData.OpVerify { 'Green' }
14+
default { $Host.UI.RawUI.ForegroundColor }
15+
}
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function Get-ErrorLocationFileAttrVal {
2+
param(
3+
[string]$ElementName,
4+
[string]$AttributeName
5+
)
6+
$LocalizedData.ExpressionErrorLocationFile_F2 -f $ElementName, $AttributeName
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function Get-ErrorLocationModifyAttrVal {
2+
param(
3+
[string]$AttributeName
4+
)
5+
$LocalizedData.ExpressionErrorLocationModify_F1 -f $AttributeName
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function Get-ErrorLocationNewModManifestAttrVal {
2+
param(
3+
[string]$AttributeName
4+
)
5+
$LocalizedData.ExpressionErrorLocationNewModManifest_F1 -f $AttributeName
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function Get-ErrorLocationParameterAttrVal {
2+
param(
3+
[string]$ParameterName,
4+
[string]$AttributeName
5+
)
6+
$LocalizedData.ExpressionErrorLocationParameter_F2 -f $ParameterName, $AttributeName
7+
}

0 commit comments

Comments
 (0)