diff --git a/__tests__/coverage.test.ts b/__tests__/coverage.test.ts index fe92d97d..0321867e 100644 --- a/__tests__/coverage.test.ts +++ b/__tests__/coverage.test.ts @@ -21,7 +21,7 @@ describe('Config tests', () => { it('checking addCoverage with PCOV on darwin', async () => { const darwin: string = await coverage.addCoverage('pcov', '7.4', 'darwin'); - expect(darwin).toContain('add_extension pcov'); + expect(darwin).toContain('add_brew_extension pcov'); expect(darwin).toContain('remove_extension xdebug'); }); @@ -71,7 +71,7 @@ describe('Config tests', () => { '7.4', 'darwin' ); - expect(darwin).toContain('add_extension xdebug'); + expect(darwin).toContain('add_brew_extension xdebug'); }); it('checking disableCoverage windows', async () => { diff --git a/__tests__/extensions.test.ts b/__tests__/extensions.test.ts index 390dbca9..29d9ed94 100644 --- a/__tests__/extensions.test.ts +++ b/__tests__/extensions.test.ts @@ -59,20 +59,15 @@ describe('Extension tests', () => { it('checking addExtensionOnLinux', async () => { let linux: string = await extensions.addExtension( - 'Xdebug, xdebug3, pcov, sqlite, :intl, ast, uopz, ast-beta, pdo_mysql, pdo-odbc, xdebug-alpha, grpc-1.2.3', + 'Xdebug, xdebug3, pcov, sqlite, :intl, ast, ast-beta, pdo_mysql, pdo-odbc, xdebug-alpha, grpc-1.2.3', '7.4', 'linux' ); expect(linux).toContain( 'add_extension_from_source xdebug xdebug/xdebug master --enable-xdebug zend_extension' ); - expect(linux).toContain('sudo $debconf_fix apt-get install -y php7.4-pcov'); - expect(linux).toContain( - 'sudo $debconf_fix apt-get install -y php7.4-sqlite3' - ); + expect(linux).toContain('add_extension sqlite3'); expect(linux).toContain('remove_extension intl'); - expect(linux).toContain('sudo $debconf_fix apt-get install -y php7.4-ast'); - expect(linux).toContain('sudo $debconf_fix apt-get install -y php-uopz'); expect(linux).toContain('add_unstable_extension ast beta extension'); expect(linux).toContain('add_pdo_extension mysql'); expect(linux).toContain('add_pdo_extension odbc'); @@ -81,11 +76,6 @@ describe('Extension tests', () => { 'add_unstable_extension xdebug alpha zend_extension' ); - linux = await extensions.addExtension('xdebug3', '8.0', 'linux'); - expect(linux).toContain( - 'sudo $debconf_fix apt-get install -y php8.0-xdebug' - ); - linux = await extensions.addExtension('pcov', '5.6', 'linux'); expect(linux).toContain( 'add_log "$cross" "pcov" "pcov is not supported on PHP 5.6"' @@ -134,14 +124,14 @@ describe('Extension tests', () => { '7.2', 'darwin' ); - expect(darwin).toContain('add_brew_extension xdebug'); - expect(darwin).toContain('add_brew_extension pcov'); - expect(darwin).toContain('add_brew_extension grpc'); - expect(darwin).toContain('add_brew_extension igbinary'); - expect(darwin).toContain('add_brew_extension imagick'); - expect(darwin).toContain('add_brew_extension protobuf'); - expect(darwin).toContain('add_brew_extension swoole'); - expect(darwin).toContain('pecl_install sqlite3'); + expect(darwin).toContain('add_brew_extension xdebug zend_extension'); + expect(darwin).toContain('add_brew_extension pcov extension'); + expect(darwin).toContain('add_brew_extension grpc extension'); + expect(darwin).toContain('add_brew_extension igbinary extension'); + expect(darwin).toContain('add_brew_extension imagick extension'); + expect(darwin).toContain('add_brew_extension protobuf extension'); + expect(darwin).toContain('add_brew_extension swoole extension'); + expect(darwin).toContain('add_extension sqlite3'); expect(darwin).toContain('remove_extension intl'); expect(darwin).toContain('add_unstable_extension ast beta extension'); expect(darwin).toContain('add_pecl_extension grpc 1.2.3 extension'); @@ -177,14 +167,13 @@ describe('Extension tests', () => { expect(darwin).toContain('add_brew_extension xdebug'); darwin = await extensions.addExtension('redis', '5.6', 'darwin'); - expect(darwin).toContain('pecl_install redis-2.2.8'); + expect(darwin).toContain('add_extension redis-2.2.8'); darwin = await extensions.addExtension('redis', '7.2', 'darwin'); - expect(darwin).toContain('pecl_install redis'); + expect(darwin).toContain('add_extension redis'); darwin = await extensions.addExtension('imagick', '5.5', 'darwin'); - expect(darwin).toContain('brew install pkg-config imagemagick'); - expect(darwin).toContain('pecl_install imagick'); + expect(darwin).toContain('add_extension imagick'); darwin = await extensions.addExtension('blackfire', '7.3', 'darwin'); expect(darwin).toContain('add_blackfire blackfire'); diff --git a/dist/index.js b/dist/index.js index 162ad27a..92522fbe 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2570,6 +2570,7 @@ async function getScript(filename, version, os_version) { const name = 'setup-php'; const url = 'https://setup-php.com/support'; // taking inputs + process.env['fail_fast'] = await utils.getInput('fail-fast', false); const extension_csv = await utils.getInput('extensions', false); const ini_values_csv = await utils.getInput('ini-values', false); const coverage_driver = await utils.getInput('coverage', false); @@ -2603,8 +2604,7 @@ async function run() { const tool = await utils.scriptTool(os_version); const script = os_version + (await utils.scriptExtension(os_version)); const location = await getScript(script, version, os_version); - const fail_fast = await utils.readEnv('fail-fast'); - await exec_1.exec(await utils.joins(tool, location, version, __dirname, fail_fast)); + await exec_1.exec(await utils.joins(tool, location, version, __dirname)); } catch (error) { core.setFailed(error.message); @@ -2865,9 +2865,8 @@ const utils = __importStar(__webpack_require__(163)); * * @param extension_csv * @param version - * @param pipe */ -async function addExtensionDarwin(extension_csv, version, pipe) { +async function addExtensionDarwin(extension_csv, version) { const extensions = await utils.extensionArray(extension_csv); let add_script = '\n'; let remove_script = ''; @@ -2875,8 +2874,6 @@ async function addExtensionDarwin(extension_csv, version, pipe) { const version_extension = version + extension; const [ext_name, ext_version] = extension.split('-'); const ext_prefix = await utils.getExtensionPrefix(ext_name); - const command_prefix = 'pecl_install '; - let command = ''; switch (true) { // match :extension case /^:/.test(ext_name): @@ -2911,26 +2908,20 @@ async function addExtensionDarwin(extension_csv, version, pipe) { case /(5\.6|7\.[0-4]|8\.[0-9])(xdebug|igbinary)/.test(version_extension): case /(5\.6|7\.[0-4])(grpc|imagick|protobuf|swoole)/.test(version_extension): case /(7\.[1-4]|8\.[0-9])pcov/.test(version_extension): - command = 'add_brew_extension ' + ext_name; - break; + add_script += await utils.joins('\nadd_brew_extension', ext_name, ext_prefix); + return; // match 5.6redis case /5\.6redis/.test(version_extension): - command = command_prefix + 'redis-2.2.8'; - break; - // match 5.4imagick and 5.5imagick - case /^5\.[4-5]imagick$/.test(version_extension): - command = await utils.joins('brew install pkg-config imagemagick' + pipe, '&& ' + command_prefix + 'imagick' + pipe); + extension = 'redis-2.2.8'; break; // match sqlite case /^sqlite$/.test(extension): extension = 'sqlite3'; - command = command_prefix + extension; break; default: - command = command_prefix + extension; break; } - add_script += await utils.joins('\nadd_extension', extension, '"' + command + '"', ext_prefix); + add_script += await utils.joins('\nadd_extension', extension, ext_prefix); }); return add_script + remove_script; } @@ -3013,9 +3004,8 @@ exports.addExtensionWindows = addExtensionWindows; * * @param extension_csv * @param version - * @param pipe */ -async function addExtensionLinux(extension_csv, version, pipe) { +async function addExtensionLinux(extension_csv, version) { const extensions = await utils.extensionArray(extension_csv); let add_script = '\n'; let remove_script = ''; @@ -3023,8 +3013,6 @@ async function addExtensionLinux(extension_csv, version, pipe) { const version_extension = version + extension; const [ext_name, ext_version] = extension.split('-'); const ext_prefix = await utils.getExtensionPrefix(ext_name); - const command_prefix = 'sudo $debconf_fix apt-get install -y php'; - let command = ''; switch (true) { // Match :extension case /^:/.test(ext_name): @@ -3066,27 +3054,20 @@ async function addExtensionLinux(extension_csv, version, pipe) { // match 8.0xdebug3...8.9xdebug3 case /^8\.[0-9]xdebug3$/.test(version_extension): extension = 'xdebug'; - command = command_prefix + version + '-' + extension + pipe; break; // match pdo extensions case /.*pdo[_-].*/.test(version_extension): extension = extension.replace(/pdo[_-]|3/, ''); add_script += '\nadd_pdo_extension ' + extension; return; - // match uopz - case /^(uopz)$/.test(extension): - command = command_prefix + '-' + extension + pipe; - break; // match sqlite case /^sqlite$/.test(extension): extension = 'sqlite3'; - command = command_prefix + version + '-' + extension + pipe; break; default: - command = command_prefix + version + '-' + extension + pipe; break; } - add_script += await utils.joins('\nadd_extension', extension, '"' + command + '"', ext_prefix); + add_script += await utils.joins('\nadd_extension', extension, ext_prefix); }); return add_script + remove_script; } @@ -3100,24 +3081,24 @@ exports.addExtensionLinux = addExtensionLinux; * @param no_step */ async function addExtension(extension_csv, version, os_version, no_step = false) { - const pipe = await utils.suppressOutput(os_version); + const log = await utils.stepLog('Setup Extensions', os_version); let script = '\n'; switch (no_step) { case true: - script += (await utils.stepLog('Setup Extensions', os_version)) + pipe; + script += log + (await utils.suppressOutput(os_version)); break; case false: default: - script += await utils.stepLog('Setup Extensions', os_version); + script += log; break; } switch (os_version) { case 'win32': return script + (await addExtensionWindows(extension_csv, version)); case 'darwin': - return script + (await addExtensionDarwin(extension_csv, version, pipe)); + return script + (await addExtensionDarwin(extension_csv, version)); case 'linux': - return script + (await addExtensionLinux(extension_csv, version, pipe)); + return script + (await addExtensionLinux(extension_csv, version)); default: return await utils.log('Platform ' + os_version + ' is not supported', os_version, 'error'); } diff --git a/src/extensions.ts b/src/extensions.ts index d208e1a1..7a0017e8 100644 --- a/src/extensions.ts +++ b/src/extensions.ts @@ -5,12 +5,10 @@ import * as utils from './utils'; * * @param extension_csv * @param version - * @param pipe */ export async function addExtensionDarwin( extension_csv: string, - version: string, - pipe: string + version: string ): Promise { const extensions: Array = await utils.extensionArray(extension_csv); let add_script = '\n'; @@ -19,8 +17,6 @@ export async function addExtensionDarwin( const version_extension: string = version + extension; const [ext_name, ext_version]: string[] = extension.split('-'); const ext_prefix = await utils.getExtensionPrefix(ext_name); - const command_prefix = 'pecl_install '; - let command = ''; switch (true) { // match :extension case /^:/.test(ext_name): @@ -74,34 +70,24 @@ export async function addExtensionDarwin( version_extension ): case /(7\.[1-4]|8\.[0-9])pcov/.test(version_extension): - command = 'add_brew_extension ' + ext_name; - break; + add_script += await utils.joins( + '\nadd_brew_extension', + ext_name, + ext_prefix + ); + return; // match 5.6redis case /5\.6redis/.test(version_extension): - command = command_prefix + 'redis-2.2.8'; - break; - // match 5.4imagick and 5.5imagick - case /^5\.[4-5]imagick$/.test(version_extension): - command = await utils.joins( - 'brew install pkg-config imagemagick' + pipe, - '&& ' + command_prefix + 'imagick' + pipe - ); + extension = 'redis-2.2.8'; break; // match sqlite case /^sqlite$/.test(extension): extension = 'sqlite3'; - command = command_prefix + extension; break; default: - command = command_prefix + extension; break; } - add_script += await utils.joins( - '\nadd_extension', - extension, - '"' + command + '"', - ext_prefix - ); + add_script += await utils.joins('\nadd_extension', extension, ext_prefix); }); return add_script + remove_script; } @@ -210,12 +196,10 @@ export async function addExtensionWindows( * * @param extension_csv * @param version - * @param pipe */ export async function addExtensionLinux( extension_csv: string, - version: string, - pipe: string + version: string ): Promise { const extensions: Array = await utils.extensionArray(extension_csv); let add_script = '\n'; @@ -224,8 +208,6 @@ export async function addExtensionLinux( const version_extension: string = version + extension; const [ext_name, ext_version]: string[] = extension.split('-'); const ext_prefix = await utils.getExtensionPrefix(ext_name); - const command_prefix = 'sudo $debconf_fix apt-get install -y php'; - let command = ''; switch (true) { // Match :extension case /^:/.test(ext_name): @@ -288,32 +270,20 @@ export async function addExtensionLinux( // match 8.0xdebug3...8.9xdebug3 case /^8\.[0-9]xdebug3$/.test(version_extension): extension = 'xdebug'; - command = command_prefix + version + '-' + extension + pipe; break; // match pdo extensions case /.*pdo[_-].*/.test(version_extension): extension = extension.replace(/pdo[_-]|3/, ''); add_script += '\nadd_pdo_extension ' + extension; return; - // match uopz - case /^(uopz)$/.test(extension): - command = command_prefix + '-' + extension + pipe; - break; // match sqlite case /^sqlite$/.test(extension): extension = 'sqlite3'; - command = command_prefix + version + '-' + extension + pipe; break; default: - command = command_prefix + version + '-' + extension + pipe; break; } - add_script += await utils.joins( - '\nadd_extension', - extension, - '"' + command + '"', - ext_prefix - ); + add_script += await utils.joins('\nadd_extension', extension, ext_prefix); }); return add_script + remove_script; } @@ -332,15 +302,15 @@ export async function addExtension( os_version: string, no_step = false ): Promise { - const pipe: string = await utils.suppressOutput(os_version); + const log: string = await utils.stepLog('Setup Extensions', os_version); let script = '\n'; switch (no_step) { case true: - script += (await utils.stepLog('Setup Extensions', os_version)) + pipe; + script += log + (await utils.suppressOutput(os_version)); break; case false: default: - script += await utils.stepLog('Setup Extensions', os_version); + script += log; break; } @@ -348,9 +318,9 @@ export async function addExtension( case 'win32': return script + (await addExtensionWindows(extension_csv, version)); case 'darwin': - return script + (await addExtensionDarwin(extension_csv, version, pipe)); + return script + (await addExtensionDarwin(extension_csv, version)); case 'linux': - return script + (await addExtensionLinux(extension_csv, version, pipe)); + return script + (await addExtensionLinux(extension_csv, version)); default: return await utils.log( 'Platform ' + os_version + ' is not supported', diff --git a/src/install.ts b/src/install.ts index 99c6aaa6..b68f08c1 100644 --- a/src/install.ts +++ b/src/install.ts @@ -21,6 +21,7 @@ export async function getScript( const name = 'setup-php'; const url = 'https://setup-php.com/support'; // taking inputs + process.env['fail_fast'] = await utils.getInput('fail-fast', false); const extension_csv: string = await utils.getInput('extensions', false); const ini_values_csv: string = await utils.getInput('ini-values', false); const coverage_driver: string = await utils.getInput('coverage', false); @@ -60,10 +61,7 @@ export async function run(): Promise { const tool = await utils.scriptTool(os_version); const script = os_version + (await utils.scriptExtension(os_version)); const location = await getScript(script, version, os_version); - const fail_fast = await utils.readEnv('fail-fast'); - await exec( - await utils.joins(tool, location, version, __dirname, fail_fast) - ); + await exec(await utils.joins(tool, location, version, __dirname)); } catch (error) { core.setFailed(error.message); } diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index 80d67130..2f3d56b7 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -1,46 +1,9 @@ -# Function to log start of a operation. -step_log() { - message=$1 - printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message" -} - -# Function to log result of a operation. -add_log() { - mark=$1 - subject=$2 - message=$3 - if [ "$mark" = "$tick" ]; then - 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; - fi -} - -# Function to log result of installing extension. -add_extension_log() { - extension=$1 - status=$2 - extension_name=$(echo "$extension" | cut -d '-' -f 1) - ( - check_extension "$extension_name" && add_log "$tick" "$extension_name" "$status" - ) || add_log "$cross" "$extension_name" "Could not install $extension on PHP $semver" -} - -# Function to read env inputs. -read_env() { - [[ -z "${update}" ]] && update='false' && UPDATE='false' || update="${update}" - [ "$update" = false ] && [[ -n ${UPDATE} ]] && update="${UPDATE}" - [[ -z "${runner}" ]] && runner='github' && RUNNER='github' || runner="${runner}" - [ "$runner" = false ] && [[ -n ${RUNNER} ]] && runner="${RUNNER}" -} - # Function to setup environment for self-hosted runners. -self_hosted_setup() { - if [[ $(command -v brew) == "" ]]; then +self_hosted_helper() { + if ! command -v brew >/dev/null; then step_log "Setup Brew" - curl "${curl_opts[@]}" https://raw.githubusercontent.com/Homebrew/install/master/install.sh | bash -s >/dev/null 2>&1 - add_log "$tick" "Brew" "Installed Homebrew" + curl "${curl_opts[@]:?}" https://raw.githubusercontent.com/Homebrew/install/master/install.sh | bash -s >/dev/null 2>&1 + add_log "${tick:?}" "Brew" "Installed Homebrew" fi } @@ -48,59 +11,27 @@ self_hosted_setup() { remove_extension() { extension=$1 if check_extension "$extension"; then - sudo sed -i '' "/$extension/d" "$ini_file" - sudo rm -rf "$scan_dir"/*"$extension"* >/dev/null 2>&1 - sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1 - (! check_extension "$extension" && add_log "$tick" ":$extension" "Removed") || - add_log "$cross" ":$extension" "Could not remove $extension on PHP $semver" + sudo sed -i '' "/$extension/d" "${ini_file:?}" + sudo rm -rf "${scan_dir:?}"/*"$extension"* "${ext_dir:?}"/"$extension".so >/dev/null 2>&1 + (! check_extension "$extension" && add_log "${tick:?}" ":$extension" "Removed") || + add_log "${cross:?}" ":$extension" "Could not remove $extension on PHP ${semver:?}" else - add_log "$tick" ":$extension" "Could not find $extension on PHP $semver" + add_log "${tick:?}" ":$extension" "Could not find $extension on PHP $semver" fi } -# Function to test if extension is loaded. -check_extension() { - extension=$1 - if [ "$extension" != "mysql" ]; then - php -m | grep -i -q -w "$extension" - else - php -m | grep -i -q "$extension" - fi -} - -# Function to get the PECL version. -get_pecl_version() { - extension=$1 - stability="$(echo "$2" | grep -m 1 -Eio "(alpha|beta|rc|snapshot|preview)")" - pecl_rest='https://pecl.php.net/rest/r/' - response=$(curl "${curl_opts[@]}" "$pecl_rest$extension"/allreleases.xml) - pecl_version=$(echo "$response" | grep -m 1 -Eio "(\d*\.\d*\.\d*$stability\d*)") - if [ ! "$pecl_version" ]; then - pecl_version=$(echo "$response" | grep -m 1 -Eo "(\d*\.\d*\.\d*)") - fi - echo "$pecl_version" -} - -# Function to install PECL extensions and accept default options -pecl_install() { - local extension=$1 - yes '' | sudo pecl install -f "$extension" >/dev/null 2>&1 -} - # Function to install a specific version of PECL extension. add_pecl_extension() { 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 - if ! check_extension "$extension" && [ -e "$ext_dir/$extension.so" ]; then - echo "$prefix=$ext_dir/$extension.so" >>"$ini_file" - fi ext_version=$(php -r "echo phpversion('$extension');") if [ "$ext_version" = "$pecl_version" ]; then - add_log "$tick" "$extension" "Enabled" + add_log "${tick:?}" "$extension" "Enabled" else remove_extension "$extension" >/dev/null 2>&1 pecl_install "$extension-$pecl_version" @@ -111,169 +42,65 @@ add_pecl_extension() { # Function to install a php extension from shivammathur/extensions tap. add_brew_extension() { extension=$1 - if ! brew tap | grep shivammathur/extensions; then - brew tap --shallow shivammathur/extensions + prefix=$2 + enable_extension "$extension" "$prefix" + if check_extension "$extension"; then + add_log "${tick:?}" "$extension" "Enabled" + else + if ! brew tap | grep -q shivammathur/extensions; then + brew tap --shallow shivammathur/extensions >/dev/null 2>&1 + fi + brew install "$extension@$version" >/dev/null 2>&1 + sudo cp "$(brew --prefix)/opt/$extension@$version/$extension.so" "$ext_dir" + add_extension_log "$extension" "Installed and enabled" fi - brew install "$extension@$version" - sudo cp "$(brew --prefix)/opt/$extension@$version/$extension.so" "$ext_dir" } # Function to setup extensions add_extension() { extension=$1 - install_command=$2 - prefix=$3 - if ! check_extension "$extension" && [ -e "$ext_dir/$extension.so" ]; then - echo "$prefix=$ext_dir/$extension.so" >>"$ini_file" && add_log "$tick" "$extension" "Enabled" - elif check_extension "$extension"; then - add_log "$tick" "$extension" "Enabled" - elif ! check_extension "$extension"; then - eval "$install_command" >/dev/null 2>&1 && - if [[ "$version" =~ $old_versions ]]; then echo "$prefix=$ext_dir/$extension.so" >>"$ini_file"; fi + prefix=$2 + enable_extension "$extension" "$prefix" + if check_extension "$extension"; then + add_log "${tick:?}" "$extension" "Enabled" + else + [[ "$version" =~ 5.[4-5] ]] && [ "$extension" = "imagick" ] && brew install pkg-config imagemagick >/dev/null 2>&1 + 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" fi } -# Function to setup pre-release extensions using PECL. -add_unstable_extension() { - extension=$1 - stability=$2 - prefix=$3 - pecl_version=$(get_pecl_version "$extension" "$stability") - add_pecl_extension "$extension" "$pecl_version" "$prefix" -} - -# Function to configure composer -configure_composer() { - tool_path=$1 - sudo ln -sf "$tool_path" "$tool_path.phar" - php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}" - if [ $? -eq 1 ]; then - add_log "$cross" "composer" "Could not download composer" - exit 1 - fi - composer -q global config process-timeout 0 - echo "$composer_bin" >> "$GITHUB_PATH" - if [ -n "$COMPOSER_TOKEN" ]; then - composer -q global config github-oauth.github.com "$COMPOSER_TOKEN" - fi -} - -# Function to extract tool version. -get_tool_version() { - tool=$1 - param=$2 - version_regex="[0-9]+((\.{1}[0-9]+)+)(\.{0})(-[a-zA-Z0-9]+){0,1}" - if [ "$tool" = "composer" ]; then - if [ "$param" != "snapshot" ]; then - grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex" - else - trunk=$(grep -Ea "const\sBRANCH_ALIAS_VERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex") - commit=$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "[a-zA-z0-9]+" | tail -n 1) - echo "$trunk+$commit" - fi - else - $tool "$param" 2>/dev/null | sed -Ee "s/[Cc]omposer(.)?$version_regex//g" | grep -Eo "$version_regex" | head -n 1 - fi -} - -# Function to setup a remote tool. -add_tool() { - url=$1 - tool=$2 - ver_param=$3 - tool_path="$tool_path_dir/$tool" - if [ ! -e "$tool_path" ]; then - rm -rf "$tool_path" - fi - if [ "$tool" = "composer" ]; then - IFS="," read -r -a urls <<< "$url" - status_code=$(sudo curl -f -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[0]}") || - status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[1]}") - else - status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") - if [ "$status_code" != "200" ] && [[ "$url" =~ .*github.com.*releases.*latest.* ]]; then - url=$(echo $url | sed -e "s|releases/latest/download|releases/download/$(curl "${curl_opts[@]}" "$(echo "$url" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "$url" | sed -e "s/.*\///")" | cut -d '/' -f 1)|") - status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") - fi - fi - if [ "$status_code" = "200" ]; then - sudo chmod a+x "$tool_path" - if [ "$tool" = "composer" ]; then - configure_composer "$tool_path" - elif [ "$tool" = "phan" ]; then - add_extension fileinfo "pecl_install fileinfo" extension >/dev/null 2>&1 - add_extension ast "pecl_install ast" extension >/dev/null 2>&1 - elif [ "$tool" = "phive" ]; then - add_extension curl "pecl_install curl" extension >/dev/null 2>&1 - add_extension mbstring "pecl_install mbstring" extension >/dev/null 2>&1 - add_extension xml "pecl_install xml" extension >/dev/null 2>&1 - elif [ "$tool" = "cs2pr" ]; then - sudo sed -i '' 's/exit(9)/exit(0)/' "$tool_path" - tr -d '\r' <"$tool_path" | sudo tee "$tool_path.tmp" >/dev/null 2>&1 && sudo mv "$tool_path.tmp" "$tool_path" - sudo chmod a+x "$tool_path" - elif [ "$tool" = "wp-cli" ]; then - sudo cp -p "$tool_path" "$tool_path_dir"/wp - fi - tool_version=$(get_tool_version "$tool" "$ver_param") - add_log "$tick" "$tool" "Added $tool $tool_version" - else - add_log "$cross" "$tool" "Could not setup $tool" - fi -} - -# Function to add a tool using composer. -add_composertool() { - tool=$1 - release=$2 - prefix=$3 - ( - composer global require "$prefix$release" >/dev/null 2>&1 && - json=$(grep "$prefix$tool" /Users/"$USER"/.composer/composer.json) && - tool_version=$(get_tool_version 'echo' "$json") && - add_log "$tick" "$tool" "Added $tool $tool_version" - ) || add_log "$cross" "$tool" "Could not setup $tool" - if [ -e "$composer_bin/composer" ]; then - sudo cp -p "$tool_path_dir/composer" "$composer_bin" - fi -} - # Function to handle request to add phpize and php-config. add_devtools() { tool=$1 - add_log "$tick" "$tool" "Added $tool $semver" -} - -# Function to configure PECL -configure_pecl() { - for tool in pear pecl; do - sudo "$tool" config-set php_ini "$ini_file" - sudo "$tool" channel-update "$tool".php.net - done + add_log "${tick:?}" "$tool" "Added $tool $semver" } # Function to handle request to add PECL. add_pecl() { pecl_version=$(get_tool_version "pecl" "version") - add_log "$tick" "PECL" "Found PECL $pecl_version" + add_log "${tick:?}" "PECL" "Found PECL $pecl_version" } # Function to update dependencies. update_dependencies() { - if [[ "$version" =~ $nightly_versions ]] && [ "$runner" != "self-hosted" ]; then + if [[ "$version" =~ ${nightly_versions:?} ]] && [ "${runner:?}" != "self-hosted" ]; then + tap_dir="$(brew --prefix)/Homebrew/Library/Taps" while read -r formula; do - curl -o "$(brew --prefix)/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/$formula.rb" "${curl_opts[@]}" "https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/$formula.rb" & - to_wait+=( $! ) - done < "$(brew --prefix)/Homebrew/Library/Taps/shivammathur/homebrew-php/.github/deps/${ImageOS:?}_${ImageVersion:?}" + curl -o "$tap_dir/homebrew/homebrew-core/Formula/$formula.rb" "${curl_opts[@]:?}" "https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/$formula.rb" & + to_wait+=($!) + done <"$tap_dir/shivammathur/homebrew-php/.github/deps/${ImageOS:?}_${ImageVersion:?}" wait "${to_wait[@]}" fi } -# Function to setup PHP 5.6 and newer. -setup_php() { +# Function to setup PHP 5.6 and newer using Homebrew. +add_php() { action=$1 - export HOMEBREW_NO_INSTALL_CLEANUP=TRUE - brew tap --shallow shivammathur/homebrew-php + if ! brew tap | grep -q shivammathur/php; then + brew tap --shallow shivammathur/php + fi update_dependencies if brew list php@"$version" 2>/dev/null | grep -q "Error" && [ "$action" != "upgrade" ]; then brew unlink php@"$version" @@ -283,51 +110,42 @@ setup_php() { brew link --force --overwrite php@"$version" } +# Function to Setup PHP +setup_php() { + step_log "Setup PHP" + existing_version=$(php-config --version 2>/dev/null | cut -c 1-3) + if [[ "$version" =~ ${old_versions:?} ]]; then + curl "${curl_opts[@]:?}" "${github:?}/php5-darwin/releases/latest/download/install.sh" | bash -s "${version/./}" >/dev/null 2>&1 + status="Installed" + elif [ "$existing_version" != "$version" ]; then + add_php "install" >/dev/null 2>&1 + status="Installed" + elif [ "$existing_version" = "$version" ] && [ "${update:?}" = "true" ]; then + add_php "upgrade" >/dev/null 2>&1 + status="Updated to" + else + status="Found" + 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:?}" + echo -e "date.timezone=UTC\nmemory_limit=-1" >>"$ini_file" + 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 ' ') + if [[ ! "$version" =~ $old_versions ]]; then configure_pecl >/dev/null 2>&1; fi + sudo cp "$dist"/../src/configs/*.json "$RUNNER_TOOL_CACHE/" + add_log "$tick" "PHP" "$status PHP $semver" +} + # Variables -tick="✓" -cross="✗" version=$1 dist=$2 -fail_fast=$3 -nodot_version=${1/./} -nightly_versions="8.[0-1]" -old_versions="5.[3-5]" -composer_bin="/Users/$USER/.composer/vendor/bin" -tool_path_dir="/usr/local/bin" -curl_opts=(-sL) -existing_version=$(php-config --version 2>/dev/null | cut -c 1-3) +export HOMEBREW_NO_INSTALL_CLEANUP=1 +export HOMEBREW_NO_AUTO_UPDATE=1 +# shellcheck source=. +. "${dist}"/../src/scripts/common.sh read_env -if [ "$runner" = "self-hosted" ]; then - if [[ "$version" =~ $old_versions ]]; then - add_log "$cross" "PHP" "PHP $version is not supported on self-hosted runner" - exit 1 - else - self_hosted_setup >/dev/null 2>&1 - fi -fi - -# Setup PHP -step_log "Setup PHP" -if [[ "$version" =~ $old_versions ]]; then - curl "${curl_opts[@]}" https://github.com/shivammathur/php5-darwin/releases/latest/download/install.sh | bash -s "$nodot_version" >/dev/null 2>&1 - status="Installed" -elif [ "$existing_version" != "$version" ]; then - setup_php "install" >/dev/null 2>&1 - status="Installed" -elif [ "$existing_version" = "$version" ] && [ "$update" = "true" ]; then - setup_php "upgrade" >/dev/null 2>&1 - status="Updated to" -else - status="Found" -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" -echo -e "date.timezone=UTC\nmemory_limit=-1" >>"$ini_file" -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" "/Users/$USER/.composer" -semver=$(php -v | head -n 1 | cut -f 2 -d ' ') -if [[ ! "$version" =~ $old_versions ]]; then configure_pecl >/dev/null 2>&1; fi -sudo cp "$dist"/../src/configs/*.json "$RUNNER_TOOL_CACHE/" -add_log "$tick" "PHP" "$status PHP $semver" +self_hosted_setup +setup_php diff --git a/src/scripts/ext/oci.sh b/src/scripts/ext/oci.sh index 5b462501..dce1c16c 100644 --- a/src/scripts/ext/oci.sh +++ b/src/scripts/ext/oci.sh @@ -83,16 +83,9 @@ patch_pdo_oci_config() { add_dependencies() { if [ "$os" = 'Linux' ]; then if [ "${runner:?}" = "self-hosted" ]; then - if ! [[ ${version:?} =~ $nightly_versions ]]; then - ${apt_install:?} --no-upgrade --no-install-recommends autoconf automake libaio-dev gcc g++ php"$version"-dev - else - ${apt_install:?} --no-upgrade --no-install-recommends autoconf automake libaio-dev gcc g++ - fi - else - ! [[ ${version:?} =~ $nightly_versions ]] && update_lists && ${apt_install:?} --no-upgrade --no-install-recommends php"$version"-dev + ${apt_install:?} --no-upgrade --no-install-recommends autoconf automake libaio-dev gcc g++ fi - sudo update-alternatives --set php-config /usr/bin/php-config"$version" - sudo update-alternatives --set phpize /usr/bin/phpize"$version" + ! [[ ${version:?} =~ $nightly_versions ]] && add_devtools phpize fi } diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 071dce37..fdcfa9c4 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -1,39 +1,10 @@ -# Function to log start of a operation. -step_log() { - message=$1 - printf "\n\033[90;1m==> \033[0m\033[37;1m%s\033[0m\n" "$message" -} - -# Function to log result of a operation. -add_log() { - mark=$1 - subject=$2 - message=$3 - if [ "$mark" = "$tick" ]; then - 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; +# Function to setup environment for self-hosted runners. +self_hosted_helper() { + if ! command -v apt-fast >/dev/null; then + sudo ln -sf /usr/bin/apt-get /usr/bin/apt-fast fi -} - -# Function to log result of installing extension. -add_extension_log() { - extension=$1 - status=$2 - extension_name=$(echo "$extension" | cut -d '-' -f 1) - ( - check_extension "$extension_name" && add_log "$tick" "$extension_name" "$status" - ) || add_log "$cross" "$extension_name" "Could not install $extension on PHP $semver" -} - -# Function to read env inputs. -read_env() { - . /etc/lsb-release - [[ -z "${update}" ]] && update='false' && UPDATE='false' || update="${update}" - [ "$update" = false ] && [[ -n ${UPDATE} ]] && update="${UPDATE}" - [[ -z "${runner}" ]] && runner='github' && RUNNER='github' || runner="${runner}" - [ "$runner" = false ] && [[ -n ${RUNNER} ]] && runner="${RUNNER}" + install_packages curl make software-properties-common unzip + add_ppa } # Function to backup and cleanup package lists. @@ -64,70 +35,24 @@ update_lists() { [ "$DISTRIB_RELEASE" = "20.04" ] && add_ppa >/dev/null 2>&1 cleanup_lists sudo "$debconf_fix" apt-get update >/dev/null 2>&1 - echo '' | sudo tee "/tmp/setup_php" >/dev/null 2>&1 + echo '' | sudo tee /tmp/setup_php >/dev/null 2>&1 fi } -# Function to setup environment for self-hosted runners. -self_hosted_setup() { - if ! command -v apt-fast >/dev/null; then - sudo ln -sf /usr/bin/apt-get /usr/bin/apt-fast - fi - update_lists && $apt_install curl make software-properties-common unzip - add_ppa -} - -# Function to configure PECL. -configure_pecl() { - if [ "$pecl_config" = "false" ] && [ -e /usr/bin/pecl ]; then - - for tool in pear pecl; do - sudo "$tool" config-set php_ini "$scan_dir"/99-pecl.ini - sudo "$tool" channel-update "$tool".php.net - done - pecl_config="true" - fi -} - -# Function to get the PECL version of an extension. -get_pecl_version() { - extension=$1 - stability="$(echo "$2" | grep -m 1 -Eio "(alpha|beta|rc|snapshot|preview)")" - pecl_rest='https://pecl.php.net/rest/r/' - response=$(curl "${curl_opts[@]}" "$pecl_rest$extension"/allreleases.xml) - pecl_version=$(echo "$response" | grep -m 1 -Pio "(\d*\.\d*\.\d*$stability\d*)") - if [ ! "$pecl_version" ]; then - pecl_version=$(echo "$response" | grep -m 1 -Po "(\d*\.\d*\.\d*)") - fi - echo "$pecl_version" -} - -# Function to install PECL extensions and accept default options -pecl_install() { - local extension=$1 - yes '' | sudo pecl install -f "$extension" >/dev/null 2>&1 -} - -# Function to check if an extension is loaded. -check_extension() { - extension=$1 - if [ "$extension" != "mysql" ]; then - php -m | grep -i -q -w "$extension" - else - php -m | grep -i -q "$extension" - fi +# Function to install a package +install_packages() { + packages=("$@") + $apt_install "${packages[@]}" >/dev/null 2>&1 || update_lists && $apt_install "${packages[@]}" >/dev/null 2>&1 } # Function to delete extensions. delete_extension() { extension=$1 - sudo sed -i "/$extension/d" "$ini_file" - sudo sed -i "/$extension/d" "$pecl_file" - sudo rm -rf "$scan_dir"/*"$extension"* >/dev/null 2>&1 - sudo rm -rf "$ext_dir"/"$extension".so >/dev/null 2>&1 - if [ "$runner" = "self-hosted" ]; then - $apt_remove "php-$extension" >/dev/null 2>&1 || true - $apt_remove "php$version-$extension" >/dev/null 2>&1 || true + sudo sed -i "/$extension/d" "${ini_file:?}" + sudo sed -i "/$extension/d" "${pecl_file:?}" + sudo rm -rf "${scan_dir:?}"/*"$extension"* "${ext_dir:?}"/"$extension".so >/dev/null 2>&1 + if [ "${runner:?}" = "self-hosted" ]; then + $apt_remove "php-$extension" "php$version-$extension" >/dev/null 2>&1 || true fi } @@ -135,21 +60,14 @@ delete_extension() { remove_extension() { extension=$1 if check_extension "$extension"; then - if [[ ! "$version" =~ $old_versions ]] && [ -e /etc/php/"$version"/mods-available/"$extension".ini ]; then + if [[ ! "$version" =~ ${old_versions:?} ]] && [ -e /etc/php/"$version"/mods-available/"$extension".ini ]; then sudo phpdismod -v "$version" "$extension" >/dev/null 2>&1 fi delete_extension "$extension" - (! check_extension "$extension" && add_log "$tick" ":$extension" "Removed") || - add_log "$cross" ":$extension" "Could not remove $extension on PHP $semver" + (! check_extension "$extension" && add_log "${tick:?}" ":$extension" "Removed") || + add_log "${cross:?}" ":$extension" "Could not remove $extension on PHP ${semver:?}" else - add_log "$tick" ":$extension" "Could not find $extension on PHP $semver" - fi -} - -# Function to enable existing extensions. -enable_extension() { - if ! check_extension "$1" && [ -e "$ext_dir/$1.so" ]; then - echo "$2=$1.so" >>"$pecl_file" + add_log "${tick:?}" ":$extension" "Could not find $extension on PHP $semver" fi } @@ -157,19 +75,19 @@ enable_extension() { add_pdo_extension() { pdo_ext="pdo_$1" if check_extension "$pdo_ext"; then - add_log "$tick" "$pdo_ext" "Enabled" + add_log "${tick:?}" "$pdo_ext" "Enabled" else - read -r ext ext_name <<< "$1 $1" + ext=$1; ext_name=$1; sudo rm -rf "$scan_dir"/*pdo.ini >/dev/null 2>&1 - if ! check_extension "pdo" 2>/dev/null; then echo "extension=pdo.so" >> "$ini_file"; fi + if ! check_extension "pdo" 2>/dev/null; then echo "extension=pdo.so" >>"$ini_file"; fi if [ "$ext" = "mysql" ]; then enable_extension "mysqlnd" "extension" ext_name="mysqli" elif [ "$ext" = "sqlite" ]; then - read -r ext ext_name <<< "sqlite3 sqlite3" + ext="sqlite3"; ext_name="sqlite3"; fi - add_extension "$ext_name" "$apt_install php$version-$ext" "extension" >/dev/null 2>&1 - add_extension "$pdo_ext" "pecl_install $pdo_ext" "extension" >/dev/null 2>&1 + add_extension "$ext_name" "extension" >/dev/null 2>&1 + add_extension "$pdo_ext" "extension" >/dev/null 2>&1 add_extension_log "$pdo_ext" "Enabled" fi } @@ -177,20 +95,17 @@ add_pdo_extension() { # Function to add extensions. add_extension() { extension=$1 - install_command=$2 - prefix=$3 + prefix=$2 enable_extension "$extension" "$prefix" if check_extension "$extension"; then - add_log "$tick" "$extension" "Enabled" + add_log "${tick:?}" "$extension" "Enabled" else if [[ "$version" =~ 5.[4-5] ]]; then - install_command="update_lists && ${install_command/5\.[4-5]-$extension/5-$extension=$release_version}" - fi - if [[ "$version" =~ $nightly_versions ]]; then + install_packages "php5-$extension=$release_version" + elif [[ "$version" =~ ${nightly_versions:?} ]]; then pecl_install "$extension" else - eval "$install_command" >/dev/null 2>&1 || - (update_lists && eval "$install_command" >/dev/null 2>&1) || pecl_install "$extension" + install_packages "php$version-$extension" || pecl_install "$extension" fi add_extension_log "$extension" "Installed and enabled" fi @@ -205,12 +120,10 @@ add_pecl_extension() { if [[ $pecl_version =~ .*(alpha|beta|rc|snapshot|preview).* ]]; then pecl_version=$(get_pecl_version "$extension" "$pecl_version") fi - if ! check_extension "$extension" && [ -e "$ext_dir/$extension.so" ]; then - echo "$prefix=$ext_dir/$extension.so" >>"$pecl_file" - fi + enable_extension "$extension" "$prefix" ext_version=$(php -r "echo phpversion('$extension');") if [ "$ext_version" = "$pecl_version" ]; then - add_log "$tick" "$extension" "Enabled" + add_log "${tick:?}" "$extension" "Enabled" else delete_extension "$extension" pecl_install "$extension-$pecl_version" @@ -218,15 +131,6 @@ add_pecl_extension() { fi } -# Function to pre-release extensions using PECL. -add_unstable_extension() { - extension=$1 - stability=$2 - prefix=$3 - pecl_version=$(get_pecl_version "$extension" "$stability") - add_pecl_extension "$extension" "$pecl_version" "$prefix" -} - # Function to install extension from source add_extension_from_source() { extension=$1 @@ -237,128 +141,37 @@ add_extension_from_source() { ( add_devtools phpize delete_extension "$extension" - curl -o /tmp/"$extension".tar.gz "${curl_opts[@]}" https://github.com/"$repo"/archive/"$release".tar.gz + curl -o /tmp/"$extension".tar.gz "${curl_opts[@]:?}" https://github.com/"$repo"/archive/"$release".tar.gz tar xf /tmp/"$extension".tar.gz -C /tmp cd /tmp/"$extension-$release" || exit 1 - phpize && ./configure "$args" && make && sudo make install + phpize && ./configure "$args" && make -j"$(nproc)" && sudo make install enable_extension "$extension" "$prefix" ) >/dev/null 2>&1 add_extension_log "$extension-$release" "Installed and enabled" } -# Function to configure composer -configure_composer() { - tool_path=$1 - sudo ln -sf "$tool_path" "$tool_path.phar" - php -r "try {\$p=new Phar('$tool_path.phar', 0);exit(0);} catch(Exception \$e) {exit(1);}" - if [ $? -eq 1 ]; then - add_log "$cross" "composer" "Could not download composer" - exit 1; - fi - composer -q global config process-timeout 0 - echo "$composer_bin" >> "$GITHUB_PATH" - if [ -n "$COMPOSER_TOKEN" ]; then - composer -q global config github-oauth.github.com "$COMPOSER_TOKEN" - fi -} - -# Function to extract tool version. -get_tool_version() { - tool=$1 - param=$2 - version_regex="[0-9]+((\.{1}[0-9]+)+)(\.{0})(-[a-zA-Z0-9]+){0,1}" - if [ "$tool" = "composer" ]; then - if [ "$param" != "snapshot" ]; then - grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex" - else - trunk=$(grep -Ea "const\sBRANCH_ALIAS_VERSION" "$tool_path_dir/composer" | grep -Eo "$version_regex") - commit=$(grep -Ea "const\sVERSION" "$tool_path_dir/composer" | grep -Eo "[a-zA-z0-9]+" | tail -n 1) - echo "$trunk+$commit" - fi - else - $tool "$param" 2>/dev/null | sed -Ee "s/[Cc]omposer(.)?$version_regex//g" | grep -Eo "$version_regex" | head -n 1 - fi -} - -# Function to setup a remote tool. -add_tool() { - url=$1 - tool=$2 - ver_param=$3 - tool_path="$tool_path_dir/$tool" - if [ ! -e "$tool_path" ]; then - rm -rf "$tool_path" - fi - if [ "$tool" = "composer" ]; then - IFS="," read -r -a urls <<< "$url" - status_code=$(sudo curl -f -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[0]}") || - status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "${urls[1]}") - else - status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") - if [ "$status_code" != "200" ] && [[ "$url" =~ .*github.com.*releases.*latest.* ]]; then - url=$(echo $url | sed -e "s|releases/latest/download|releases/download/$(curl "${curl_opts[@]}" "$(echo "$url" | cut -d '/' -f '1-5')/releases" | grep -Eo -m 1 "([0-9]+\.[0-9]+\.[0-9]+)/$(echo "$url" | sed -e "s/.*\///")" | cut -d '/' -f 1)|") - status_code=$(sudo curl -w "%{http_code}" -o "$tool_path" "${curl_opts[@]}" "$url") - fi - fi - if [ "$status_code" = "200" ]; then - sudo chmod a+x "$tool_path" - if [ "$tool" = "composer" ]; then - configure_composer "$tool_path" - elif [ "$tool" = "cs2pr" ]; then - sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path" - elif [ "$tool" = "phan" ]; then - add_extension fileinfo "$apt_install php$version-fileinfo" extension >/dev/null 2>&1 - add_extension ast "$apt_install php-ast" extension >/dev/null 2>&1 - elif [ "$tool" = "phive" ]; then - add_extension curl "$apt_install php$version-curl" extension >/dev/null 2>&1 - add_extension mbstring "$apt_install php$version-mbstring" extension >/dev/null 2>&1 - add_extension xml "$apt_install php$version-xml" extension >/dev/null 2>&1 - elif [ "$tool" = "wp-cli" ]; then - sudo cp -p "$tool_path" "$tool_path_dir"/wp - fi - tool_version=$(get_tool_version "$tool" "$ver_param") - add_log "$tick" "$tool" "Added $tool $tool_version" - else - add_log "$cross" "$tool" "Could not setup $tool" - fi -} - -# Function to setup a tool using composer. -add_composertool() { - tool=$1 - release=$2 - prefix=$3 - ( - composer global require "$prefix$release" >/dev/null 2>&1 && - json=$(grep "$prefix$tool" /home/"$USER"/.composer/composer.json) && - tool_version=$(get_tool_version 'echo' "$json") && - add_log "$tick" "$tool" "Added $tool $tool_version" - ) || add_log "$cross" "$tool" "Could not setup $tool" - if [ -e "$composer_bin/composer" ]; then - sudo cp -p "$tool_path_dir/composer" "$composer_bin" - fi -} - # Function to setup phpize and php-config. add_devtools() { tool=$1 - if ! [ -e "/usr/bin/phpize$version" ] || ! [ -e "/usr/bin/php-config$version" ]; then - update_lists && $apt_install php"$version"-dev php"$version"-xml >/dev/null 2>&1 + if ! command -v "$tool$version" >/dev/null; then + install_packages "php$version-dev" "php$version-xml" fi sudo update-alternatives --set php-config /usr/bin/php-config"$version" >/dev/null 2>&1 sudo update-alternatives --set phpize /usr/bin/phpize"$version" >/dev/null 2>&1 - configure_pecl >/dev/null 2>&1 - add_log "$tick" "$tool" "Added $tool $semver" + if command -v pecl >/dev/null; then + configure_pecl >/dev/null 2>&1 + fi + add_log "${tick:?}" "$tool" "Added $tool $semver" } # Function to setup the nightly build from shivammathur/php-builder setup_nightly() { - curl "${curl_opts[@]}" "$github"/php-builder/releases/latest/download/install.sh | bash -s "$runner" "$version" + curl "${curl_opts[@]:?}" "${github:?}"/php-builder/releases/latest/download/install.sh | bash -s "$runner" "$version" } # Function to setup PHP 5.3, PHP 5.4 and PHP 5.5. setup_old_versions() { - curl "${curl_opts[@]}" "$github"/php5-ubuntu/releases/latest/download/install.sh | bash -s "$version" + curl "${curl_opts[@]:?}" "${github:?}"/php5-ubuntu/releases/latest/download/install.sh | bash -s "$version" configure_pecl release_version=$(php -v | head -n 1 | cut -d' ' -f 2) } @@ -366,48 +179,40 @@ setup_old_versions() { # Function to add PECL. add_pecl() { add_devtools phpize >/dev/null 2>&1 - if [ ! -e /usr/bin/pecl ]; then - $apt_install php-pear >/dev/null 2>&1 || update_lists && $apt_install php-pear >/dev/null 2>&1 + if ! command -v pecl >/dev/null; then + install_packages php-pear fi configure_pecl >/dev/null 2>&1 pecl_version=$(get_tool_version "pecl" "version") - add_log "$tick" "PECL" "Added PECL $pecl_version" + add_log "${tick:?}" "PECL" "Added PECL $pecl_version" } # Function to switch versions of PHP binaries. switch_version() { for tool in pear pecl php phar phar.phar php-cgi php-config phpize phpdbg; do if [ -e "/usr/bin/$tool$version" ]; then - sudo update-alternatives --set $tool /usr/bin/"$tool$version" + sudo update-alternatives --set $tool /usr/bin/"$tool$version" & + to_wait+=( $! ) fi done -} - -# Function to get PHP version in semver format. -php_semver() { - if ! [[ "$version" =~ $nightly_versions ]]; then - php"$version" -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-' - else - php -v | head -n 1 | cut -f 2 -d ' ' - fi + wait "${to_wait[@]}" } # Function to install packaged PHP add_packaged_php() { if [ "$runner" = "self-hosted" ] || [ "${use_package_cache:-true}" = "false" ]; then update_lists - IFS=' ' read -r -a packages <<< "$(echo "cli curl mbstring xml intl" | sed "s/[^ ]*/php$version-&/g")" + IFS=' ' read -r -a packages <<<"$(echo "cli curl mbstring xml intl" | sed "s/[^ ]*/php$version-&/g")" $apt_install "${packages[@]}" else - curl "${curl_opts[@]}" "$github"/php-ubuntu/releases/latest/download/install.sh | bash -s "$version" + curl "${curl_opts[@]:?}" "${github:?}"/php-ubuntu/releases/latest/download/install.sh | bash -s "$version" fi } # Function to update PHP. update_php() { initial_version=$(php_semver) - use_package_cache="false" - add_packaged_php + use_package_cache="false" add_packaged_php updated_version=$(php_semver) if [ "$updated_version" != "$initial_version" ]; then status="Updated to" @@ -418,9 +223,9 @@ update_php() { # Function to install PHP. add_php() { - if [[ "$version" =~ $nightly_versions ]]; then + if [[ "$version" =~ ${nightly_versions:?} ]]; then setup_nightly - elif [[ "$version" =~ $old_versions ]]; then + elif [[ "$version" =~ ${old_versions:?} ]]; then setup_old_versions else add_packaged_php @@ -428,68 +233,52 @@ add_php() { status="Installed" } -# Variables -tick="✓" -cross="✗" -pecl_config="false" -version=$1 -dist=$2 -fail_fast=$3 -nightly_versions="8.[0-1]" -old_versions="5.[3-5]" -debconf_fix="DEBIAN_FRONTEND=noninteractive" -github="https://github.com/shivammathur" -apt_install="sudo $debconf_fix apt-get install -y" -apt_remove="sudo $debconf_fix apt-get remove -y" -composer_bin="/home/$USER/.composer/vendor/bin" -tool_path_dir="/usr/local/bin" -curl_opts=(-sL) -existing_version=$(php-config --version 2>/dev/null | cut -c 1-3) - -read_env -if [ "$runner" = "self-hosted" ]; then - if [[ "$version" =~ $old_versions ]]; then - add_log "$cross" "PHP" "PHP $version is not supported on self-hosted runner" - exit 1 - else - self_hosted_setup >/dev/null 2>&1 - fi -fi - -# Setup PHP -step_log "Setup PHP" -sudo mkdir -m 777 -p /home/"$USER"/.composer /var/run /run/php -if [ "$existing_version" != "$version" ]; then - if [ ! -e "/usr/bin/php$version" ]; then - add_php >/dev/null 2>&1 +# Function to Setup PHP +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 + if [ ! -e "/usr/bin/php$version" ]; then + add_php >/dev/null 2>&1 + else + if [ "${update:?}" = "true" ]; then + update_php >/dev/null 2>&1 + else + status="Switched to" + fi + fi + if ! [[ "$version" =~ ${old_versions:?}|${nightly_versions:?} ]]; then + switch_version >/dev/null 2>&1 + fi else if [ "$update" = "true" ]; then update_php >/dev/null 2>&1 else - status="Switched to" + status="Found" fi fi - if ! [[ "$version" =~ $old_versions ]]; then - switch_version >/dev/null 2>&1 - fi -else - if [ "$update" = "true" ]; then - update_php >/dev/null 2>&1 - else - status="Found" - if [[ "$version" =~ $nightly_versions ]]; then - switch_version >/dev/null 2>&1 - fi - fi -fi + semver=$(php_semver) + ext_dir=$(php -i | grep "extension_dir => /" | sed -e "s|.*=> s*||") + scan_dir=$(php --ini | grep additional | sed -e "s|.*: s*||") + ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g") + pecl_file="$scan_dir"/99-pecl.ini + echo '' | sudo tee "$pecl_file" >/dev/null 2>&1 + sudo rm -rf /usr/local/bin/phpunit >/dev/null 2>&1 + sudo chmod 777 "$ini_file" "$pecl_file" "${tool_path_dir:?}" + sudo cp "$dist"/../src/configs/*.json "$RUNNER_TOOL_CACHE/" + add_log "${tick:?}" "PHP" "$status PHP $semver" +} -semver=$(php_semver) -ext_dir=$(php -i | grep "extension_dir => /" | sed -e "s|.*=> s*||") -scan_dir=$(php --ini | grep additional | sed -e "s|.*: s*||") -ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g") -pecl_file="$scan_dir"/99-pecl.ini -echo '' | sudo tee "$pecl_file" >/dev/null 2>&1 -sudo rm -rf /usr/local/bin/phpunit >/dev/null 2>&1 -sudo chmod 777 "$ini_file" "$pecl_file" "$tool_path_dir" -sudo cp "$dist"/../src/configs/*.json "$RUNNER_TOOL_CACHE/" -add_log "$tick" "PHP" "$status PHP $semver" +# Variables +version=$1 +dist=$2 +debconf_fix="DEBIAN_FRONTEND=noninteractive" +apt_install="sudo $debconf_fix apt-get install -y" +apt_remove="sudo $debconf_fix apt-get remove -y" + +# shellcheck source=. +. "${dist}"/../src/scripts/common.sh +. /etc/lsb-release +read_env +self_hosted_setup +setup_php diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index b730e5cc..1e7b9e8b 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -97,7 +97,7 @@ Function Get-CleanPSProfile { } # Function to install a powershell package from GitHub. -Function Install-GitHubPackage() { +Function Install-PSPackage() { param( [Parameter(Position = 0, Mandatory = $true)] $package, @@ -334,12 +334,15 @@ $cross = ([char]10007) $php_dir = 'C:\tools\php' $ext_dir = "$php_dir\ext" $bin_dir = $php_dir +$bintray = 'https://dl.bintray.com/shivammathur/php' $github = 'https://github.com' $composer_bin = "$env:APPDATA\Composer\vendor\bin" $current_profile = "$env:TEMP\setup-php.ps1" $ProgressPreference = 'SilentlyContinue' $nightly_version = '8.[0-9]' $cert_source='CurrentUser' +$enable_extensions = ('openssl', 'curl', 'mbstring') +$wc = New-Object System.Net.WebClient $arch = 'x64' if(-not([Environment]::Is64BitOperatingSystem) -or $version -lt '7.0') { @@ -378,7 +381,7 @@ if($env:RUNNER -eq 'self-hosted') { Add-Printf >$null 2>&1 Step-Log "Setup PhpManager" -Install-GitHubPackage PhpManager PhpManager\PhpManager "$github/mlocati/powershell-phpmanager/releases/latest/download/PhpManager.zip" >$null 2>&1 +Install-PSPackage PhpManager PhpManager\PhpManager "$github/mlocati/powershell-phpmanager/releases/latest/download/PhpManager.zip" >$null 2>&1 Add-Log $tick "PhpManager" "Installed" Step-Log "Setup PHP" @@ -391,11 +394,11 @@ if (Test-Path -LiteralPath $php_dir -PathType Container) { $status = "Installed" if ($null -eq $installed -or -not("$($installed.Version).".StartsWith(($version -replace '^(\d+(\.\d+)*).*', '$1.'))) -or $ts -ne $installed.ThreadSafe) { if ($version -lt '7.0' -and (Get-InstalledModule).Name -notcontains 'VcRedist') { - Install-GitHubPackage VcRedist VcRedist-main\VcRedist\VcRedist "$github/aaronparker/VcRedist/archive/main.zip" >$null 2>&1 + Install-PSPackage VcRedist VcRedist-main\VcRedist\VcRedist "$github/aaronparker/VcRedist/archive/main.zip" >$null 2>&1 } if ($version -match $nightly_version) { - Invoke-WebRequest -UseBasicParsing -Uri https://dl.bintray.com/shivammathur/php/Install-PhpNightly.ps1 -OutFile $php_dir\Install-PhpNightly.ps1 > $null 2>&1 - & $php_dir\Install-PhpNightly.ps1 -Architecture $arch -ThreadSafe $ts -Path $php_dir -Version $version > $null 2>&1 + $wc.DownloadFile("$bintray/Get-PhpNightly.ps1", "$php_dir\Get-PhpNightly.ps1") > $null 2>&1 + & $php_dir\Get-PhpNightly.ps1 -Architecture $arch -ThreadSafe $ts -Path $php_dir -Version $version > $null 2>&1 } else { Install-Php -Version $version -Architecture $arch -ThreadSafe $ts -InstallVC -Path $php_dir -TimeZone UTC -InitialPhpIni Production -Force > $null 2>&1 } @@ -409,16 +412,13 @@ if ($null -eq $installed -or -not("$($installed.Version).".StartsWith(($version } $installed = Get-Php -Path $php_dir -Set-PhpIniKey -Key 'date.timezone' -Value 'UTC' -Path $php_dir -Set-PhpIniKey -Key 'memory_limit' -Value '-1' -Path $php_dir +('date.timezone=UTC', 'memory_limit=-1') | foreach { $p=$_.split('='); Set-PhpIniKey -Key $p[0] -Value $p[1] -Path $php_dir } if($version -lt "5.5") { - ForEach($lib in "libeay32.dll", "ssleay32.dll") { - Invoke-WebRequest -UseBasicParsing -Uri https://dl.bintray.com/shivammathur/php/$lib -OutFile $php_dir\$lib >$null 2>&1 - } - Enable-PhpExtension -Extension openssl, curl, mbstring -Path $php_dir + ('libeay32.dll', 'ssleay32.dll') | ForEach { $wc.DownloadFile("$bintray/$_", "$php_dir\$_") >$null 2>&1 } } else { - Enable-PhpExtension -Extension openssl, curl, opcache, mbstring -Path $php_dir + $enable_extensions += ('opcache') } +Enable-PhpExtension -Extension $enable_extensions -Path $php_dir Update-PhpCAInfo -Path $php_dir -Source $cert_source Copy-Item -Path $dist\..\src\configs\*.json -Destination $env:RUNNER_TOOL_CACHE New-Item -ItemType Directory -Path $composer_bin -Force 2>&1 | Out-Null