2023-12-26 14:16:29 +07:00
|
|
|
# Handle dependency extensions
|
|
|
|
handle_dependency_extensions() {
|
|
|
|
local formula=$1
|
|
|
|
local extension=$2
|
2022-09-02 02:10:31 +07:00
|
|
|
formula_file="${tap_dir:?}/$ext_tap/Formula/$extension@${version:?}.rb"
|
2023-12-26 14:16:29 +07:00
|
|
|
[ -e "$formula_file" ] || formula_file="$tap_dir/$ext_tap/Formula/$formula@$version.rb"
|
2021-09-19 19:41:39 +07:00
|
|
|
if [ -e "$formula_file" ]; then
|
|
|
|
IFS=" " read -r -a dependency_extensions <<< "$(grep -Eo "shivammathur.*@" "$formula_file" | xargs -I {} -n 1 basename '{}' | cut -d '@' -f 1 | tr '\n' ' ')"
|
|
|
|
for dependency_extension in "${dependency_extensions[@]}"; do
|
|
|
|
sudo sed -Ei '' "/=(.*\/)?\"?$dependency_extension(.so)?$/d" "${ini_file:?}"
|
|
|
|
done
|
|
|
|
fi
|
2023-12-26 14:16:29 +07:00
|
|
|
suffix="$(get_php_formula_suffix)"
|
|
|
|
if [[ -n "$suffix" ]]; then
|
|
|
|
brew_opts=(-sf)
|
|
|
|
patch_abstract_file >/dev/null 2>&1
|
|
|
|
for dependency_extension in "${dependency_extensions[@]}"; do
|
|
|
|
brew install "${brew_opts[@]}" "$ext_tap/$dependency_extension@$version" >/dev/null 2>&1 && copy_brew_extensions "$dependency_extension"
|
|
|
|
done
|
|
|
|
fi
|
2021-09-19 19:41:39 +07:00
|
|
|
}
|
|
|
|
|
2021-08-24 17:44:52 +07:00
|
|
|
# Helper function to disable an extension.
|
|
|
|
disable_extension_helper() {
|
2021-08-25 20:30:52 +07:00
|
|
|
local extension=$1
|
|
|
|
local disable_dependents=${2:-false}
|
2022-01-17 10:28:30 +07:00
|
|
|
get_extension_map
|
2021-08-25 20:30:52 +07:00
|
|
|
if [ "$disable_dependents" = "true" ]; then
|
|
|
|
disable_extension_dependents "$extension"
|
|
|
|
fi
|
2021-04-03 06:43:18 +07:00
|
|
|
sudo sed -Ei '' "/=(.*\/)?\"?$extension(.so)?$/d" "${ini_file:?}"
|
2022-03-06 13:08:28 +07:00
|
|
|
sudo rm -rf "$scan_dir"/*"$extension"* /tmp/php"$version"_extensions
|
2022-01-17 10:28:30 +07:00
|
|
|
mkdir -p /tmp/extdisabled/"$version"
|
|
|
|
echo '' | sudo tee /tmp/extdisabled/"$version"/"$extension" >/dev/null 2>&1
|
2020-01-30 13:33:30 +07:00
|
|
|
}
|
|
|
|
|
2022-03-15 17:15:44 +07:00
|
|
|
# Function to get extension name from brew formula.
|
|
|
|
get_extension_from_formula() {
|
|
|
|
local formula=$1
|
|
|
|
local extension
|
2022-11-16 12:10:40 +07:00
|
|
|
extension=$(grep -E "^$formula=" "$src"/configs/brew_extensions | cut -d '=' -f 2)
|
2022-03-15 17:15:44 +07:00
|
|
|
[[ -z "$extension" ]] && extension="$(echo "$formula" | sed -E "s/pecl_|[0-9]//g")"
|
|
|
|
echo "$extension"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Function to copy extension binaries to the extension directory.
|
|
|
|
copy_brew_extensions() {
|
|
|
|
local formula=$1
|
|
|
|
formula_file="$tap_dir/$ext_tap/Formula/$formula@$version.rb"
|
|
|
|
deps="$(grep -Eo 'depends_on "shivammathur[^"]+' "$formula_file" | cut -d '/' -f 3 | tr '\n' ' ')"
|
|
|
|
IFS=' ' read -r -a deps <<< "$formula@$version $deps"
|
|
|
|
for dependency in "${deps[@]}"; do
|
2022-09-02 02:10:31 +07:00
|
|
|
extension_file="${brew_prefix:?}/opt/$dependency/$(get_extension_from_formula "${dependency%@*}").so"
|
2022-03-15 17:15:44 +07:00
|
|
|
[ -e "$extension_file" ] && sudo cp "$extension_file" "$ext_dir"
|
|
|
|
done
|
2023-09-22 15:37:12 +07:00
|
|
|
if [ -d "$brew_prefix"/Cellar/"$formula"@"$version" ]; then
|
|
|
|
sudo find -- "$brew_prefix"/Cellar/"$formula"@"$version" -name "*.dylib" -exec cp {} "$ext_dir" \;
|
|
|
|
fi
|
2022-03-15 17:15:44 +07:00
|
|
|
}
|
|
|
|
|
2020-06-19 05:39:13 +07:00
|
|
|
# Function to install a php extension from shivammathur/extensions tap.
|
|
|
|
add_brew_extension() {
|
2020-12-24 19:50:42 +07:00
|
|
|
formula=$1
|
2020-11-08 14:36:21 +07:00
|
|
|
prefix=$2
|
2022-03-15 17:15:44 +07:00
|
|
|
extension="$(get_extension_from_formula "$formula")"
|
2020-11-08 14:36:21 +07:00
|
|
|
enable_extension "$extension" "$prefix"
|
|
|
|
if check_extension "$extension"; then
|
|
|
|
add_log "${tick:?}" "$extension" "Enabled"
|
|
|
|
else
|
2021-09-19 19:41:39 +07:00
|
|
|
add_brew_tap "$php_tap"
|
|
|
|
add_brew_tap "$ext_tap"
|
2022-09-02 02:10:31 +07:00
|
|
|
sudo mv "$tap_dir"/"$ext_tap"/.github/deps/"$formula"/* "${core_repo:?}/Formula/" 2>/dev/null || true
|
2021-05-15 19:57:18 +07:00
|
|
|
update_dependencies >/dev/null 2>&1
|
2023-12-26 14:16:29 +07:00
|
|
|
handle_dependency_extensions "$formula" "$extension" >/dev/null 2>&1
|
2023-12-25 04:39:27 +07:00
|
|
|
(brew install "${brew_opts[@]}" "$ext_tap/$formula@$version" >/dev/null 2>&1 && copy_brew_extensions "$formula") || pecl_install "$extension" >/dev/null 2>&1
|
2020-11-08 14:36:21 +07:00
|
|
|
add_extension_log "$extension" "Installed and enabled"
|
2020-06-19 05:39:13 +07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2023-12-26 03:49:32 +07:00
|
|
|
# Function to patch the abstract file in the extensions tap.
|
|
|
|
patch_abstract_file() {
|
|
|
|
abstract_path="$tap_dir"/"$ext_tap"/Abstract/abstract-php-extension.rb
|
2023-12-26 14:16:29 +07:00
|
|
|
if [[ -e "$abstract_path" && ! -e /tmp/abstract_patch ]]; then
|
|
|
|
echo '' | sudo tee /tmp/abstract_patch >/dev/null 2>&1
|
2023-12-26 03:49:32 +07:00
|
|
|
sudo sed -i '' -e "s|php@#{\(.*\)}|php@#{\1}$suffix|g" -e "s|php_version /|\"#{php_version}$suffix\" /|g" "$abstract_path"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-01-13 08:03:07 +07:00
|
|
|
# Helper function to add an extension.
|
|
|
|
add_extension_helper() {
|
2021-09-19 17:03:45 +07:00
|
|
|
local extension=$1
|
2020-11-08 14:36:21 +07:00
|
|
|
prefix=$2
|
2022-01-13 08:03:07 +07:00
|
|
|
if [[ "$version" =~ ${old_versions:?} ]] && [ "$extension" = "imagick" ]; then
|
|
|
|
run_script "php5-darwin" "${version/./}" "$extension" >/dev/null 2>&1
|
2020-02-23 11:14:33 +07:00
|
|
|
else
|
2022-01-13 08:03:07 +07:00
|
|
|
pecl_install "$extension" >/dev/null 2>&1 &&
|
|
|
|
if [[ "$version" =~ ${old_versions:?} ]]; then echo "$prefix=$ext_dir/$extension.so" >>"$ini_file"; fi
|
2020-10-23 22:04:56 +07:00
|
|
|
fi
|
2022-01-13 08:03:07 +07:00
|
|
|
add_extension_log "$extension" "Installed and enabled"
|
2019-12-27 08:26:49 +07:00
|
|
|
}
|
|
|
|
|
2020-09-28 12:28:02 +07:00
|
|
|
# Function to handle request to add phpize and php-config.
|
|
|
|
add_devtools() {
|
|
|
|
tool=$1
|
2020-11-08 14:36:21 +07:00
|
|
|
add_log "${tick:?}" "$tool" "Added $tool $semver"
|
2020-01-30 13:33:30 +07:00
|
|
|
}
|
|
|
|
|
2020-05-27 17:53:52 +07:00
|
|
|
# Function to handle request to add PECL.
|
2019-12-27 08:26:49 +07:00
|
|
|
add_pecl() {
|
2022-01-13 06:52:02 +07:00
|
|
|
enable_extension xml extension >/dev/null 2>&1
|
2021-04-03 06:43:18 +07:00
|
|
|
configure_pecl >/dev/null 2>&1
|
2021-09-19 04:47:13 +07:00
|
|
|
pear_version=$(get_tool_version "pecl" "version")
|
|
|
|
add_log "${tick:?}" "PECL" "Found PECL $pear_version"
|
2019-12-27 08:26:49 +07:00
|
|
|
}
|
|
|
|
|
2023-07-29 23:15:53 +07:00
|
|
|
# Link opcache extension to extensions directory.
|
|
|
|
link_opcache() {
|
|
|
|
opcache_ini="$brew_prefix"/etc/php/"$version"/conf.d/ext-opcache.ini
|
|
|
|
if [ -e "$opcache_ini" ]; then
|
|
|
|
opcache_ext=$(grep -Eo "zend_extension.*opcache.*\.so" "$opcache_ini" | cut -d '"' -f 2)
|
|
|
|
sudo ln -sf "$opcache_ext" "$ext_dir"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-05-27 22:15:47 +07:00
|
|
|
# Patch brew to overwrite packages.
|
2021-05-27 21:42:50 +07:00
|
|
|
patch_brew() {
|
2022-09-02 02:10:31 +07:00
|
|
|
formula_installer="${brew_repo:?}"/Library/Homebrew/formula_installer.rb
|
2022-04-09 20:44:52 +07:00
|
|
|
code=" keg.link\(verbose: verbose\?"
|
|
|
|
sudo sed -Ei '' "s/$code.*/$code, overwrite: true\)/" "$formula_installer"
|
2021-05-27 21:42:50 +07:00
|
|
|
# shellcheck disable=SC2064
|
2022-04-09 20:44:52 +07:00
|
|
|
trap "sudo sed -Ei '' 's/$code.*/$code, overwrite: overwrite?\)/' $formula_installer" exit
|
2021-05-27 21:42:50 +07:00
|
|
|
}
|
|
|
|
|
2020-10-23 08:41:23 +07:00
|
|
|
# Function to update dependencies.
|
|
|
|
update_dependencies() {
|
2022-04-09 23:16:00 +07:00
|
|
|
patch_brew
|
2021-10-16 00:02:24 +07:00
|
|
|
if ! [ -e /tmp/update_dependencies ]; then
|
2024-03-31 05:21:38 +07:00
|
|
|
for repo in "$brew_repo" "$core_repo"; do
|
|
|
|
git_retry -C "$repo" fetch origin master && git -C "$repo" reset --hard origin/master
|
|
|
|
done
|
2021-05-15 19:57:18 +07:00
|
|
|
echo '' | sudo tee /tmp/update_dependencies >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-05-05 00:45:13 +07:00
|
|
|
# Function to get PHP version if it is already installed using Homebrew.
|
|
|
|
get_brewed_php() {
|
2023-09-22 15:30:55 +07:00
|
|
|
cellar="$brew_prefix"/Cellar
|
|
|
|
php_cellar="$cellar"/php
|
|
|
|
if [ -d "$cellar" ] && ! [[ "$(find "$cellar" -maxdepth 1 -name "php@$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then
|
|
|
|
php_semver | cut -c 1-3
|
|
|
|
elif [ -d "$php_cellar" ] && ! [[ "$(find "$php_cellar" -maxdepth 1 -name "$version*" | wc -l 2>/dev/null)" -eq 0 ]]; then
|
2022-01-18 10:14:46 +07:00
|
|
|
php_semver | cut -c 1-3
|
2021-05-05 00:45:13 +07:00
|
|
|
else
|
|
|
|
echo 'false';
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-11-08 14:36:21 +07:00
|
|
|
# Function to setup PHP 5.6 and newer using Homebrew.
|
|
|
|
add_php() {
|
2020-02-21 09:28:19 +07:00
|
|
|
action=$1
|
2021-05-05 00:45:13 +07:00
|
|
|
existing_version=$2
|
2021-09-19 19:41:39 +07:00
|
|
|
add_brew_tap "$php_tap"
|
2020-10-23 08:41:23 +07:00
|
|
|
update_dependencies
|
2023-12-25 04:39:27 +07:00
|
|
|
suffix="$(get_php_formula_suffix)"
|
|
|
|
php_formula="shivammathur/php/php@$version$suffix"
|
|
|
|
if [[ "$existing_version" != "false" && -z "$suffix" ]]; then
|
2024-06-03 00:34:56 +07:00
|
|
|
([ "$action" = "upgrade" ] && brew upgrade -f --overwrite "$php_formula") || brew unlink "$php_formula"
|
2020-05-06 08:23:25 +07:00
|
|
|
else
|
2024-06-03 00:34:56 +07:00
|
|
|
brew install -f --overwrite "$php_formula"
|
2020-04-30 18:32:17 +07:00
|
|
|
fi
|
2024-04-24 05:08:15 +07:00
|
|
|
sudo chown -R "$(id -un)":"$(id -gn)" "$brew_prefix"
|
2021-05-05 00:45:13 +07:00
|
|
|
brew link --force --overwrite "$php_formula"
|
2020-02-21 09:28:19 +07:00
|
|
|
}
|
|
|
|
|
2023-12-25 04:39:27 +07:00
|
|
|
# Function to get formula suffix
|
|
|
|
get_php_formula_suffix() {
|
|
|
|
local suffix
|
|
|
|
[ "${debug:?}" = "debug" ] && suffix="-debug"
|
|
|
|
[ "${ts:?}" = "zts" ] && suffix="$suffix-zts"
|
|
|
|
echo "$suffix"
|
|
|
|
}
|
|
|
|
|
2021-07-12 18:30:40 +07:00
|
|
|
# Function to get extra version.
|
|
|
|
php_extra_version() {
|
2021-09-19 19:41:39 +07:00
|
|
|
php_formula_file="$tap_dir"/"$php_tap"/Formula/php@"$version".rb
|
|
|
|
if [ -e "$php_formula_file" ] && ! grep -q "deprecate!" "$php_formula_file" && grep -Eq "archive/[0-9a-zA-Z]+" "$php_formula_file"; then
|
2021-09-19 04:08:31 +07:00
|
|
|
echo " ($(grep -Eo "archive/[0-9a-zA-Z]+" "$php_formula_file" | cut -d'/' -f 2))"
|
2021-07-12 18:30:40 +07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-01-25 15:13:49 +07:00
|
|
|
# Function to set php.ini
|
|
|
|
add_php_config() {
|
|
|
|
if ! [ -e "$ini_dir"/php.ini-development ]; then
|
|
|
|
sudo cp "$ini_dir"/php.ini "$ini_dir"/php.ini-development
|
|
|
|
fi
|
|
|
|
if [[ "$ini" = "production" || "$ini" = "development" ]]; then
|
|
|
|
sudo cp "$ini_dir"/php.ini-"$ini" "$ini_dir"/php.ini
|
|
|
|
elif [ "$ini" = "none" ]; then
|
|
|
|
echo '' | sudo tee "${ini_file[@]}" >/dev/null 2>&1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:11:35 +07:00
|
|
|
# Function to get scan directory.
|
|
|
|
get_scan_dir() {
|
|
|
|
if [[ "$version" =~ ${old_versions:?} ]]; then
|
|
|
|
php --ini | grep additional | sed -e "s|.*: s*||"
|
|
|
|
else
|
|
|
|
echo "$ini_dir"/conf.d
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2021-05-27 22:15:47 +07:00
|
|
|
# Function to Setup PHP.
|
2020-11-08 14:36:21 +07:00
|
|
|
setup_php() {
|
|
|
|
step_log "Setup PHP"
|
2022-01-18 10:14:46 +07:00
|
|
|
php_config="$(command -v php-config 2>/dev/null)"
|
2021-05-05 00:45:13 +07:00
|
|
|
existing_version=$(get_brewed_php)
|
2020-11-08 14:36:21 +07:00
|
|
|
if [[ "$version" =~ ${old_versions:?} ]]; then
|
2020-11-11 03:53:45 +07:00
|
|
|
run_script "php5-darwin" "${version/./}" >/dev/null 2>&1
|
2020-11-08 14:36:21 +07:00
|
|
|
status="Installed"
|
|
|
|
elif [ "$existing_version" != "$version" ]; then
|
2021-05-05 00:45:13 +07:00
|
|
|
add_php "install" "$existing_version" >/dev/null 2>&1
|
2020-11-08 14:36:21 +07:00
|
|
|
status="Installed"
|
|
|
|
elif [ "$existing_version" = "$version" ] && [ "${update:?}" = "true" ]; then
|
2021-05-05 00:45:13 +07:00
|
|
|
add_php "upgrade" "$existing_version" >/dev/null 2>&1
|
2020-11-08 14:36:21 +07:00
|
|
|
status="Updated to"
|
|
|
|
else
|
2024-03-25 09:00:22 +07:00
|
|
|
add_php "upgrade" "$existing_version" >/dev/null 2>&1
|
|
|
|
status="Updated to"
|
2020-11-08 14:36:21 +07:00
|
|
|
fi
|
2022-01-16 13:33:31 +07:00
|
|
|
php_config="$(command -v php-config)"
|
2024-04-21 19:46:08 +07:00
|
|
|
ext_dir="$(sed -n "s/.*extension_dir=['\"]\(.*\)['\"].*/\1/p" "$php_config")"
|
2022-01-18 10:14:46 +07:00
|
|
|
ini_dir="$(php_ini_path)"
|
2022-01-31 21:11:35 +07:00
|
|
|
scan_dir="$(get_scan_dir)"
|
2022-01-16 13:33:31 +07:00
|
|
|
ini_file="$ini_dir"/php.ini
|
2020-11-08 14:36:21 +07:00
|
|
|
sudo mkdir -m 777 -p "$ext_dir" "$HOME/.composer"
|
2022-01-16 13:33:31 +07:00
|
|
|
sudo chmod 777 "$ini_file" "${tool_path_dir:?}"
|
2022-01-18 10:14:46 +07:00
|
|
|
semver="$(php_semver)"
|
|
|
|
extra_version="$(php_extra_version)"
|
2022-01-16 13:33:31 +07:00
|
|
|
configure_php
|
2023-07-29 23:15:53 +07:00
|
|
|
link_opcache
|
2022-01-30 11:30:11 +07:00
|
|
|
set_output "php-version" "$semver"
|
2020-12-23 15:29:22 +07:00
|
|
|
if [ "${semver%.*}" != "$version" ]; then
|
2021-08-24 17:44:52 +07:00
|
|
|
add_log "${cross:?}" "PHP" "Could not setup PHP $version"
|
2020-12-17 05:50:39 +07:00
|
|
|
exit 1
|
|
|
|
fi
|
2021-07-12 18:30:40 +07:00
|
|
|
|
2022-01-29 13:44:42 +07:00
|
|
|
sudo cp "$src"/configs/pm/*.json "$RUNNER_TOOL_CACHE/"
|
2021-07-12 18:30:40 +07:00
|
|
|
add_log "$tick" "PHP" "$status PHP $semver$extra_version"
|
2020-11-08 14:36:21 +07:00
|
|
|
}
|
|
|
|
|
2019-12-20 19:59:05 +07:00
|
|
|
# Variables
|
2023-11-23 18:42:56 +07:00
|
|
|
version=${1:-'8.3'}
|
2022-01-25 15:13:49 +07:00
|
|
|
ini=${2:-'production'}
|
2022-01-29 13:44:42 +07:00
|
|
|
src=${0%/*}/..
|
2021-05-15 19:57:18 +07:00
|
|
|
php_formula=shivammathur/php/php@"$version"
|
2022-01-29 13:44:42 +07:00
|
|
|
scripts="$src"/scripts
|
2021-09-19 19:41:39 +07:00
|
|
|
ext_tap=shivammathur/homebrew-extensions
|
|
|
|
php_tap=shivammathur/homebrew-php
|
2020-12-25 23:29:40 +07:00
|
|
|
export HOMEBREW_CHANGE_ARCH_TO_ARM=1
|
2021-08-15 11:59:12 +07:00
|
|
|
export HOMEBREW_DEVELOPER=1
|
2020-11-08 14:36:21 +07:00
|
|
|
export HOMEBREW_NO_AUTO_UPDATE=1
|
2022-01-16 14:13:52 +07:00
|
|
|
export HOMEBREW_NO_ENV_HINTS=1
|
|
|
|
export HOMEBREW_NO_INSTALL_CLEANUP=1
|
2021-02-28 20:50:28 +07:00
|
|
|
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1
|
2023-11-07 15:13:28 +07:00
|
|
|
export HOMEBREW_NO_INSTALL_FROM_API=1
|
2020-05-06 08:23:25 +07:00
|
|
|
|
2020-11-08 14:36:21 +07:00
|
|
|
# shellcheck source=.
|
2022-01-02 19:20:00 +07:00
|
|
|
. "${scripts:?}"/unix.sh
|
2022-09-02 02:10:31 +07:00
|
|
|
. "${scripts:?}"/tools/brew.sh
|
2024-03-18 10:32:23 +07:00
|
|
|
. "${scripts:?}"/tools/retry.sh
|
2021-02-20 11:45:25 +07:00
|
|
|
. "${scripts:?}"/tools/add_tools.sh
|
2022-01-02 18:51:14 +07:00
|
|
|
. "${scripts:?}"/extensions/source.sh
|
|
|
|
. "${scripts:?}"/extensions/add_extensions.sh
|
2022-09-02 02:10:31 +07:00
|
|
|
configure_brew
|
2020-05-27 17:53:52 +07:00
|
|
|
read_env
|
2020-11-08 14:36:21 +07:00
|
|
|
self_hosted_setup
|
|
|
|
setup_php
|