Improve source extension setup logs

Refactor source extension setup code to src/scripts/ext/source.sh
This commit is contained in:
Shivam Mathur 2021-03-17 08:46:13 +05:30
parent b3152daa8d
commit 39faaac1d3
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
4 changed files with 94 additions and 63 deletions

View File

@ -29,7 +29,7 @@ add_log() {
printf "\033[32;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s\033[0m\n" "$mark" "$subject" "$message"
else
printf "\033[31;1m%s \033[0m\033[34;1m%s \033[0m\033[90;1m%s\033[0m\n" "$mark" "$subject" "$message"
[ "$fail_fast" = "true" ] && exit 1;
[ "$fail_fast" = "true" ] && exit 1
fi
}
@ -290,59 +290,3 @@ php_src_tag() {
fi
echo "$php_src_tag"
}
# Function to parse extension environment variables
parse_args() {
extension=$1
suffix=$2
up_extension=$(echo "$extension" | tr '[:lower:]' '[:upper:]')
var="${extension}_${suffix}"
up_var="${up_extension}_${suffix}"
output=$(echo "${!var} ${!up_var}" | sed "s/, */ /g")
echo "$output" | xargs -n 1 | sort | uniq | xargs
}
# Function to add required libraries
add_libs() {
libs=("$@")
if [ "$(uname -s)" = "Linux" ]; then
install_packages "${libs[@]}"
else
brew install "${libs[@]}"
fi
}
# Function to install extension from a git repository
add_extension_from_source() {
extension=$1
domain=$2
org=$3
repo=$4
sub_dir=$5
release=$6
prefix=$7
slug="$extension-$release"
IFS=' ' read -r -a libs <<< "$(parse_args "$extension" LIBS)"
IFS=' ' read -r -a opts <<< "$(parse_args "$extension" CONFIGURE_OPTS)"
IFS=' ' read -r -a prefix_opts <<< "$(parse_args "$extension" CONFIGURE_PREFIX_OPTS)"
IFS=' ' read -r -a suffix_opts <<< "$(parse_args "$extension" CONFIGURE_SUFFIX_OPTS)"
printf "::group::\033[34;1m%s \033[0m\033[90;1m%s \033[0m\n" "$slug" "Click for $extension installation logs"
(
add_devtools phpize >/dev/null
delete_extension "$extension"
git clone -q -n "$domain/$org/$repo" /tmp/"$repo-$release"
cd /tmp/"$repo-$release/$sub_dir" || exit 1
git checkout -q "$release"
if [ "$(find . -maxdepth 1 -name '*.m4' -exec grep -H 'PHP_NEW_EXTENSION' {} \;| wc -l)" != "0" ]; then
git submodule -q update --init --recursive
add_libs "${libs[@]}"
phpize && sudo "${prefix_opts[@]}" ./configure "${suffix_opts[@]}" "${opts[@]}"
sudo make -j"$(nproc 2>/dev/null || sysctl -n hw.ncpu)" && sudo make install
enable_extension "$extension" "$prefix"
else
add_log "$cross" "$domain/$org/$repo" "Provided repository does not contain a PHP extension"
fi
)
echo "::endgroup::"
add_extension_log "$slug" "Installed and enabled from $domain/$org/$repo"
}

View File

@ -197,6 +197,7 @@ export HOMEBREW_NO_AUTO_UPDATE=1
export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1
# shellcheck source=.
. "${scripts:?}"/ext/source.sh
. "${scripts:?}"/tools/add_tools.sh
. "${scripts:?}"/common.sh
read_env

85
src/scripts/ext/source.sh Normal file
View File

@ -0,0 +1,85 @@
# Function to parse extension environment variables
parse_args() {
extension=$1
suffix=$2
up_extension=$(echo "$extension" | tr '[:lower:]' '[:upper:]')
var="${extension}_${suffix}"
up_var="${up_extension}_${suffix}"
output=$(echo "${!var} ${!up_var}" | sed "s/, */ /g")
echo "$output" | xargs -n 1 | sort | uniq | xargs
}
# Function to log if a library is installed
add_lib_log() {
lib=$1
output=$2
if [ "x$output" != "x" ]; then
add_log "${tick:?}" "$lib" "Installed"
else
add_log "${cross:?}" "$lib" "Could not install $lib"
fi
}
# Function to add required libraries
add_libs() {
libs=("$@")
for lib in "${libs[@]}"; do
if [ "$(uname -s)" = "Linux" ]; then
install_packages "$lib" >/dev/null 2>&1
add_lib_log "$lib" "$(dpkg -s "$lib" 2>/dev/null | grep Status)"
else
brew install "$lib" >/dev/null 2>&1
add_lib_log "$lib" "$(find "${brew_cellar:?}" -maxdepth 1 -name "$lib")"
fi
done
}
# Function to run command in a group
run_group() {
command=$1
log=$2
echo "$command" | sudo tee ./run_group.sh >/dev/null 2>&1
echo "::group::$log"
. ./run_group.sh
rm ./run_group.sh
echo "::endgroup::"
}
# Function to install extension from a git repository
add_extension_from_source() {
extension=$1
domain=$2
org=$3
repo=$4
sub_dir=$5
release=$6
prefix=$7
slug="$extension-$release"
libraries="$(parse_args "$extension" LIBS)"
opts="$(parse_args "$extension" CONFIGURE_OPTS)"
prefix_opts="$(parse_args "$extension" CONFIGURE_PREFIX_OPTS)"
suffix_opts="$(parse_args "$extension" CONFIGURE_SUFFIX_OPTS)"
step_log "Setup $slug"
(
add_devtools phpize >/dev/null
delete_extension "$extension"
run_group "git clone -nv $domain/$org/$repo /tmp/$repo-$release" "git clone"
cd /tmp/"$repo-$release/$sub_dir" || exit 1
git checkout -q "$release"
if ! [ "$(find . -maxdepth 1 -name '*.m4' -exec grep -H 'PHP_NEW_EXTENSION' {} \; | wc -l)" != "0" ]; then
add_log "${cross:?}" "$domain/$org/$repo" "$domain/$org/$repo does not have a PHP extension"
else
if [ -e .gitmodules ]; then
jobs="$(grep -c "\[submodule" .gitmodules)"
run_group "git submodule update --jobs $jobs --init --recursive" "git submodule"
fi
[ "x$libraries" != "x" ] && run_group "add_libs $libraries" "add libraries"
run_group "phpize" "phpize"
run_group "sudo $prefix_opts ./configure $suffix_opts $opts" "configure"
run_group "sudo make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu)" "make"
run_group "sudo make install" "make install"
enable_extension "$extension" "$prefix"
fi
)
add_extension_log "$slug" "Installed from $domain/$org/$repo and enabled"
}

View File

@ -287,6 +287,7 @@ apt_remove="sudo $debconf_fix apt-fast remove -y"
scripts="${dist}"/../src/scripts
# shellcheck source=.
. "${scripts:?}"/ext/source.sh
. "${scripts:?}"/tools/add_tools.sh
. "${scripts:?}"/common.sh
. /etc/lsb-release