/
win32.ps1
352 lines (328 loc) · 11.4 KB
/
win32.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
param (
[Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]
$version = '8.1',
[Parameter(Position = 1, Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]
$ini = 'production'
)
# Function to log start of a operation.
Function Step-Log($message) {
printf "\n\033[90;1m==> \033[0m\033[37;1m%s \033[0m\n" $message
}
# Function to log result of a operation.
Function Add-Log($mark, $subject, $message) {
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
if($env:fail_fast -eq 'true') {
exit 1;
}
}
}
# Function to set output on GitHub Actions.
Function Set-Output() {
param(
[Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]
$output,
[Parameter(Position = 1, Mandatory = $true)]
[ValidateNotNull()]
[ValidateLength(1, [int]::MaxValue)]
[string]
$value
)
if ($env:GITHUB_ACTIONS -eq 'true') {
Write-Output "::set-output name=$output::$value"
}
}
# 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
}
}
# Function to fetch PATH from the registry.
Function Get-PathFromRegistry {
$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'
}
# Function to add a location to PATH.
Function Add-Path {
param(
[string]$PathItem
)
if("$env:PATH;".contains("$PathItem;")) {
return
}
if ($env:GITHUB_PATH) {
Add-Content $PathItem -Path $env:GITHUB_PATH -Encoding utf8
} else {
$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
}
}
# Function to add an environment variable.
Function Add-Env {
param(
[string]$EnvName,
[string]$EnvValue
)
if ($env:GITHUB_ENV) {
Add-Content "$EnvName=$EnvValue" -Path $env:GITHUB_ENV -Encoding utf8
} else {
Set-ItemProperty -Path 'hkcu:\Environment' -Name $EnvName -Value $EnvValue
}
}
# Function to add environment variables using a PATH.
Function Add-EnvPATH {
param(
[string]$EnvPATH
)
if(-not(Test-Path $EnvPATH)) {
return
}
$env_file = $current_profile
if ($env:GITHUB_ENV) {
$env_file = $env:GITHUB_ENV
}
Get-Content -Path $EnvPATH | Add-Content -Path $env_file -Encoding utf8
}
# 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"
Expand-Archive -Path $bin_dir\printf.zip -DestinationPath $bin_dir -Force
}
} else {
New-Item -Path $bin_dir\printf.exe -ItemType SymbolicLink -Value "C:\Program Files\Git\usr\bin\printf.exe" -Force > $null 2>&1
}
}
# Function to get a clean Powershell profile.
Function Get-CleanPSProfile {
if(-not(Test-Path -LiteralPath $profile)) {
New-Item -Path $profile -ItemType "file" -Force > $null 2>&1
}
Set-Content $current_profile -Value ''
Add-ToProfile $profile $current_profile.replace('\', '\\') ". $current_profile"
}
# Function to install a powershell package from GitHub.
Function Install-PSPackage() {
param(
[Parameter(Position = 0, Mandatory = $true)]
$package,
[Parameter(Position = 1, Mandatory = $true)]
$psm1_path,
[Parameter(Position = 2, Mandatory = $true)]
$url,
[Parameter(Position = 3, Mandatory = $true)]
$cmdlet
)
$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
Expand-Archive -Path $zip_file -DestinationPath $bin_dir -Force
}
Import-Module $module_path
if($null -eq (Get-Command $cmdlet -ErrorAction SilentlyContinue)) {
Install-Module -Name $package -Force
} else {
Add-ToProfile $current_profile "$package-search" "Import-Module $module_path"
}
}
# 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
}
}
# Function to set PHP config.
Function Add-PhpConfig {
$current = Get-Content -Path $php_dir\php.ini-current -ErrorAction SilentlyContinue
if($ini -eq 'development' -or ($ini -eq 'production' -and $current -and $current -ne 'production')) {
Copy-Item -Path $php_dir\php.ini-$ini -Destination $php_dir\php.ini -Force
} elseif ($ini -eq 'none') {
Set-Content -Path $php_dir\php.ini -Value ''
}
Set-Content -Path $php_dir\php.ini-current -Value $ini
$ini_config_dir = "$src\configs\ini"
$ini_files = @("$ini_config_dir\php.ini")
$version -match $jit_versions -and ($ini_files += ("$ini_config_dir\jit.ini")) > $null 2>&1
$version -match $xdebug3_versions -and ($ini_files += ("$ini_config_dir\xdebug.ini")) > $null 2>&1
Add-Content -Path $ini_config_dir\php.ini -Value extension_dir=$ext_dir
Get-Content -Path $ini_files | Add-Content -Path $php_dir\php.ini
}
# Function to get PHP from GitHub releases cache
Function Set-PhpCache {
try {
$release = Invoke-RestMethod https://api.github.com/repos/shivammathur/php-builder-windows/releases/tags/php$version
$asset = $release.assets | ForEach-Object {
if($_.name -match "php-$version.[0-9]+$env:PHPTS-Win32-.*-$arch.zip") {
return $_.name
}
}
Invoke-WebRequest -UseBasicParsing -Uri $php_builder/releases/download/php$version/$asset -OutFile $php_dir\$asset
Set-PhpDownloadCache -Path $php_dir CurrentUser
} catch { }
}
# 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
& $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 ))"
}
return;
}
# Variables
$tick = ([char]8730)
$cross = ([char]10007)
$php_dir = 'C:\tools\php'
$ext_dir = "$php_dir\ext"
$bin_dir = $php_dir
$github = 'https://github.com'
$php_builder = "$github/shivammathur/php-builder-windows"
$current_profile = "$env:TEMP\setup-php.ps1"
$ProgressPreference = 'SilentlyContinue'
$jit_versions = '8.[0-9]'
$nightly_versions = '8.[2-9]'
$xdebug3_versions = "7.[2-4]|8.[0-9]"
$enable_extensions = ('openssl', 'curl', 'mbstring')
$arch = 'x64'
if(-not([Environment]::Is64BitOperatingSystem) -or $version -lt '7.0') {
$arch = 'x86'
}
$ts = $env:PHPTS -eq 'ts'
if($env:PHPTS -ne 'ts') {
$env:PHPTS = '-nts'
} else {
$env:PHPTS = ''
}
if ( $env:GITHUB_ACTIONS -eq 'true') {
$env:GROUP = '::group::'
$env:END_GROUP = '::endgroup::'
} else {
$env:GROUP = ''
$env:END_GROUP = ''
}
if(-not($env:ImageOS) -and -not($env:ImageVersion)) {
if($env:RUNNER -eq 'github') {
Add-Log $cross "Runner" "Runner set as github in self-hosted environment"
exit 1
}
$bin_dir = 'C:\tools\bin'
$php_dir = "$php_dir$version"
$ext_dir = "$php_dir\ext"
Get-CleanPSProfile >$null 2>&1
New-Item $bin_dir -Type Directory -Force > $null 2>&1
Add-Path -PathItem $bin_dir
if($version -lt 5.6) {
Add-Log $cross "PHP" "PHP $version is not supported on self-hosted runner"
Start-Sleep 1
exit 1
}
if ($null -eq (Get-Module -ListAvailable -Name VcRedist)) {
Install-Module -Name VcRedist -Force
}
New-Item $php_dir -Type Directory -Force > $null 2>&1
Add-Path -PathItem $php_dir
setx PHPROOT $php_dir >$null 2>&1
} else {
$current_profile = "$PSHOME\Profile.ps1"
if(-not(Test-Path -LiteralPath $current_profile)) {
New-Item -Path $current_profile -ItemType "file" -Force >$null 2>&1
}
}
$src = Join-Path -Path $PSScriptRoot -ChildPath \..
. $src\scripts\tools\add_tools.ps1
. $src\scripts\extensions\add_extensions.ps1
Add-Printf >$null 2>&1
Step-Log "Setup PhpManager"
Install-PSPackage PhpManager PhpManager\PhpManager "$github/mlocati/powershell-phpmanager/releases/latest/download/PhpManager.zip" Get-Php >$null 2>&1
Add-Log $tick "PhpManager" "Installed"
Step-Log "Setup PHP"
$installed = $null
if (Test-Path -LiteralPath $php_dir -PathType Container) {
try {
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'
}
} catch { }
}
$status = "Installed"
$extra_version = ""
if ($null -eq $installed -or -not("$($installed.Version).".StartsWith(($version -replace '^(\d+(\.\d+)*).*', '$1.'))) -or $ts -ne $installed.ThreadSafe) {
if ($version -lt '7.0' -and ($null -eq (Get-Module -ListAvailable -Name VcRedist))) {
Install-PSPackage VcRedist VcRedist-main\VcRedist\VcRedist "$github/aaronparker/VcRedist/archive/main.zip" Get-VcList >$null 2>&1
}
try {
if ($version -match $nightly_versions) {
$extra_version = Install-PhpNightly
} else {
Set-PhpCache
Install-Php -Version $version -Architecture $arch -ThreadSafe $ts -InstallVC -Path $php_dir -TimeZone UTC -InitialPhpIni production -Force > $null 2>&1
}
Add-PhpConfig
} catch { }
} else {
if($env:update -eq 'true') {
Update-Php $php_dir >$null 2>&1
$status = "Updated to"
} else {
$status = "Found"
}
Add-PhpConfig
}
$installed = Get-Php -Path $php_dir
if($installed.MajorMinorVersion -ne $version) {
Add-Log $cross "PHP" "Could not setup PHP $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 }
} else {
$enable_extensions += ('opcache')
}
Enable-PhpExtension -Extension $enable_extensions -Path $php_dir
Add-PhpCAInfo
Copy-Item -Path $src\configs\pm\*.json -Destination $env:RUNNER_TOOL_CACHE
Set-Output php-version $($installed.FullVersion)
Add-Log $tick "PHP" "$status PHP $($installed.FullVersion)$extra_version"