From fec9df11d64f5056bcf6b6195be0ba29cf57503b Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 25 Nov 2019 11:53:13 +0100 Subject: [PATCH 1/5] Simplify win32 script - Always specify the PHP install dir - Use default PhpManager functions - Fix CA Authorities (so that HTTPS calls work) - Don't handle the special xdebug case (it's bundled in PhpManager) --- __tests__/extensions.test.ts | 17 ++---- dist/index.js | 25 +-------- src/extensions.ts | 27 +-------- src/scripts/win32.ps1 | 106 +++++++++++++++++------------------ 4 files changed, 56 insertions(+), 119 deletions(-) diff --git a/__tests__/extensions.test.ts b/__tests__/extensions.test.ts index d9e310cd..8c02e9b1 100644 --- a/__tests__/extensions.test.ts +++ b/__tests__/extensions.test.ts @@ -7,17 +7,10 @@ describe('Extension tests', () => { '7.2', 'win32' ); - expect(win32).toContain('Install-PhpExtension xdebug'); - expect(win32).toContain('Install-PhpExtension pcov'); + expect(win32).toContain('Add-Extension xdebug'); + expect(win32).toContain('Add-Extension pcov'); win32 = await extensions.addExtension('xdebug, pcov', '7.4', 'win32'); - const extension_url = - 'https://xdebug.org/files/php_xdebug-2.8.0-7.4-vc15.dll'; - expect(win32).toContain( - 'Invoke-WebRequest -Uri ' + - extension_url + - ' -OutFile C:\\tools\\php\\ext\\php_xdebug.dll' - ); - expect(win32).toContain('Install-PhpExtension pcov'); + expect(win32).toContain('Add-Extension pcov'); win32 = await extensions.addExtension( 'does_not_exist', @@ -25,9 +18,7 @@ describe('Extension tests', () => { 'win32', true ); - expect(win32).toContain( - 'Add-Extension does_not_exist "Install-PhpExtension does_not_exist" extension' - ); + expect(win32).toContain('Add-Extension does_not_exist'); win32 = await extensions.addExtension('xdebug', '7.2', 'fedora'); expect(win32).toContain('Platform fedora is not supported'); diff --git a/dist/index.js b/dist/index.js index 84c61ecd..9967fae5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1648,31 +1648,8 @@ function addExtensionWindows(extension_csv, version) { let script = '\n'; yield utils.asyncForEach(extensions, function (extension) { return __awaiter(this, void 0, void 0, function* () { - extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - let install_command = ''; - switch (version + extension) { - case '7.4xdebug': { - const extension_url = 'https://xdebug.org/files/php_xdebug-2.8.0-7.4-vc15.dll'; - install_command = - 'Invoke-WebRequest -Uri ' + - extension_url + - ' -OutFile C:\\tools\\php\\ext\\php_xdebug.dll\n'; - install_command += 'Enable-PhpExtension xdebug'; - break; - } - case '7.2xdebug': - default: - install_command = 'Install-PhpExtension ' + extension; - break; - } - script += - '\nAdd-Extension ' + - extension + - ' "' + - install_command + - '" ' + - (yield utils.getExtensionPrefix(extension)); + script += '\nAdd-Extension ' + extension; }); }); return script; diff --git a/src/extensions.ts b/src/extensions.ts index ef5ffdcd..7d52b39d 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -49,33 +49,8 @@ export async function addExtensionWindows( const extensions: Array = await utils.extensionArray(extension_csv); let script = '\n'; await utils.asyncForEach(extensions, async function(extension: string) { - extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - - let install_command = ''; - switch (version + extension) { - case '7.4xdebug': { - const extension_url = - 'https://xdebug.org/files/php_xdebug-2.8.0-7.4-vc15.dll'; - install_command = - 'Invoke-WebRequest -Uri ' + - extension_url + - ' -OutFile C:\\tools\\php\\ext\\php_xdebug.dll\n'; - install_command += 'Enable-PhpExtension xdebug'; - break; - } - case '7.2xdebug': - default: - install_command = 'Install-PhpExtension ' + extension; - break; - } - script += - '\nAdd-Extension ' + - extension + - ' "' + - install_command + - '" ' + - (await utils.getExtensionPrefix(extension)); + script += '\nAdd-Extension ' + extension; }); return script; } diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 9f01b365..1707714d 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -1,89 +1,83 @@ param ( - [Parameter(Mandatory=$true)][string]$version = "7.3", - [Parameter(Mandatory=$true)][string]$dir + [Parameter(Mandatory = $true)][string]$version = "7.3", + [Parameter(Mandatory = $true)][string]$dir ) $tick = ([char]8730) $cross = ([char]10007) +$php_dir = 'C:\tools\php' Function Step-Log($message) { printf "\n\033[90;1m==> \033[0m\033[37;1m%s \033[0m\n" $message } Function Add-Log($mark, $subject, $message) { - $code = if($mark -eq $cross) {"31"} else {"32"} + $code = if ($mark -eq $cross) { "31" } else { "32" } printf "\033[%s;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s \033[0m\n" $code $mark $subject $message } -if($version -eq '7.4') { - $version = '7.4RC' -} - Step-Log "Setup PhpManager" Install-Module -Name PhpManager -Force -Scope CurrentUser Add-Log $tick "PhpManager" "Installed" -$installed = $($(php -v)[0] -join '')[4..6] -join '' +$installed = $null +if (Test-Path -LiteralPath $php_dir -PathType Container) { + try { + $installed = Get-Php -Path $php_dir + } + catch { + } +} Step-Log "Setup PHP and Composer" -$status = "Switched to PHP$version" -if($installed -ne $version) { - if($version -lt '7.0') { +if ($null -eq $installed -or -not("$($installed.Version).".StartsWith(($version -replace '^(\d+(\.\d+)*).*', '$1.')))) { + if ($version -lt '7.0') { Install-Module -Name VcRedist -Force } - Install-Php -Version $version -Architecture x86 -ThreadSafe $true -InstallVC -Path C:\tools\php -TimeZone UTC -InitialPhpIni Production -Force >$null 2>&1 - $status = "Installed PHP$version" + if ($version -eq '7.4') { + $version = '7.4RC' + } + Install-Php -Version $version -Architecture x86 -ThreadSafe $true -InstallVC -Path $php_dir -TimeZone UTC -InitialPhpIni Production -Force >$null 2>&1 + $installed = Get-Php -Path $php_dir + $status = "Installed PHP $($installed.FullVersion)" +} +else { + $status = "Switched to PHP $($installed.FullVersion)" } -$ext_dir = "C:\tools\php\ext" -Add-Content C:\tools\php\php.ini "date.timezone = 'UTC'" -Set-PhpIniKey extension_dir $ext_dir -if($version -lt '7.4') { - Enable-PhpExtension openssl - Enable-PhpExtension curl -} else { - Add-Content C:\tools\php\php.ini "extension=php_openssl.dll`nextension=php_curl.dll" - Copy-Item $dir"\..\src\ext\php_pcov.dll" -Destination $ext_dir"\php_pcov.dll" +Set-PhpIniKey -Key 'date.timezone' -Value 'UTC' -Path $php_dir +Enable-PhpExtension -Extension openssl, curl -Path $php_dir +Update-PhpCAInfo -Path $php_dir -Source CurrentUser +if ([Version]$installed.Version -ge '7.4') { + Copy-Item "$dir\..\src\ext\php_pcov.dll" -Destination "$($installed.ExtensionsPath)\php_pcov.dll" } Add-Log $tick "PHP" $status -Install-Composer -Scope System -Path C:\tools\php +Install-Composer -Scope System -Path $php_dir -PhpPath $php_dir Add-Log $tick "Composer" "Installed" -Function Add-Extension($extension, $install_command, $prefix) -{ +Function Add-Extension($extension) { try { - $existing_extensions = Get-PhpExtension -Path C:\tools\php - $match = @($existing_extensions | Where-Object { $_.Name -like $extension }) - if(!(php -m | findstr -i ${extension}) -and $match) { - $filename = $match."Filename".split('\')[-1] - Add-Content C:\tools\php\php.ini "`n$prefix=$filename" - Add-Log $tick $extension "Enabled" - } elseif(php -m | findstr -i $extension) { - Add-Log $tick $extension "Enabled" - } - } catch [Exception] { - Add-Log $cross $extension "Could not enable" - } - - $status = 404 - try { - $status = (Invoke-WebRequest -Uri "https://pecl.php.net/json.php?package=$extension" -UseBasicParsing -DisableKeepAlive).StatusCode - } catch [Exception] { - $status = 500 - } - - if($status -eq 200) { - if(!(php -m | findstr -i $extension)) { - try { - Invoke-Expression $install_command - Add-Log $tick $extension "Installed and enabled" - } catch [Exception] { - Add-Log $cross $extension "Could not install on PHP$version" + $extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension } + if ($null -ne $extension_info) { + switch ($extension_info.State) { + 'Builtin' { + Add-Log $tick $extension "Enabled" + } + 'Enabled' { + Add-Log $tick $extension "Enabled" + } + default { + Enable-PhpExtension -Extension $extension_info.Handle -Path $php_dir + Add-Log $tick $extension "Enabled" + } } } - } else { - if(!(php -m | findstr -i $extension)) { - Add-Log $cross $extension "Could not find $extension for PHP$version on PECL" + else { + Install-PhpExtension -Extension $extension -Path $php_dir + Add-Log $tick $extension "Downloaded and enabled" } } -} \ No newline at end of file + catch { + Add-Log $cross $extension "Could not enable" + } +} From 47387dd6e9b8f37c70dd77277043aecbe5699b85 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 25 Nov 2019 12:33:52 +0100 Subject: [PATCH 2/5] Use the curl CA list if PKI Powershell Module is not available --- src/scripts/win32.ps1 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 1707714d..7812e175 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -46,7 +46,11 @@ else { Set-PhpIniKey -Key 'date.timezone' -Value 'UTC' -Path $php_dir Enable-PhpExtension -Extension openssl, curl -Path $php_dir -Update-PhpCAInfo -Path $php_dir -Source CurrentUser +try { + Update-PhpCAInfo -Path $php_dir -Source CurrentUser +} catch { + Update-PhpCAInfo -Path $php_dir -Source Curl +} if ([Version]$installed.Version -ge '7.4') { Copy-Item "$dir\..\src\ext\php_pcov.dll" -Destination "$($installed.ExtensionsPath)\php_pcov.dll" } From 71c631cffbd2369754f9a44df81205380a6c6be5 Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 25 Nov 2019 14:03:28 +0100 Subject: [PATCH 3/5] Fix installing xdebug with PHP 7.4 on Windows --- __tests__/extensions.test.ts | 2 +- src/extensions.ts | 9 ++++++++- src/scripts/win32.ps1 | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/__tests__/extensions.test.ts b/__tests__/extensions.test.ts index 8c02e9b1..08b9fe44 100644 --- a/__tests__/extensions.test.ts +++ b/__tests__/extensions.test.ts @@ -10,7 +10,7 @@ describe('Extension tests', () => { expect(win32).toContain('Add-Extension xdebug'); expect(win32).toContain('Add-Extension pcov'); win32 = await extensions.addExtension('xdebug, pcov', '7.4', 'win32'); - expect(win32).toContain('Add-Extension pcov'); + expect(win32).toContain('Add-Extension xdebug beta'); win32 = await extensions.addExtension( 'does_not_exist', diff --git a/src/extensions.ts b/src/extensions.ts index 7d52b39d..f9efb6fe 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -50,7 +50,14 @@ export async function addExtensionWindows( let script = '\n'; await utils.asyncForEach(extensions, async function(extension: string) { // add script to enable extension is already installed along with php - script += '\nAdd-Extension ' + extension; + let minimum_stability = 'stable'; + switch (version + extension.toLowerCase()) { + case '7.4xdebug': + minimum_stability = 'beta'; + break; + } + script += ` +Add-Extension ${extension} ${minimum_stability}`; }); return script; } diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 7812e175..97a95f29 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -59,7 +59,7 @@ Add-Log $tick "PHP" $status Install-Composer -Scope System -Path $php_dir -PhpPath $php_dir Add-Log $tick "Composer" "Installed" -Function Add-Extension($extension) { +Function Add-Extension($extension, $mininum_stability) { try { $extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension } if ($null -ne $extension_info) { @@ -77,7 +77,7 @@ Function Add-Extension($extension) { } } else { - Install-PhpExtension -Extension $extension -Path $php_dir + Install-PhpExtension -Extension $extension -MinimumStability $mininum_stability -Path $php_dir Add-Log $tick $extension "Downloaded and enabled" } } From 981c662f5d573039552d813383b2acaaf12b945b Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 25 Nov 2019 14:56:31 +0100 Subject: [PATCH 4/5] Minor change to trigger build --- src/extensions.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/extensions.ts b/src/extensions.ts index f9efb6fe..ec6754b8 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -56,8 +56,7 @@ export async function addExtensionWindows( minimum_stability = 'beta'; break; } - script += ` -Add-Extension ${extension} ${minimum_stability}`; + script += '\nAdd-Extension ' + extension + ' ' + minimum_stability; }); return script; } From 0be846de83e017205f744f9f6ff895e1a3dde06f Mon Sep 17 00:00:00 2001 From: Michele Locati Date: Mon, 25 Nov 2019 16:35:11 +0100 Subject: [PATCH 5/5] Install the stable release of xdebug even on PHP 7.4RC --- __tests__/extensions.test.ts | 2 +- src/extensions.ts | 8 +------- src/scripts/win32.ps1 | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/__tests__/extensions.test.ts b/__tests__/extensions.test.ts index 08b9fe44..42290743 100644 --- a/__tests__/extensions.test.ts +++ b/__tests__/extensions.test.ts @@ -10,7 +10,7 @@ describe('Extension tests', () => { expect(win32).toContain('Add-Extension xdebug'); expect(win32).toContain('Add-Extension pcov'); win32 = await extensions.addExtension('xdebug, pcov', '7.4', 'win32'); - expect(win32).toContain('Add-Extension xdebug beta'); + expect(win32).toContain('Add-Extension xdebug'); win32 = await extensions.addExtension( 'does_not_exist', diff --git a/src/extensions.ts b/src/extensions.ts index ec6754b8..7d52b39d 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -50,13 +50,7 @@ export async function addExtensionWindows( let script = '\n'; await utils.asyncForEach(extensions, async function(extension: string) { // add script to enable extension is already installed along with php - let minimum_stability = 'stable'; - switch (version + extension.toLowerCase()) { - case '7.4xdebug': - minimum_stability = 'beta'; - break; - } - script += '\nAdd-Extension ' + extension + ' ' + minimum_stability; + script += '\nAdd-Extension ' + extension; }); return script; } diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 97a95f29..d965460d 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -48,7 +48,8 @@ Set-PhpIniKey -Key 'date.timezone' -Value 'UTC' -Path $php_dir Enable-PhpExtension -Extension openssl, curl -Path $php_dir try { Update-PhpCAInfo -Path $php_dir -Source CurrentUser -} catch { +} +catch { Update-PhpCAInfo -Path $php_dir -Source Curl } if ([Version]$installed.Version -ge '7.4') { @@ -59,7 +60,19 @@ Add-Log $tick "PHP" $status Install-Composer -Scope System -Path $php_dir -PhpPath $php_dir Add-Log $tick "Composer" "Installed" -Function Add-Extension($extension, $mininum_stability) { +Function Add-Extension { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $extension, + [Parameter(Position = 1, Mandatory = $false)] + [ValidateNotNull()] + [ValidateSet('stable', 'beta', 'alpha', 'devel', 'snapshot')] + [string] + $mininum_stability = 'stable' + ) try { $extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension } if ($null -ne $extension_info) {