diff --git a/__tests__/install.test.ts b/__tests__/install.test.ts index 9842a38e..b5daa8ae 100644 --- a/__tests__/install.test.ts +++ b/__tests__/install.test.ts @@ -32,7 +32,7 @@ jest.mock('../src/install', () => ({ async (): Promise => { const os_version: string = process.env['RUNNER_OS'] || ''; let version: string = process.env['php-version'] || ''; - version = version.length > 1 ? version : version + '.0'; + version = version.length > 1 ? version.slice(0, 3) : version + '.0'; let script = ''; switch (os_version) { case 'darwin': @@ -69,14 +69,14 @@ jest.mock('../src/install', () => ({ * @param coverage_driver */ function setEnv( - version: string, + version: string | number, os: string, extension_csv: string, ini_values_csv: string, coverage_driver: string, pecl: string ): void { - process.env['php-version'] = version; + process.env['php-version'] = version.toString(); process.env['RUNNER_OS'] = os; process.env['extensions'] = extension_csv; process.env['ini-values'] = ini_values_csv; @@ -150,4 +150,24 @@ describe('Install', () => { expect(script).toContain('set coverage driver'); expect(script).toContain('sh script.sh 7.3 ' + __dirname); }); + + it('Test malformed version inputs', async () => { + setEnv('7.4.1', 'darwin', '', '', '', ''); + // @ts-ignore + let script: string = await install.run(); + expect(script).toContain('initial script'); + expect(script).toContain('sh script.sh 7.4 ' + __dirname); + + setEnv(8.0, 'darwin', '', '', '', ''); + // @ts-ignore + script = await install.run(); + expect(script).toContain('initial script'); + expect(script).toContain('sh script.sh 8.0 ' + __dirname); + + setEnv(8, 'darwin', '', '', '', ''); + // @ts-ignore + script = await install.run(); + expect(script).toContain('initial script'); + expect(script).toContain('sh script.sh 8.0 ' + __dirname); + }); }); diff --git a/dist/index.js b/dist/index.js index eb724e3a..accea60c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1906,7 +1906,7 @@ function run() { try { const os_version = process.platform; let version = yield utils.getInput('php-version', true); - version = version.length > 1 ? version : version + '.0'; + version = version.length > 1 ? version.slice(0, 3) : version + '.0'; // check the os version and run the respective script let script_path = ''; switch (os_version) { diff --git a/src/install.ts b/src/install.ts index b36434d8..1d7fd741 100644 --- a/src/install.ts +++ b/src/install.ts @@ -49,7 +49,7 @@ export async function run(): Promise { try { const os_version: string = process.platform; let version: string = await utils.getInput('php-version', true); - version = version.length > 1 ? version : version + '.0'; + version = version.length > 1 ? version.slice(0, 3) : version + '.0'; // check the os version and run the respective script let script_path = ''; switch (os_version) { diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index f5f6934f..068e2141 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -1,11 +1,10 @@ -tick="✓" -cross="✗" - +# 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 @@ -17,46 +16,16 @@ add_log() { fi } -get_extension_regex() { - extension=$1 - case $extension in - "opcache") - echo "^Zend\sOPcache$" - ;; - "xdebug") - echo "^Xdebug$" - ;; - *) - echo ^"$extension"$ - ;; - esac -} - -step_log "Setup PHP and Composer" -export HOMEBREW_NO_INSTALL_CLEANUP=TRUE -brew tap shivammathur/homebrew-php >/dev/null 2>&1 -brew install shivammathur/php/php@"$1" composer >/dev/null 2>&1 -brew link --force --overwrite php@"$1" >/dev/null 2>&1 -ini_file=$(php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g") -echo "date.timezone=UTC" >> "$ini_file" -ext_dir=$(php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||") -sudo chmod 777 "$ini_file" -mkdir -p "$(pecl config-get ext_dir)" -composer global require hirak/prestissimo >/dev/null 2>&1 -semver=$(php -v | head -n 1 | cut -f 2 -d ' ') -add_log "$tick" "PHP" "Installed PHP $semver" -add_log "$tick" "Composer" "Installed" - +# Function to setup extensions add_extension() { extension=$1 install_command=$2 prefix=$3 - extension_regex="$(get_extension_regex "$extension")" - if ! php -m | grep -i -q "$extension_regex" && [ -e "$ext_dir/$extension.so" ]; then + if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then echo "$prefix=$extension" >>"$ini_file" && add_log "$tick" "$extension" "Enabled" - elif php -m | grep -i -q "$extension_regex"; then + elif php -m | grep -i -q -w "$extension"; then add_log "$tick" "$extension" "Enabled" - elif ! php -m | grep -i -q "$extension_regex"; then + elif ! php -m | grep -i -q -w "$extension"; then exists=$(curl -sL https://pecl.php.net/json.php?package="$extension" -w "%{http_code}" -o /dev/null) if [ "$exists" = "200" ]; then ( @@ -64,9 +33,34 @@ add_extension() { add_log "$tick" "$extension" "Installed and enabled" ) || add_log "$cross" "$extension" "Could not install $extension on PHP $semver" else - if ! php -m | grep -i -q "$extension_regex"; then + if ! php -m | grep -i -q -w "$extension"; then add_log "$cross" "$extension" "Could not find $extension for PHP $semver on PECL" fi fi fi -} \ No newline at end of file +} + +# Function to setup PHP and composer +setup_php_and_composer() { + export HOMEBREW_NO_INSTALL_CLEANUP=TRUE + brew tap shivammathur/homebrew-php >/dev/null 2>&1 + brew install shivammathur/php/php@"$version" composer >/dev/null 2>&1 + brew link --force --overwrite php@"$version" >/dev/null 2>&1 +} + +# Variables +tick="✓" +cross="✗" +version=$1 + +# Setup PHP and composer +step_log "Setup PHP and Composer" +setup_php_and_composer +ini_file=$(php -d "date.timezone=UTC" --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s/ //g") +echo "date.timezone=UTC" >> "$ini_file" +ext_dir=$(php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||") +sudo chmod 777 "$ini_file" +mkdir -p "$(pecl config-get ext_dir)" +semver=$(php -v | head -n 1 | cut -f 2 -d ' ') +add_log "$tick" "PHP" "Installed PHP $semver" +add_log "$tick" "Composer" "Installed" diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 0e89a247..b18346c8 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -1,11 +1,10 @@ -tick="✓" -cross="✗" - +# 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 @@ -17,63 +16,122 @@ add_log() { fi } -get_extension_regex() { - extension=$1 - case $extension in - "opcache") - echo "^Zend\sOPcache$" - ;; - "xdebug") - echo "^Xdebug$" - ;; - *) - echo ^"$extension"$ - ;; - esac +# Function to update php ppa +update_ppa() { + if [ "$ppa_updated" = "false" ]; then + find /etc/apt/sources.list.d -type f -name 'ondrej-ubuntu-php*.list' -exec sudo DEBIAN_FRONTEND=noninteractive apt-fast update -o Dir::Etc::sourcelist="{}" ';' >/dev/null 2>&1 + ppa_updated="true" + fi } +# Function to setup extension +add_extension() { + extension=$1 + install_command=$2 + prefix=$3 + if ! php -m | grep -i -q -w "$extension" && [ -e "$ext_dir/$extension.so" ]; then + echo "$prefix=$extension" >>"$ini_file" && add_log "$tick" "$extension" "Enabled" + elif php -m | grep -i -q -w "$extension"; then + add_log "$tick" "$extension" "Enabled" + elif ! php -m | grep -i -q -w "$extension"; then + (eval "$install_command" && add_log "$tick" "$extension" "Installed and enabled") || + (update_ppa && eval "$install_command" && add_log "$tick" "$extension" "Installed and enabled") || + add_log "$cross" "$extension" "Could not install $extension on PHP $semver" + fi +} + +# Function to setup the nightly build from master branch +setup_master() { + tar_file=php_"$version"%2Bubuntu"$(lsb_release -r -s)".tar.xz + install_dir=~/php/"$version" + sudo mkdir -m 777 -p ~/php + $apt_install libicu-dev >/dev/null 2>&1 + curl -o "$tar_file" -L https://bintray.com/shivammathur/php/download_file?file_path="$tar_file" >/dev/null 2>&1 + sudo tar xf "$tar_file" -C ~/php >/dev/null 2>&1 + rm -rf "$tar_file" + sudo ln -sf -S "$version" "$install_dir"/bin/* /usr/bin/ + sudo ln -sf "$install_dir"/etc/php.ini /etc/php.ini +} + +# Function to setup PECL +setup_pecl() { + $apt_install php"$version"-dev php"$version"-xml >/dev/null 2>&1 + 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 + wget https://github.com/pear/pearweb_phars/raw/master/install-pear-nozlib.phar >/dev/null 2>&1 + sudo php install-pear-nozlib.phar >/dev/null 2>&1 + sudo pear config-set php_ini "$ini_file" >/dev/null 2>&1 + sudo pear config-set auto_discover 1 >/dev/null 2>&1 + sudo pear channel-update pear.php.net >/dev/null 2>&1 +} + +# Function to setup composer +setup_composer() { + if [ ! -e "/usr/bin/composer" ]; then + curl -s -L https://getcomposer.org/installer >composer-setup.php + if [ "$(curl -s https://composer.github.io/installer.sig)" != "$(php -r "echo hash_file('sha384', 'composer-setup.php');")" ]; then + echo >&2 'ERROR: Invalid installer signature' + else + export COMPOSER_ALLOW_SUPERUSER=1 + sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer + fi + rm composer-setup.php + fi + add_log "$tick" "Composer" "Installed" +} + +# 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" >/dev/null 2>&1 + fi + done +} + +# Variables +tick="✓" +cross="✗" +ppa_updated="false" +version=$1 +pecl=$2 +apt_install="sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y" existing_version=$(php-config --version | cut -c 1-3) semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') + +# Setup PHP step_log "Setup PHP and Composer" sudo mkdir -p /var/run sudo mkdir -p /run/php -find /etc/apt/sources.list.d -type f -name 'ondrej-ubuntu-php*.list' -exec sudo DEBIAN_FRONTEND=noninteractive apt-fast update -o Dir::Etc::sourcelist="{}" ';' >/dev/null 2>&1 -if [ "$existing_version" != "$1" ]; then - if [ ! -e "/usr/bin/php$1" ]; then - if [ "$1" = "7.4" ]; then - sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y php"$1" php"$1"-phpdbg php"$1"-xml curl php"$1"-curl >/dev/null 2>&1 - elif [ "$1" = "8.0" ]; then - tar_file=php_"$1"%2Bubuntu"$(lsb_release -r -s)".tar.xz - install_dir=~/php/"$1" - sudo DEBIAN_FRONTEND=noninteractive apt-get -y install libicu-dev >/dev/null 2>&1 - curl -o "$tar_file" -L https://bintray.com/shivammathur/php/download_file?file_path="$tar_file" >/dev/null 2>&1 - sudo mkdir -m 777 -p ~/php - sudo tar xf "$tar_file" -C ~/php >/dev/null 2>&1 && rm -rf "$tar_file" - sudo ln -sf -S "$1" "$install_dir"/bin/* /usr/bin/ && sudo ln -sf "$install_dir"/etc/php.ini /etc/php.ini - else - sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y php"$1" curl php"$1"-curl >/dev/null 2>&1 - fi - status="installed" - else - status="switched" - fi - for tool in pear pecl php phar phar.phar php-cgi php-config phpize phpdbg; do - if [ -e "/usr/bin/$tool$1" ]; then - sudo update-alternatives --set $tool /usr/bin/"$tool$1" >/dev/null 2>&1 - fi - done +if [ "$existing_version" != "$version" ]; then + if [ ! -e "/usr/bin/php$version" ]; then + update_ppa + ppa_updated=1 + if [ "$version" = "7.4" ]; then + $apt_install php"$version" php"$version"-phpdbg php"$version"-xml curl php"$version"-curl >/dev/null 2>&1 + elif [ "$version" = "8.0" ]; then + setup_master + else + $apt_install php"$version" curl php"$version"-curl >/dev/null 2>&1 + fi + status="installed" + else + status="switched" + fi - semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') - if [ "$1" = "8.0" ]; then - semver=$(php -v | head -n 1 | cut -f 2 -d ' ') - fi + switch_version - if [ "$status" != "switched" ]; then - status="Installed PHP $semver" - else - status="Switched to PHP $semver" - fi + semver=$(php -v | head -n 1 | cut -f 2 -d ' ' | cut -f 1 -d '-') + if [ "$version" = "8.0" ]; then + semver=$(php -v | head -n 1 | cut -f 2 -d ' ') + fi + + if [ "$status" != "switched" ]; then + status="Installed PHP $semver" + else + status="Switched to PHP $semver" + fi else status="PHP $semver Found" fi @@ -82,45 +140,13 @@ ini_file=$(php --ini | grep "Loaded Configuration" | sed -e "s|.*:s*||" | sed "s ext_dir=$(php -i | grep "extension_dir => /usr" | sed -e "s|.*=> s*||") sudo chmod 777 "$ini_file" add_log "$tick" "PHP" "$status" -if [ "$2" = "true" ]; then - sudo DEBIAN_FRONTEND=noninteractive apt-fast install -y php"$1"-dev php"$1"-xml >/dev/null 2>&1 - sudo update-alternatives --set php-config /usr/bin/php-config"$1" >/dev/null 2>&1 - sudo update-alternatives --set phpize /usr/bin/phpize"$1" >/dev/null 2>&1 - wget https://github.com/pear/pearweb_phars/raw/master/install-pear-nozlib.phar >/dev/null 2>&1 - sudo php install-pear-nozlib.phar >/dev/null 2>&1 - sudo pear config-set php_ini "$ini_file" >/dev/null 2>&1 - sudo pear config-set auto_discover 1 - sudo pear channel-update pear.php.net + +# Setup PECL +if [ "$pecl" = "true" ]; then + update_ppa + setup_pecl add_log "$tick" "PECL" "Installed" fi -if [ ! -e "/usr/bin/composer" ]; then - curl -s -L https://getcomposer.org/installer > composer-setup.php - if [ "$(curl -s https://composer.github.io/installer.sig)" != "$(php -r "echo hash_file('sha384', 'composer-setup.php');")" ]; then - >&2 echo 'ERROR: Invalid installer signature' - else - export COMPOSER_ALLOW_SUPERUSER=1 - sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer - fi - rm composer-setup.php -fi -composer global require hirak/prestissimo >/dev/null 2>&1 -add_log "$tick" "Composer" "Installed" - -add_extension() -{ - extension=$1 - install_command=$2 - prefix=$3 - extension_regex="$(get_extension_regex "$extension")" - if ! php -m | grep -i -q "$extension_regex" && [ -e "$ext_dir/$extension.so" ]; then - echo "$prefix=$extension" >> "$ini_file" && add_log "$tick" "$extension" "Enabled" - elif php -m | grep -i -q "$extension_regex"; then - add_log "$tick" "$extension" "Enabled" - elif ! php -m | grep -i -q "$extension_regex"; then - ( - eval "$install_command" && \ - add_log "$tick" "$extension" "Installed and enabled" - ) || add_log "$cross" "$extension" "Could not install $extension on PHP $semver" - fi -} \ No newline at end of file +# Setup composer +setup_composer