2020-05-27 17:53:52 +07:00
# Function to setup environment for self-hosted runners.
2020-11-08 14:36:21 +07:00
self_hosted_helper( ) {
if ! command -v brew >/dev/null; then
2020-07-26 17:18:19 +07:00
step_log "Setup Brew"
2020-11-11 03:53:45 +07:00
get -q -e "/tmp/install.sh" "https://raw.githubusercontent.com/Homebrew/install/master/install.sh" && /tmp/install.sh >/dev/null 2>& 1
2020-11-08 14:36:21 +07:00
add_log " ${ tick : ? } " "Brew" "Installed Homebrew"
2020-05-27 17:53:52 +07:00
fi
}
2021-05-27 22:15:47 +07:00
# Function to delete extension.
2021-04-03 06:43:18 +07:00
delete_extension( ) {
extension = $1
sudo rm -rf " ${ scan_dir : ? } " /*" $extension " * " ${ ext_dir : ? } " /" $extension " .so >/dev/null 2>& 1
}
2021-05-27 22:15:47 +07:00
# Function to disable extension.
2021-04-03 06:43:18 +07:00
disable_extension( ) {
extension = $1
sudo sed -Ei '' " /=(.*\/)?\"? $extension (.so)? $/d " " ${ ini_file : ? } "
}
2020-05-27 17:53:52 +07:00
# Function to remove extensions.
2020-01-30 13:33:30 +07:00
remove_extension( ) {
extension = $1
2020-05-07 04:57:00 +07:00
if check_extension " $extension " ; then
2021-04-03 06:43:18 +07:00
disable_extension " $extension "
delete_extension " $extension "
2020-11-08 14:36:21 +07:00
( ! check_extension " $extension " && add_log " ${ tick : ? } " " : $extension " "Removed" ) ||
add_log " ${ cross : ? } " " : $extension " " Could not remove $extension on PHP ${ semver : ? } "
2020-05-07 04:57:00 +07:00
else
2020-11-08 14:36:21 +07:00
add_log " ${ tick : ? } " " : $extension " " Could not find $extension on PHP $semver "
2020-05-07 04:57:00 +07:00
fi
2020-01-30 13:33:30 +07:00
}
2021-05-27 22:15:47 +07:00
# Function to fetch a brew tap.
2020-12-23 15:29:22 +07:00
fetch_brew_tap( ) {
tap = $1
tap_user = $( dirname " $tap " )
tap_name = $( basename " $tap " )
mkdir -p " $tap_dir / $tap_user "
get -s -n "" " https://github.com/ $tap /archive/master.tar.gz " | sudo tar -xzf - -C " $tap_dir / $tap_user "
if [ -d " $tap_dir / $tap_user / $tap_name -master " ] ; then
sudo mv " $tap_dir / $tap_user / $tap_name -master " " $tap_dir / $tap_user / $tap_name "
fi
}
2020-11-26 00:16:59 +07:00
# Function to add a brew tap.
add_brew_tap( ) {
tap = $1
if ! [ -d " $tap_dir / $tap " ] ; then
2020-12-23 15:29:22 +07:00
if [ " ${ runner : ? } " = "self-hosted" ] ; then
2020-12-14 18:12:15 +07:00
brew tap --shallow " $tap " >/dev/null 2>& 1
2020-12-23 15:29:22 +07:00
else
fetch_brew_tap " $tap " >/dev/null 2>& 1
if ! [ -d " $tap_dir / $tap " ] ; then
brew tap --shallow " $tap " >/dev/null 2>& 1
fi
2020-12-14 18:12:15 +07:00
fi
2020-11-26 00:16:59 +07:00
fi
}
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
2021-02-10 06:42:37 +07:00
extension = " $( echo " $formula " | sed -E "s/pecl_|[0-9]//g" ) "
2020-11-08 14:36:21 +07:00
enable_extension " $extension " " $prefix "
if check_extension " $extension " ; then
add_log " ${ tick : ? } " " $extension " "Enabled"
else
2020-12-23 15:29:22 +07:00
add_brew_tap shivammathur/homebrew-php
2020-11-26 00:16:59 +07:00
add_brew_tap shivammathur/homebrew-extensions
2021-02-10 06:42:37 +07:00
sudo mv " $tap_dir " /shivammathur/homebrew-extensions/.github/deps/" $formula " /* " $tap_dir /homebrew/homebrew-core/Formula/ " 2>/dev/null || true
2021-05-15 19:57:18 +07:00
update_dependencies >/dev/null 2>& 1
2021-03-08 20:03:43 +07:00
brew install -f " $formula @ $version " >/dev/null 2>& 1
2020-12-24 19:50:42 +07:00
sudo cp " $brew_prefix /opt/ $formula @ $version / $extension .so " " $ext_dir "
2020-11-08 14:36:21 +07:00
add_extension_log " $extension " "Installed and enabled"
2020-06-19 05:39:13 +07:00
fi
}
2021-05-27 22:15:47 +07:00
# Function to setup extensions.
2020-02-23 20:30:40 +07:00
add_extension( ) {
extension = $1
2020-11-08 14:36:21 +07:00
prefix = $2
enable_extension " $extension " " $prefix "
if check_extension " $extension " ; then
add_log " ${ tick : ? } " " $extension " "Enabled"
2020-02-23 11:14:33 +07:00
else
2021-03-08 20:03:43 +07:00
[ [ " $version " = ~ 5.[ 4-5] ] ] && [ " $extension " = "imagick" ] && brew install -f pkg-config imagemagick >/dev/null 2>& 1
2020-11-08 14:36:21 +07:00
pecl_install " $extension " >/dev/null 2>& 1 &&
if [ [ " $version " = ~ ${ old_versions : ? } ] ] ; then echo " $prefix = $ext_dir / $extension .so " >>" $ini_file " ; fi
add_extension_log " $extension " "Installed and enabled"
2020-10-23 22:04:56 +07:00
fi
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( ) {
2021-04-03 06:43:18 +07:00
configure_pecl >/dev/null 2>& 1
2020-09-28 12:28:02 +07:00
pecl_version = $( get_tool_version "pecl" "version" )
2020-11-08 14:36:21 +07:00
add_log " ${ tick : ? } " "PECL" " Found PECL $pecl_version "
2019-12-27 08:26:49 +07:00
}
2021-05-27 22:15:47 +07:00
# Function to link all libraries of a formula.
2021-02-28 20:50:28 +07:00
link_libraries( ) {
formula = $1
formula_prefix = " $( brew --prefix " $formula " ) "
sudo mkdir -p " $formula_prefix " /lib
2021-03-08 20:43:00 +07:00
for lib in " $formula_prefix " /lib/*.dylib; do
lib_name = $( basename " $lib " )
2021-05-27 21:42:50 +07:00
sudo cp -a " $lib " " $brew_prefix /lib/ $lib_name " 2>/dev/null || true
2021-03-08 20:43:00 +07:00
done
2021-02-28 20:50:28 +07:00
}
2021-05-27 22:15:47 +07:00
# Patch brew to overwrite packages.
2021-05-27 21:42:50 +07:00
patch_brew( ) {
2021-05-27 23:30:47 +07:00
formula_installer = " $brew_repo " /Library/Homebrew/formula_installer.rb
code = " keg.link(verbose: verbose?"
sudo sed -i '' " s/ $code )/ $code , overwrite: true)/ " " $formula_installer "
2021-05-27 21:42:50 +07:00
# shellcheck disable=SC2064
2021-05-27 23:30:47 +07:00
trap " sudo sed -i '' 's/ $code , overwrite: true)/ $code )/' $formula_installer " exit
2021-05-27 21:42:50 +07:00
}
2021-05-27 22:15:47 +07:00
# Helper function to update the dependencies.
2021-02-28 20:50:28 +07:00
update_dependencies_helper( ) {
2021-05-15 19:57:18 +07:00
dependency = $1
get -q -n " $tap_dir /homebrew/homebrew-core/Formula/ $dependency .rb " " https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/ $dependency .rb "
link_libraries " $dependency "
2021-02-28 20:50:28 +07:00
}
2020-10-23 08:41:23 +07:00
# Function to update dependencies.
update_dependencies( ) {
2021-05-15 19:57:18 +07:00
if ! [ -e /tmp/update_dependencies ] && [ " ${ runner : ? } " != "self-hosted" ] && [ " ${ ImageOS :- } " != "" ] && [ " ${ ImageVersion :- } " != "" ] ; then
2021-05-27 21:42:50 +07:00
patch_brew
2021-05-15 19:57:18 +07:00
while read -r dependency; do
update_dependencies_helper " $dependency " &
2020-11-08 14:36:21 +07:00
to_wait += ( $! )
done <" $tap_dir /shivammathur/homebrew-php/.github/deps/ ${ ImageOS : ? } _ ${ ImageVersion : ? } "
2020-10-23 08:41:23 +07:00
wait " ${ to_wait [@] } "
2021-05-15 19:57:18 +07:00
echo '' | sudo tee /tmp/update_dependencies >/dev/null 2>& 1
fi
}
2021-05-27 22:15:47 +07:00
# Function to fix dependencies on install PHP version.
2021-05-15 19:57:18 +07:00
fix_dependencies( ) {
broken_deps_paths = $( php -v 2>& 1 | grep -Eo '/opt/[a-zA-Z0-9@\.]+' )
if [ " x $broken_deps_paths " != "x" ] ; then
update_dependencies
IFS = " " read -r -a formulae <<< " $( echo " $broken_deps_paths " | tr '\n' ' ' | sed 's|/opt/||g' 2>& 1) $php_formula "
brew reinstall " ${ formulae [@] } "
brew link --force --overwrite " $php_formula " || true
2020-10-23 08:41:23 +07:00
fi
}
2021-05-05 00:45:13 +07:00
# Function to get PHP version if it is already installed using Homebrew.
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
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
2020-11-26 00:16:59 +07:00
add_brew_tap shivammathur/homebrew-php
2020-10-23 08:41:23 +07:00
update_dependencies
2021-05-05 00:45:13 +07:00
if [ " $existing_version " != "false" ] ; then
( [ " $action " = "upgrade" ] && brew upgrade -f " $php_formula " ) || brew unlink " $php_formula "
2020-05-06 08:23:25 +07:00
else
2021-05-05 00:45:13 +07:00
brew install -f " $php_formula "
2020-04-30 18:32:17 +07:00
fi
2021-05-05 00:45:13 +07:00
brew link --force --overwrite " $php_formula "
2020-02-21 09:28:19 +07:00
}
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"
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
status = "Found"
2021-05-15 19:57:18 +07:00
fix_dependencies >/dev/null 2>& 1
2020-11-08 14:36:21 +07:00
fi
ini_file = $( php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g" )
sudo chmod 777 " $ini_file " " ${ tool_path_dir : ? } "
2020-12-17 22:19:20 +07:00
configure_php
2020-11-08 14:36:21 +07:00
ext_dir = $( php -i | grep -Ei "extension_dir => /" | sed -e "s|.*=> s*||" )
scan_dir = $( php --ini | grep additional | sed -e "s|.*: s*||" )
sudo mkdir -m 777 -p " $ext_dir " " $HOME /.composer "
semver = $( php -v | head -n 1 | cut -f 2 -d ' ' )
2020-12-23 15:29:22 +07:00
if [ " ${ semver %.* } " != " $version " ] ; then
2020-12-17 05:50:39 +07:00
add_log " $cross " "PHP" " Could not setup PHP $version "
exit 1
fi
2020-11-08 14:36:21 +07:00
sudo cp " $dist " /../src/configs/*.json " $RUNNER_TOOL_CACHE / "
add_log " $tick " "PHP" " $status PHP $semver "
}
2019-12-20 19:59:05 +07:00
# Variables
version = $1
2020-10-05 07:45:50 +07:00
dist = $2
2021-05-15 19:57:18 +07:00
php_formula = shivammathur/php/php@" $version "
2020-11-26 00:16:59 +07:00
brew_prefix = " $( brew --prefix) "
2020-12-25 23:29:40 +07:00
brew_repo = " $( brew --repository) "
tap_dir = " $brew_repo " /Library/Taps
2021-02-20 11:45:25 +07:00
scripts = " ${ dist } " /../src/scripts
2020-12-25 23:29:40 +07:00
export HOMEBREW_CHANGE_ARCH_TO_ARM = 1
2020-11-08 14:36:21 +07:00
export HOMEBREW_NO_INSTALL_CLEANUP = 1
export HOMEBREW_NO_AUTO_UPDATE = 1
2021-02-28 20:50:28 +07:00
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK = 1
2020-05-06 08:23:25 +07:00
2020-11-08 14:36:21 +07:00
# shellcheck source=.
2021-03-17 10:16:13 +07:00
. " ${ scripts : ? } " /ext/source.sh
2021-02-20 11:45:25 +07:00
. " ${ scripts : ? } " /tools/add_tools.sh
. " ${ scripts : ? } " /common.sh
2020-05-27 17:53:52 +07:00
read_env
2020-11-08 14:36:21 +07:00
self_hosted_setup
setup_php