|
3 | 3 | [Parameter(Mandatory)] [String] $version, |
4 | 4 | [Parameter(Mandatory)] [String] $vs, |
5 | 5 | [Parameter(Mandatory)] [String] $arch, |
6 | | - [Parameter(Mandatory)] [String] $stability |
| 6 | + [Parameter(Mandatory)] [String] $stability, |
| 7 | + [String] $workflow_run_id = "", |
| 8 | + [String] $workflow_run_ids = "", |
| 9 | + [String] $repository = "" |
7 | 10 | ) |
8 | 11 |
|
9 | 12 | $ErrorActionPreference = "Stop" |
@@ -31,50 +34,165 @@ if (-not $deps) { |
31 | 34 | exit |
32 | 35 | } |
33 | 36 |
|
34 | | -$needs = @{} |
35 | | -$response = Invoke-WebRequest -Uri "https://downloads.php.net/~windows/php-sdk/deps/series/packages-$version-$vs-$arch-$stability.txt" -UseBasicParsing |
36 | | -foreach ($line in $response.Content -split "\r?\n") { |
37 | | - foreach ($dep in $deps) { |
38 | | - if ($line -match "$dep-(.+)-$vs-$arch.zip") { |
39 | | - $needs.$dep = $matches[1] |
40 | | - } |
| 37 | +function Get-GitHubApiHeaders { |
| 38 | + $headers = @{ |
| 39 | + "Accept" = "application/vnd.github+json"; |
| 40 | + "X-GitHub-Api-Version" = "2022-11-28"; |
| 41 | + "User-Agent" = "winlib-builder" |
| 42 | + } |
| 43 | + |
| 44 | + if ($env:GITHUB_TOKEN) { |
| 45 | + $headers.Authorization = "Bearer $env:GITHUB_TOKEN" |
| 46 | + } else { |
| 47 | + Write-Warning "GITHUB_TOKEN is not set. GitHub API requests may fail or be rate-limited." |
41 | 48 | } |
| 49 | + |
| 50 | + return $headers |
42 | 51 | } |
43 | 52 |
|
44 | | -New-Item "deps" -ItemType "directory" |
| 53 | +function Resolve-GitHubArtifactDependencies { |
| 54 | + param ( |
| 55 | + [String[]] $Dependencies, |
| 56 | + [Object[]] $RunIds, |
| 57 | + [String] $Repository, |
| 58 | + [String] $Destination |
| 59 | + ) |
45 | 60 |
|
46 | | -$baseurl = "https://downloads.php.net/~windows/php-sdk/deps/$vs/$arch" |
47 | | -foreach ($dep in $needs.GetEnumerator()) { |
48 | | - Write-Output "Fetching $($dep.Name)-$($dep.Value)" |
49 | | - $temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru |
50 | | - $url = "$baseurl/$($dep.Name)-$($dep.Value)-$vs-$arch.zip" |
51 | | - Invoke-WebRequest $url -OutFile $temp |
52 | | - Expand-Archive $temp -DestinationPath "deps" |
| 61 | + $resolved = @{} |
| 62 | + $runIds = @( |
| 63 | + foreach ($runIdValue in $RunIds) { |
| 64 | + if ($null -eq $runIdValue) { |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + $runIdValue.ToString() -split "[,\s]+" | |
| 69 | + ForEach-Object { $_.Trim() } | |
| 70 | + Where-Object { -not [string]::IsNullOrWhiteSpace($_) } |
| 71 | + } |
| 72 | + ) |
| 73 | + if ($runIds.Count -eq 0) { |
| 74 | + return $resolved |
| 75 | + } |
| 76 | + |
| 77 | + if ([string]::IsNullOrWhiteSpace($Repository)) { |
| 78 | + $Repository = $env:GITHUB_REPOSITORY |
| 79 | + } |
| 80 | + if ([string]::IsNullOrWhiteSpace($Repository)) { |
| 81 | + $Repository = "winlibs/winlib-builder" |
| 82 | + } |
| 83 | + |
| 84 | + $headers = Get-GitHubApiHeaders |
| 85 | + foreach ($runId in $runIds) { |
| 86 | + Write-Host "Fetching dependency artifacts from $Repository workflow run $runId" |
| 87 | + $artifactsUrl = "https://api.github.com/repos/$Repository/actions/runs/$runId/artifacts?per_page=100" |
| 88 | + $response = Invoke-RestMethod -Uri $artifactsUrl -Headers $headers -Method Get |
| 89 | + if ($response.total_count -eq 0) { |
| 90 | + Write-Warning "No artifacts found for workflow run $runId" |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + foreach ($dep in $Dependencies) { |
| 95 | + if ($resolved.ContainsKey($dep)) { |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + $artifactPattern = "^$([regex]::Escape($dep))-\d.*-$([regex]::Escape($vs))-$([regex]::Escape($arch))$" |
| 100 | + $matchingArtifacts = @($response.artifacts | Where-Object { $_.name -match $artifactPattern }) |
| 101 | + $artifact = @($matchingArtifacts | Where-Object { -not $_.expired } | Select-Object -First 1) |
| 102 | + |
| 103 | + if ($artifact.Count -eq 0) { |
| 104 | + if ($matchingArtifacts.Count -gt 0) { |
| 105 | + Write-Warning "Matching artifact for $dep in workflow run $runId is expired" |
| 106 | + } |
| 107 | + continue |
| 108 | + } |
| 109 | + |
| 110 | + Write-Host "Fetching $($artifact[0].name) from workflow run $runId" |
| 111 | + $temp = New-TemporaryFile | Rename-Item -NewName { $_.Name + ".zip" } -PassThru |
| 112 | + try { |
| 113 | + Invoke-WebRequest -Uri $artifact[0].archive_download_url -Headers $headers -OutFile $temp |
| 114 | + Expand-Archive -Path $temp -DestinationPath $Destination -Force |
| 115 | + $resolved[$dep] = $artifact[0].name |
| 116 | + } finally { |
| 117 | + Remove-Item -LiteralPath $temp.FullName -Force -ErrorAction SilentlyContinue |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return $resolved |
53 | 123 | } |
54 | 124 |
|
55 | | -$deps = $deps | Where-Object {$needs.Keys -NotContains $_} |
56 | | -if ($deps.Count -gt 0) { |
57 | | - $needs = @{} |
58 | | - $response = Invoke-WebRequest -Uri "https://downloads.php.net/~windows/pecl/deps/packages.txt" -UseBasicParsing |
| 125 | +function Resolve-DownloadDependencies { |
| 126 | + param ( |
| 127 | + [String[]] $Dependencies, |
| 128 | + [String] $PackagesUrl, |
| 129 | + [String] $BaseUrl |
| 130 | + ) |
| 131 | + |
| 132 | + $resolved = @{} |
| 133 | + $response = Invoke-WebRequest -Uri $PackagesUrl -UseBasicParsing |
59 | 134 | foreach ($line in $response.Content -split "\r?\n") { |
60 | | - foreach ($dep in $deps) { |
61 | | - if ($line -match "$dep-(.+)-$vs-$arch.zip") { |
62 | | - $needs.$dep = $matches[1] |
| 135 | + foreach ($dep in $Dependencies) { |
| 136 | + if ($resolved.ContainsKey($dep)) { |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + if ($line -match "^$([regex]::Escape($dep))-(.+)-$([regex]::Escape($vs))-$([regex]::Escape($arch))\.zip$") { |
| 141 | + $resolved[$dep] = @{ |
| 142 | + Version = $matches[1]; |
| 143 | + BaseUrl = $BaseUrl |
| 144 | + } |
63 | 145 | } |
64 | 146 | } |
65 | 147 | } |
66 | 148 |
|
67 | | - $baseurl = "https://downloads.php.net/~windows/pecl/deps" |
68 | | - foreach ($dep in $needs.GetEnumerator()) { |
69 | | - Write-Output "Fetching $($dep.Name)-$($dep.Value)" |
70 | | - $temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru |
71 | | - $url = "$baseurl/$($dep.Name)-$($dep.Value)-$vs-$arch.zip" |
72 | | - Invoke-WebRequest $url -OutFile $temp |
73 | | - Expand-Archive $temp -DestinationPath "deps" |
| 149 | + return $resolved |
| 150 | +} |
| 151 | + |
| 152 | +New-Item "deps" -ItemType "directory" -Force | Out-Null |
| 153 | + |
| 154 | +$artifactDeps = Resolve-GitHubArtifactDependencies ` |
| 155 | + -Dependencies $deps ` |
| 156 | + -RunIds @($workflow_run_ids, $workflow_run_id) ` |
| 157 | + -Repository $repository ` |
| 158 | + -Destination "deps" |
| 159 | + |
| 160 | +$deps = $deps | Where-Object { -not $artifactDeps.ContainsKey($_) } |
| 161 | +if ($deps.Count -eq 0) { |
| 162 | + exit |
| 163 | +} |
| 164 | + |
| 165 | +$needs = Resolve-DownloadDependencies ` |
| 166 | + -Dependencies $deps ` |
| 167 | + -PackagesUrl "https://downloads.php.net/~windows/php-sdk/deps/series/packages-$version-$vs-$arch-$stability.txt" ` |
| 168 | + -BaseUrl "https://downloads.php.net/~windows/php-sdk/deps/$vs/$arch" |
| 169 | + |
| 170 | +$deps = $deps | Where-Object {$needs.Keys -NotContains $_} |
| 171 | +if ($deps.Count -gt 0) { |
| 172 | + $peclNeeds = Resolve-DownloadDependencies ` |
| 173 | + -Dependencies $deps ` |
| 174 | + -PackagesUrl "https://downloads.php.net/~windows/pecl/deps/packages.txt" ` |
| 175 | + -BaseUrl "https://downloads.php.net/~windows/pecl/deps" |
| 176 | + |
| 177 | + foreach ($dep in $peclNeeds.Keys) { |
| 178 | + $needs[$dep] = $peclNeeds[$dep] |
74 | 179 | } |
75 | 180 | } |
76 | 181 |
|
77 | 182 | $deps = $deps | Where-Object {$needs.Keys -NotContains $_} |
78 | 183 | if ($deps.Count -gt 0) { |
79 | 184 | throw "dependencies not found: $deps" |
80 | 185 | } |
| 186 | + |
| 187 | +foreach ($dep in $needs.Keys) { |
| 188 | + $package = $needs[$dep] |
| 189 | + $url = "$($package.BaseUrl)/$dep-$($package.Version)-$vs-$arch.zip" |
| 190 | + Write-Output "Fetching $url" |
| 191 | + $temp = New-TemporaryFile | Rename-Item -NewName {$_.Name + ".zip"} -PassThru |
| 192 | + try { |
| 193 | + Invoke-WebRequest -Uri $url -OutFile $temp |
| 194 | + Expand-Archive -Path $temp -DestinationPath "deps" -Force |
| 195 | + } finally { |
| 196 | + Remove-Item -LiteralPath $temp.FullName -Force -ErrorAction SilentlyContinue |
| 197 | + } |
| 198 | +} |
0 commit comments