Add support for adding PHP extensions

This commit is contained in:
Shivam Mathur
2019-09-07 18:01:50 +05:30
parent 1c6748567c
commit c5513d9a78
6842 changed files with 266 additions and 1017047 deletions

View File

@ -1,62 +1,109 @@
import * as core from '@actions/core';
import {exec} from '@actions/exec/lib/exec';
const fs = require('fs');
import * as path from 'path';
import * as httpm from 'typed-rest-client/HttpClient';
import * as fs from 'fs';
let darwin = `echo $1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew unlink php
brew tap exolnet/homebrew-deprecated
brew tap homebrew/homebrew-php
brew install php@$1
brew link --force --overwrite php@$1
curl -sS https://getcomposer.org/installer | php
chmod +x composer.phar
mv composer.phar /usr/local/bin/composer
php -v
composer -V`;
/*
Read the scripts
*/
let darwin = fs.readFileSync(path.join(__dirname, '../src/darwin.sh'), 'utf8');
let linux = fs.readFileSync(path.join(__dirname, '../src/linux.sh'), 'utf8');
let windows = fs.readFileSync(
path.join(__dirname, '../src/windows.ps1'),
'utf8'
);
let linux = `sudo add-apt-repository ppa:ondrej/php -y
sudo apt update -y
sudo apt install -y php$1 curl
sudo update-alternatives --set php /usr/bin/php$1
sudo curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
php -v
composer -V`;
let windows = `param (
[Parameter(Mandatory=$true)][string]$version = "7.3"
)
echo "Installing NuGet"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
if($version -lt '7.0') {
echo "Installing Visual C++"
Install-Module -Name VcRedist -Force
New-Item -Path C:\\Temp\\VcRedist -ItemType Directory
Get-VcList | Save-VcRedist -Path C:\\Temp\\VcRedist
$VcList = Get-VcList
Install-VcRedist -Path C:\\Temp\\VcRedist -VcList $VcList -Silent
/*
Credit: https://github.com/Atinux
*/
async function asyncForEach(array: any, callback: any) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
echo "Installing PhpManager"
Install-Module -Name PhpManager -Force -Scope CurrentUser
echo "Installing PHP"
Uninstall-Php C:\\tools\\php
Install-Php -Version $version -Architecture x86 -ThreadSafe $true -Path C:\\tools\\php$version -TimeZone UTC
echo "Switch PHP"
(Get-PhpSwitcher).targets
Initialize-PhpSwitcher -Alias C:\\tools\\php -Scope CurrentUser -Force
Add-PhpToSwitcher -Name $version -Path C:\\tools\\php$version -Force
Switch-Php $version -Force
echo "Housekeeping in PHP.ini, enabling openssl"
Move-item -Path C:\\tools\\php$version\\php.ini-development -Destination C:\\tools\\php$version\\php.ini -Force
Add-Content C:\\tools\\php$version\\php.ini "extension=C:\\tools\\php$version\\ext\\php_openssl.dll"
Add-Content C:\\tools\\php$version\\php.ini "date.timezone = 'UTC'"
echo "Installing Composer"
Install-Composer -Scope System -Path C:\\tools\\php
php -v
composer -V`;
/*
Enable functions which are installed but not enabled
*/
async function enableExtension(extension: string) {
windows += `try {
$ext_dir = Get-PhpIniKey extension_dir
$exist = Test-Path -Path $ext_dir\\php_${extension}.dll
$enabled = php -r "if (in_array('${extension}', get_loaded_extensions())) {echo 'yes';} else {echo 'no';}"
if($enabled -eq 'no' -and $exist) {
Enable-PhpExtension ${extension} C:\\tools\\php$version
}
} catch [Exception] {
echo $_
}\n`;
let shell_code = `ext_dir=$(php -i | grep "extension_dir" | sed -e "s|.*=>\s*||")
enabled=$(php -r "if (in_array('${extension}', get_loaded_extensions())) {echo 'yes';} else {echo 'no';}")
if [ "$enabled" == 'no' ] && [ test -f "$ext_dir/${extension}.so" ]; then
echo "extension=${extension}.so" >> 'php -i | grep "Loaded Configuration" | sed -e "s|.*:\s*||"'
fi\n`;
linux += shell_code;
darwin += shell_code;
}
/*
Install and enable extensions
*/
async function addExtension(extension_csv: string, version: string) {
let extensions: any = extension_csv
.split(',')
.map(function(extension: string) {
return extension
.trim()
.replace('php-', '')
.replace('php_', '');
});
linux += '\n';
windows += '\n';
darwin += '\n';
await asyncForEach(extensions, async function(extension: string) {
enableExtension(extension);
linux +=
'sudo apt install -y php' +
version +
'-' +
extension +
' || echo "Couldn\'t find extension php' +
version +
'-' +
extension +
'"\n';
const http = new httpm.HttpClient('shivammathur/php-setup', [], {
allowRetries: true,
maxRetries: 2
});
const response: httpm.HttpClientResponse = await http.get(
'https://pecl.php.net/package/' + extension
);
if (response.message.statusCode == 200) {
windows +=
'try { Install-PhpExtension ' +
extension +
' } catch [Exception] { echo $_; echo "Could not install extension: "' +
extension +
' }\n';
darwin +=
'pecl install ' +
extension +
' || echo "Couldn\'t find extension: ' +
extension +
'"\n';
} else {
console.log('Cannot find pecl extension: ' + extension);
}
});
linux += 'sudo apt autoremove -y';
}
/*
Write final script which runs
*/
async function createScript(filename: string, version: string) {
let script = '';
if (filename == 'linux.sh') {
@ -75,6 +122,9 @@ async function createScript(filename: string, version: string) {
});
}
/*
Run the script
*/
async function run() {
try {
let version = process.env['php-version'];
@ -83,6 +133,15 @@ async function run() {
}
console.log('Input: ' + version);
let extension_csv = process.env['extension-csv'];
if (!extension_csv) {
extension_csv = core.getInput('extension-csv');
}
if (extension_csv) {
console.log('Input: ' + extension_csv);
await addExtension(extension_csv, version);
}
let os_version = process.platform;
if (os_version == 'darwin') {
await createScript('darwin.sh', version);
@ -103,6 +162,7 @@ async function run() {
}
}
run().then(() => {
// call the run function
run().then(function() {
console.log('done');
});

View File

@ -1,6 +1,8 @@
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update -y
sudo mkdir -p /run/php
sudo apt install -y php$1 curl
sudo apt autoremove -y
sudo update-alternatives --set php /usr/bin/php$1
sudo curl -s https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

View File

@ -4,36 +4,30 @@ param (
echo "Installing NuGet"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
if($version -lt '7.0') {
echo "Installing Visual C++"
Install-Module -Name VcRedist -Force
New-Item -Path C:\Temp\VcRedist -ItemType Directory
Get-VcList | Save-VcRedist -Path C:\Temp\VcRedist
$VcList = Get-VcList
Install-VcRedist -Path C:\Temp\VcRedist -VcList $VcList -Silent
echo "Installing Visual C++"
Install-Module -Name VcRedist -Force
New-Item -Path C:\Temp\VcRedist -ItemType Directory
Get-VcList | Save-VcRedist -Path C:\Temp\VcRedist
$VcList = Get-VcList
Install-VcRedist -Path C:\Temp\VcRedist -VcList $VcList -Silent
}
echo "Installing PhpManager"
Install-Module -Name PhpManager -Force -Scope CurrentUser
echo "Installing PHP"
Uninstall-Php C:\tools\php
Install-Php -Version $version -Architecture x86 -ThreadSafe $true -Path C:\tools\php$version -TimeZone UTC
echo "Switch PHP"
(Get-PhpSwitcher).targets
Initialize-PhpSwitcher -Alias C:\tools\php -Scope CurrentUser -Force
Add-PhpToSwitcher -Name $version -Path C:\tools\php$version -Force
Switch-Php $version -Force
echo "Housekeeping in PHP.ini, enabling openssl"
Move-item -Path C:\tools\php$version\php.ini-development -Destination C:\tools\php$version\php.ini -Force
Add-Content C:\tools\php$version\php.ini "extension=C:\tools\php$version\ext\php_openssl.dll"
Move-item -Path C:\tools\php$version\php.ini-production -Destination C:\tools\php$version\php.ini -Force
Add-Content C:\tools\php$version\php.ini "date.timezone = 'UTC'"
Set-PhpIniKey extension_dir C:\tools\php$version\ext
Enable-PhpExtension openssl
echo "Installing Composer"
Install-Composer -Scope System
Install-Composer -Scope System -Path C:\tools\php
php -v
composer -V