|
| 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. |
| 12 | +function Copy-FileWithConflictDetection { |
| 13 | + [CmdletBinding(SupportsShouldProcess = $true)] |
| 14 | + param( |
| 15 | + [string]$SrcPath, |
| 16 | + [string]$DstPath |
| 17 | + ) |
| 18 | + # Just double-checking that DstPath parameter is an absolute path otherwise |
| 19 | + # it could fail the check that the DstPath is under the overall DestinationPath. |
| 20 | + if (![System.IO.Path]::IsPathRooted($DstPath)) { |
| 21 | + $DstPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($DstPath) |
| 22 | + } |
| 23 | + |
| 24 | + # Check if DstPath file conflicts with an existing SrcPath file. |
| 25 | + $operation = $LocalizedData.OpCreate |
| 26 | + $opmessage = (ConvertTo-DestinationRelativePath $DstPath) |
| 27 | + if (Test-Path -LiteralPath $DstPath) { |
| 28 | + if (Test-FilesIdentical $SrcPath $DstPath) { |
| 29 | + $operation = $LocalizedData.OpIdentical |
| 30 | + } elseif ($script:templateCreatedFiles.ContainsKey($DstPath)) { |
| 31 | + # Plaster created this file previously during template invocation |
| 32 | + # therefore, there is no conflict. We're simply updating the file. |
| 33 | + $operation = $LocalizedData.OpUpdate |
| 34 | + } elseif ($Force) { |
| 35 | + $operation = $LocalizedData.OpForce |
| 36 | + } else { |
| 37 | + $operation = $LocalizedData.OpConflict |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + # Copy the file to the destination |
| 42 | + if ($PSCmdlet.ShouldProcess($DstPath, $operation)) { |
| 43 | + Write-OperationStatus $operation $opmessage |
| 44 | + |
| 45 | + if ($operation -eq $LocalizedData.OpIdentical) { |
| 46 | + # If the files are identical, no need to do anything |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + if (($operation -eq $LocalizedData.OpCreate) -or ($operation -eq $LocalizedData.OpUpdate)) { |
| 51 | + Copy-Item -LiteralPath $SrcPath -Destination $DstPath |
| 52 | + if ($PassThru) { |
| 53 | + $InvokePlasterInfo.CreatedFiles += $DstPath |
| 54 | + } |
| 55 | + $script:templateCreatedFiles[$DstPath] = $null |
| 56 | + } elseif ($Force -or $PSCmdlet.ShouldContinue(($LocalizedData.OverwriteFile_F1 -f $DstPath), |
| 57 | + $LocalizedData.FileConflict, |
| 58 | + [ref]$script:fileConflictConfirmYesToAll, |
| 59 | + [ref]$script:fileConflictConfirmNoToAll)) { |
| 60 | + $backupFilename = New-BackupFilename $DstPath |
| 61 | + Copy-Item -LiteralPath $DstPath -Destination $backupFilename |
| 62 | + Copy-Item -LiteralPath $SrcPath -Destination $DstPath |
| 63 | + if ($PassThru) { |
| 64 | + $InvokePlasterInfo.UpdatedFiles += $DstPath |
| 65 | + } |
| 66 | + $script:templateCreatedFiles[$DstPath] = $null |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments