|
| 1 | +$ErrorActionPreference = 'Stop' |
| 2 | + |
| 3 | +$workspaceRoot = [System.IO.Path]::GetFullPath((Get-Location).Path) |
| 4 | +$sourceRoot = [System.IO.Path]::GetFullPath((Join-Path $workspaceRoot 'bin/ci-artifacts/replacements')) |
| 5 | +$targetRoot = [System.IO.Path]::GetFullPath((Join-Path $workspaceRoot $env:TARGET_ROOT)) |
| 6 | + |
| 7 | +function Test-IsUnderRoot { |
| 8 | + param( |
| 9 | + [string] $Path, |
| 10 | + [string] $Root |
| 11 | + ) |
| 12 | + |
| 13 | + $normalizedRoot = [System.IO.Path]::TrimEndingDirectorySeparator($Root) |
| 14 | + $normalizedPath = [System.IO.Path]::TrimEndingDirectorySeparator($Path) |
| 15 | + |
| 16 | + return $normalizedPath -eq $normalizedRoot -or $normalizedPath.StartsWith($normalizedRoot + [System.IO.Path]::DirectorySeparatorChar, [System.StringComparison]::OrdinalIgnoreCase) |
| 17 | +} |
| 18 | + |
| 19 | +function Get-SafeFullPath { |
| 20 | + param( |
| 21 | + [string] $Root, |
| 22 | + [string] $RelativePath |
| 23 | + ) |
| 24 | + |
| 25 | + if ([string]::IsNullOrWhiteSpace($RelativePath)) { |
| 26 | + throw 'Replacement path must not be empty.' |
| 27 | + } |
| 28 | + |
| 29 | + if ($RelativePath -match '^[\\/]' -or $RelativePath -match '^[A-Za-z]:') { |
| 30 | + throw "Replacement path '$RelativePath' must be relative." |
| 31 | + } |
| 32 | + |
| 33 | + $segments = $RelativePath -split '[\\/]+' | Where-Object { $_ -ne '' } |
| 34 | + if ($segments.Count -eq 0) { |
| 35 | + throw "Replacement path '$RelativePath' must not be empty." |
| 36 | + } |
| 37 | + |
| 38 | + foreach ($segment in $segments) { |
| 39 | + if ($segment -eq '.' -or $segment -eq '..') { |
| 40 | + throw "Replacement path '$RelativePath' must not contain '.' or '..' segments." |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + $combinedPath = [System.IO.Path]::GetFullPath((Join-Path $Root $RelativePath)) |
| 45 | + if (-not (Test-IsUnderRoot -Path $combinedPath -Root $Root)) { |
| 46 | + throw "Replacement path '$RelativePath' escapes root '$Root'." |
| 47 | + } |
| 48 | + |
| 49 | + return $combinedPath |
| 50 | +} |
| 51 | + |
| 52 | +function Get-ReplacementFileList { |
| 53 | + param( |
| 54 | + [string] $Path, |
| 55 | + [string] $DisplayRoot |
| 56 | + ) |
| 57 | + |
| 58 | + if (-not (Test-Path -LiteralPath $Path)) { |
| 59 | + return @() |
| 60 | + } |
| 61 | + |
| 62 | + $item = Get-Item -LiteralPath $Path -Force |
| 63 | + if ($item.PSIsContainer) { |
| 64 | + $items = Get-ChildItem -LiteralPath $Path -File -Recurse | ForEach-Object { |
| 65 | + $relativePath = [System.IO.Path]::GetRelativePath($Path, $_.FullName) |
| 66 | + ($DisplayRoot.TrimEnd('/', '\') + '/' + $relativePath.Replace('\', '/')).TrimStart('/') |
| 67 | + } |
| 68 | + |
| 69 | + return @($items | Sort-Object -Unique) |
| 70 | + } |
| 71 | + |
| 72 | + return @($DisplayRoot.Replace('\', '/')) |
| 73 | +} |
| 74 | + |
| 75 | +$replacementEntries = ($env:REPLACE_ARTIFACTS ?? '') -split '\r?\n' |
| 76 | + |
| 77 | +foreach ($rawEntry in $replacementEntries) { |
| 78 | + $replacement = $rawEntry.Trim() |
| 79 | + if ([string]::IsNullOrWhiteSpace($replacement)) { |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + $separatorIndex = $replacement.IndexOf('!') |
| 84 | + if ($separatorIndex -lt 1 -or $separatorIndex -eq ($replacement.Length - 1)) { |
| 85 | + throw "Invalid replacement entry '$replacement'. Expected <artifact_name>!<path>." |
| 86 | + } |
| 87 | + |
| 88 | + $artifactName = $replacement.Substring(0, $separatorIndex).Trim() |
| 89 | + $replacePath = $replacement.Substring($separatorIndex + 1).Trim() |
| 90 | + |
| 91 | + if ($artifactName -match '[\\/:*?"<>|]') { |
| 92 | + throw "Artifact name '$artifactName' contains invalid path characters." |
| 93 | + } |
| 94 | + |
| 95 | + $artifactRoot = [System.IO.Path]::GetFullPath((Join-Path $sourceRoot $artifactName)) |
| 96 | + if (-not (Test-IsUnderRoot -Path $artifactRoot -Root $sourceRoot)) { |
| 97 | + throw "Artifact name '$artifactName' resolves outside '$sourceRoot'." |
| 98 | + } |
| 99 | + |
| 100 | + $sourcePath = Get-SafeFullPath -Root $artifactRoot -RelativePath $replacePath |
| 101 | + $targetPath = Get-SafeFullPath -Root $targetRoot -RelativePath $replacePath |
| 102 | + |
| 103 | + Write-Output "::group::Replace $replacePath from $artifactName" |
| 104 | + |
| 105 | + if (-not (Test-Path -LiteralPath $sourcePath)) { |
| 106 | + throw "Replacement source '$sourcePath' does not exist." |
| 107 | + } |
| 108 | + |
| 109 | + $deletedFiles = Get-ReplacementFileList -Path $targetPath -DisplayRoot $replacePath |
| 110 | + $addedFiles = Get-ReplacementFileList -Path $sourcePath -DisplayRoot $replacePath |
| 111 | + |
| 112 | + $targetParent = Split-Path -Parent $targetPath |
| 113 | + if (-not [string]::IsNullOrWhiteSpace($targetParent)) { |
| 114 | + New-Item -ItemType Directory -Path $targetParent -Force | Out-Null |
| 115 | + } |
| 116 | + |
| 117 | + if (Test-Path -LiteralPath $targetPath) { |
| 118 | + Remove-Item -LiteralPath $targetPath -Recurse -Force |
| 119 | + } |
| 120 | + |
| 121 | + Copy-Item -LiteralPath $sourcePath -Destination $targetPath -Recurse -Force |
| 122 | + |
| 123 | + if ($deletedFiles.Count -gt 0) { |
| 124 | + Write-Output 'Deleted files:' |
| 125 | + $deletedFiles | ForEach-Object { Write-Output " $_" } |
| 126 | + } |
| 127 | + else { |
| 128 | + Write-Output 'Deleted files: (none)' |
| 129 | + } |
| 130 | + |
| 131 | + if ($addedFiles.Count -gt 0) { |
| 132 | + Write-Output 'Added files:' |
| 133 | + $addedFiles | ForEach-Object { Write-Output " $_" } |
| 134 | + } |
| 135 | + else { |
| 136 | + Write-Output 'Added files: (none)' |
| 137 | + } |
| 138 | + |
| 139 | + $deletedOnly = @($deletedFiles | Where-Object { $_ -notin $addedFiles }) |
| 140 | + $addedOnly = @($addedFiles | Where-Object { $_ -notin $deletedFiles }) |
| 141 | + |
| 142 | + if ($deletedOnly.Count -gt 0 -or $addedOnly.Count -gt 0) { |
| 143 | + $warningDetails = New-Object System.Collections.Generic.List[string] |
| 144 | + $warningDetails.Add("Replacement '$replacement' changed the file list.") |
| 145 | + |
| 146 | + if ($deletedOnly.Count -gt 0) { |
| 147 | + $warningDetails.Add('Deleted but not added: ' + ($deletedOnly -join ', ')) |
| 148 | + } |
| 149 | + |
| 150 | + if ($addedOnly.Count -gt 0) { |
| 151 | + $warningDetails.Add('Added but not deleted: ' + ($addedOnly -join ', ')) |
| 152 | + } |
| 153 | + |
| 154 | + Write-Output "::warning::$($warningDetails -join ' ')" |
| 155 | + } |
| 156 | + |
| 157 | + Write-Output "::endgroup::" |
| 158 | +} |
0 commit comments