diff --git a/src/scripts/extensions/add_extensions.ps1 b/src/scripts/extensions/add_extensions.ps1 index 4168f660..af700e50 100644 --- a/src/scripts/extensions/add_extensions.ps1 +++ b/src/scripts/extensions/add_extensions.ps1 @@ -65,7 +65,7 @@ Function Add-NightlyExtension { if($ts) { $ts_part = 'ts' } else { $ts_part = 'nts' } $repo = "$github/shivammathur/php-extensions-windows" $url = "$repo/releases/download/builds/php$version`_$ts_part`_$arch`_$extension.dll" - Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile "$ext_dir\php_$extension.dll" + Get-File -Url $url -OutFile "$ext_dir\php_$extension.dll" if(Test-Path "$ext_dir\php_$extension.dll") { Enable-Extension $extension > $null } else { diff --git a/src/scripts/extensions/blackfire.ps1 b/src/scripts/extensions/blackfire.ps1 index 49a9e04a..78013809 100644 --- a/src/scripts/extensions/blackfire.ps1 +++ b/src/scripts/extensions/blackfire.ps1 @@ -21,7 +21,7 @@ Function Add-Blackfire() { $status="Enabled" } else { $nts = if (!$installed.ThreadSafe) { "_nts" } else { "" } - Invoke-WebRequest -Uri "https://packages.blackfire.io/binaries/blackfire-php/${extension_version}/blackfire-php-windows_${arch}-php-${no_dot_version}${nts}.dll" -OutFile $ext_dir\blackfire.dll > $null 2>&1 + Get-File -Url "https://packages.blackfire.io/binaries/blackfire-php/${extension_version}/blackfire-php-windows_${arch}-php-${no_dot_version}${nts}.dll" -OutFile $ext_dir\blackfire.dll > $null 2>&1 Enable-PhpExtension -Extension blackfire -Path $php_dir $status="Installed and enabled" } diff --git a/src/scripts/extensions/http.ps1 b/src/scripts/extensions/http.ps1 index a244fd6e..16d9da3a 100644 --- a/src/scripts/extensions/http.ps1 +++ b/src/scripts/extensions/http.ps1 @@ -13,7 +13,7 @@ Function Get-ICUUrl() { $trunk = "https://windows.php.net" $urls=@("${trunk}/downloads/php-sdk/deps/${vs_version}/${arch}", "${trunk}/downloads/php-sdk/deps/archives/${vs_version}/${arch}") foreach ($url in $urls) { - $web_content = Invoke-WebRequest -Uri $url + $web_content = Get-File -Url $url foreach ($link in $web_content.Links) { if ($link -match "/.*ICU-${icu_version}.*/") { return $trunk + $link.HREF @@ -32,7 +32,7 @@ Function Repair-ICU() { $zip_url = Get-ICUUrl $icu.Groups[1].Value $installed.Architecture $vs if ($zip_url -ne '') { New-Item -Path "$php_dir" -Name "icu" -ItemType "directory" -Force > $null 2>&1 - Invoke-WebRequest -Uri $zip_url -OutFile "$php_dir\icu\icu.zip" + Get-File -Url $zip_url -OutFile "$php_dir\icu\icu.zip" Expand-Archive -Path $php_dir\icu\icu.zip -DestinationPath $php_dir\icu -Force Get-ChildItem $php_dir\icu\bin -Filter *.dll | Copy-Item -Destination $php_dir -Force } diff --git a/src/scripts/extensions/ioncube.ps1 b/src/scripts/extensions/ioncube.ps1 index d5a9cab0..88b09e69 100644 --- a/src/scripts/extensions/ioncube.ps1 +++ b/src/scripts/extensions/ioncube.ps1 @@ -20,7 +20,7 @@ Function Add-Ioncube() { if (-not($installed.ThreadSafe)) { $ts_part = "_nonts" } - Invoke-WebRequest -Uri "https://downloads.ioncube.com/loader_downloads/ioncube_loaders_win$ts_part`_vc$vc`_$arch_part.zip" -OutFile $ext_dir\ioncube.zip + Get-File -Url "https://downloads.ioncube.com/loader_downloads/ioncube_loaders_win$ts_part`_vc$vc`_$arch_part.zip" -OutFile $ext_dir\ioncube.zip Expand-Archive -Path $ext_dir\ioncube.zip -DestinationPath $ext_dir -Force Copy-Item $ext_dir\ioncube\ioncube_loader_win_$version.dll $ext_dir\php_ioncube.dll } diff --git a/src/scripts/extensions/oci.ps1 b/src/scripts/extensions/oci.ps1 index d89d1764..3cd21308 100644 --- a/src/scripts/extensions/oci.ps1 +++ b/src/scripts/extensions/oci.ps1 @@ -14,7 +14,7 @@ Function Add-InstantClient() { if ($arch -eq 'x86') { $suffix = 'nt' } - Invoke-WebRequest -Uri https://download.oracle.com/otn_software/nt/instantclient/instantclient-basiclite-$suffix.zip -OutFile $php_dir\instantclient.zip + Get-File -Url https://download.oracle.com/otn_software/nt/instantclient/instantclient-basiclite-$suffix.zip -OutFile $php_dir\instantclient.zip Expand-Archive -Path $php_dir\instantclient.zip -DestinationPath $php_dir -Force Copy-Item $php_dir\instantclient*\* $php_dir } @@ -71,7 +71,7 @@ Function Add-Oci() { Copy-Item -Path $oci8DLL -Destination $ext_dir\php_oci8.dll } else { $status = 'Installed and enabled' - Invoke-WebRequest -Uri (Get-Oci8Url) -OutFile $php_dir\oci8.zip + Get-File -Url (Get-Oci8Url) -OutFile $php_dir\oci8.zip Expand-Archive -Path $php_dir\oci8.zip -DestinationPath $ext_dir -Force } } diff --git a/src/scripts/extensions/phalcon.ps1 b/src/scripts/extensions/phalcon.ps1 index b5039579..3d00873a 100644 --- a/src/scripts/extensions/phalcon.ps1 +++ b/src/scripts/extensions/phalcon.ps1 @@ -13,14 +13,14 @@ Function Get-PhalconReleaseAssetUrl() { try { $match = (Invoke-RestMethod -Uri "$domain/$releases/tags/v$Semver").assets | Select-String -Pattern "browser_download_url=.*(phalcon_${arch}_.*_php${version}_${extension_version}.*[0-9]${nts}.zip)" } catch { - $match = (Invoke-WebRequest -Uri "$github/$releases/expanded_assets/v$Semver").Links.href | Select-String -Pattern "(phalcon_${arch}_.*_php${version}_${extension_version}.*[0-9]${nts}.zip)" + $match = (Get-File -Url "$github/$releases/expanded_assets/v$Semver").Links.href | Select-String -Pattern "(phalcon_${arch}_.*_php${version}_${extension_version}.*[0-9]${nts}.zip)" } } else { $nts = if (!$installed.ThreadSafe) { "-nts" } else { "-ts" } try { $match = (Invoke-RestMethod -Uri "$domain/$releases/tags/v$Semver").assets | Select-String -Pattern "browser_download_url=.*(phalcon-php${version}${nts}-windows.*-x64.zip)" } catch { - $match = (Invoke-WebRequest -Uri "$github/$releases/expanded_assets/v$Semver").Links.href | Select-String -Pattern "(phalcon-php${version}${nts}-windows.*-x64.zip)" + $match = (Get-File -Url "$github/$releases/expanded_assets/v$Semver").Links.href | Select-String -Pattern "(phalcon-php${version}${nts}-windows.*-x64.zip)" } } if($NULL -ne $match) { @@ -39,7 +39,7 @@ Function Add-PhalconFromGitHub() { ) $zip_url = Get-PhalconReleaseAssetUrl $Semver if($zip_url) { - Invoke-WebRequest -Uri $zip_url -OutFile $ENV:RUNNER_TOOL_CACHE\phalcon.zip > $null 2>&1 + Get-File -Url $zip_url -OutFile $ENV:RUNNER_TOOL_CACHE\phalcon.zip > $null 2>&1 Expand-Archive -Path $ENV:RUNNER_TOOL_CACHE\phalcon.zip -DestinationPath $ENV:RUNNER_TOOL_CACHE\phalcon -Force > $null 2>&1 Copy-Item -Path "$ENV:RUNNER_TOOL_CACHE\phalcon\php_phalcon.dll" -Destination "$ext_dir\php_phalcon.dll" Enable-PhpExtension -Extension phalcon -Path $php_dir diff --git a/src/scripts/extensions/zephir_parser.ps1 b/src/scripts/extensions/zephir_parser.ps1 index 17d1dc9e..b7dbe734 100644 --- a/src/scripts/extensions/zephir_parser.ps1 +++ b/src/scripts/extensions/zephir_parser.ps1 @@ -11,7 +11,7 @@ Function Get-ZephirParserReleaseAssetUrl() { try { $match = (Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/tags/$extension_version").assets | Select-String -Pattern "browser_download_url=.*(zephir_parser-php-${version}.*windows.*.zip)" } catch { - $match = (Invoke-WebRequest -Uri "$zp_releases/expanded_assets/$extension_version").Links.href | Select-String -Pattern "(zephir_parser-php-${version}.*windows.*.zip)" + $match = (Get-File -Url "$zp_releases/expanded_assets/$extension_version").Links.href | Select-String -Pattern "(zephir_parser-php-${version}.*windows.*.zip)" } if($NULL -ne $match) { return "$zp_releases/download/$extension_version/$($match.Matches[0].Groups[1].Value)" @@ -30,7 +30,7 @@ Function Get-ZephirParserVersion() { $repo = 'zephir-lang/php-zephir-parser' $zp_releases = "$github/$repo/releases" if($extension -eq 'zephir_parser') { - return (Invoke-WebRequest -UseBasicParsing -Uri $zp_releases/latest).BaseResponse.RequestMessage.RequestUri.Segments[-1] + return (Get-File -Url $zp_releases/latest).BaseResponse.RequestMessage.RequestUri.Segments[-1] } else { return 'v' + ($extension.split('-')[1] -replace 'v') } @@ -47,7 +47,7 @@ Function Add-ZephirParserFromGitHub() { $extension_version = Get-ZephirParserVersion $extension $zip_url = Get-ZephirParserReleaseAssetUrl $extension_version if($zip_url) { - Invoke-WebRequest -Uri $zip_url -OutFile $ENV:RUNNER_TOOL_CACHE\zp.zip > $null 2>&1 + Get-File -Url $zip_url -OutFile $ENV:RUNNER_TOOL_CACHE\zp.zip > $null 2>&1 Expand-Archive -Path $ENV:RUNNER_TOOL_CACHE\zp.zip -DestinationPath $ENV:RUNNER_TOOL_CACHE\zp -Force > $null 2>&1 Copy-Item -Path "$ENV:RUNNER_TOOL_CACHE\zp\php_zephir_parser.dll" -Destination "$ext_dir\php_zephir_parser.dll" Enable-PhpExtension -Extension zephir_parser -Path $php_dir diff --git a/src/scripts/tools/add_tools.ps1 b/src/scripts/tools/add_tools.ps1 index ef678fbc..364410da 100644 --- a/src/scripts/tools/add_tools.ps1 +++ b/src/scripts/tools/add_tools.ps1 @@ -152,7 +152,7 @@ Function Add-Tool() { } catch { if($url -match '.*github.com.*releases.*latest.*') { try { - $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Invoke-WebRequest -Uri ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0]) + $url = $url.replace("releases/latest/download", "releases/download/" + ([regex]::match((Get-File -Url ($url.split('/release')[0] + "/releases")).Content, "([0-9]+\.[0-9]+\.[0-9]+)/" + ($url.Substring($url.LastIndexOf("/") + 1))).Groups[0].Value).split('/')[0]) $status_code = (Invoke-WebRequest -Passthru -Uri $url -OutFile $tool_path).StatusCode } catch { } } diff --git a/src/scripts/tools/blackfire.ps1 b/src/scripts/tools/blackfire.ps1 index f319116c..4427ef7d 100644 --- a/src/scripts/tools/blackfire.ps1 +++ b/src/scripts/tools/blackfire.ps1 @@ -6,7 +6,7 @@ Function Add-Blackfire() { } $cli_version = (Invoke-RestMethod https://blackfire.io/api/v1/releases).cli $url = "https://packages.blackfire.io/binaries/blackfire/${cli_version}/blackfire-windows_${arch_name}.zip" - Invoke-WebRequest -Uri $url -OutFile $bin_dir\blackfire.zip >$null 2>&1 + Get-File -Url $url -OutFile $bin_dir\blackfire.zip >$null 2>&1 Expand-Archive -Path $bin_dir\blackfire.zip -DestinationPath $bin_dir -Force >$null 2>&1 Add-ToProfile $current_profile 'blackfire' "New-Alias blackfire $bin_dir\blackfire.exe" if ((Test-Path env:BLACKFIRE_SERVER_ID) -and (Test-Path env:BLACKFIRE_SERVER_TOKEN)) { diff --git a/src/scripts/tools/protoc.ps1 b/src/scripts/tools/protoc.ps1 index 704c3912..ea5e6172 100644 --- a/src/scripts/tools/protoc.ps1 +++ b/src/scripts/tools/protoc.ps1 @@ -1,7 +1,7 @@ Function Get-ProtobufTag() { $releases = 'https://github.com/protocolbuffers/protobuf/releases' if("$protobuf_tag" -eq "latest") { - $protobuf_tag = (Invoke-WebRequest -UseBasicParsing -Uri $releases/latest).BaseResponse.RequestMessage.RequestUri.Segments[-1] + $protobuf_tag = (Get-File -Url $releases/latest).BaseResponse.RequestMessage.RequestUri.Segments[-1] } else { try { $protobuf_tag = $protobuf_tag -replace '^v', '' @@ -11,7 +11,7 @@ Function Get-ProtobufTag() { $response.Close() $protobuf_tag = "v$protobuf_tag" } catch { - $protobuf_tag = (Invoke-WebRequest -UseBasicParsing -Uri $releases/latest).BaseResponse.RequestMessage.RequestUri.Segments[-1] + $protobuf_tag = (Get-File -Url $releases/latest).BaseResponse.RequestMessage.RequestUri.Segments[-1] } } return $protobuf_tag @@ -29,7 +29,7 @@ Function Add-Protoc() { $arch_num = '32' } $url = "https://github.com/protocolbuffers/protobuf/releases/download/$protobuf_tag/protoc-$($protobuf_tag -replace 'v', '')-win$arch_num.zip" - Invoke-WebRequest -Uri $url -OutFile $bin_dir\protoc.zip >$null 2>&1 + Get-File -Url $url -OutFile $bin_dir\protoc.zip >$null 2>&1 Expand-Archive -Path $bin_dir\protoc.zip -DestinationPath $bin_dir\protoc -Force >$null 2>&1 Move-Item -Path $bin_dir\protoc\bin\protoc.exe -Destination $bin_dir\protoc.exe Add-ToProfile $current_profile 'protoc' "New-Alias protoc $bin_dir\protoc.exe" diff --git a/src/scripts/tools/symfony.ps1 b/src/scripts/tools/symfony.ps1 index c52cc22f..3c334d2f 100644 --- a/src/scripts/tools/symfony.ps1 +++ b/src/scripts/tools/symfony.ps1 @@ -4,7 +4,7 @@ Function Add-Symfony() { $arch_name = '386' } $url = "https://github.com/symfony-cli/symfony-cli/releases/latest/download/symfony-cli_windows_${arch_name}.zip" - Invoke-WebRequest -Uri $url -OutFile $bin_dir\symfony.zip >$null 2>&1 + Get-File -Url $url -OutFile $bin_dir\symfony.zip >$null 2>&1 Expand-Archive -Path $bin_dir\symfony.zip -DestinationPath $bin_dir -Force >$null 2>&1 if(Test-Path $bin_dir\symfony.exe) { Copy-Item -Path $bin_dir\symfony.exe -Destination $bin_dir\symfony-cli.exe > $null 2>&1 diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index cbbcb1ac..bd5d4331 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -128,13 +128,47 @@ Function Add-EnvPATH { $env_data | Add-Content -Path $env_file -Encoding utf8 } +# Function to fetch a file from a URL. +Function Get-File { + param ( + [string]$Url, + [string]$FallbackUrl, + [string]$OutFile, + [int]$Retries = 3, + [int]$TimeoutSec = 0 + ) + + for ($i = 0; $i -lt $Retries; $i++) { + try { + if($null -ne $OutFile) { + Invoke-WebRequest -Uri $Url -OutFile $OutFile -TimeoutSec $TimeoutSec + } else { + Invoke-WebRequest -Uri $Url -TimeoutSec $TimeoutSec + } + break; + } catch { + if ($i -eq ($Retries - 1) -and ($null -ne $FallbackUrl)) { + try { + if($null -ne $OutFile) { + Invoke-WebRequest -Uri $FallbackUrl -OutFile $OutFile -TimeoutSec $TimeoutSec + } else { + Invoke-WebRequest -Uri $FallbackUrl -TimeoutSec $TimeoutSec + } + } catch { + throw "Failed to download the build" + } + } + } + } +} + # Function to make sure printf is in PATH. Function Add-Printf { if (-not(Test-Path "C:\Program Files\Git\usr\bin\printf.exe")) { if(Test-Path "C:\msys64\usr\bin\printf.exe") { New-Item -Path $bin_dir\printf.exe -ItemType SymbolicLink -Value C:\msys64\usr\bin\printf.exe -Force > $null 2>&1 } else { - Invoke-WebRequest -Uri "$github/shivammathur/printf/releases/latest/download/printf-x64.zip" -OutFile "$bin_dir\printf.zip" + Get-File -Url "$github/shivammathur/printf/releases/latest/download/printf-x64.zip" -OutFile "$bin_dir\printf.zip" Expand-Archive -Path $bin_dir\printf.zip -DestinationPath $bin_dir -Force } } else { @@ -166,7 +200,7 @@ Function Install-PSPackage() { $module_path = "$bin_dir\$psm1_path.psm1" if(-not (Test-Path $module_path -PathType Leaf)) { $zip_file = "$bin_dir\$package.zip" - Invoke-WebRequest -Uri $url -OutFile $zip_file + Get-File -Url $url -OutFile $zip_file Expand-Archive -Path $zip_file -DestinationPath $bin_dir -Force } Import-Module $module_path @@ -229,14 +263,14 @@ Function Set-PhpCache { throw "Asset not found" } } catch { - $release = Invoke-WebRequest $php_builder/releases/expanded_assets/php$version + $release = Get-File -Url $php_builder/releases/expanded_assets/php$version $asset = $release.links.href | ForEach-Object { if($_ -match "php-$version.[0-9]+$env:PHPTS-Win32-.*-$arch.zip") { return $_.split('/')[-1] } } } - Invoke-WebRequest -UseBasicParsing -Uri $php_builder/releases/download/php$version/$asset -OutFile $php_dir\$asset + Get-File -Url $php_builder/releases/download/php$version/$asset -OutFile $php_dir\$asset Set-PhpDownloadCache -Path $php_dir CurrentUser } catch { } } @@ -250,14 +284,14 @@ Function Add-DebugSymbols { return $_.name } } - Invoke-WebRequest -UseBasicParsing -Uri $php_builder/releases/download/php$version/$asset -OutFile $php_dir\$asset + Get-File -Url $php_builder/releases/download/php$version/$asset -OutFile $php_dir\$asset Expand-Archive -Path $php_dir\$asset -DestinationPath $php_dir -Force Get-ChildItem -Path $php_dir -Filter php_*.pdb | Move-Item -Destination $ext_dir } # Function to install nightly version of PHP Function Install-PhpNightly { - Invoke-WebRequest -UseBasicParsing -Uri $php_builder/releases/latest/download/Get-PhpNightly.ps1 -OutFile $php_dir\Get-PhpNightly.ps1 > $null 2>&1 + Get-File -Url $php_builder/releases/latest/download/Get-PhpNightly.ps1 -FallbackUrl https://dl.cloudsmith.io/public/shivammathur/php-builder-windows/raw/files/Get-PhpNightly.ps1 -OutFile $php_dir\Get-PhpNightly.ps1 > $null 2>&1 & $php_dir\Get-PhpNightly.ps1 -Architecture $arch -ThreadSafe $ts -Path $php_dir -Version $version > $null 2>&1 if(Test-Path $php_dir\COMMIT) { return " ($( Get-Content $php_dir\COMMIT ))" @@ -387,7 +421,7 @@ if($installed.MajorMinorVersion -ne $version) { exit 1 } if($version -lt "5.5") { - ('libeay32.dll', 'ssleay32.dll') | ForEach-Object -Parallel { Invoke-WebRequest -Uri "$using:php_builder/releases/download/openssl-1.0.2u/$_" -OutFile $using:php_dir\$_ >$null 2>&1 } + ('libeay32.dll', 'ssleay32.dll') | ForEach-Object -Parallel { Get-File -Url "$using:php_builder/releases/download/openssl-1.0.2u/$_" -OutFile $using:php_dir\$_ >$null 2>&1 } } else { $enable_extensions += ('opcache') }