From 43cfa2b1a83db0b693694ca0984425dc399a86ca Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Thu, 30 Jan 2020 12:03:30 +0530 Subject: [PATCH] Refactor scripts --- __tests__/extensions.test.ts | 9 ++- dist/index.js | 31 ++++++---- src/extensions.ts | 31 ++++++---- src/scripts/darwin.sh | 74 ++++++++++++++++++------ src/scripts/linux.sh | 108 +++++++++++++++++++++++++---------- src/scripts/win32.ps1 | 8 +-- 6 files changed, 183 insertions(+), 78 deletions(-) diff --git a/__tests__/extensions.test.ts b/__tests__/extensions.test.ts index 7d5789b4..e83efbf7 100644 --- a/__tests__/extensions.test.ts +++ b/__tests__/extensions.test.ts @@ -27,7 +27,7 @@ describe('Extension tests', () => { it('checking addExtensionOnLinux', async () => { let linux: string = await extensions.addExtension( - 'xdebug, pcov, ast-beta', + 'xdebug, pcov, ast-beta, xdebug-alpha', '7.4', 'linux' ); @@ -35,7 +35,10 @@ describe('Extension tests', () => { expect(linux).toContain( 'sudo DEBIAN_FRONTEND=noninteractive apt-get install -y php7.4-pcov' ); - expect(linux).toContain('install_extension ast-beta'); + expect(linux).toContain('add_unstable_extension ast beta extension'); + expect(linux).toContain( + 'add_unstable_extension xdebug alpha zend_extension' + ); linux = await extensions.addExtension('gearman', '7.0', 'linux'); expect(linux).toContain('gearman.sh 7.0'); @@ -67,7 +70,7 @@ describe('Extension tests', () => { ); expect(darwin).toContain('sudo pecl install -f xdebug'); expect(darwin).toContain('sudo pecl install -f pcov'); - expect(darwin).toContain('install_extension ast-beta'); + expect(darwin).toContain('add_unstable_extension ast beta extension'); darwin = await extensions.addExtension('phalcon3', '7.0', 'darwin'); expect(darwin).toContain('phalcon_darwin.sh phalcon3 7.0'); diff --git a/dist/index.js b/dist/index.js index 506e4c9a..9da436c5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2642,12 +2642,20 @@ function addExtensionDarwin(extension_csv, version, pipe) { return __awaiter(this, void 0, void 0, function* () { extension = extension.toLowerCase(); const version_extension = version + extension; + const [extension_name, stability] = extension.split('-'); + const prefix = yield utils.getExtensionPrefix(extension_name); let install_command = ''; switch (true) { // match pre-release versions case /.*-(beta|alpha|devel|snapshot)/.test(version_extension): - install_command = 'install_extension ' + extension + pipe; - break; + script += + '\nadd_unstable_extension ' + + extension_name + + ' ' + + stability + + ' ' + + prefix; + return; case /5\.6xdebug/.test(version_extension): install_command = 'sudo pecl install -f xdebug-2.5.5' + pipe; break; @@ -2748,12 +2756,20 @@ function addExtensionLinux(extension_csv, version, pipe) { return __awaiter(this, void 0, void 0, function* () { extension = extension.toLowerCase(); const version_extension = version + extension; + const [extension_name, stability] = extension.split('-'); + const prefix = yield utils.getExtensionPrefix(extension_name); let install_command = ''; switch (true) { // match pre-release versions case /.*-(beta|alpha|devel|snapshot)/.test(version_extension): - install_command = 'install_extension ' + extension + pipe; - break; + script += + '\nadd_unstable_extension ' + + extension_name + + ' ' + + stability + + ' ' + + prefix; + return; // match 5.6gearman..7.4gearman case /^((5\.6)|(7\.[0-4]))gearman$/.test(version_extension): install_command = @@ -2791,12 +2807,7 @@ function addExtensionLinux(extension_csv, version, pipe) { break; } script += - '\nadd_extension ' + - extension + - ' "' + - install_command + - '" ' + - (yield utils.getExtensionPrefix(extension)); + '\nadd_extension ' + extension + ' "' + install_command + '" ' + prefix; }); }); return script; diff --git a/src/extensions.ts b/src/extensions.ts index 7e52d953..86cb4144 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -18,12 +18,20 @@ export async function addExtensionDarwin( await utils.asyncForEach(extensions, async function(extension: string) { extension = extension.toLowerCase(); const version_extension: string = version + extension; + const [extension_name, stability]: string[] = extension.split('-'); + const prefix = await utils.getExtensionPrefix(extension_name); let install_command = ''; switch (true) { // match pre-release versions case /.*-(beta|alpha|devel|snapshot)/.test(version_extension): - install_command = 'install_extension ' + extension + pipe; - break; + script += + '\nadd_unstable_extension ' + + extension_name + + ' ' + + stability + + ' ' + + prefix; + return; case /5\.6xdebug/.test(version_extension): install_command = 'sudo pecl install -f xdebug-2.5.5' + pipe; break; @@ -124,12 +132,20 @@ export async function addExtensionLinux( await utils.asyncForEach(extensions, async function(extension: string) { extension = extension.toLowerCase(); const version_extension: string = version + extension; + const [extension_name, stability]: string[] = extension.split('-'); + const prefix = await utils.getExtensionPrefix(extension_name); let install_command = ''; switch (true) { // match pre-release versions case /.*-(beta|alpha|devel|snapshot)/.test(version_extension): - install_command = 'install_extension ' + extension + pipe; - break; + script += + '\nadd_unstable_extension ' + + extension_name + + ' ' + + stability + + ' ' + + prefix; + return; // match 5.6gearman..7.4gearman case /^((5\.6)|(7\.[0-4]))gearman$/.test(version_extension): install_command = @@ -167,12 +183,7 @@ export async function addExtensionLinux( break; } script += - '\nadd_extension ' + - extension + - ' "' + - install_command + - '" ' + - (await utils.getExtensionPrefix(extension)); + '\nadd_extension ' + extension + ' "' + install_command + '" ' + prefix; }); return script; } diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 7a060163..de04fd30 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -16,6 +16,13 @@ add_log() { fi } +# Function to remove extensions +remove_extension() { + extension=$1 + sudo sed -i '' "/$extension/d" "$ini_file" + sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1 +} + # Function to setup extensions add_extension() { extension=$1 @@ -26,26 +33,49 @@ add_extension() { elif php -m | grep -i -q -w "$extension"; then add_log "$tick" "$extension" "Enabled" elif ! php -m | grep -i -q -w "$extension"; then - (eval "$install_command" && add_log "$tick" "$extension" "Installed and enabled") || + (eval "$install_command" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled") || add_log "$cross" "$extension" "Could not install $extension on PHP $semver" fi } -# Function to force install extensions using PECL -install_extension() { +# Fuction to get the PECL version +get_pecl_version() { extension=$1 - extension_name="$(echo "$extension" | cut -d'-' -f 1)" - sudo sed -i "/$extension_name/d" "$ini_file" - sudo rm -rf /etc/php/"$version"/cli/conf.d/*"$extension_name"* >/dev/null 2>&1 - sudo rm -rf "$ext_dir"/"$extension_name".so >/dev/null 2>&1 - sudo pecl install -f "$extension" >/dev/null 2>&1 + stability=$2 + pecl_rest='https://pecl.php.net/rest/r/' + response=$(curl -q -sSL "$pecl_rest$extension"/allreleases.xml) + pecl_version=$(echo "$response" | grep -m 1 -Eo "(\d*\.\d*\.\d*$stability\d*)") + if [ ! "$pecl_version" ]; then + pecl_version=$(echo "$response" | grep -m 1 -Eo "(\d*\.\d*\.\d*)") + fi + echo "$pecl_version" } -# Function to remove extensions -remove_extension() { +# Function to pre-release extensions using PECL +add_unstable_extension() { extension=$1 - sudo sed -i '' "/$1/d" "$ini_file" - sudo rm -rf "$ext_dir"/"$1".so >/dev/null 2>&1 + stability=$2 + prefix=$3 + pecl_version=$(get_pecl_version "$extension" "$stability") + if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then + extension_version=$(php -d="$prefix=$extension" -r "echo phpversion('$extension');") + if [ "$extension_version" = "$pecl_version" ]; then + echo "$prefix=$extension" >>"$ini_file" && add_log "$tick" "$extension" "Enabled" + else + remove_extension "$extension" + add_extension "$extension" "sudo pecl install -f $extension-$pecl_version" "$prefix" + fi + elif php -m | grep -i -q -w "$extension"; then + extension_version=$(php -r "echo phpversion('$extension');") + if [ "$extension_version" = "$pecl_version" ]; then + add_log "$tick" "$extension" "Enabled" + else + remove_extension "$extension" + add_extension "$extension" "sudo pecl install -f $extension-$pecl_version" "$prefix" + fi + else + add_extension "$extension" "sudo pecl install -f $extension-$pecl_version" "$prefix" + fi } # Function to setup a remote tool @@ -75,22 +105,29 @@ add_tool() { fi } +# Function to add a tool using composer add_composer_tool() { tool=$1 release=$2 prefix=$3 ( - composer global require "$prefix$release" >/dev/null 2>&1 && \ - sudo ln -sf "$(composer -q global config home)"/vendor/bin/"$tool" /usr/local/bin/"$tool" && \ + composer global require "$prefix$release" >/dev/null 2>&1 && + sudo ln -sf "$(composer -q global config home)"/vendor/bin/"$tool" /usr/local/bin/"$tool" && add_log "$tick" "$tool" "Added" ) || add_log "$cross" "$tool" "Could not setup $tool" } +# Function to configure PECL +configure_pecl() { + for tool in pear pecl; do + sudo "$tool" config-set php_ini "$ini_file" >/dev/null 2>&1 + sudo "$tool" config-set auto_discover 1 >/dev/null 2>&1 + sudo "$tool" channel-update "$tool".php.net >/dev/null 2>&1 + done +} + +# Function to log PECL, it is installed along with PHP add_pecl() { - sudo pear config-set php_ini "$ini_file" >/dev/null 2>&1 - sudo pear config-set auto_discover 1 >/dev/null 2>&1 - sudo pear channel-update pear.php.net >/dev/null 2>&1 - sudo pecl channel-update pecl.php.net >/dev/null 2>&1 add_log "$tick" "PECL" "Added" } @@ -117,3 +154,4 @@ sudo chmod 777 "$ini_file" mkdir -p "$(pecl config-get ext_dir)" semver=$(php -v | head -n 1 | cut -f 2 -d ' ') add_log "$tick" "PHP" "Installed PHP $semver" +configure_pecl diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 767ce854..850bd6e9 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -27,14 +27,27 @@ update_ppa() { configure_pecl() { if [ "$pecl_config" = "false" ] && [ -e /usr/bin/pecl ]; then for tool in pear pecl; do - sudo $tool config-set php_ini "$ini_file" >/dev/null 2>&1 - sudo $tool config-set auto_discover 1 >/dev/null 2>&1 - sudo $tool channel-update $tool.php.net >/dev/null 2>&1 + sudo "$tool" config-set php_ini "$ini_file" >/dev/null 2>&1 + sudo "$tool" config-set auto_discover 1 >/dev/null 2>&1 + sudo "$tool" channel-update "$tool".php.net >/dev/null 2>&1 done pecl_config="true" fi } +# Fuction to get the PECL version +get_pecl_version() { + extension=$1 + stability=$2 + pecl_rest='https://pecl.php.net/rest/r/' + response=$(curl -q -sSL "$pecl_rest$extension"/allreleases.xml) + pecl_version=$(echo "$response" | grep -m 1 -Po "(\d*\.\d*\.\d*$stability\d*)") + if [ ! "$pecl_version" ]; then + pecl_version=$(echo "$response" | grep -m 1 -Po "(\d*\.\d*\.\d*)") + fi + echo "$pecl_version" +} + # Function to setup extensions add_extension() { extension=$1 @@ -53,25 +66,57 @@ add_extension() { sudo chmod 777 "$ini_file" } -# Function to force install extensions using PECL -install_extension() { +# Function to delete extensions +delete_extension() { extension=$1 - extension_name="$(echo "$extension" | cut -d'-' -f 1)" - sudo sed -i "/$extension_name/d" "$ini_file" - sudo rm -rf /etc/php/"$version"/cli/conf.d/*"$extension_name"* >/dev/null 2>&1 - sudo rm -rf "$ext_dir"/"$extension_name".so >/dev/null 2>&1 - sudo pecl install -f "$extension" >/dev/null 2>&1 + sudo sed -i "/$extension/d" "$ini_file" + sudo rm -rf /etc/php/"$version"/cli/conf.d/*"$extension"* >/dev/null 2>&1 + sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1 } -# Function to remove extensions +# Function to disable and delete extensions remove_extension() { extension=$1 if [ -e /etc/php/"$version"/mods-available/"$extension".ini ]; then sudo phpdismod -v "$version" "$extension" fi - sudo sed -i "/$extension/d" "$ini_file" - sudo rm -rf /etc/php/"$version"/cli/conf.d/*"$extension"* >/dev/null 2>&1 - sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1 + delete_extension "$extension" +} + +# Function to install a PECL version +add_pecl_extension() { + extension=$1 + pecl_version=$2 + (sudo pecl install -f "$extension-$pecl_version" >/dev/null 2>&1 && + add_log "$tick" "$extension" "Installed and enabled") || + add_log "$cross" "$extension" "Could not install $extension-$pecl_version on PHP $semver" +} + +# Function to pre-release extensions using PECL +add_unstable_extension() { + extension=$1 + stability=$2 + prefix=$3 + pecl_version=$(get_pecl_version "$extension" "$stability") + if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then + extension_version=$(php -d="$prefix=$extension" -r "echo phpversion('$extension');") + if [ "$extension_version" = "$pecl_version" ]; then + echo "$prefix=$extension" >>"$ini_file" && add_log "$tick" "$extension" "Enabled" + else + delete_extension "$extension" + add_pecl_extension "$extension" "$pecl_version" + fi + elif php -m | grep -i -q -w "$extension"; then + extension_version=$(php -r "echo phpversion('$extension');") + if [ "$extension_version" = "$pecl_version" ]; then + add_log "$tick" "$extension" "Enabled" + else + delete_extension "$extension" + add_pecl_extension "$extension" "$pecl_version" + fi + else + add_pecl_extension "$extension" "$pecl_version" + fi } # Function to update extension @@ -99,28 +144,28 @@ add_tool() { status_code=$(sudo curl -s -w "%{http_code}" -o /usr/local/bin/"$tool" -L "$url") if [ "$status_code" = "200" ]; then sudo chmod a+x /usr/local/bin/"$tool" + if [ "$tool" = "composer" ]; then + composer -q global config process-timeout 0 + elif [ "$tool" = "phive" ]; then + add_extension curl >/dev/null 2>&1 + add_extension mbstring >/dev/null 2>&1 + add_extension xml >/dev/null 2>&1 + fi add_log "$tick" "$tool" "Added" else add_log "$cross" "$tool" "Could not setup $tool" fi - if [ "$tool" = "composer" ]; then - composer -q global config process-timeout 0 - fi - if [ "$tool" = "phive" ]; then - add_extension curl >/dev/null 2>&1 - add_extension mbstring >/dev/null 2>&1 - add_extension xml >/dev/null 2>&1 - fi } +# Function to setup a tool using composer add_composer_tool() { tool=$1 release=$2 prefix=$3 ( - composer global require "$prefix$release" >/dev/null 2>&1 && \ - sudo ln -sf "$(composer -q global config home)"/vendor/bin/"$tool" /usr/local/bin/"$tool" && \ - add_log "$tick" "$tool" "Added" + composer global require "$prefix$release" >/dev/null 2>&1 && + sudo ln -sf "$(composer -q global config home)"/vendor/bin/"$tool" /usr/local/bin/"$tool" && + add_log "$tick" "$tool" "Added" ) || add_log "$cross" "$tool" "Could not setup $tool" } @@ -150,7 +195,9 @@ setup_master() { # Function to setup PECL add_pecl() { add_devtools - $apt_install php-pear >/dev/null 2>&1 + if [ ! -e /usr/bin/pecl ]; then + $apt_install php-pear >/dev/null 2>&1 + fi configure_pecl add_log "$tick" "PECL" "Added" } @@ -172,7 +219,6 @@ pecl_config="false" version=$1 apt_install="sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y" existing_version=$(php-config --version | cut -c 1-3) -semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') # Setup PHP step_log "Setup PHP" @@ -182,9 +228,7 @@ sudo mkdir -p /run/php if [ "$existing_version" != "$version" ]; then if [ ! -e "/usr/bin/php$version" ]; then update_ppa - if [ "$version" = "7.4" ]; then - $apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml php"$version"-phpdbg >/dev/null 2>&1 - elif [ "$version" = "8.0" ]; then + if [ "$version" = "8.0" ]; then setup_master else $apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml >/dev/null 2>&1 @@ -196,9 +240,10 @@ if [ "$existing_version" != "$version" ]; then switch_version - semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') if [ "$version" = "8.0" ]; then semver=$(php -v | head -n 1 | cut -f 2 -d ' ') + else + semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') fi if [ "$status" != "switched" ]; then @@ -207,6 +252,7 @@ if [ "$existing_version" != "$version" ]; then status="Switched to PHP $semver" fi else + semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') status="PHP $semver Found" fi diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 56bc0f3f..ab31bd77 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -35,7 +35,7 @@ Function Add-Extension { ) try { $extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension } - if ($null -ne $extension_info -and $mininum_stability -eq 'stable') { + if ($null -ne $extension_info) { switch ($extension_info.State) { 'Builtin' { Add-Log $tick $extension "Enabled" @@ -51,11 +51,7 @@ Function Add-Extension { } else { Install-PhpExtension -Extension $extension -MinimumStability $mininum_stability -Path $php_dir - if($mininum_stability -ne 'stable') { - Add-Log $tick "$extension-$mininum_stability" "Installed and enabled" - } else { - Add-Log $tick $extension "Installed and enabled" - } + Add-Log $tick $extension "Installed and enabled" } } catch {