Refactor installation scripts

This commit is contained in:
Shivam Mathur
2019-10-11 04:41:25 +05:30
parent 3087ceb811
commit e755fb7a69
25 changed files with 345 additions and 520 deletions

View File

@ -31,13 +31,8 @@ export async function addINIValues(ini_values_csv: string, os_version: string) {
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;
return '\necho "' + ini_values.join('\n') + '" >> $ini_file\n';
}
/**
@ -48,11 +43,8 @@ export async function addINIValuesUnix(
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\\php.ini "' + ini_value + '"\n';
});
return script;
return (
'Add-Content C:\\tools\\php\\php.ini "' + ini_values.join('\n') + '"\n'
);
}

View File

@ -1,5 +1,4 @@
import * as utils from './utils';
import * as pecl from './pecl';
import * as extensions from './extensions';
import * as config from './config';

View File

@ -1,5 +1,4 @@
import * as utils from './utils';
import * as pecl from './pecl';
export async function addExtension(
extension_csv: string,
@ -24,75 +23,6 @@ export async function addExtension(
}
}
/**
* Enable extensions which are installed but not enabled on windows
*
* @param extension
*/
export async function enableExtensionWindows(
extension: string,
log_prefix: string
) {
return (
`try {
$exist = Test-Path -Path $ext_dir\\php_${extension}.dll
if(!(php -m | findstr -i ${extension}) -and $exist) {
Add-Content C:\\tools\\php\\php.ini "${await utils.getExtensionPrefix(
extension
)}=php_${extension}.dll"\n` +
(await utils.log('Enabled ' + extension, 'win32', 'success', log_prefix)) +
` } elseif(php -m | findstr -i ${extension}) {\n` +
(await utils.log(
extension + ' was already enabled',
'win32',
'success',
log_prefix
)) +
` }
} catch [Exception] {\n` +
(await utils.log(
extension + ' could not be enabled',
'win32',
'error',
log_prefix
)) +
` }\n`
);
}
/**
* Enable extensions which are installed but not enabled on unix
*
* @param extension
* @param os_version
*/
export async function enableExtensionUnix(
extension: string,
os_version: string,
log_prefix: string
) {
return (
`if [ ! "$(php -m | grep -i ${extension})" ] && [ -e "$ext_dir/${extension}.so" ]; then
echo "${await utils.getExtensionPrefix(
extension
)}=${extension}" >> $ini_file\n` +
(await utils.log(
'Enabled ' + extension,
os_version,
'success',
log_prefix
)) +
`;\n elif [ "$(php -m | grep -i ${extension})" ]; then \n` +
(await utils.log(
extension + ' was already enabled',
os_version,
'success',
log_prefix
)) +
`; fi\n`
);
}
/**
* Install and enable extensions for darwin
*
@ -109,63 +39,33 @@ export async function addExtensionDarwin(
await utils.asyncForEach(extensions, async function(extension: string) {
extension = extension.toLowerCase();
// add script to enable extension is already installed along with php
script += await enableExtensionUnix(extension, 'darwin', log_prefix);
switch (await pecl.checkPECLExtension(extension)) {
case true:
let install_command: string = '';
switch (version + extension) {
case '7.4xdebug':
install_command =
'sh ./xdebug_darwin.sh >/dev/null 2>&1 && echo "zend_extension=xdebug.so" >> $ini_file';
break;
case '7.4pcov':
install_command =
'sh ./pcov.sh >/dev/null 2>&1 && echo "extension=pcov.so" >> $ini_file';
break;
case '5.6xdebug':
install_command = 'sudo pecl install xdebug-2.5.5 >/dev/null 2>&1';
break;
default:
install_command =
'sudo pecl install ' + extension + ' >/dev/null 2>&1';
break;
}
script +=
'if [ ! "$(php -m | grep -i ' +
extension +
')" ]; then ' +
install_command +
' && ' +
(await utils.log(
'Installed and enabled ' + extension,
'darwin',
'success',
log_prefix
)) +
' || ' +
(await utils.log(
'Could not install ' + extension + ' on PHP' + version,
'darwin',
'error',
log_prefix
)) +
'; fi\n';
let install_command: string = '';
switch (version + extension) {
case '7.4xdebug':
install_command =
'sh ./xdebug_darwin.sh >/dev/null 2>&1 && echo "zend_extension=xdebug.so" >> $ini_file';
break;
case '7.4pcov':
install_command =
'sh ./pcov.sh >/dev/null 2>&1 && echo "extension=pcov.so" >> $ini_file';
break;
case '5.6xdebug':
install_command = 'sudo pecl install xdebug-2.5.5 >/dev/null 2>&1';
break;
case false:
default:
script +=
'if [ ! "$(php -m | grep -i ' +
extension +
')" ]; then \n' +
(await utils.log(
'Could not find ' + extension + ' for PHP' + version + ' on PECL',
'darwin',
'error',
log_prefix
)) +
'; fi\n';
install_command = 'sudo pecl install ' + extension + ' >/dev/null 2>&1';
break;
}
script +=
'add_extension ' +
extension +
' "' +
install_command +
'" ' +
(await utils.getExtensionPrefix(extension)) +
' "' +
log_prefix +
'"\n';
});
return script;
}
@ -186,63 +86,33 @@ export async function addExtensionWindows(
await utils.asyncForEach(extensions, async function(extension: string) {
extension = extension.toLowerCase();
// add script to enable extension is already installed along with php
script += await enableExtensionWindows(extension, log_prefix);
switch (await pecl.checkPECLExtension(extension)) {
case true:
let install_command: string = '';
switch (version + extension) {
case '7.4xdebug':
const extension_url: string =
'https://xdebug.org/files/php_xdebug-2.8.0beta2-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 +=
'if(!(php -m | findstr -i ' +
extension +
')) { ' +
'try { ' +
install_command +
'\n' +
(await utils.log(
'Installed and enabled ' + extension,
'win32',
'success',
log_prefix
)) +
' } catch [Exception] { ' +
(await utils.log(
'Could not install ' + extension + ' on PHP' + version,
'win32',
'error',
log_prefix
)) +
' } }\n';
let install_command: string = '';
switch (version + extension) {
case '7.4xdebug':
const extension_url: string =
'https://xdebug.org/files/php_xdebug-2.8.0beta2-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 false:
case '7.2xdebug':
default:
script +=
'if(!(php -m | findstr -i ' +
extension +
')) { ' +
(await utils.log(
'Could not find ' + extension + ' for PHP' + version + ' on PECL',
'win32',
'error',
log_prefix
)) +
' } \n';
install_command = 'Install-PhpExtension ' + extension;
break;
}
script +=
'Add-Extension ' +
extension +
' "' +
install_command +
'" ' +
(await utils.getExtensionPrefix(extension)) +
' "' +
log_prefix +
'"\n';
});
return script;
}
@ -263,7 +133,6 @@ export async function addExtensionLinux(
await utils.asyncForEach(extensions, async function(extension: string) {
extension = extension.toLowerCase();
// add script to enable extension is already installed along with php
script += await enableExtensionUnix(extension, 'linux', log_prefix);
let install_command: string = '';
switch (version + extension) {
@ -285,25 +154,15 @@ export async function addExtensionLinux(
break;
}
script +=
'if [ ! "$(php -m | grep -i ' +
'add_extension ' +
extension +
')" ]; then ' +
' "' +
install_command +
' && ' +
(await utils.log(
'Installed and enabled ' + extension,
'linux',
'success',
log_prefix
)) +
' || ' +
(await utils.log(
'Could not find php' + version + '-' + extension + ' on APT repository',
'linux',
'error',
log_prefix
)) +
'; fi\n';
'" ' +
(await utils.getExtensionPrefix(extension)) +
' "' +
log_prefix +
'"\n';
});
return script;
}

View File

@ -79,6 +79,4 @@ async function run() {
}
// call the run function
run().then(function() {
console.log('done');
});
run();

View File

@ -1,17 +0,0 @@
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/json.php?package=' + extension
);
return response.message.statusCode === 200;
}

View File

@ -1,3 +1,4 @@
version='7.4.0RC3'
brew install pkg-config autoconf bison re2c openssl@1.1 krb5 enchant libffi freetype intltool icu4c libiconv t1lib gd libzip gmp tidyp libxml2 libxslt postgresql curl >/dev/null 2>&1
brew link icu4c gettext --force >/dev/null 2>&1
@ -48,12 +49,36 @@ export PHPBREW_HOME=/opt/phpbrew
echo "[[ -e ~/.phpbrew/bashrc ]] && source ~/.phpbrew/bashrc" >> ~/.bashrc
source ~/.bash_profile >/dev/null 2>&1
source ~/.bashrc >/dev/null 2>&1
phpbrew install -j 6 7.4.0RC3 +dev >/dev/null 2>&1
phpbrew switch 7.4.0RC3
sudo ln -sf /opt/phpbrew/php/php-7.4.0RC3/bin/* /usr/local/bin/
sudo ln -sf /opt/phpbrew/php/php-7.4.0RC3/etc/php.ini /etc/php.ini
phpbrew install -j 6 $version +dev >/dev/null 2>&1
phpbrew switch $version
sudo ln -sf /opt/phpbrew/php/php-$version/bin/* /usr/local/bin/
sudo ln -sf /opt/phpbrew/php/php-$version/etc/php.ini /etc/php.ini
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
ext_dir=$(php -i | grep "extension_dir => /opt" | sed -e "s|.*=> s*||")
pecl config-set php_ini "$ini_file"
sudo chmod 777 "$ini_file"
brew install composer
brew install composer
add_extension()
{
extension=$1
install_command=$2
prefix=$3
log_prefix=$4
if ! php -m | grep -i -q "$extension" && [ -e "$ext_dir/$extension.so" ]; then
echo "$prefix=$extension" >> "$ini_file" && echo "\033[32;1m$log_prefix: Enabled $extension\033[0m";
elif php -m | grep -i -q "$extension"; then
echo "\033[33;1m$log_prefix: $extension was already enabled\033[0m";
elif ! php -m | grep -i -q "$extension"; then
exists=$(curl -sL https://pecl.php.net/json.php?package="$extension" -w "%{http_code}" -o /dev/null)
if [ "$exists" = "200" ]; then
eval "$install_command" && \
echo "\033[32;1m$log_prefix: Installed and enabled $extension\033[0m" || \
echo "\033[31;1m$log_prefix: Could not install $extension on PHP$version\033[0m";
else
if ! php -m | grep -i -q "$extension"; then
echo "\033[31;1m$log_prefix: Could not find $extension for PHP$version on PECL\033[0m";
fi
fi
fi
}

View File

@ -1,3 +1,4 @@
version=$1
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE
if [ "$1" = "5.6" ] || [ "$1" = "7.0" ]; then
brew tap exolnet/homebrew-deprecated >/dev/null 2>&1
@ -10,4 +11,28 @@ sudo chmod 777 "$ini_file"
mkdir -p "$(pecl config-get ext_dir)"
composer global require hirak/prestissimo >/dev/null 2>&1
php -v
composer -V
composer -V
add_extension()
{
extension=$1
install_command=$2
prefix=$3
log_prefix=$4
if ! php -m | grep -i -q "$extension" && [ -e "$ext_dir/$extension.so" ]; then
echo "$prefix=$extension" >> "$ini_file" && echo "\033[32;1m$log_prefix: Enabled $extension\033[0m";
elif php -m | grep -i -q "$extension"; then
echo "\033[33;1m$log_prefix: $extension was already enabled\033[0m";
elif ! php -m | grep -i -q "$extension"; then
exists=$(curl -sL https://pecl.php.net/json.php?package="$extension" -w "%{http_code}" -o /dev/null)
if [ "$exists" = "200" ]; then
eval "$install_command" && \
echo "\033[32;1m$log_prefix: Installed and enabled $extension\033[0m" || \
echo "\033[31;1m$log_prefix: Could not install $extension on PHP$version\033[0m";
else
if ! php -m | grep -i -q "$extension"; then
echo "\033[31;1m$log_prefix: Could not find $extension for PHP$version on PECL\033[0m";
fi
fi
fi
}

View File

@ -1,5 +1,6 @@
version=$(php-config --version | cut -c 1-3)
if [ "$version" != "$1" ]; then
existing_version=$(php-config --version | cut -c 1-3)
version=$1
if [ "$existing_version" != "$1" ]; then
if [ ! -e "/usr/bin/php$1" ]; then
sudo DEBIAN_FRONTEND=noninteractive add-apt-repository ppa:ondrej/php -y >/dev/null 2>&1
sudo DEBIAN_FRONTEND=noninteractive apt update -y >/dev/null 2>&1
@ -37,3 +38,20 @@ sudo chmod 777 "$ini_file"
sudo mkdir -p /run/php
php -v
composer -V
add_extension()
{
extension=$1
install_command=$2
prefix=$3
log_prefix=$4
if ! php -m | grep -i -q "$extension" && [ -e "$ext_dir/$extension.so" ]; then
echo "$prefix=$extension" >> "$ini_file" && echo "\033[32;1m$log_prefix: Enabled $extension\033[0m";
elif php -m | grep -i -q "$extension"; then
echo "\033[33;1m$log_prefix: $extension was already enabled\033[0m";
elif ! php -m | grep -i -q "$extension"; then
eval "$install_command" && \
echo "\033[32;1m$log_prefix: Installed and enabled $extension\033[0m" || \
echo "\033[31;1m$log_prefix: Could not find php$version-$extension on APT repository\033[0m";
fi
}

View File

@ -40,4 +40,41 @@ if($version -lt '7.4') {
Write-Host "Installing Composer" -ForegroundColor Blue
Install-Composer -Scope System -Path C:\tools\php
php -v
composer -V
composer -V
Function Add-Extension($extension, $install_command, $prefix, $log_prefix)
{
try {
$exist = Test-Path -Path C:\tools\php\ext\php_$extension.dll
if(!(php -m | findstr -i ${extension}) -and $exist) {
Add-Content C:\tools\php\php.ini "$prefix=php_$extension.dll"
Write-Host "$log_prefix`: Enabled $extension" -ForegroundColor green
} elseif(php -m | findstr -i $extension) {
Write-Host "$log_prefix`: $extension was already enabled" -ForegroundColor yellow
}
} catch [Exception] {
Write-Host "$log_prefix`: $extension could not be enabled" -ForegroundColor red
}
$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
Write-Host "$log_prefix`: Installed and enabled $extension" -ForegroundColor green
} catch [Exception] {
Write-Host "$log_prefix`: Could not install $extension on PHP $version" -ForegroundColor red
}
}
} else {
if(!(php -m | findstr -i $extension)) {
Write-Host "$log_prefix`: Could not find $extension for PHP$version on PECL" -ForegroundColor red
}
}
}

View File

@ -167,7 +167,7 @@ export async function log(
};
switch (prefix) {
case '':
prefix = prefix;
prefix = '';
break;
default:
prefix = prefix + ': ';