|
| 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 | +} |
0 commit comments