From 2fd8046c9d6d7fb951091a97bab192539fa2f1c4 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sun, 2 Jan 2022 17:21:14 +0530 Subject: [PATCH] Refactor extension functions to add_extensions scripts --- __tests__/extensions.test.ts | 2 +- dist/index.js | 6 +- src/extensions.ts | 6 +- src/scripts/common.sh | 161 +-------------- src/scripts/darwin.sh | 3 +- src/scripts/extensions/add_extensions.ps1 | 184 +++++++++++++++++ src/scripts/extensions/add_extensions.sh | 158 +++++++++++++++ src/scripts/{ext => extensions}/blackfire.ps1 | 0 src/scripts/{ext => extensions}/blackfire.sh | 0 src/scripts/{ext => extensions}/couchbase.sh | 0 src/scripts/{ext => extensions}/cubrid.sh | 2 +- .../{ext => extensions}/extension_map.php | 0 src/scripts/{ext => extensions}/firebird.ps1 | 0 src/scripts/{ext => extensions}/firebird.sh | 0 src/scripts/{ext => extensions}/gearman.sh | 0 src/scripts/{ext => extensions}/geos.sh | 0 src/scripts/{ext => extensions}/http.ps1 | 0 src/scripts/{ext => extensions}/http.sh | 0 src/scripts/{ext => extensions}/intl.sh | 0 src/scripts/{ext => extensions}/ioncube.ps1 | 0 src/scripts/{ext => extensions}/ioncube.sh | 0 src/scripts/{ext => extensions}/oci.ps1 | 0 src/scripts/{ext => extensions}/oci.sh | 2 +- .../{ext => extensions}/patches/firebird.sh | 0 .../{ext => extensions}/patches/geos.sh | 0 .../{ext => extensions}/patches/http.sh | 0 .../{ext => extensions}/patches/pdo_oci.sh | 0 .../{ext => extensions}/patches/phpize.sh | 0 .../{ext => extensions}/patches/protobuf.sh | 0 src/scripts/{ext => extensions}/phalcon.ps1 | 0 src/scripts/{ext => extensions}/phalcon.sh | 0 src/scripts/{ext => extensions}/source.sh | 4 +- src/scripts/linux.sh | 3 +- src/scripts/win32.ps1 | 186 +----------------- 34 files changed, 359 insertions(+), 358 deletions(-) create mode 100644 src/scripts/extensions/add_extensions.ps1 create mode 100644 src/scripts/extensions/add_extensions.sh rename src/scripts/{ext => extensions}/blackfire.ps1 (100%) rename src/scripts/{ext => extensions}/blackfire.sh (100%) rename src/scripts/{ext => extensions}/couchbase.sh (100%) rename src/scripts/{ext => extensions}/cubrid.sh (97%) rename src/scripts/{ext => extensions}/extension_map.php (100%) rename src/scripts/{ext => extensions}/firebird.ps1 (100%) rename src/scripts/{ext => extensions}/firebird.sh (100%) rename src/scripts/{ext => extensions}/gearman.sh (100%) rename src/scripts/{ext => extensions}/geos.sh (100%) rename src/scripts/{ext => extensions}/http.ps1 (100%) rename src/scripts/{ext => extensions}/http.sh (100%) rename src/scripts/{ext => extensions}/intl.sh (100%) rename src/scripts/{ext => extensions}/ioncube.ps1 (100%) rename src/scripts/{ext => extensions}/ioncube.sh (100%) rename src/scripts/{ext => extensions}/oci.ps1 (100%) rename src/scripts/{ext => extensions}/oci.sh (97%) rename src/scripts/{ext => extensions}/patches/firebird.sh (100%) rename src/scripts/{ext => extensions}/patches/geos.sh (100%) rename src/scripts/{ext => extensions}/patches/http.sh (100%) rename src/scripts/{ext => extensions}/patches/pdo_oci.sh (100%) rename src/scripts/{ext => extensions}/patches/phpize.sh (100%) rename src/scripts/{ext => extensions}/patches/protobuf.sh (100%) rename src/scripts/{ext => extensions}/phalcon.ps1 (100%) rename src/scripts/{ext => extensions}/phalcon.sh (100%) rename src/scripts/{ext => extensions}/source.sh (97%) diff --git a/__tests__/extensions.test.ts b/__tests__/extensions.test.ts index 45c9272e..8e2db92a 100644 --- a/__tests__/extensions.test.ts +++ b/__tests__/extensions.test.ts @@ -110,7 +110,7 @@ describe('Extension tests', () => { const [formula, extension]: string[] = line.split('='); const prefix: string = extension == 'xdebug' ? 'zend_extension' : 'extension'; - const output: string = fs.existsSync(`src/scripts/ext/${extension}.sh`) + const output: string = fs.existsSync(`src/scripts/extensions/${extension}.sh`) ? `add_${extension}` : `add_brew_extension ${formula} ${prefix}`; return [formula, '7.3', output]; diff --git a/dist/index.js b/dist/index.js index f39c857d..013aa552 100644 --- a/dist/index.js +++ b/dist/index.js @@ -238,7 +238,7 @@ async function addExtensionDarwin(extension_csv, version) { case /^couchbase$|^geos$|^pdo_oci$|^oci8$|^(pecl_)?http|^pdo_firebird$/.test(extension): case /^(5\.[3-6]|7\.[0-4])ioncube$/.test(version_extension): case /(5\.6|7\.[0-3])phalcon3|7\.[2-4]phalcon4/.test(version_extension): - add_script += await utils.customPackage(ext_name, 'ext', extension, 'darwin'); + add_script += await utils.customPackage(ext_name, 'extensions', extension, 'darwin'); return; case /.+-(stable|beta|alpha|devel|snapshot|rc|preview)/.test(extension): add_script += await utils.joins('\nadd_unstable_extension', ext_name, ext_version, ext_prefix); @@ -286,7 +286,7 @@ async function addExtensionWindows(extension_csv, version) { case /^(5\.[3-6]|7\.[0-4])ioncube$/.test(version_extension): case /^7\.[0-3]phalcon3$|^7\.[2-4]phalcon4$/.test(version_extension): case /^(7\.[1-4]|8\.0)(pecl_)?http/.test(version_extension): - add_script += await utils.customPackage(ext_name, 'ext', extension, 'win32'); + add_script += await utils.customPackage(ext_name, 'extensions', extension, 'win32'); return; case /.+-(stable|beta|alpha|devel|snapshot)/.test(extension): add_script += await utils.joins('\nAdd-Extension', ext_name, ext_version.replace('stable', '')); @@ -352,7 +352,7 @@ async function addExtensionLinux(extension_csv, version) { case /(?/dev/null | grep -i -q "$1"; then - for ext in "${deps[@]}"; do - sudo rm -rf /tmp/extcache/"$ext" - enable_extension "$ext" "$2" - done - fi - fi - fi -} - -# Function to enable existing extensions. -enable_extension() { - modules_dir="/var/lib/php/modules/$version" - [ -d "$modules_dir" ] && sudo find "$modules_dir" -path "*disabled*$1" -delete - enable_extension_dependencies "$1" "$2" - enable_cache_extension_dependencies "$1" "$2" - if ! check_extension "$1" && shared_extension "$1"; then - echo "$2=${ext_dir:?}/$1.so" | sudo tee -a "${pecl_file:-${ini_file[@]}}" >/dev/null - fi -} - -# Function to get a map of extensions and their dependent shared extensions. -get_extension_map() { - php -d'error_reporting=0' "${dist:?}"/../src/scripts/ext/extension_map.php -} - -# Function to enable extension dependencies which are also extensions. -enable_extension_dependencies() { - local extension=$1 - prefix=$2 - if ! [ -e /tmp/map.orig ]; then - get_extension_map | sudo tee /tmp/map.orig >/dev/null - fi - for dependency in $(grep "$extension:" /tmp/map.orig | cut -d ':' -f 2 | tr '\n' ' '); do - enable_extension "$dependency" "$prefix" - done -} - -# Function to disable dependent extensions. -disable_extension_dependents() { - local extension=$1 - for dependent in $(get_extension_map | grep -E ".*:.*\s$extension(\s|$)" | cut -d ':' -f 1 | tr '\n' ' '); do - disable_extension_helper "$dependent" true - add_log "${tick:?}" ":$extension" "Disabled $dependent as it depends on $extension" - done -} - -# Function to disable an extension. -disable_extension() { - local extension=$1 - if check_extension "$extension"; then - if shared_extension "$extension"; then - disable_extension_helper "$extension" true - (! check_extension "$extension" && add_log "${tick:?}" ":$extension" "Disabled") || - add_log "${cross:?}" ":$extension" "Could not disable $extension on PHP ${semver:?}" - else - add_log "${cross:?}" ":$extension" "Could not disable $extension on PHP $semver as it not a shared extension" - fi - elif shared_extension "$extension"; then - add_log "${tick:?}" ":$extension" "Disabled" - else - add_log "${tick:?}" ":$extension" "Could not find $extension on PHP $semver" - fi -} - -# Function to disable shared extensions. -disable_all_shared() { - sudo sed -i.orig -E -e "/^(zend_)?extension\s*=/d" "${ini_file[@]}" "$pecl_file" 2>/dev/null || true - sudo find "${ini_dir:-$scan_dir}"/.. -name "*.ini" -not -path "*php.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true - add_log "${tick:?}" "none" "Disabled all shared extensions" -} - # Function to configure PHP configure_php() { ( @@ -190,65 +90,6 @@ configure_php() { ) | sudo tee -a "${pecl_file:-${ini_file[@]}}" >/dev/null } -# Function to configure PECL. -configure_pecl() { - if ! [ -e /tmp/pecl_config ]; then - for script in pear pecl; do - sudo "$script" config-set php_ini "${pecl_file:-${ini_file[@]}}" - sudo "$script" channel-update "$script".php.net - done - echo '' | sudo tee /tmp/pecl_config >/dev/null 2>&1 - fi -} - -# Function to get the PECL version of an extension. -get_pecl_version() { - local extension=$1 - stability="$(echo "$2" | grep -m 1 -Eio "(stable|alpha|beta|rc|snapshot|preview)")" - pecl_rest='https://pecl.php.net/rest/r/' - response=$(get -s -n "" "$pecl_rest$extension"/allreleases.xml) - pecl_version=$(echo "$response" | grep -m 1 -Eio "([0-9]+\.[0-9]+\.[0-9]+${stability}[0-9]+)") - if [ ! "$pecl_version" ]; then - pecl_version=$(echo "$response" | grep -m 1 -Eo "([0-9]+\.[0-9]+\.[0-9]+)") - fi - echo "$pecl_version" -} - -# Function to install PECL extensions and accept default options -pecl_install() { - local extension=$1 - add_pecl >/dev/null 2>&1 - yes '' 2>/dev/null | sudo pecl install -f "$extension" >/dev/null 2>&1 -} - -# Function to install a specific version of PECL extension. -add_pecl_extension() { - local extension=$1 - pecl_version=$2 - prefix=$3 - enable_extension "$extension" "$prefix" - if [[ $pecl_version =~ .*(alpha|beta|rc|snapshot|preview).* ]]; then - pecl_version=$(get_pecl_version "$extension" "$pecl_version") - fi - ext_version=$(php -r "echo phpversion('$extension');") - if [ "${ext_version/-/}" = "$pecl_version" ]; then - add_log "${tick:?}" "$extension" "Enabled" - else - disable_extension_helper "$extension" >/dev/null 2>&1 - pecl_install "$extension-$pecl_version" - add_extension_log "$extension-$pecl_version" "Installed and enabled" - fi -} - -# Function to setup pre-release extensions using PECL. -add_unstable_extension() { - local extension=$1 - stability=$2 - prefix=$3 - pecl_version=$(get_pecl_version "$extension" "$stability") - add_pecl_extension "$extension" "$pecl_version" "$prefix" -} - # Function to get PHP version in semver format. php_semver() { php -v | grep -Eo -m 1 "[0-9]+\.[0-9]+\.[0-9]+((-?[a-zA-Z]+([0-9]+)?)?){2}" | head -n 1 @@ -260,6 +101,6 @@ php_src_tag() { if [[ -n "${commit}" ]]; then echo "$commit" else - echo "php-$semver" + echo "php-${semver:?}" fi } diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 014d6f90..48f01e08 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -250,8 +250,9 @@ export HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK=1 # shellcheck source=. . "${scripts:?}"/common.sh -. "${scripts:?}"/ext/source.sh . "${scripts:?}"/tools/add_tools.sh +. "${scripts:?}"/extensions/source.sh +. "${scripts:?}"/extensions/add_extensions.sh read_env self_hosted_setup setup_php diff --git a/src/scripts/extensions/add_extensions.ps1 b/src/scripts/extensions/add_extensions.ps1 new file mode 100644 index 00000000..60fffcdc --- /dev/null +++ b/src/scripts/extensions/add_extensions.ps1 @@ -0,0 +1,184 @@ +# Function to link dependencies to PHP directory. +Function Set-ExtensionPrerequisites +{ + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $deps_dir + ) + $deps = Get-ChildItem -Recurse -Path $deps_dir + if ($deps.Count -ne 0) { + # Symlink dependencies instead of adding the directory to PATH ... + # as other actions change the PATH thus breaking extensions. + $deps | ForEach-Object { + New-Item -Itemtype SymbolicLink -Path $php_dir -Name $_.Name -Target $_.FullName -Force > $null 2>&1 + } + } else { + Remove-Item $deps_dir -Recurse -Force + } +} + +# Function to add PHP extensions. +Function Add-Extension { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $extension, + [Parameter(Position = 1, Mandatory = $false)] + [ValidateNotNull()] + [ValidateSet('stable', 'beta', 'alpha', 'devel', 'snapshot')] + [string] + $stability = 'stable', + [Parameter(Position = 2, Mandatory = $false)] + [ValidateNotNull()] + [ValidatePattern('^\d+(\.\d+){0,2}$')] + [string] + $extension_version = '' + ) + try { + $extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension } + $deps_dir = "$ext_dir\$extension-vc$($installed.VCVersion)-$arch" + New-Item $deps_dir -Type Directory -Force > $null 2>&1 + if ($null -ne $extension_info) { + switch ($extension_info.State) { + 'Builtin' { + Add-Log $tick $extension "Enabled" + } + 'Enabled' { + Add-Log $tick $extension "Enabled" + } + default { + Enable-ExtensionDependencies $extension + Enable-PhpExtension -Extension $extension_info.Handle -Path $php_dir + Set-ExtensionPrerequisites $deps_dir + Add-Log $tick $extension "Enabled" + } + } + } + else { + # Patch till PHP 8.1 DLLs are released as stable. + $minimumStability = 'stable' + if($version -eq '8.1' -and $stability -eq 'stable') { + $minimumStability = 'snapshot' + } + + $params = @{ Extension = $extension; MinimumStability = $minimumStability; MaximumStability = $stability; Path = $php_dir; AdditionalFilesPath = $deps_dir; NoDependencies = $true } + if($extension_version -ne '') { + $params["Version"] = $extension_version + } + Install-PhpExtension @params + Set-ExtensionPrerequisites $deps_dir + Add-Log $tick $extension "Installed and enabled" + } + } + catch { + Add-Log $cross $extension "Could not install $extension on PHP $($installed.FullVersion)" + } +} + +# Function to get a map of extensions and their dependent shared extensions. +Function Get-ExtensionMap { + php -d'error_reporting=0' $dist\..\src\scripts\ext\extension_map.php +} + +# Function to enable extension dependencies which are also extensions. +Function Enable-ExtensionDependencies { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $extension + ) + if (-not(Test-Path $env:TEMP\map.orig)) { + Get-ExtensionMap | Set-Content -Path $env:TEMP\map.orig + } + $entry = findstr /r "$extension`:.*" $env:TEMP\map.orig + if($entry) { + $entry.split(':')[1].trim().split(' ') | ForEach-Object { + if (-not(php -m | findstr -i $_)) { + Enable-PhpExtension -Extension $_ -Path $php_dir + } + } + } +} + +# Function to disable dependent extensions. +Function Disable-DependentExtensions() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $extension + ) + Get-ExtensionMap | Select-String -Pattern ".*:.*\s$extension(\s|$)" | ForEach-Object { + $dependent = $_.Matches[0].Value.split(':')[0]; + Disable-ExtensionHelper -Extension $dependent -DisableDependents + Add-Log $tick ":$extension" "Disabled $dependent as it depends on $extension" + } +} + +# Helper function to disable an extension. +Function Disable-ExtensionHelper() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $extension, + [switch] $DisableDependents + ) + if($DisableDependents) { + Disable-DependentExtensions $extension + } + Disable-PhpExtension -Extension $extension -Path $php_dir +} + +# Function to disable an extension. +Function Disable-Extension() { + Param ( + [Parameter(Position = 0, Mandatory = $true)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $extension, + [Parameter(Position = 1, Mandatory = $false)] + [ValidateNotNull()] + [ValidateLength(1, [int]::MaxValue)] + [string] + $DisableDependents + ) + if(php -m | findstr -i $extension) { + if(Test-Path $ext_dir\php_$extension.dll) { + try { + $params = @{ Extension = $extension; DisableDependents = ($DisableDependents -ne 'false') } + Disable-ExtensionHelper @params + Add-Log $tick ":$extension" "Disabled" + } catch { + Add-Log $cross ":$extension" "Could not disable $extension on PHP $($installed.FullVersion)" + } + } else { + Add-Log $cross ":$extension" "Could not disable $extension on PHP $($installed.FullVersion) as it not a shared extension" + } + } elseif(Test-Path $ext_dir\php_$extension.dll) { + Add-Log $tick ":$extension" "Disabled" + } else { + Add-Log $tick ":$extension" "Could not find $extension on PHP $($installed.FullVersion)" + } +} + +# Function to disable shared extensions. +Function Disable-AllShared() { + (Get-Content $php_dir\php.ini) | Where-Object {$_ -notmatch '^(zend_)?extension\s*='} | Set-Content $php_dir\php.ini + Add-Log $tick "none" "Disabled all shared extensions" +} + +# Function to handle request to add PECL. +Function Add-Pecl() { + Add-Log $tick "PECL" "Use extensions input to setup PECL extensions on windows" +} \ No newline at end of file diff --git a/src/scripts/extensions/add_extensions.sh b/src/scripts/extensions/add_extensions.sh new file mode 100644 index 00000000..a0e08981 --- /dev/null +++ b/src/scripts/extensions/add_extensions.sh @@ -0,0 +1,158 @@ +# Function to log result of installing extension. +add_extension_log() { + ( + check_extension "$(echo "$1" | cut -d '-' -f 1)" && add_log "${tick:?}" "$1" "$2" + ) || add_log "${cross:?}" "$1" "Could not install $1 on PHP ${semver:?}" +} + +# Function to test if extension is loaded. +check_extension() { + local extension=$1 + if [ "$extension" != "mysql" ]; then + php -m | grep -i -q -w "$extension" + else + php -m | grep -i -q "$extension" + fi +} + +# Function to check if extension is shared +shared_extension() { + [ -e "${ext_dir:?}/$1.so" ] +} + +# Function to enable cached extension's dependencies. +enable_cache_extension_dependencies() { + if [ -d /tmp/extcache ] && shared_extension "$1"; then + cache_dir=$(find /tmp/extcache -maxdepth 1 -type d -regex ".*$1[0-9]*") + if [[ -n "$cache_dir" ]]; then + IFS=" " read -r -a deps <<<"$(find "$cache_dir" -maxdepth 1 -type f -name "*" -exec basename {} \; | tr '\n' ' ')" + if [[ -n "${deps[*]}" ]] && php "${deps[@]/#/-d ${2}=}" -d "${2}=$1" -m 2>/dev/null | grep -i -q "$1"; then + for ext in "${deps[@]}"; do + sudo rm -rf /tmp/extcache/"$ext" + enable_extension "$ext" "$2" + done + fi + fi + fi +} + +# Function to enable existing extensions. +enable_extension() { + modules_dir="/var/lib/php/modules/${version:?}" + [ -d "$modules_dir" ] && sudo find "$modules_dir" -path "*disabled*$1" -delete + enable_extension_dependencies "$1" "$2" + enable_cache_extension_dependencies "$1" "$2" + if ! check_extension "$1" && shared_extension "$1"; then + echo "$2=${ext_dir:?}/$1.so" | sudo tee -a "${pecl_file:-${ini_file[@]}}" >/dev/null + fi +} + +# Function to get a map of extensions and their dependent shared extensions. +get_extension_map() { + php -d'error_reporting=0' "${dist:?}"/../src/scripts/extensions/extension_map.php +} + +# Function to enable extension dependencies which are also extensions. +enable_extension_dependencies() { + local extension=$1 + prefix=$2 + if ! [ -e /tmp/map.orig ]; then + get_extension_map | sudo tee /tmp/map.orig >/dev/null + fi + for dependency in $(grep "$extension:" /tmp/map.orig | cut -d ':' -f 2 | tr '\n' ' '); do + enable_extension "$dependency" "$prefix" + done +} + +# Function to disable dependent extensions. +disable_extension_dependents() { + local extension=$1 + for dependent in $(get_extension_map | grep -E ".*:.*\s$extension(\s|$)" | cut -d ':' -f 1 | tr '\n' ' '); do + disable_extension_helper "$dependent" true + add_log "${tick:?}" ":$extension" "Disabled $dependent as it depends on $extension" + done +} + +# Function to disable an extension. +disable_extension() { + local extension=$1 + if check_extension "$extension"; then + if shared_extension "$extension"; then + disable_extension_helper "$extension" true + (! check_extension "$extension" && add_log "${tick:?}" ":$extension" "Disabled") || + add_log "${cross:?}" ":$extension" "Could not disable $extension on PHP ${semver:?}" + else + add_log "${cross:?}" ":$extension" "Could not disable $extension on PHP $semver as it not a shared extension" + fi + elif shared_extension "$extension"; then + add_log "${tick:?}" ":$extension" "Disabled" + else + add_log "${tick:?}" ":$extension" "Could not find $extension on PHP $semver" + fi +} + +# Function to disable shared extensions. +disable_all_shared() { + sudo sed -i.orig -E -e "/^(zend_)?extension\s*=/d" "${ini_file[@]}" "$pecl_file" 2>/dev/null || true + sudo find "${ini_dir:-$scan_dir}"/.. -name "*.ini" -not -path "*php.ini" -not -path "*mods-available*" -delete >/dev/null 2>&1 || true + add_log "${tick:?}" "none" "Disabled all shared extensions" +} + +# Function to configure PECL. +configure_pecl() { + if ! [ -e /tmp/pecl_config ]; then + for script in pear pecl; do + sudo "$script" config-set php_ini "${pecl_file:-${ini_file[@]}}" + sudo "$script" channel-update "$script".php.net + done + echo '' | sudo tee /tmp/pecl_config >/dev/null 2>&1 + fi +} + +# Function to get the PECL version of an extension. +get_pecl_version() { + local extension=$1 + stability="$(echo "$2" | grep -m 1 -Eio "(stable|alpha|beta|rc|snapshot|preview)")" + pecl_rest='https://pecl.php.net/rest/r/' + response=$(get -s -n "" "$pecl_rest$extension"/allreleases.xml) + pecl_version=$(echo "$response" | grep -m 1 -Eio "([0-9]+\.[0-9]+\.[0-9]+${stability}[0-9]+)") + if [ ! "$pecl_version" ]; then + pecl_version=$(echo "$response" | grep -m 1 -Eo "([0-9]+\.[0-9]+\.[0-9]+)") + fi + echo "$pecl_version" +} + +# Function to install PECL extensions and accept default options +pecl_install() { + local extension=$1 + add_pecl >/dev/null 2>&1 + yes '' 2>/dev/null | sudo pecl install -f "$extension" >/dev/null 2>&1 +} + +# Function to install a specific version of PECL extension. +add_pecl_extension() { + local extension=$1 + pecl_version=$2 + prefix=$3 + enable_extension "$extension" "$prefix" + if [[ $pecl_version =~ .*(alpha|beta|rc|snapshot|preview).* ]]; then + pecl_version=$(get_pecl_version "$extension" "$pecl_version") + fi + ext_version=$(php -r "echo phpversion('$extension');") + if [ "${ext_version/-/}" = "$pecl_version" ]; then + add_log "${tick:?}" "$extension" "Enabled" + else + disable_extension_helper "$extension" >/dev/null 2>&1 + pecl_install "$extension-$pecl_version" + add_extension_log "$extension-$pecl_version" "Installed and enabled" + fi +} + +# Function to setup pre-release extensions using PECL. +add_unstable_extension() { + local extension=$1 + stability=$2 + prefix=$3 + pecl_version=$(get_pecl_version "$extension" "$stability") + add_pecl_extension "$extension" "$pecl_version" "$prefix" +} diff --git a/src/scripts/ext/blackfire.ps1 b/src/scripts/extensions/blackfire.ps1 similarity index 100% rename from src/scripts/ext/blackfire.ps1 rename to src/scripts/extensions/blackfire.ps1 diff --git a/src/scripts/ext/blackfire.sh b/src/scripts/extensions/blackfire.sh similarity index 100% rename from src/scripts/ext/blackfire.sh rename to src/scripts/extensions/blackfire.sh diff --git a/src/scripts/ext/couchbase.sh b/src/scripts/extensions/couchbase.sh similarity index 100% rename from src/scripts/ext/couchbase.sh rename to src/scripts/extensions/couchbase.sh diff --git a/src/scripts/ext/cubrid.sh b/src/scripts/extensions/cubrid.sh similarity index 97% rename from src/scripts/ext/cubrid.sh rename to src/scripts/extensions/cubrid.sh index 8864f688..cb38fdac 100644 --- a/src/scripts/ext/cubrid.sh +++ b/src/scripts/extensions/cubrid.sh @@ -57,4 +57,4 @@ add_cubrid() { } # shellcheck source=. -. "${scripts:?}"/ext/patches/phpize.sh +. "${scripts:?}"/extensions/patches/phpize.sh diff --git a/src/scripts/ext/extension_map.php b/src/scripts/extensions/extension_map.php similarity index 100% rename from src/scripts/ext/extension_map.php rename to src/scripts/extensions/extension_map.php diff --git a/src/scripts/ext/firebird.ps1 b/src/scripts/extensions/firebird.ps1 similarity index 100% rename from src/scripts/ext/firebird.ps1 rename to src/scripts/extensions/firebird.ps1 diff --git a/src/scripts/ext/firebird.sh b/src/scripts/extensions/firebird.sh similarity index 100% rename from src/scripts/ext/firebird.sh rename to src/scripts/extensions/firebird.sh diff --git a/src/scripts/ext/gearman.sh b/src/scripts/extensions/gearman.sh similarity index 100% rename from src/scripts/ext/gearman.sh rename to src/scripts/extensions/gearman.sh diff --git a/src/scripts/ext/geos.sh b/src/scripts/extensions/geos.sh similarity index 100% rename from src/scripts/ext/geos.sh rename to src/scripts/extensions/geos.sh diff --git a/src/scripts/ext/http.ps1 b/src/scripts/extensions/http.ps1 similarity index 100% rename from src/scripts/ext/http.ps1 rename to src/scripts/extensions/http.ps1 diff --git a/src/scripts/ext/http.sh b/src/scripts/extensions/http.sh similarity index 100% rename from src/scripts/ext/http.sh rename to src/scripts/extensions/http.sh diff --git a/src/scripts/ext/intl.sh b/src/scripts/extensions/intl.sh similarity index 100% rename from src/scripts/ext/intl.sh rename to src/scripts/extensions/intl.sh diff --git a/src/scripts/ext/ioncube.ps1 b/src/scripts/extensions/ioncube.ps1 similarity index 100% rename from src/scripts/ext/ioncube.ps1 rename to src/scripts/extensions/ioncube.ps1 diff --git a/src/scripts/ext/ioncube.sh b/src/scripts/extensions/ioncube.sh similarity index 100% rename from src/scripts/ext/ioncube.sh rename to src/scripts/extensions/ioncube.sh diff --git a/src/scripts/ext/oci.ps1 b/src/scripts/extensions/oci.ps1 similarity index 100% rename from src/scripts/ext/oci.ps1 rename to src/scripts/extensions/oci.ps1 diff --git a/src/scripts/ext/oci.sh b/src/scripts/extensions/oci.sh similarity index 97% rename from src/scripts/ext/oci.sh rename to src/scripts/extensions/oci.sh index cd899ad4..11544c05 100644 --- a/src/scripts/ext/oci.sh +++ b/src/scripts/extensions/oci.sh @@ -62,4 +62,4 @@ add_oci() { } # shellcheck source=. -. "${scripts:?}"/ext/patches/phpize.sh +. "${scripts:?}"/extensions/patches/phpize.sh diff --git a/src/scripts/ext/patches/firebird.sh b/src/scripts/extensions/patches/firebird.sh similarity index 100% rename from src/scripts/ext/patches/firebird.sh rename to src/scripts/extensions/patches/firebird.sh diff --git a/src/scripts/ext/patches/geos.sh b/src/scripts/extensions/patches/geos.sh similarity index 100% rename from src/scripts/ext/patches/geos.sh rename to src/scripts/extensions/patches/geos.sh diff --git a/src/scripts/ext/patches/http.sh b/src/scripts/extensions/patches/http.sh similarity index 100% rename from src/scripts/ext/patches/http.sh rename to src/scripts/extensions/patches/http.sh diff --git a/src/scripts/ext/patches/pdo_oci.sh b/src/scripts/extensions/patches/pdo_oci.sh similarity index 100% rename from src/scripts/ext/patches/pdo_oci.sh rename to src/scripts/extensions/patches/pdo_oci.sh diff --git a/src/scripts/ext/patches/phpize.sh b/src/scripts/extensions/patches/phpize.sh similarity index 100% rename from src/scripts/ext/patches/phpize.sh rename to src/scripts/extensions/patches/phpize.sh diff --git a/src/scripts/ext/patches/protobuf.sh b/src/scripts/extensions/patches/protobuf.sh similarity index 100% rename from src/scripts/ext/patches/protobuf.sh rename to src/scripts/extensions/patches/protobuf.sh diff --git a/src/scripts/ext/phalcon.ps1 b/src/scripts/extensions/phalcon.ps1 similarity index 100% rename from src/scripts/ext/phalcon.ps1 rename to src/scripts/extensions/phalcon.ps1 diff --git a/src/scripts/ext/phalcon.sh b/src/scripts/extensions/phalcon.sh similarity index 100% rename from src/scripts/ext/phalcon.sh rename to src/scripts/extensions/phalcon.sh diff --git a/src/scripts/ext/source.sh b/src/scripts/extensions/source.sh similarity index 97% rename from src/scripts/ext/source.sh rename to src/scripts/extensions/source.sh index 7128a0a1..acd9be67 100644 --- a/src/scripts/ext/source.sh +++ b/src/scripts/extensions/source.sh @@ -76,9 +76,9 @@ run_group() { patch_extension() { extension=$1 - if [ -e "${scripts:?}"/ext/patches/"$extension".sh ]; then + if [ -e "${scripts:?}"/extensions/patches/"$extension".sh ]; then # shellcheck source=. - . "${scripts:?}"/ext/patches/"$extension".sh + . "${scripts:?}"/extensions/patches/"$extension".sh patch_"${extension}" fi } diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index f0c0f5b9..faa016f1 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -231,9 +231,10 @@ scripts="${dist}"/../src/scripts . /etc/os-release # shellcheck source=. . "${scripts:?}"/common.sh -. "${scripts:?}"/ext/source.sh . "${scripts:?}"/tools/ppa.sh . "${scripts:?}"/tools/add_tools.sh +. "${scripts:?}"/extensions/source.sh +. "${scripts:?}"/extensions/add_extensions.sh read_env self_hosted_setup setup_php diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index 63410393..591cf048 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -119,28 +119,6 @@ Function Install-PSPackage() { } } -# Function to link dependencies to PHP directory. -Function Set-ExtensionPrerequisites -{ - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $deps_dir - ) - $deps = Get-ChildItem -Recurse -Path $deps_dir - if ($deps.Count -ne 0) { - # Symlink dependencies instead of adding the directory to PATH ... - # as other actions change the PATH thus breaking extensions. - $deps | ForEach-Object { - New-Item -Itemtype SymbolicLink -Path $php_dir -Name $_.Name -Target $_.FullName -Force > $null 2>&1 - } - } else { - Remove-Item $deps_dir -Recurse -Force - } -} - # Function to add CA certificates to PHP. Function Add-PhpCAInfo { try { @@ -151,169 +129,6 @@ Function Add-PhpCAInfo { } } -# Function to add PHP extensions. -Function Add-Extension { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $extension, - [Parameter(Position = 1, Mandatory = $false)] - [ValidateNotNull()] - [ValidateSet('stable', 'beta', 'alpha', 'devel', 'snapshot')] - [string] - $stability = 'stable', - [Parameter(Position = 2, Mandatory = $false)] - [ValidateNotNull()] - [ValidatePattern('^\d+(\.\d+){0,2}$')] - [string] - $extension_version = '' - ) - try { - $extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension } - $deps_dir = "$ext_dir\$extension-vc$($installed.VCVersion)-$arch" - New-Item $deps_dir -Type Directory -Force > $null 2>&1 - if ($null -ne $extension_info) { - switch ($extension_info.State) { - 'Builtin' { - Add-Log $tick $extension "Enabled" - } - 'Enabled' { - Add-Log $tick $extension "Enabled" - } - default { - Enable-ExtensionDependencies $extension - Enable-PhpExtension -Extension $extension_info.Handle -Path $php_dir - Set-ExtensionPrerequisites $deps_dir - Add-Log $tick $extension "Enabled" - } - } - } - else { - # Patch till PHP 8.1 DLLs are released as stable. - $minimumStability = 'stable' - if($version -eq '8.1' -and $stability -eq 'stable') { - $minimumStability = 'snapshot' - } - - $params = @{ Extension = $extension; MinimumStability = $minimumStability; MaximumStability = $stability; Path = $php_dir; AdditionalFilesPath = $deps_dir; NoDependencies = $true } - if($extension_version -ne '') { - $params["Version"] = $extension_version - } - Install-PhpExtension @params - Set-ExtensionPrerequisites $deps_dir - Add-Log $tick $extension "Installed and enabled" - } - } - catch { - Add-Log $cross $extension "Could not install $extension on PHP $($installed.FullVersion)" - } -} - -# Function to get a map of extensions and their dependent shared extensions. -Function Get-ExtensionMap { - php -d'error_reporting=0' $dist\..\src\scripts\ext\extension_map.php -} - -# Function to enable extension dependencies which are also extensions. -Function Enable-ExtensionDependencies { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $extension - ) - if (-not(Test-Path $env:TEMP\map.orig)) { - Get-ExtensionMap | Set-Content -Path $env:TEMP\map.orig - } - $entry = findstr /r "$extension`:.*" $env:TEMP\map.orig - if($entry) { - $entry.split(':')[1].trim().split(' ') | ForEach-Object { - if (-not(php -m | findstr -i $_)) { - Enable-PhpExtension -Extension $_ -Path $php_dir - } - } - } -} - -# Function to disable dependent extensions. -Function Disable-DependentExtensions() { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $extension - ) - Get-ExtensionMap | Select-String -Pattern ".*:.*\s$extension(\s|$)" | ForEach-Object { - $dependent = $_.Matches[0].Value.split(':')[0]; - Disable-ExtensionHelper -Extension $dependent -DisableDependents - Add-Log $tick ":$extension" "Disabled $dependent as it depends on $extension" - } -} - -# Helper function to disable an extension. -Function Disable-ExtensionHelper() { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $extension, - [switch] $DisableDependents - ) - if($DisableDependents) { - Disable-DependentExtensions $extension - } - Disable-PhpExtension -Extension $extension -Path $php_dir -} - -# Function to disable an extension. -Function Disable-Extension() { - Param ( - [Parameter(Position = 0, Mandatory = $true)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $extension, - [Parameter(Position = 1, Mandatory = $false)] - [ValidateNotNull()] - [ValidateLength(1, [int]::MaxValue)] - [string] - $DisableDependents - ) - if(php -m | findstr -i $extension) { - if(Test-Path $ext_dir\php_$extension.dll) { - try { - $params = @{ Extension = $extension; DisableDependents = ($DisableDependents -ne 'false') } - Disable-ExtensionHelper @params - Add-Log $tick ":$extension" "Disabled" - } catch { - Add-Log $cross ":$extension" "Could not disable $extension on PHP $($installed.FullVersion)" - } - } else { - Add-Log $cross ":$extension" "Could not disable $extension on PHP $($installed.FullVersion) as it not a shared extension" - } - } elseif(Test-Path $ext_dir\php_$extension.dll) { - Add-Log $tick ":$extension" "Disabled" - } else { - Add-Log $tick ":$extension" "Could not find $extension on PHP $($installed.FullVersion)" - } -} - -# Function to disable shared extensions. -Function Disable-AllShared() { - (Get-Content $php_dir\php.ini) | Where-Object {$_ -notmatch '^(zend_)?extension\s*='} | Set-Content $php_dir\php.ini - Add-Log $tick "none" "Disabled all shared extensions" -} - -# Function to handle request to add PECL. -Function Add-Pecl() { - Add-Log $tick "PECL" "Use extensions input to setup PECL extensions on windows" -} - # Variables $tick = ([char]8730) $cross = ([char]10007) @@ -363,6 +178,7 @@ if($env:RUNNER -eq 'self-hosted') { } . $dist\..\src\scripts\tools\add_tools.ps1 +. $dist\..\src\scripts\extensions\add_extensions.ps1 Add-Printf >$null 2>&1 Step-Log "Setup PhpManager"