@@ -6,6 +6,10 @@ function Get-PhpTestPack {
66 PHP Version
77 . PARAMETER TestsDirectory
88 Tests Directory
9+ . PARAMETER SourceRepository
10+ php-src repository to source tests from when SourceRef is provided.
11+ . PARAMETER SourceRef
12+ Optional branch, tag, or SHA in the custom php-src repository.
913 #>
1014 [OutputType ()]
1115 param (
@@ -15,14 +19,70 @@ function Get-PhpTestPack {
1519 [string ] $PhpVersion ,
1620 [Parameter (Mandatory = $false , Position = 1 , HelpMessage = ' Tests Directory' )]
1721 [ValidateLength (1 , [int ]::MaxValue)]
18- [string ] $TestsDirectory
22+ [string ] $TestsDirectory ,
23+ [Parameter (Mandatory = $false , Position = 2 , HelpMessage = ' php-src repository to source tests from when SourceRef is provided' )]
24+ [string ] $SourceRepository = ' php/php-src' ,
25+ [Parameter (Mandatory = $false , Position = 3 , HelpMessage = ' Optional branch, tag, or SHA in the custom php-src repository' )]
26+ [string ] $SourceRef = ' '
1927 )
2028 begin {
2129 }
2230 process {
2331 Add-Type - Assembly " System.IO.Compression.Filesystem"
2432
2533 $versionInUrl = $PhpVersion
34+ $currentDirectory = (Get-Location ).Path
35+ $testsDirectoryPath = Join-Path $currentDirectory $TestsDirectory
36+ $useCustomSource = -not [string ]::IsNullOrWhiteSpace($SourceRef )
37+
38+ if ($useCustomSource ) {
39+ if ([string ]::IsNullOrWhiteSpace($SourceRepository )) {
40+ throw " SourceRepository must be provided to source tests from a custom php-src archive."
41+ }
42+
43+ $sourceZipFile = (" php-src-tests-{0}-{1}.zip" -f `
44+ ($SourceRepository -replace ' [\\/]' , ' -' ), `
45+ ($SourceRef -replace ' [^0-9A-Za-z._-]' , ' -' ))
46+ $sourceZipPath = Join-Path $currentDirectory $sourceZipFile
47+ $extractRoot = Join-Path $currentDirectory (" php-src-tests-" + [System.Guid ]::NewGuid().ToString())
48+ $sourceUrl = " https://api.github.com/repos/$SourceRepository /zipball/$ ( [System.Uri ]::EscapeDataString($SourceRef )) "
49+ $headers = @ {
50+ ' User-Agent' = ' php-windows-builder'
51+ ' X-GitHub-Api-Version' = ' 2022-11-28'
52+ }
53+
54+ if ($env: GITHUB_TOKEN ) {
55+ $headers [' Authorization' ] = ' Bearer ' + $env: GITHUB_TOKEN
56+ } else {
57+ Write-Warning ' GITHUB_TOKEN not set. API rate limits may apply when downloading custom php-src tests.'
58+ }
59+
60+ Write-Host " Downloading PHP tests from $SourceRepository @$SourceRef ..."
61+ Invoke-WebRequest - Uri $sourceUrl - Headers $headers - OutFile $sourceZipPath - UseBasicParsing
62+
63+ New-Item - Path $extractRoot - ItemType " directory" - Force > $null 2>&1
64+ try {
65+ try {
66+ [System.IO.Compression.ZipFile ]::ExtractToDirectory($sourceZipPath , $extractRoot )
67+ } catch {
68+ 7z x $sourceZipPath " -o$extractRoot " - y | Out-Null
69+ }
70+
71+ $sourceRoots = @ (
72+ Get-ChildItem - Path $extractRoot - Directory
73+ )
74+ if ($sourceRoots.Count -ne 1 ) {
75+ throw " Expected a single root directory in custom php-src archive, found $ ( $sourceRoots.Count ) ."
76+ }
77+
78+ Move-Item - Path $sourceRoots [0 ].FullName - Destination $testsDirectoryPath
79+ } finally {
80+ Remove-Item - Path $extractRoot - Recurse - Force - ErrorAction Ignore
81+ }
82+
83+ return
84+ }
85+
2686 if ($PhpVersion -eq ' master' ) {
2787 $fallbackBaseUrl = $baseUrl = " https://github.com/shivammathur/php-builder-windows/releases/download/master"
2888 $versionInUrl = " master"
@@ -45,9 +105,7 @@ function Get-PhpTestPack {
45105 }
46106 }
47107
48- $currentDirectory = (Get-Location ).Path
49108 $testZipFilePath = Join-Path $currentDirectory $testZipFile
50- $testsDirectoryPath = Join-Path $currentDirectory $TestsDirectory
51109
52110 try {
53111 [System.IO.Compression.ZipFile ]::ExtractToDirectory($testZipFilePath , $testsDirectoryPath )
@@ -57,4 +115,4 @@ function Get-PhpTestPack {
57115 }
58116 end {
59117 }
60- }
118+ }
0 commit comments