Refactor scripts

This commit is contained in:
Shivam Mathur 2020-02-21 07:58:19 +05:30
parent 3781acab4b
commit 9dfebd1adb
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
2 changed files with 44 additions and 24 deletions

View File

@ -180,6 +180,15 @@ port_setup_php() {
add_pecl_old "$1" add_pecl_old "$1"
} }
setup_php() {
action=$1
step_log "Setup PHP"
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE >/dev/null 2>&1
brew tap shivammathur/homebrew-php >/dev/null 2>&1
brew "$action" shivammathur/php/php@"$version" >/dev/null 2>&1
brew link --force --overwrite php@"$version" >/dev/null 2>&1
}
# Variables # Variables
tick="✓" tick="✓"
cross="✗" cross="✗"
@ -190,7 +199,6 @@ existing_version=$(php-config --version | cut -c 1-3)
[[ -z "${update}" ]] && update='false' || update="${update}" [[ -z "${update}" ]] && update='false' || update="${update}"
# Setup PHP # Setup PHP
step_log "Setup PHP"
if [[ "$version" =~ $old_versions ]]; then if [[ "$version" =~ $old_versions ]]; then
export PATH="/opt/local/bin:/opt/local/sbin:$PATH" export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export TERM=xterm export TERM=xterm
@ -202,17 +210,14 @@ if [[ "$version" =~ $old_versions ]]; then
step_log "Setup PHP" step_log "Setup PHP"
port_setup_php $nodot_version >/dev/null 2>&1 port_setup_php $nodot_version >/dev/null 2>&1
status="Installed" status="Installed"
elif [ "$existing_version" != "$version" ] || [ "$update" = "true" ]; then elif [ "$existing_version" != "$version" ]; then
export HOMEBREW_NO_INSTALL_CLEANUP=TRUE >/dev/null 2>&1 setup_php "install"
brew tap shivammathur/homebrew-php >/dev/null 2>&1 status="Installed"
brew install shivammathur/php/php@"$version" >/dev/null 2>&1 elif [ "$existing_version" = "$version" ] && [ "$update" = "true" ]; then
brew link --force --overwrite php@"$version" >/dev/null 2>&1 setup_php "upgrade"
if [ "$update" = "true" ]; then status="Updated to"
status="Updated to"
else
status="Installed"
fi
else else
step_log "Setup PHP"
status="Found" status="Found"
fi fi
ini_file=$(php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g") ini_file=$(php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")

View File

@ -25,6 +25,7 @@ update_ppa() {
ppa="ondrej-ubuntu-php*.list" ppa="ondrej-ubuntu-php*.list"
fi fi
find /etc/apt/sources.list.d -type f -name "$ppa" -exec sudo "$debconf_fix" apt-fast update -o Dir::Etc::sourcelist="{}" ';' >/dev/null 2>&1 find /etc/apt/sources.list.d -type f -name "$ppa" -exec sudo "$debconf_fix" apt-fast update -o Dir::Etc::sourcelist="{}" ';' >/dev/null 2>&1
ppa_updated="true"
fi fi
} }
@ -59,7 +60,7 @@ add_extension() {
install_command=$2 install_command=$2
prefix=$3 prefix=$3
if [[ "$version" =~ $old_versions ]]; then if [[ "$version" =~ $old_versions ]]; then
install_command="update_ppa && ${install_command/5\.[4-5]-$extension/5-$extension=$release_version} && ppa_updated='true'" install_command="update_ppa && ${install_command/5\.[4-5]-$extension/5-$extension=$release_version}"
fi fi
if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then
# shellcheck disable=SC2046 # shellcheck disable=SC2046
@ -69,7 +70,7 @@ add_extension() {
add_log "$tick" "$extension" "Enabled" add_log "$tick" "$extension" "Enabled"
elif ! php -m | grep -i -q -w "$extension"; then elif ! php -m | grep -i -q -w "$extension"; then
(eval "$install_command" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled") || (eval "$install_command" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled") ||
(update_ppa && eval "$install_command" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled" && ppa_updated="true") || (update_ppa && eval "$install_command" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled") ||
(sudo pecl install -f "$extension" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled") || (sudo pecl install -f "$extension" >/dev/null 2>&1 && add_log "$tick" "$extension" "Installed and enabled") ||
add_log "$cross" "$extension" "Could not install $extension on PHP $semver" add_log "$cross" "$extension" "Could not install $extension on PHP $semver"
fi fi
@ -140,7 +141,6 @@ update_extension() {
version_exists=$(apt-cache policy -- *"$extension" | grep "$final_version") version_exists=$(apt-cache policy -- *"$extension" | grep "$final_version")
if [ -z "$version_exists" ]; then if [ -z "$version_exists" ]; then
update_ppa update_ppa
ppa_updated="true"
fi fi
$apt_install php"$version"-"$extension" $apt_install php"$version"-"$extension"
fi fi
@ -242,6 +242,28 @@ switch_version() {
done done
} }
# Function to get PHP version in semver format
php_semver() {
if [ ! "$version" = "8.0" ]; then
php"$version" -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-'
else
php -v | head -n 1 | cut -f 2 -d ' '
fi
}
# Function to update PHP
update_php() {
update_ppa
initial_version=$(php_semver)
$apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml >/dev/null 2>&1
updated_version=$(php_semver)
if [ "$updated_version" != "$initial_version" ]; then
status="Updated to"
else
status="Switched to"
fi
}
# Variables # Variables
tick="✓" tick="✓"
cross="✗" cross="✗"
@ -268,15 +290,13 @@ if [ "$existing_version" != "$version" ]; then
version_exists=$(apt-cache policy -- php"$version" | grep "$version") version_exists=$(apt-cache policy -- php"$version" | grep "$version")
if [ -z "$version_exists" ]; then if [ -z "$version_exists" ]; then
update_ppa update_ppa
ppa_updated="true"
fi fi
$apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml >/dev/null 2>&1 $apt_install php"$version" php"$version"-curl php"$version"-mbstring php"$version"-xml >/dev/null 2>&1
fi fi
status="Installed" status="Installed"
else else
if [ "$update" = "true" ]; then if [ "$update" = "true" ]; then
$apt_install php"$version" >/dev/null 2>&1 update_php
status="Updated to"
else else
status="Switched to" status="Switched to"
fi fi
@ -289,18 +309,13 @@ if [ "$existing_version" != "$version" ]; then
else else
if [ "$update" = "true" ]; then if [ "$update" = "true" ]; then
$apt_install php"$version" >/dev/null 2>&1 update_php
status="Updated to"
else else
status="Found" status="Found"
fi fi
fi fi
semver=$(php -v | head -n 1 | cut -f 2 -d ' ') semver=$(php_semver)
if [ ! "$version" = "8.0" ]; then
semver=$(echo "$semver" | cut -f 1 -d '-')
fi
ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g") ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g")
ext_dir=$(php -i | grep "extension_dir => /" | sed -e "s|.*=> s*||") ext_dir=$(php -i | grep "extension_dir => /" | sed -e "s|.*=> s*||")
scan_dir=$(php --ini | grep additional | sed -e "s|.*: s*||") scan_dir=$(php --ini | grep additional | sed -e "s|.*: s*||")