From 2ee6797b634f0f2d9ebe0cea704bab1ee3632afa Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Tue, 18 Jan 2022 08:44:46 +0530 Subject: [PATCH] Optimize Linux and macOS setup --- src/scripts/darwin.sh | 15 +++++++++------ src/scripts/linux.sh | 11 ++++++----- src/scripts/unix.sh | 5 +++++ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 735f4f45..67f93253 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -167,7 +167,7 @@ fix_dependencies() { get_brewed_php() { php_cellar="$brew_prefix"/Cellar/php if [ -d "$php_cellar" ] && ! [[ "$(find "$php_cellar" -maxdepth 1 -name "$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then - php-config --version 2>/dev/null | cut -c 1-3 + php_semver | cut -c 1-3 else echo 'false'; fi @@ -198,6 +198,7 @@ php_extra_version() { # Function to Setup PHP. setup_php() { step_log "Setup PHP" + php_config="$(command -v php-config 2>/dev/null)" existing_version=$(get_brewed_php) if [[ "$version" =~ ${old_versions:?} ]]; then run_script "php5-darwin" "${version/./}" >/dev/null 2>&1 @@ -214,13 +215,13 @@ setup_php() { fi php_config="$(command -v php-config)" ext_dir="$(grep 'extension_dir=' "$php_config" | cut -d "'" -f 2)" - ini_dir="$(php --ini | grep '(php.ini)' | sed -e "s|.*: s*||")" + ini_dir="$(php_ini_path)" scan_dir="$ini_dir"/conf.d ini_file="$ini_dir"/php.ini sudo mkdir -m 777 -p "$ext_dir" "$HOME/.composer" sudo chmod 777 "$ini_file" "${tool_path_dir:?}" - semver=$(php_semver) - extra_version=$(php_extra_version) + semver="$(php_semver)" + extra_version="$(php_extra_version)" configure_php if [ "${semver%.*}" != "$version" ]; then add_log "${cross:?}" "PHP" "Could not setup PHP $version" @@ -236,8 +237,10 @@ setup_php() { version=$1 dist=$2 php_formula=shivammathur/php/php@"$version" -brew_prefix="$(brew --prefix)" -brew_repo="$(brew --repository)" +brew_path="$(command -v brew)" +brew_path_dir="$(dirname "$brew_path")" +brew_prefix="$brew_path_dir"/.. +brew_repo="$brew_path_dir/$(dirname "$(readlink "$brew_path")")"/.. tap_dir="$brew_repo"/Library/Taps core_repo="$tap_dir"/homebrew/homebrew-core scripts="${dist}"/../src/scripts diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index bc2f5766..1491bfbf 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -182,7 +182,8 @@ php_extra_version() { setup_php() { step_log "Setup PHP" sudo mkdir -m 777 -p /var/run /run/php - if [ "$(php-config --version 2>/dev/null | cut -c 1-3)" != "$version" ]; then + php_config="$(command -v php-config)" + if [[ -z "$php_config" ]] || [ "$(php_semver | cut -c 1-3)" != "$version" ]; then if [ ! -e "/usr/bin/php$version" ]; then add_php >/dev/null 2>&1 else @@ -195,6 +196,7 @@ setup_php() { status="Switched to" fi fi + php_config="$(command -v php-config)" else if [ "$update" = "true" ]; then update_php >/dev/null 2>&1 @@ -206,13 +208,12 @@ setup_php() { add_log "${cross:?}" "PHP" "Could not setup PHP $version" exit 1 fi - php_config="$(command -v php-config)" ext_dir="/usr/$(grep -Po "extension_dir=..[^/]*/\K[^'\"]*" "$php_config")" - ini_dir=$(php --ini | grep "(php.ini)" | sed -e "s|.*: s*||") + ini_dir="$(php_ini_path)" scan_dir="$ini_dir"/conf.d pecl_file="$scan_dir"/99-pecl.ini - semver=$(php_semver) - extra_version=$(php_extra_version) + semver="$(php_semver)" + extra_version="$(php_extra_version)" export ext_dir mapfile -t ini_file < <(sudo find "$ini_dir/.." -name "php.ini" -exec readlink -m {} +) link_pecl_file diff --git a/src/scripts/unix.sh b/src/scripts/unix.sh index bdf596f7..c864b583 100644 --- a/src/scripts/unix.sh +++ b/src/scripts/unix.sh @@ -147,6 +147,11 @@ php_semver() { grep -Eo 'version="[0-9]+(\.[0-9]+){2}((-?[a-zA-Z]+([0-9]+)?)?){2}' "${php_config:?}" | cut -d '"' -f 2 } +# Function to get ini_path. +php_ini_path() { + cut -d '"' -f 2 < <(grep "ini_path=" "$php_config" || php --ini | grep '(php.ini)' | sed -e "s|.*: s*||") +} + # Function to get the tag for a php version. php_src_tag() { commit=$(php_extra_version | grep -Eo "[0-9a-zA-Z]+")