2019-09-06 06:47:43 +07:00
|
|
|
param (
|
2019-12-26 20:01:18 +07:00
|
|
|
[Parameter(Position = 0, Mandatory = $true)]
|
|
|
|
[ValidateNotNull()]
|
|
|
|
[ValidateLength(1, [int]::MaxValue)]
|
|
|
|
[string]
|
2021-11-25 19:04:13 +07:00
|
|
|
$version = '8.1',
|
2019-12-26 20:01:18 +07:00
|
|
|
[Parameter(Position = 1, Mandatory = $true)]
|
|
|
|
[ValidateNotNull()]
|
|
|
|
[ValidateLength(1, [int]::MaxValue)]
|
|
|
|
[string]
|
2020-11-25 07:09:44 +07:00
|
|
|
$dist
|
2019-09-06 06:47:43 +07:00
|
|
|
)
|
|
|
|
|
2020-05-27 17:53:52 +07:00
|
|
|
# Function to log start of a operation.
|
2019-10-17 03:11:13 +07:00
|
|
|
Function Step-Log($message) {
|
2019-10-23 16:57:40 +07:00
|
|
|
printf "\n\033[90;1m==> \033[0m\033[37;1m%s \033[0m\n" $message
|
2019-10-17 03:11:13 +07:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:53:52 +07:00
|
|
|
# Function to log result of a operation.
|
2019-10-17 03:11:13 +07:00
|
|
|
Function Add-Log($mark, $subject, $message) {
|
2020-10-17 05:43:36 +07:00
|
|
|
if ($mark -eq $tick) {
|
|
|
|
printf "\033[32;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s \033[0m\n" $mark $subject $message
|
|
|
|
} else {
|
|
|
|
printf "\033[31;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s \033[0m\n" $mark $subject $message
|
2020-11-25 07:09:44 +07:00
|
|
|
if($env:fail_fast -eq 'true') {
|
2020-10-17 05:43:36 +07:00
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 03:11:13 +07:00
|
|
|
}
|
|
|
|
|
2020-06-03 15:07:25 +07:00
|
|
|
# Function to add a line to a powershell profile safely.
|
|
|
|
Function Add-ToProfile {
|
|
|
|
param(
|
|
|
|
[Parameter(Position = 0, Mandatory = $true)]
|
|
|
|
[ValidateNotNull()]
|
|
|
|
[ValidateLength(1, [int]::MaxValue)]
|
|
|
|
[string]
|
|
|
|
$input_profile,
|
|
|
|
[Parameter(Position = 1, Mandatory = $true)]
|
|
|
|
[ValidateNotNull()]
|
|
|
|
[ValidateLength(1, [int]::MaxValue)]
|
|
|
|
[string]
|
|
|
|
$search,
|
|
|
|
[Parameter(Position = 2, Mandatory = $true)]
|
|
|
|
[ValidateNotNull()]
|
|
|
|
[ValidateLength(1, [int]::MaxValue)]
|
|
|
|
[string]
|
|
|
|
$value
|
|
|
|
)
|
|
|
|
if($null -eq (Get-Content $input_profile | findstr $search)) {
|
|
|
|
Add-Content -Path $input_profile -Value $value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:53:52 +07:00
|
|
|
# Function to fetch PATH from the registry.
|
2020-05-05 01:36:09 +07:00
|
|
|
Function Get-PathFromRegistry {
|
2020-06-03 15:07:25 +07:00
|
|
|
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","User") + ";" +
|
|
|
|
[System.Environment]::GetEnvironmentVariable("Path","Machine")
|
|
|
|
Add-ToProfile $current_profile 'Get-PathFromRegistry' 'Function Get-PathFromRegistry { $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine") }; Get-PathFromRegistry'
|
2020-05-05 01:36:09 +07:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:53:52 +07:00
|
|
|
# Function to add a location to PATH.
|
2020-05-05 01:36:09 +07:00
|
|
|
Function Add-Path {
|
|
|
|
param(
|
|
|
|
[string]$PathItem
|
|
|
|
)
|
|
|
|
$newPath = (Get-ItemProperty -Path 'hkcu:\Environment' -Name PATH).Path.replace("$PathItem;", '')
|
|
|
|
$newPath = $PathItem + ';' + $newPath
|
|
|
|
Set-ItemProperty -Path 'hkcu:\Environment' -Name Path -Value $newPath
|
|
|
|
Get-PathFromRegistry
|
|
|
|
}
|
|
|
|
|
2020-09-06 15:47:06 +07:00
|
|
|
# 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") {
|
2021-08-25 11:16:36 +07:00
|
|
|
New-Item -Path $bin_dir\printf.exe -ItemType SymbolicLink -Value C:\msys64\usr\bin\printf.exe -Force > $null 2>&1
|
2020-09-06 15:47:06 +07:00
|
|
|
} else {
|
2020-11-11 03:53:45 +07:00
|
|
|
Invoke-WebRequest -Uri "$github/shivammathur/printf/releases/latest/download/printf-x64.zip" -OutFile "$bin_dir\printf.zip"
|
2020-09-06 15:47:06 +07:00
|
|
|
Expand-Archive -Path $bin_dir\printf.zip -DestinationPath $bin_dir -Force
|
|
|
|
}
|
|
|
|
} else {
|
2021-08-25 11:16:36 +07:00
|
|
|
New-Item -Path $bin_dir\printf.exe -ItemType SymbolicLink -Value "C:\Program Files\Git\usr\bin\printf.exe" -Force > $null 2>&1
|
2020-09-06 15:47:06 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-27 17:53:52 +07:00
|
|
|
# Function to get a clean Powershell profile.
|
2020-05-05 01:36:09 +07:00
|
|
|
Function Get-CleanPSProfile {
|
|
|
|
if(-not(Test-Path -LiteralPath $profile)) {
|
2021-08-25 11:16:36 +07:00
|
|
|
New-Item -Path $profile -ItemType "file" -Force > $null 2>&1
|
2020-05-05 01:36:09 +07:00
|
|
|
}
|
|
|
|
Set-Content $current_profile -Value ''
|
2020-06-03 15:07:25 +07:00
|
|
|
Add-ToProfile $profile $current_profile.replace('\', '\\') ". $current_profile"
|
2020-05-05 01:36:09 +07:00
|
|
|
}
|
|
|
|
|
2020-10-30 12:58:54 +07:00
|
|
|
# Function to install a powershell package from GitHub.
|
2020-11-08 14:36:21 +07:00
|
|
|
Function Install-PSPackage() {
|
2020-10-30 12:58:54 +07:00
|
|
|
param(
|
|
|
|
[Parameter(Position = 0, Mandatory = $true)]
|
|
|
|
$package,
|
|
|
|
[Parameter(Position = 1, Mandatory = $true)]
|
|
|
|
$psm1_path,
|
|
|
|
[Parameter(Position = 2, Mandatory = $true)]
|
2021-04-18 06:38:38 +07:00
|
|
|
$url,
|
|
|
|
[Parameter(Position = 3, Mandatory = $true)]
|
|
|
|
$cmdlet
|
2020-10-30 12:58:54 +07:00
|
|
|
)
|
|
|
|
$module_path = "$bin_dir\$psm1_path.psm1"
|
2020-07-15 11:15:40 +07:00
|
|
|
if(-not (Test-Path $module_path -PathType Leaf)) {
|
2020-10-30 12:58:54 +07:00
|
|
|
$zip_file = "$bin_dir\$package.zip"
|
2020-11-11 03:53:45 +07:00
|
|
|
Invoke-WebRequest -Uri $url -OutFile $zip_file
|
2020-08-25 23:00:03 +07:00
|
|
|
Expand-Archive -Path $zip_file -DestinationPath $bin_dir -Force
|
2020-05-05 01:36:09 +07:00
|
|
|
}
|
2020-06-03 15:07:25 +07:00
|
|
|
Import-Module $module_path
|
2020-10-30 12:58:54 +07:00
|
|
|
Add-ToProfile $current_profile "$package-search" "Import-Module $module_path"
|
2021-04-18 06:38:38 +07:00
|
|
|
|
|
|
|
if($null -eq (Get-Command $cmdlet -ErrorAction SilentlyContinue)) {
|
2021-12-27 21:23:04 +07:00
|
|
|
Install-Module -Name $package -Force
|
2021-04-18 06:38:38 +07:00
|
|
|
}
|
2020-03-16 12:48:01 +07:00
|
|
|
}
|
|
|
|
|
2021-10-14 23:38:11 +07:00
|
|
|
# Function to add CA certificates to PHP.
|
|
|
|
Function Add-PhpCAInfo {
|
|
|
|
try {
|
|
|
|
Update-PhpCAInfo -Path $php_dir -Source Curl
|
|
|
|
} catch {
|
|
|
|
Add-Log $cross PHP "Could not fetch CA certificate bundle from Curl"
|
|
|
|
Update-PhpCAInfo -Path $php_dir -Source CurrentUser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 20:01:18 +07:00
|
|
|
# Variables
|
|
|
|
$tick = ([char]8730)
|
|
|
|
$cross = ([char]10007)
|
|
|
|
$php_dir = 'C:\tools\php'
|
2020-05-05 01:36:09 +07:00
|
|
|
$ext_dir = "$php_dir\ext"
|
2020-06-03 15:07:25 +07:00
|
|
|
$bin_dir = $php_dir
|
2020-10-30 12:58:54 +07:00
|
|
|
$github = 'https://github.com'
|
2021-04-13 18:25:32 +07:00
|
|
|
$php_builder = "$github/shivammathur/php-builder-windows"
|
2020-05-05 01:36:09 +07:00
|
|
|
$current_profile = "$env:TEMP\setup-php.ps1"
|
2019-12-26 20:01:18 +07:00
|
|
|
$ProgressPreference = 'SilentlyContinue'
|
2021-02-23 15:29:28 +07:00
|
|
|
$jit_versions = '8.[0-9]'
|
2021-12-06 11:43:24 +07:00
|
|
|
$nightly_versions = '8.[2-9]'
|
2020-11-08 14:36:21 +07:00
|
|
|
$enable_extensions = ('openssl', 'curl', 'mbstring')
|
2020-05-05 01:36:09 +07:00
|
|
|
|
2020-01-20 23:20:44 +07:00
|
|
|
$arch = 'x64'
|
2020-05-05 01:36:09 +07:00
|
|
|
if(-not([Environment]::Is64BitOperatingSystem) -or $version -lt '7.0') {
|
|
|
|
$arch = 'x86'
|
2020-01-20 23:20:44 +07:00
|
|
|
}
|
2019-12-26 20:01:18 +07:00
|
|
|
|
2020-05-05 01:36:09 +07:00
|
|
|
$ts = $env:PHPTS -eq 'ts'
|
|
|
|
if($env:PHPTS -ne 'ts') {
|
|
|
|
$env:PHPTS = 'nts'
|
|
|
|
}
|
|
|
|
if($env:RUNNER -eq 'self-hosted') {
|
|
|
|
$bin_dir = 'C:\tools\bin'
|
|
|
|
$php_dir = "$php_dir$version"
|
|
|
|
$ext_dir = "$php_dir\ext"
|
|
|
|
Get-CleanPSProfile >$null 2>&1
|
2021-08-25 11:16:36 +07:00
|
|
|
New-Item $bin_dir -Type Directory -Force > $null 2>&1
|
2020-05-05 01:36:09 +07:00
|
|
|
Add-Path -PathItem $bin_dir
|
|
|
|
if($version -lt 5.6) {
|
|
|
|
Add-Log $cross "PHP" "PHP $version is not supported on self-hosted runner"
|
2020-06-07 04:11:28 +07:00
|
|
|
Start-Sleep 1
|
2020-05-05 01:36:09 +07:00
|
|
|
exit 1
|
|
|
|
}
|
2020-06-03 15:07:25 +07:00
|
|
|
if ((Get-InstalledModule).Name -notcontains 'VcRedist') {
|
|
|
|
Install-Module -Name VcRedist -Force
|
|
|
|
}
|
2021-08-25 11:16:36 +07:00
|
|
|
New-Item $php_dir -Type Directory -Force > $null 2>&1
|
2020-05-05 01:36:09 +07:00
|
|
|
Add-Path -PathItem $php_dir
|
|
|
|
setx PHPROOT $php_dir >$null 2>&1
|
|
|
|
} else {
|
|
|
|
$current_profile = "$PSHOME\Profile.ps1"
|
2020-06-03 15:07:25 +07:00
|
|
|
if(-not(Test-Path -LiteralPath $current_profile)) {
|
|
|
|
New-Item -Path $current_profile -ItemType "file" -Force >$null 2>&1
|
|
|
|
}
|
2020-05-05 01:36:09 +07:00
|
|
|
}
|
2020-09-06 15:47:06 +07:00
|
|
|
|
2021-02-20 11:45:25 +07:00
|
|
|
. $dist\..\src\scripts\tools\add_tools.ps1
|
2022-01-02 18:51:14 +07:00
|
|
|
. $dist\..\src\scripts\extensions\add_extensions.ps1
|
2021-02-20 11:45:25 +07:00
|
|
|
|
2020-09-06 15:47:06 +07:00
|
|
|
Add-Printf >$null 2>&1
|
2019-12-26 20:01:18 +07:00
|
|
|
Step-Log "Setup PhpManager"
|
2021-12-08 22:04:44 +07:00
|
|
|
Install-PSPackage PhpManager PhpManager\PhpManager "$github/mlocati/powershell-phpmanager/releases/latest/download/PhpManager.zip" Get-Php >$null 2>&1
|
2019-12-26 20:01:18 +07:00
|
|
|
Add-Log $tick "PhpManager" "Installed"
|
|
|
|
|
2020-05-05 01:36:09 +07:00
|
|
|
Step-Log "Setup PHP"
|
2019-12-26 20:01:18 +07:00
|
|
|
$installed = $null
|
|
|
|
if (Test-Path -LiteralPath $php_dir -PathType Container) {
|
|
|
|
try {
|
2021-11-26 00:54:48 +07:00
|
|
|
if(Test-Path $php_dir\php.ini) {
|
|
|
|
Rename-Item -Path $php_dir\php.ini -NewName 'php.ini.bak'
|
|
|
|
}
|
|
|
|
$installed = Get-Php -Path $php_dir -ErrorAction SilentlyContinue 2>$null 3>$null
|
|
|
|
if(Test-Path $php_dir\php.ini.bak) {
|
|
|
|
Rename-Item -Path $php_dir\php.ini.bak -NewName 'php.ini'
|
|
|
|
}
|
2020-05-05 01:36:09 +07:00
|
|
|
} catch { }
|
2019-12-24 00:49:38 +07:00
|
|
|
}
|
2020-01-16 11:46:44 +07:00
|
|
|
$status = "Installed"
|
2021-07-12 18:30:40 +07:00
|
|
|
$extra_version = ""
|
2020-01-20 23:20:44 +07:00
|
|
|
if ($null -eq $installed -or -not("$($installed.Version).".StartsWith(($version -replace '^(\d+(\.\d+)*).*', '$1.'))) -or $ts -ne $installed.ThreadSafe) {
|
2020-05-05 01:36:09 +07:00
|
|
|
if ($version -lt '7.0' -and (Get-InstalledModule).Name -notcontains 'VcRedist') {
|
2021-12-08 22:04:44 +07:00
|
|
|
Install-PSPackage VcRedist VcRedist-main\VcRedist\VcRedist "$github/aaronparker/VcRedist/archive/main.zip" Get-VcList >$null 2>&1
|
2019-12-26 20:01:18 +07:00
|
|
|
}
|
2020-12-17 05:50:39 +07:00
|
|
|
try {
|
2021-02-23 15:29:28 +07:00
|
|
|
if ($version -match $nightly_versions) {
|
2021-12-08 22:04:44 +07:00
|
|
|
Invoke-WebRequest -UseBasicParsing -Uri $php_builder/releases/latest/download/Get-PhpNightly.ps1 -OutFile $php_dir\Get-PhpNightly.ps1 > $null 2>&1
|
2021-09-02 02:05:19 +07:00
|
|
|
& $php_dir\Get-PhpNightly.ps1 -Architecture $arch -ThreadSafe $ts -Path $php_dir -Version $version > $null 2>&1
|
2021-11-24 19:31:01 +07:00
|
|
|
if(Test-Path $php_dir\COMMIT) {
|
|
|
|
$extra_version = " ($( Get-Content $php_dir\COMMIT ))"
|
|
|
|
}
|
2020-12-17 05:50:39 +07:00
|
|
|
} else {
|
|
|
|
Install-Php -Version $version -Architecture $arch -ThreadSafe $ts -InstallVC -Path $php_dir -TimeZone UTC -InitialPhpIni Production -Force > $null 2>&1
|
|
|
|
}
|
|
|
|
} catch { }
|
2020-01-16 11:46:44 +07:00
|
|
|
} else {
|
2021-08-25 20:30:52 +07:00
|
|
|
Set-PhpIniKey -Key 'extension_dir' -Value $ext_dir -Path $php_dir
|
2021-02-23 15:29:28 +07:00
|
|
|
if($version -match $jit_versions) {
|
|
|
|
('opcache.enable=1', 'opcache.jit_buffer_size=256M', 'opcache.jit=1235') | ForEach-Object { $p=$_.split('='); Set-PhpIniKey -Key $p[0] -Value $p[1] -Path $php_dir }
|
|
|
|
}
|
2020-05-05 01:36:09 +07:00
|
|
|
if($env:update -eq 'true') {
|
2020-02-28 14:49:12 +07:00
|
|
|
Update-Php $php_dir >$null 2>&1
|
2020-02-14 00:09:00 +07:00
|
|
|
$status = "Updated to"
|
|
|
|
} else {
|
2020-01-16 11:46:44 +07:00
|
|
|
$status = "Found"
|
|
|
|
}
|
2019-12-26 20:01:18 +07:00
|
|
|
}
|
|
|
|
|
2019-12-29 01:59:28 +07:00
|
|
|
$installed = Get-Php -Path $php_dir
|
2020-12-17 05:50:39 +07:00
|
|
|
if($installed.MajorMinorVersion -ne $version) {
|
|
|
|
Add-Log $cross "PHP" "Could not setup PHP $version"
|
|
|
|
exit 1
|
|
|
|
}
|
2020-12-17 22:19:20 +07:00
|
|
|
('date.timezone=UTC', 'memory_limit=-1', 'xdebug.mode=coverage') | ForEach-Object { $p=$_.split('='); Set-PhpIniKey -Key $p[0] -Value $p[1] -Path $php_dir }
|
2020-02-06 06:54:45 +07:00
|
|
|
if($version -lt "5.5") {
|
2021-04-13 18:25:32 +07:00
|
|
|
('libeay32.dll', 'ssleay32.dll') | ForEach-Object { Invoke-WebRequest -Uri "$php_builder/releases/download/openssl-1.0.2u/$_" -OutFile $php_dir\$_ >$null 2>&1 }
|
2020-02-06 06:54:45 +07:00
|
|
|
} else {
|
2020-11-08 14:36:21 +07:00
|
|
|
$enable_extensions += ('opcache')
|
2020-02-06 06:54:45 +07:00
|
|
|
}
|
2020-11-08 14:36:21 +07:00
|
|
|
Enable-PhpExtension -Extension $enable_extensions -Path $php_dir
|
2021-10-14 23:38:11 +07:00
|
|
|
Add-PhpCAInfo
|
2021-11-10 07:15:18 +07:00
|
|
|
Copy-Item -Path $dist\..\src\configs\pm\*.json -Destination $env:RUNNER_TOOL_CACHE
|
2021-07-12 18:30:40 +07:00
|
|
|
Write-Output "::set-output name=php-version::$($installed.FullVersion)"
|
|
|
|
Add-Log $tick "PHP" "$status PHP $($installed.FullVersion)$extra_version"
|