Improve code quality and write tests

This commit is contained in:
Shivam Mathur
2019-09-20 08:11:20 +05:30
parent db44db4b97
commit 43178a7254
3597 changed files with 255478 additions and 785554 deletions

View File

@ -11,7 +11,6 @@ echo 'export PATH="/usr/local/opt/krb5/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/krb5/sbin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/bzip2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/Users/runner/.phpbrew/php/php-7.4.0RC1/bin:$PATH"' >> ~/.bash_profile
@ -33,7 +32,7 @@ export READLINE_CFLAGS="-I/usr/local/opt/readline/include"
export BZIP2_LIBS="-L/usr/local/opt/bzip2/lib"
export BZIP2_CFLAGS="-I/usr/local/opt/bzip2/include"
export PKG_CONFIG_PATH="/usr/local/opt/krb5/lib/pkgconfig:/usr/local/opt/icu4c/lib/pkgconfig:/usr/local/obzip2pt/libffi/lib/pkgconfig:/usr/local/opt/openssl@1.1/lib/pkgconfig:/usr/local/opt/readline/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig:/usr/local/opt/krb5/lib/pkgconfig:/usr/local/opt/icu4c/lib/pkgconfig:/usr/local/opt/libffi/lib/pkgconfig:/usr/local/opt/libxml2/lib/pkgconfig"
cd ~
cd ~ || echo "could not move to ~"
curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x ./phpbrew
sudo mv phpbrew /usr/local/bin/phpbrew
@ -46,5 +45,6 @@ phpbrew switch php-7.4.0RC2 >> /dev/null
sudo mkdir -p /usr/local/bin
sudo ln -sf /Users/runner/.phpbrew/php/php-7.4.0RC2/bin/php /usr/local/bin/php
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
sudo chmod 777 $ini_file
ext_dir=$(/usr/bin/php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||")
sudo chmod 777 "$ini_file"
brew install composer

View File

@ -1,15 +1,17 @@
version=$(php-config --version | cut -c 1-3)
if [ "$version" != "$1" ]; then
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE
brew tap exolnet/homebrew-deprecated;
brew unlink php;
brew install php@$1;
brew link --force --overwrite php@$1;
brew install php@"$1";
brew link --force --overwrite php@"$1";
else
sudo cp /etc/php.ini.default /etc/php.ini
fi
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
sudo chmod 777 $ini_file
mkdir -p $(pecl config-get ext_dir)
ext_dir=$(/usr/bin/php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||")
sudo chmod 777 "$ini_file"
mkdir -p "$(pecl config-get ext_dir)"
curl -sS https://getcomposer.org/installer | php
chmod +x composer.phar
mv composer.phar /usr/local/bin/composer

187
src/features.ts Normal file
View File

@ -0,0 +1,187 @@
import * as utils from './utils';
import * as pecl from './pecl';
export async function addExtension(
extensions: string,
version: string,
os_version: string
): Promise<string> {
if (os_version === 'win32') {
return await addExtensionWindows(extensions, version);
} else if (os_version === 'linux') {
return await addExtensionLinux(extensions, version);
}
return await addExtensionDarwin(extensions);
}
export async function addINIValues(ini_values_csv: string, os_version: string) {
if (os_version === 'win32') {
return await addINIValuesWindows(ini_values_csv);
}
return await addINIValuesUnix(ini_values_csv);
}
/**
* Enable extensions which are installed but not enabled on windows
*
* @param extension
*/
export async function enableExtensionWindows(extension: string) {
return `try {
$${extension}_found = 0
$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
$${extension}_found = 1
}
} catch [Exception] {
echo $_
}\n`;
}
/**
* Enable extensions which are installed but not enabled on unix
*
* @param extension
*/
export async function enableExtensionUnix(extension: string) {
return `${extension}_found=0
enabled=$(php -r "if (in_array('${extension}', get_loaded_extensions())) {echo 'yes';} else {echo 'no';}")
if [ "$enabled" = "no" ] && [ -e "$ext_dir/${extension}.so" ]; then
echo "extension=${extension}.so" >> 'php -i | grep "Loaded Configuration" | sed -e "s|.*=>\s*||"'
echo "${extension} enabled"
${extension}_found=1
fi\n`;
}
/**
* Install and enable extensions for darwin
*
* @param extension_csv
*/
export async function addExtensionDarwin(
extension_csv: string
): Promise<string> {
let extensions: Array<string> = await utils.extensionArray(extension_csv);
let script: string = '\n';
await utils.asyncForEach(extensions, async function(extension: string) {
// add script to enable extension is already installed along with php
script += await enableExtensionUnix(extension);
if (await pecl.checkPECLExtension(extension)) {
script +=
'if [ $' +
extension +
'_found -eq 0 ]; then sudo pecl install ' +
extension +
' || echo "Couldn\'t find extension: ' +
extension +
'"; fi\n';
}
});
return script;
}
/**
* Install and enable extensions for windows
*
* @param extension_csv
* @param version
*/
export async function addExtensionWindows(
extension_csv: string,
version: string
): Promise<string> {
let extensions: Array<string> = await utils.extensionArray(extension_csv);
let script: string = '\n';
await utils.asyncForEach(extensions, async function(extension: string) {
// add script to enable extension is already installed along with php
script += await enableExtensionWindows(extension);
let extension_version = 'stable';
if (version == '7.4') {
extension_version = 'alpha';
}
if (await pecl.checkPECLExtension(extension)) {
script +=
'if($' +
extension +
'_found -eq 0) { ' +
'try { Install-PhpExtension ' +
extension +
' -MinimumStability ' +
extension_version +
' } catch [Exception] { echo $_; echo "Could not install extension: "' +
extension +
' } }\n';
}
});
return script;
}
/**
* Install and enable extensions for linux
*
* @param extension_csv
* @param version
*/
export async function addExtensionLinux(
extension_csv: string,
version: string
): Promise<string> {
let extensions: Array<string> = await utils.extensionArray(extension_csv);
let script: string = '\n';
await utils.asyncForEach(extensions, async function(extension: string) {
// add script to enable extension is already installed along with php
script += await enableExtensionUnix(extension);
script +=
'if [ $' +
extension +
'_found -eq 0 ]; then sudo DEBIAN_FRONTEND=noninteractive apt install -y php' +
version +
'-' +
extension +
' || echo "Couldn\'t find extension php' +
version +
'-' +
extension +
'"; fi\n';
});
return script;
}
/**
* Add script to set custom ini values for unix
*
* @param ini_values_csv
*/
export async function addINIValuesUnix(
ini_values_csv: string
): Promise<string> {
let script: string = '\n';
let ini_values: Array<string> = await utils.INIArray(ini_values_csv);
await utils.asyncForEach(ini_values, async function(ini_value: string) {
// add script to set ini value
script += 'echo "' + ini_value + '" >> $ini_file\n';
});
return script;
}
/**
* Add script to set custom ini values for windows
*
* @param ini_values_csv
*/
export async function addINIValuesWindows(
ini_values_csv: string
): Promise<string> {
let script: string = '\n';
let ini_values: Array<string> = await utils.INIArray(ini_values_csv);
await utils.asyncForEach(ini_values, async function(ini_value: string) {
// add script to set ini value
script +=
'Add-Content C:\\tools\\php$version\\php.ini "' + ini_value + '"\n';
});
return script;
}

View File

@ -1,217 +1,53 @@
import * as core from '@actions/core';
import {exec} from '@actions/exec/lib/exec';
import * as path from 'path';
import * as httpm from 'typed-rest-client/HttpClient';
import * as fs from 'fs';
import * as core from '@actions/core';
import * as utils from './utils';
import * as features from './features';
/*
Read the scripts
*/
let os_version = process.platform;
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'
);
/*
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);
}
}
/*
Enable extensions which are installed but not enabled
*/
function enableExtension(extension: string) {
windows += `try {
$${extension}_found = 0
$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
$${extension}_found = 1
}
} catch [Exception] {
echo $_
}\n`;
let shell_code = `ext_dir=$(php-config --extension-dir)
${extension}_found=0
enabled=$(php -r "if (in_array('${extension}', get_loaded_extensions())) {echo 'yes';} else {echo 'no';}")
if [ "$enabled" = "no" ] && [ -e "$ext_dir/${extension}.so" ]; then
echo "extension=${extension}.so" >> 'php -i | grep "Loaded Configuration" | sed -e "s|.*=>\s*||"'
echo "${extension} enabled"
${extension}_found=1
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) {
// add script to enable extension is already installed along with php
enableExtension(extension);
// else add script to attempt to install the extension
if (os_version == 'linux') {
linux +=
'if [ $' +
extension +
'_found -eq 0 ]; then sudo DEBIAN_FRONTEND=noninteractive apt install -y php' +
version +
'-' +
extension +
' || echo "Couldn\'t find extension php' +
version +
'-' +
extension +
'"; fi\n';
} else {
// check if pecl extension exists
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) {
let extension_version = 'stable';
if (version == '7.4') {
extension_version = 'alpha';
}
windows +=
'if($' +
extension +
'_found -eq 0) { ' +
'try { Install-PhpExtension ' +
extension +
' -MinimumStability ' +
extension_version +
' } catch [Exception] { echo $_; echo "Could not install extension: "' +
extension +
' } }\n';
darwin +=
'if [ $' +
extension +
'_found -eq 0 ]; then sudo pecl install ' +
extension +
' || echo "Couldn\'t find extension: ' +
extension +
'"; fi\n';
} else {
console.log('Cannot find pecl extension: ' + extension);
}
}
});
linux += 'sudo DEBIAN_FRONTEND=noninteractive apt autoremove -y';
}
/*
Add script to set custom ini values
*/
async function addINIValues(ini_values_csv: string) {
let ini_values: any = ini_values_csv
.split(',')
.map(function(ini_value: string) {
return ini_value.trim();
});
await asyncForEach(ini_values, async function(ini_value: string) {
// add script to set ini value
linux += '\necho "' + ini_value + '" >> $ini_file';
darwin += '\necho "' + ini_value + '" >> $ini_file';
windows +=
'\nAdd-Content C:\\tools\\php$version\\php.ini "' + ini_value + '"';
});
}
/*
Write final script which runs
*/
async function createScript(filename: string, version: string) {
let script = '';
if (filename == 'linux.sh') {
script = linux;
} else if (filename == 'darwin.sh') {
script = darwin;
} else if (filename == 'windows.ps1') {
script = windows;
}
fs.writeFile(version + filename, script, function(error: any) {
if (error) {
return console.log(error);
}
console.log('The file was saved!');
});
}
/*
Run the script
*/
/**
* Run the script
*/
async function run() {
try {
// taking inputs
let version = process.env['php-version'];
if (!version) {
version = core.getInput('php-version', {required: true});
}
console.log('Version: ' + version);
if (version == '7.4') {
darwin = fs.readFileSync(path.join(__dirname, '../src/7.4.sh'), 'utf8');
}
let extension_csv = process.env['extension-csv'];
if (!extension_csv) {
extension_csv = core.getInput('extension-csv');
}
if (extension_csv) {
console.log('Extensions: ' + extension_csv);
await addExtension(extension_csv, version);
}
let ini_values_csv = process.env['ini-values-csv'];
if (!ini_values_csv) {
ini_values_csv = core.getInput('ini-values-csv');
}
if (ini_values_csv) {
console.log('INI Values: ' + ini_values_csv);
await addINIValues(ini_values_csv);
}
let version: string = await utils.getInput('php-version', true);
let extension_csv: string = await utils.getInput('extension-csv', false);
let ini_values_csv: string = await utils.getInput('ini-values-csv', false);
let os_version: string = process.platform;
// check the os version and run the respective script
if (os_version == 'darwin') {
await createScript('darwin.sh', version);
await exec('sudo chmod a+x ' + version + 'darwin.sh');
let darwin: string = await utils.readScript(
'darwin.sh',
version,
os_version
);
darwin += await features.addExtension(extension_csv, version, os_version);
darwin += await features.addINIValues(ini_values_csv, os_version);
await utils.writeScript('darwin.sh', version, darwin);
await exec('sh -x ./' + version + 'darwin.sh ' + version);
} else if (os_version == 'win32') {
await createScript('windows.ps1', version);
await exec(
'powershell .\\' + version + 'windows.ps1 -version ' + version
let windows: string = await utils.readScript(
'win32.ps1',
version,
os_version
);
windows += await features.addExtension(
extension_csv,
version,
os_version
);
windows += await features.addINIValues(ini_values_csv, os_version);
await utils.writeScript('win32.ps1', version, windows);
await exec('powershell .\\' + version + 'win32.ps1 -version ' + version);
} else if (os_version == 'linux') {
await createScript('linux.sh', version);
await exec('sudo chmod a+x ' + version + 'linux.sh');
let linux: string = await utils.readScript(
'linux.sh',
version,
os_version
);
linux += await features.addExtension(extension_csv, version, os_version);
linux += await features.addINIValues(ini_values_csv, os_version);
await utils.writeScript('linux.sh', version, linux);
await exec('./' + version + 'linux.sh ' + version);
}
} catch (err) {

View File

@ -3,12 +3,12 @@ if [ "$version" != "$1" ]; then
if [ ! -e "/usr/bin/php$1" ]; then
sudo DEBIAN_FRONTEND=noninteractive add-apt-repository ppa:ondrej/php -y
sudo DEBIAN_FRONTEND=noninteractive apt update -y
sudo DEBIAN_FRONTEND=noninteractive apt install -y php$1 curl;
sudo DEBIAN_FRONTEND=noninteractive apt install -y php"$1" curl php"$1"-curl;
sudo DEBIAN_FRONTEND=noninteractive apt autoremove -y;
fi
for tool in php phar phar.phar php-cgi php-config phpize; do
if [ -e "/usr/bin/$tool$1" ]; then
sudo update-alternatives --set $tool /usr/bin/$tool$1;
sudo update-alternatives --set $tool /usr/bin/"$tool$1";
fi
done
fi
@ -19,7 +19,8 @@ if [ ! -e "/usr/bin/composer" ]; then
fi
composer global require hirak/prestissimo
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
sudo chmod 777 $ini_file
ext_dir=$(/usr/bin/php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||")
sudo chmod 777 "$ini_file"
sudo mkdir -p /run/php
php -v
composer -V

17
src/pecl.ts Normal file
View File

@ -0,0 +1,17 @@
import * as hc from 'typed-rest-client/HttpClient';
/**
* Function to check if PECL extension exists
*
* @param extension
*/
export async function checkPECLExtension(extension: string): Promise<boolean> {
const http: hc.HttpClient = new hc.HttpClient('shivammathur/php-setup', [], {
allowRetries: true,
maxRetries: 2
});
const response: hc.HttpClientResponse = await http.get(
'https://pecl.php.net/package/' + extension
);
return response.message.statusCode === 200;
}

91
src/utils.ts Normal file
View File

@ -0,0 +1,91 @@
import * as fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';
export async function getInput(
name: string,
mandatory: boolean
): Promise<string> {
let input = process.env[name];
if (!input) {
input = core.getInput(name, {required: mandatory});
}
return input;
}
/**
* Async foreach loop
*
* @author https://github.com/Atinux
* @param array
* @param callback
*/
export async function asyncForEach(
array: Array<any>,
callback: any
): Promise<any> {
for (let index: number = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
/**
* Read the scripts
*
* @param filename
* @param version
* @param os_version
*/
export async function readScript(
filename: string,
version: string,
os_version: string
): Promise<string> {
if (version === '7.4' && os_version === 'darwin') {
return fs.readFileSync(path.join(__dirname, '../src/7.4.sh'), 'utf8');
}
return fs.readFileSync(path.join(__dirname, '../src/' + filename), 'utf8');
}
/**
* Write final script which runs
*
* @param filename
* @param version
* @param script
*/
export async function writeScript(
filename: string,
version: string,
script: string
): Promise<any> {
fs.writeFileSync(version + filename, script, {mode: 0o755});
}
/**
* Function to break extension csv into an array
* @param extension_csv
*/
export async function extensionArray(
extension_csv: string
): Promise<Array<string>> {
return extension_csv.split(',').map(function(extension: string) {
return extension
.trim()
.replace('php-', '')
.replace('php_', '');
});
}
/**
* Function to break ini values csv into an array
*
* @param ini_values_csv
* @constructor
*/
export async function INIArray(ini_values_csv: string): Promise<Array<string>> {
return ini_values_csv.split(',').map(function(ini_value: string) {
return ini_value.trim();
});
}