|
| 1 | +Function Update-CurlDependencyConfig { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Add curl brotli/zstd CHECK_LIB calls to config.w32 when required. |
| 5 | + .PARAMETER PhpVersion |
| 6 | + PHP Version |
| 7 | + .PARAMETER ConfigW32Path |
| 8 | + Path to config.w32 |
| 9 | + #> |
| 10 | + [OutputType([bool])] |
| 11 | + param ( |
| 12 | + [Parameter(Mandatory = $true, Position=0, HelpMessage='PHP Version')] |
| 13 | + [string] $PhpVersion, |
| 14 | + [Parameter(Mandatory = $false, Position=1, HelpMessage='Path to config.w32')] |
| 15 | + [string] $ConfigW32Path = 'config.w32' |
| 16 | + ) |
| 17 | + begin { |
| 18 | + } |
| 19 | + process { |
| 20 | + if (-not (Test-Path -LiteralPath $ConfigW32Path -PathType Leaf)) { |
| 21 | + return $false |
| 22 | + } |
| 23 | + |
| 24 | + if ($PhpVersion -ne 'master') { |
| 25 | + if ($PhpVersion -notmatch '^(\d+\.\d+(?:\.\d+)?)') { |
| 26 | + return $false |
| 27 | + } |
| 28 | + |
| 29 | + if ([version] $matches[1] -lt [version] '8.4') { |
| 30 | + return $false |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + $configLines = Get-Content -Path $ConfigW32Path |
| 35 | + $configW32Content = $configLines -join "`r`n" |
| 36 | + if ($configW32Content -notmatch 'libcurl') { |
| 37 | + return $false |
| 38 | + } |
| 39 | + |
| 40 | + $curlLibraries = @('brotlidec.lib', 'brotlicommon.lib', 'libzstd.lib') |
| 41 | + $missingLibraries = @($curlLibraries | Where-Object { |
| 42 | + $configW32Content -notmatch ("CHECK_LIB\((['""])" + [regex]::Escape($_) + '\1') |
| 43 | + }) |
| 44 | + if ($missingLibraries.Count -eq 0) { |
| 45 | + return $false |
| 46 | + } |
| 47 | + |
| 48 | + $updatedLines = New-Object 'System.Collections.Generic.List[string]' |
| 49 | + $updated = $false |
| 50 | + |
| 51 | + foreach ($line in $configLines) { |
| 52 | + if (-not $updated) { |
| 53 | + $negatedPattern = '^(?<indent>\s*)if\s*\(\s*!\s*CHECK_LIB\((?<quote>[''"])(?<lib>[^''"]*nghttp2[^''"]*)\k<quote>(?<signature>\s*,\s*[^)]*)\)(?<suffix>.*)$' |
| 54 | + $negatedMatch = [regex]::Match($line, $negatedPattern) |
| 55 | + if ($negatedMatch.Success) { |
| 56 | + $indent = $negatedMatch.Groups['indent'].Value |
| 57 | + $quote = $negatedMatch.Groups['quote'].Value |
| 58 | + $library = $negatedMatch.Groups['lib'].Value |
| 59 | + $signature = $negatedMatch.Groups['signature'].Value |
| 60 | + $suffix = $negatedMatch.Groups['suffix'].Value |
| 61 | + $continuationIndent = $indent + ' ' |
| 62 | + $updatedLines.Add("${indent}if(!CHECK_LIB($quote$library$quote$signature) ||") |
| 63 | + for ($i = 0; $i -lt $missingLibraries.Count; $i++) { |
| 64 | + $lineSuffix = if ($i -eq ($missingLibraries.Count - 1)) { $suffix } else { ' ||' } |
| 65 | + $updatedLines.Add("${continuationIndent}!CHECK_LIB($quote$($missingLibraries[$i])$quote$signature)$lineSuffix") |
| 66 | + } |
| 67 | + $updated = $true |
| 68 | + continue |
| 69 | + } |
| 70 | + |
| 71 | + $chainPattern = '^(?<indent>\s*)(?<prefix>&&\s*)?CHECK_LIB\((?<quote>[''"])(?<lib>[^''"]*nghttp2[^''"]*)\k<quote>(?<signature>\s*,\s*[^)]*)\)(?<suffix>.*)$' |
| 72 | + $chainMatch = [regex]::Match($line, $chainPattern) |
| 73 | + if ($chainMatch.Success) { |
| 74 | + $indent = $chainMatch.Groups['indent'].Value |
| 75 | + $prefix = $chainMatch.Groups['prefix'].Value |
| 76 | + $quote = $chainMatch.Groups['quote'].Value |
| 77 | + $library = $chainMatch.Groups['lib'].Value |
| 78 | + $signature = $chainMatch.Groups['signature'].Value |
| 79 | + $suffix = $chainMatch.Groups['suffix'].Value |
| 80 | + $updatedLines.Add("${indent}${prefix}CHECK_LIB($quote$library$quote$signature)") |
| 81 | + for ($i = 0; $i -lt $missingLibraries.Count; $i++) { |
| 82 | + $lineSuffix = if ($i -eq ($missingLibraries.Count - 1)) { $suffix } else { '' } |
| 83 | + $updatedLines.Add("${indent}&& CHECK_LIB($quote$($missingLibraries[$i])$quote$signature)$lineSuffix") |
| 84 | + } |
| 85 | + $updated = $true |
| 86 | + continue |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + $updatedLines.Add($line) |
| 91 | + } |
| 92 | + |
| 93 | + $updatedContent = $updatedLines -join "`r`n" |
| 94 | + if (-not $updated -or $updatedContent -eq $configW32Content) { |
| 95 | + return $false |
| 96 | + } |
| 97 | + |
| 98 | + Set-Content -Path $ConfigW32Path -Value $updatedContent -Encoding ASCII |
| 99 | + return $true |
| 100 | + } |
| 101 | + end { |
| 102 | + } |
| 103 | +} |
0 commit comments