From 17d90ace86636230996133068005d51503200283 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Fri, 4 Oct 2019 19:34:05 +0530 Subject: [PATCH] Improve examples and logging, and fix PCOV setup on linux --- README.md | 21 ++-- __tests__/features.test.ts | 6 +- __tests__/utils.test.ts | 40 +++++-- example/{laravel.yml => laravel-mysql.yml} | 14 +-- example/laravel-postgres.yml | 54 ++++++++++ example/slim-framework.yml | 21 ++++ lib/features.js | 68 ++++++------ lib/utils.js | 23 +++- src/features.ts | 119 ++++++++++++++------- src/utils.ts | 26 ++++- 10 files changed, 289 insertions(+), 103 deletions(-) rename example/{laravel.yml => laravel-mysql.yml} (86%) create mode 100644 example/laravel-postgres.yml create mode 100644 example/slim-framework.yml diff --git a/README.md b/README.md index c10339dd..dfbe1001 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,8 @@ GitHub Actions status LICENSE PHP Versions Supported - Support me on Patreon Support me on Paypal - Get Help on codementor + Support me on Patreon Support me on PayPal + Contact me on Codementor

Setup PHP with required extensions, php.ini configuration and composer in [GitHub Actions](https://github.com/features/actions). This action can be added as a step in your action workflow and it will setup the PHP environment you need to test your application. Refer to [Usage](#memo-usage) section to see how to use this. @@ -44,7 +44,7 @@ Setup PHP with required extensions, php.ini configuration and composer in [GitHu - On `ubuntu` extensions which have the package in apt are installed. - On `windows` and `macOS` PECL extensions are installed. - Extensions which are installed along with PHP if specified are enabled. -- Extensions which cannot be installed gracefully leave an error message in the logs, the action is not interruped. +- Extensions which cannot be installed gracefully leave an error message in the logs, the action is not interrupted. ## :signal_strength: Coverage support - Specify `coverage: xdebug` to use `Xdebug`. @@ -70,10 +70,10 @@ with: Inputs supported by this GitHub Action. -- php-version -- extension-csv (optional) -- ini-values-csv (optional) -- coverage (optional) +- php-version `required` +- extension-csv `optional` +- ini-values-csv `optional` +- coverage `optional` See [action.yml](action.yml) for more info @@ -131,8 +131,13 @@ jobs: ### Examples -- [Laravel](./example/laravel.yml) +Examples for setting up this GitHub Action with different PHP Frameworks/Packages. +**Note:** Make sure you add steps to run your tests after the setup steps given in these examples. + +- [Laravel with MySQL](./example/laravel-mysql.yml) +- [Laravel with PostgreSQL](./example/laravel-postgres.yml) +- [Slim Framework](./example/slim-framework.yml) ## :scroll: License diff --git a/__tests__/features.test.ts b/__tests__/features.test.ts index a744c096..6083ee43 100644 --- a/__tests__/features.test.ts +++ b/__tests__/features.test.ts @@ -173,10 +173,10 @@ describe('Features tests', () => { expect(win32).toContain(''); win32 = await features.addCoverage('pcov', '7.0', 'win32'); - expect(win32).toContain('pcov requires php 7.1 or newer'); + expect(win32).toContain('PCOV requires PHP 7.1 or newer'); win32 = await features.addCoverage('pcov', '5.6', 'win32'); - expect(win32).toContain('pcov requires php 7.1 or newer'); + expect(win32).toContain('PCOV requires PHP 7.1 or newer'); win32 = await features.addCoverage('', '7.4', 'win32'); expect(win32).toEqual(''); @@ -189,6 +189,7 @@ describe('Features tests', () => { linux = await features.addCoverage('pcov', '7.4', 'linux'); expect(linux).toContain('./pcov.sh'); expect(linux).toContain('sudo sed -i "/xdebug/d" $ini_file'); + expect(linux).toContain('sudo phpdismod xdebug'); linux = await features.addCoverage('', '7.4', 'linux'); expect(linux).toEqual(''); @@ -203,7 +204,6 @@ describe('Features tests', () => { darwin = await features.addCoverage('pcov', '7.4', 'darwin'); expect(darwin).toContain('sh ./pcov.sh'); - expect(darwin).toContain('sudo sed -i \'\' "/xdebug/d" $ini_file\n'); darwin = await features.addCoverage('', '7.4', 'win32'); expect(darwin).toEqual(''); diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index da5fdec5..672de019 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -100,31 +100,53 @@ describe('Utils tests', () => { let message: string = 'Test message'; let warning_log: string = await utils.log(message, 'win32', 'warning'); - // expect(warning_log).toEqual( - // "Write-Host '" + message + "' -ForegroundColor yellow" - // ); + expect(warning_log).toEqual( + "Write-Host '" + message + "' -ForegroundColor yellow" + ); warning_log = await utils.log(message, 'linux', 'warning'); expect(warning_log).toEqual('echo "\\033[33;1m' + message + '\\033[0m"'); warning_log = await utils.log(message, 'darwin', 'warning'); expect(warning_log).toEqual('echo "\\033[33;1m' + message + '\\033[0m"'); let error_log: string = await utils.log(message, 'win32', 'error'); - // expect(error_log).toEqual( - // "Write-Host '" + message + "' -ForegroundColor red" - // ); + expect(error_log).toEqual( + "Write-Host '" + message + "' -ForegroundColor red" + ); error_log = await utils.log(message, 'linux', 'error'); expect(error_log).toEqual('echo "\\033[31;1m' + message + '\\033[0m"'); error_log = await utils.log(message, 'darwin', 'error'); expect(error_log).toEqual('echo "\\033[31;1m' + message + '\\033[0m"'); let success_log: string = await utils.log(message, 'win32', 'success'); - // expect(success_log).toEqual( - // "Write-Host '" + message + "' -ForegroundColor green" - // ); + expect(success_log).toEqual( + "Write-Host '" + message + "' -ForegroundColor green" + ); success_log = await utils.log(message, 'linux', 'success'); expect(success_log).toEqual('echo "\\033[32;1m' + message + '\\033[0m"'); success_log = await utils.log(message, 'darwin', 'success'); expect(success_log).toEqual('echo "\\033[32;1m' + message + '\\033[0m"'); + + success_log = await utils.log(message, 'win32', 'success', 'Test win'); + expect(success_log).toEqual( + "Write-Host 'Test win: " + message + "' -ForegroundColor green" + ); + }); + + it('checking log with prefix', async () => { + let message: string = 'Test message'; + let prefix_log: string = await utils.log( + message, + 'linux', + 'success', + 'Test Prefix' + ); + expect(prefix_log).toEqual( + 'echo "\\033[32;1mTest Prefix: ' + message + '\\033[0m"' + ); + prefix_log = await utils.log(message, 'darwin', 'success', 'Test'); + expect(prefix_log).toEqual( + 'echo "\\033[32;1mTest: ' + message + '\\033[0m"' + ); }); it('checking getExtensionPrefix', async () => { diff --git a/example/laravel.yml b/example/laravel-mysql.yml similarity index 86% rename from example/laravel.yml rename to example/laravel-mysql.yml index 38ff0d0b..36af164c 100644 --- a/example/laravel.yml +++ b/example/laravel-mysql.yml @@ -1,11 +1,8 @@ -name: Laravel - +name: Testing Laravel with MySQL on: [push, pull_request] - jobs: - - phpunit: - name: PHPUnit (PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}) + laravel: + name: Laravel (PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}) runs-on: ubuntu-latest env: DB_PASSWORD: password @@ -37,12 +34,11 @@ jobs: steps: - name: Checkout uses: actions/checkout@v1 - - name: Install PHP + - name: Setup PHP, with composer and extensions uses: shivammathur/setup-php@master #https://github.com/shivammathur/setup-php with: php-version: ${{ matrix.php-versions }} - extension-csv: mbstring, xdebug #optional - ini-values-csv: "post_max_size=256M, short_open_tag=On" #optional + extension-csv: mbstring #optional coverage: xdebug #optional - name: Install Composer dependencies run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader diff --git a/example/laravel-postgres.yml b/example/laravel-postgres.yml new file mode 100644 index 00000000..b996bf23 --- /dev/null +++ b/example/laravel-postgres.yml @@ -0,0 +1,54 @@ +name: Testing Laravel with MySQL +on: [push, pull_request] +jobs: + laravel: + name: Laravel (PHP ${{ matrix.php-versions }} on ${{ matrix.operating-system }}) + runs-on: ubuntu-latest + env: + BROADCAST_DRIVER: log + CACHE_DRIVER: redis + QUEUE_CONNECTION: redis + SESSION_DRIVER: redis + services: + postgres: + image: postgres:10.8 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres + ports: + - 5432/tcp + options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3 + redis: + image: redis + ports: + - 6379/tcp + options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3 + strategy: + fail-fast: false + max-parallel: 15 + matrix: + operating-system: [ubuntu-latest, windows-latest, macOS-latest] + php-versions: ['7.2', '7.3'] + steps: + - name: Checkout + uses: actions/checkout@v1 + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@master #https://github.com/shivammathur/setup-php + with: + php-version: ${{ matrix.php-versions }} + extension-csv: mbstring #optional + coverage: xdebug #optional + - name: Install Composer dependencies + run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader + - name: Prepare the application + run: | + cp .env.example .env + php artisan key:generate + - name: Clear Config + run: php artisan config:clear + - name: Run Migration + run: php artisan migrate -v + env: + POSTGRES_HOST: localhost + POSTGRES_PORT: ${{ job.services.postgres.ports[5432] }} \ No newline at end of file diff --git a/example/slim-framework.yml b/example/slim-framework.yml new file mode 100644 index 00000000..a051ccc3 --- /dev/null +++ b/example/slim-framework.yml @@ -0,0 +1,21 @@ +name: Testing Slim Framework +on: [push, pull_request] +jobs: + build: + strategy: + max-parallel: 6 + matrix: + operating-system: [ubuntu-latest, windows-latest, macOS-latest] + php-versions: [7.2, 7.3] + runs-on: ${{ matrix.operating-system }} + steps: + - name: Checkout + uses: actions/checkout@master + - name: Setup PHP, with composer and extensions + uses: shivammathur/setup-php@master #https://github.com/shivammathur/setup-php + with: + php-version: ${{ matrix.php-versions }} + extension-csv: 'mbstring' + coverage: 'xdebug' + - name: Install dependencies + run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader \ No newline at end of file diff --git a/lib/features.js b/lib/features.js index 262f2b84..23cb8c16 100644 --- a/lib/features.js +++ b/lib/features.js @@ -18,17 +18,17 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const utils = __importStar(require("./utils")); const pecl = __importStar(require("./pecl")); -function addExtension(extension_csv, version, os_version) { +function addExtension(extension_csv, version, os_version, log_prefix = 'Add Extension') { return __awaiter(this, void 0, void 0, function* () { switch (os_version) { case 'win32': - return yield addExtensionWindows(extension_csv, version); + return yield addExtensionWindows(extension_csv, version, log_prefix); case 'darwin': - return yield addExtensionDarwin(extension_csv, version); + return yield addExtensionDarwin(extension_csv, version, log_prefix); case 'linux': - return yield addExtensionLinux(extension_csv, version); + return yield addExtensionLinux(extension_csv, version, log_prefix); default: - return yield utils.log('Platform ' + os_version + ' is not supported', os_version, 'error'); + return yield utils.log('Platform ' + os_version + ' is not supported', os_version, 'error', log_prefix); } }); } @@ -42,7 +42,7 @@ function addINIValues(ini_values_csv, os_version) { case 'linux': return yield addINIValuesUnix(ini_values_csv); default: - return yield utils.log('Platform ' + os_version + ' is not supported', os_version, 'error'); + return yield utils.log('Platform ' + os_version + ' is not supported', os_version, 'error', 'Add Config'); } }); } @@ -52,18 +52,18 @@ exports.addINIValues = addINIValues; * * @param extension */ -function enableExtensionWindows(extension) { +function enableExtensionWindows(extension, log_prefix) { return __awaiter(this, void 0, void 0, function* () { return (`try { $exist = Test-Path -Path $ext_dir\\php_${extension}.dll if(!(php -m | findstr -i ${extension}) -and $exist) { Add-Content C:\\tools\\php\\php.ini "${yield utils.getExtensionPrefix(extension)}=php_${extension}.dll"\n` + - (yield utils.log('Enabled ' + extension, 'win32', 'success')) + + (yield utils.log('Enabled ' + extension, 'win32', 'success', log_prefix)) + ` } elseif(php -m | findstr -i ${extension}) {\n` + - (yield utils.log('Extension ' + extension + ' was already enabled', 'win32', 'success')) + + (yield utils.log(extension + ' was already enabled', 'win32', 'success', log_prefix)) + ` } } catch [Exception] {\n` + - (yield utils.log(extension + ' could not be enabled', 'win32', 'error')) + + (yield utils.log(extension + ' could not be enabled', 'win32', 'error', log_prefix)) + ` }\n`); }); } @@ -74,13 +74,13 @@ exports.enableExtensionWindows = enableExtensionWindows; * @param extension * @param os_version */ -function enableExtensionUnix(extension, os_version) { +function enableExtensionUnix(extension, os_version, log_prefix) { return __awaiter(this, void 0, void 0, function* () { return (`if [ ! "$(php -m | grep -i ${extension})" ] && [ -e "$ext_dir/${extension}.so" ]; then echo "${yield utils.getExtensionPrefix(extension)}=${extension}" >> 'php -i | grep "Loaded Configuration" | sed -e "s|.*=>\s*||"'\n` + - (yield utils.log('Enabled ' + extension, os_version, 'success')) + + (yield utils.log('Enabled ' + extension, os_version, 'success', log_prefix)) + `;\n elif [ "$(php -m | grep -i ${extension})" ]; then \n` + - (yield utils.log('Extension ' + extension + ' was already enabled', os_version, 'success')) + + (yield utils.log(extension + ' was already enabled', os_version, 'success', log_prefix)) + `; fi\n`); }); } @@ -91,7 +91,7 @@ exports.enableExtensionUnix = enableExtensionUnix; * @param extension_csv * @param version */ -function addExtensionDarwin(extension_csv, version) { +function addExtensionDarwin(extension_csv, version, log_prefix) { return __awaiter(this, void 0, void 0, function* () { let extensions = yield utils.extensionArray(extension_csv); let script = '\n'; @@ -99,7 +99,7 @@ function addExtensionDarwin(extension_csv, version) { return __awaiter(this, void 0, void 0, function* () { extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - script += yield enableExtensionUnix(extension, 'darwin'); + script += yield enableExtensionUnix(extension, 'darwin', log_prefix); switch (yield pecl.checkPECLExtension(extension)) { case true: let install_command = ''; @@ -126,9 +126,9 @@ function addExtensionDarwin(extension_csv, version) { ')" ]; then ' + install_command + ' && ' + - (yield utils.log('Installed and enabled ' + extension, 'darwin', 'success')) + + (yield utils.log('Installed and enabled ' + extension, 'darwin', 'success', log_prefix)) + ' || ' + - (yield utils.log('Could not install ' + extension + ' on PHP' + version, 'darwin', 'error')) + + (yield utils.log('Could not install ' + extension + ' on PHP' + version, 'darwin', 'error', log_prefix)) + '; fi\n'; break; case false: @@ -137,7 +137,7 @@ function addExtensionDarwin(extension_csv, version) { 'if [ ! "$(php -m | grep -i ' + extension + ')" ]; then \n' + - (yield utils.log('Could not find ' + extension + ' for PHP' + version + ' on PECL', 'darwin', 'error')) + + (yield utils.log('Could not find ' + extension + ' for PHP' + version + ' on PECL', 'darwin', 'error', log_prefix)) + '; fi\n'; break; } @@ -153,7 +153,7 @@ exports.addExtensionDarwin = addExtensionDarwin; * @param extension_csv * @param version */ -function addExtensionWindows(extension_csv, version) { +function addExtensionWindows(extension_csv, version, log_prefix) { return __awaiter(this, void 0, void 0, function* () { let extensions = yield utils.extensionArray(extension_csv); let script = '\n'; @@ -161,7 +161,7 @@ function addExtensionWindows(extension_csv, version) { return __awaiter(this, void 0, void 0, function* () { extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - script += yield enableExtensionWindows(extension); + script += yield enableExtensionWindows(extension, log_prefix); switch (yield pecl.checkPECLExtension(extension)) { case true: let install_command = ''; @@ -186,9 +186,9 @@ function addExtensionWindows(extension_csv, version) { 'try { ' + install_command + '\n' + - (yield utils.log('Installed and enabled ' + extension, 'win32', 'success')) + + (yield utils.log('Installed and enabled ' + extension, 'win32', 'success', log_prefix)) + ' } catch [Exception] { ' + - (yield utils.log('Could not install ' + extension + ' on PHP' + version, 'win32', 'error')) + + (yield utils.log('Could not install ' + extension + ' on PHP' + version, 'win32', 'error', log_prefix)) + ' } }\n'; break; case false: @@ -197,7 +197,7 @@ function addExtensionWindows(extension_csv, version) { 'if(!(php -m | findstr -i ' + extension + ')) { ' + - (yield utils.log('Could not find ' + extension + ' for PHP' + version + ' on PECL', 'win32', 'error')) + + (yield utils.log('Could not find ' + extension + ' for PHP' + version + ' on PECL', 'win32', 'error', log_prefix)) + ' } \n'; break; } @@ -213,7 +213,7 @@ exports.addExtensionWindows = addExtensionWindows; * @param extension_csv * @param version */ -function addExtensionLinux(extension_csv, version) { +function addExtensionLinux(extension_csv, version, log_prefix) { return __awaiter(this, void 0, void 0, function* () { let extensions = yield utils.extensionArray(extension_csv); let script = '\n'; @@ -221,7 +221,7 @@ function addExtensionLinux(extension_csv, version) { return __awaiter(this, void 0, void 0, function* () { extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - script += yield enableExtensionUnix(extension, 'linux'); + script += yield enableExtensionUnix(extension, 'linux', log_prefix); let install_command = ''; switch (version + extension) { case '7.4xdebug': @@ -247,9 +247,9 @@ function addExtensionLinux(extension_csv, version) { ')" ]; then ' + install_command + ' && ' + - (yield utils.log('Installed and enabled ' + extension, 'linux', 'success')) + + (yield utils.log('Installed and enabled ' + extension, 'linux', 'success', log_prefix)) + ' || ' + - (yield utils.log('Could not find php' + version + '-' + extension + ' on APT repository', 'linux', 'error')) + + (yield utils.log('Could not find php' + version + '-' + extension + ' on APT repository', 'linux', 'error', log_prefix)) + '; fi\n'; }); }); @@ -306,11 +306,15 @@ function addCoverage(coverage, version, os_version) { // if version is 7.1 or newer switch (version) { default: - script += yield addExtension('pcov', version, os_version); + script += yield addExtension('pcov', version, os_version, 'Set Coverage Driver'); script += yield addINIValues('pcov.enabled=1', os_version); // add command to disable xdebug and enable pcov switch (os_version) { case 'linux': + script += + 'if [ -e /etc/php/' + + version + + '/mods-available/xdebug.ini ]; then sudo phpdismod xdebug; fi\n'; script += 'sudo sed -i "/xdebug/d" $ini_file\n'; break; case 'darwin': @@ -322,19 +326,19 @@ function addCoverage(coverage, version, os_version) { break; } // success - script += yield utils.log('pcov enabled as coverage driver', os_version, 'success'); + script += yield utils.log('PCOV enabled as coverage driver', os_version, 'success', 'Set Coverage Driver'); // version is not supported break; case '5.6': case '7.0': - script += yield utils.log('pcov requires php 7.1 or newer', os_version, 'warning'); + script += yield utils.log('PCOV requires PHP 7.1 or newer', os_version, 'warning', 'Set Coverage Driver'); break; } break; //xdebug case 'xdebug': - script += yield addExtension('xdebug', version, os_version); - script += yield utils.log('Xdebug enabled as coverage driver', os_version, 'success'); + script += yield addExtension('xdebug', version, os_version, 'Set Coverage Driver'); + script += yield utils.log('Xdebug enabled as coverage driver', os_version, 'success', 'Set Coverage Driver'); break; // unknown coverage driver default: diff --git a/lib/utils.js b/lib/utils.js index 26155909..beae17af 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -156,13 +156,21 @@ function INIArray(ini_values_csv) { }); } exports.INIArray = INIArray; -function log(message, os_version, log_type) { +function log(message, os_version, log_type, prefix = '') { return __awaiter(this, void 0, void 0, function* () { const unix_color = { error: '31', success: '32', warning: '33' }; + switch (prefix) { + case '': + prefix = prefix; + break; + default: + prefix = prefix + ': '; + break; + } switch (os_version) { case 'win32': const color = { @@ -170,11 +178,20 @@ function log(message, os_version, log_type) { success: 'green', warning: 'yellow' }; - return "Write-Host '" + message + "' -ForegroundColor " + color[log_type]; + return ("Write-Host '" + + prefix + + message + + "' -ForegroundColor " + + color[log_type]); case 'linux': case 'darwin': default: - return ('echo "\\033[' + unix_color[log_type] + ';1m' + message + '\\033[0m"'); + return ('echo "\\033[' + + unix_color[log_type] + + ';1m' + + prefix + + message + + '\\033[0m"'); } }); } diff --git a/src/features.ts b/src/features.ts index 4ceed88a..e9108079 100644 --- a/src/features.ts +++ b/src/features.ts @@ -4,20 +4,22 @@ import * as pecl from './pecl'; export async function addExtension( extension_csv: string, version: string, - os_version: string + os_version: string, + log_prefix = 'Add Extension' ): Promise { switch (os_version) { case 'win32': - return await addExtensionWindows(extension_csv, version); + return await addExtensionWindows(extension_csv, version, log_prefix); case 'darwin': - return await addExtensionDarwin(extension_csv, version); + return await addExtensionDarwin(extension_csv, version, log_prefix); case 'linux': - return await addExtensionLinux(extension_csv, version); + return await addExtensionLinux(extension_csv, version, log_prefix); default: return await utils.log( 'Platform ' + os_version + ' is not supported', os_version, - 'error' + 'error', + log_prefix ); } } @@ -33,7 +35,8 @@ export async function addINIValues(ini_values_csv: string, os_version: string) { return await utils.log( 'Platform ' + os_version + ' is not supported', os_version, - 'error' + 'error', + 'Add Config' ); } } @@ -43,7 +46,10 @@ export async function addINIValues(ini_values_csv: string, os_version: string) { * * @param extension */ -export async function enableExtensionWindows(extension: string) { +export async function enableExtensionWindows( + extension: string, + log_prefix: string +) { return ( `try { $exist = Test-Path -Path $ext_dir\\php_${extension}.dll @@ -51,16 +57,22 @@ export async function enableExtensionWindows(extension: string) { Add-Content C:\\tools\\php\\php.ini "${await utils.getExtensionPrefix( extension )}=php_${extension}.dll"\n` + - (await utils.log('Enabled ' + extension, 'win32', 'success')) + + (await utils.log('Enabled ' + extension, 'win32', 'success', log_prefix)) + ` } elseif(php -m | findstr -i ${extension}) {\n` + (await utils.log( - 'Extension ' + extension + ' was already enabled', + extension + ' was already enabled', 'win32', - 'success' + 'success', + log_prefix )) + ` } } catch [Exception] {\n` + - (await utils.log(extension + ' could not be enabled', 'win32', 'error')) + + (await utils.log( + extension + ' could not be enabled', + 'win32', + 'error', + log_prefix + )) + ` }\n` ); } @@ -73,19 +85,26 @@ export async function enableExtensionWindows(extension: string) { */ export async function enableExtensionUnix( extension: string, - os_version: string + os_version: string, + log_prefix: string ) { return ( `if [ ! "$(php -m | grep -i ${extension})" ] && [ -e "$ext_dir/${extension}.so" ]; then echo "${await utils.getExtensionPrefix( extension )}=${extension}" >> 'php -i | grep "Loaded Configuration" | sed -e "s|.*=>\s*||"'\n` + - (await utils.log('Enabled ' + extension, os_version, 'success')) + + (await utils.log( + 'Enabled ' + extension, + os_version, + 'success', + log_prefix + )) + `;\n elif [ "$(php -m | grep -i ${extension})" ]; then \n` + (await utils.log( - 'Extension ' + extension + ' was already enabled', + extension + ' was already enabled', os_version, - 'success' + 'success', + log_prefix )) + `; fi\n` ); @@ -99,14 +118,15 @@ export async function enableExtensionUnix( */ export async function addExtensionDarwin( extension_csv: string, - version: string + version: string, + log_prefix: string ): Promise { let extensions: Array = await utils.extensionArray(extension_csv); let script: string = '\n'; await utils.asyncForEach(extensions, async function(extension: string) { extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - script += await enableExtensionUnix(extension, 'darwin'); + script += await enableExtensionUnix(extension, 'darwin', log_prefix); switch (await pecl.checkPECLExtension(extension)) { case true: let install_command: string = ''; @@ -136,13 +156,15 @@ export async function addExtensionDarwin( (await utils.log( 'Installed and enabled ' + extension, 'darwin', - 'success' + 'success', + log_prefix )) + ' || ' + (await utils.log( 'Could not install ' + extension + ' on PHP' + version, 'darwin', - 'error' + 'error', + log_prefix )) + '; fi\n'; break; @@ -155,7 +177,8 @@ export async function addExtensionDarwin( (await utils.log( 'Could not find ' + extension + ' for PHP' + version + ' on PECL', 'darwin', - 'error' + 'error', + log_prefix )) + '; fi\n'; break; @@ -172,14 +195,15 @@ export async function addExtensionDarwin( */ export async function addExtensionWindows( extension_csv: string, - version: string + version: string, + log_prefix: string ): Promise { let extensions: Array = await utils.extensionArray(extension_csv); let script: string = '\n'; await utils.asyncForEach(extensions, async function(extension: string) { extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - script += await enableExtensionWindows(extension); + script += await enableExtensionWindows(extension, log_prefix); switch (await pecl.checkPECLExtension(extension)) { case true: @@ -209,13 +233,15 @@ export async function addExtensionWindows( (await utils.log( 'Installed and enabled ' + extension, 'win32', - 'success' + 'success', + log_prefix )) + ' } catch [Exception] { ' + (await utils.log( 'Could not install ' + extension + ' on PHP' + version, 'win32', - 'error' + 'error', + log_prefix )) + ' } }\n'; break; @@ -228,7 +254,8 @@ export async function addExtensionWindows( (await utils.log( 'Could not find ' + extension + ' for PHP' + version + ' on PECL', 'win32', - 'error' + 'error', + log_prefix )) + ' } \n'; break; @@ -245,14 +272,15 @@ export async function addExtensionWindows( */ export async function addExtensionLinux( extension_csv: string, - version: string + version: string, + log_prefix: string ): Promise { let extensions: Array = await utils.extensionArray(extension_csv); let script: string = '\n'; await utils.asyncForEach(extensions, async function(extension: string) { extension = extension.toLowerCase(); // add script to enable extension is already installed along with php - script += await enableExtensionUnix(extension, 'linux'); + script += await enableExtensionUnix(extension, 'linux', log_prefix); let install_command: string = ''; switch (version + extension) { @@ -282,13 +310,15 @@ export async function addExtensionLinux( (await utils.log( 'Installed and enabled ' + extension, 'linux', - 'success' + 'success', + log_prefix )) + ' || ' + (await utils.log( 'Could not find php' + version + '-' + extension + ' on APT repository', 'linux', - 'error' + 'error', + log_prefix )) + '; fi\n'; }); @@ -343,12 +373,21 @@ export async function addCoverage( // if version is 7.1 or newer switch (version) { default: - script += await addExtension('pcov', version, os_version); + script += await addExtension( + 'pcov', + version, + os_version, + 'Set Coverage Driver' + ); script += await addINIValues('pcov.enabled=1', os_version); // add command to disable xdebug and enable pcov switch (os_version) { case 'linux': + script += + 'if [ -e /etc/php/' + + version + + '/mods-available/xdebug.ini ]; then sudo phpdismod xdebug; fi\n'; script += 'sudo sed -i "/xdebug/d" $ini_file\n'; break; case 'darwin': @@ -362,29 +401,37 @@ export async function addCoverage( // success script += await utils.log( - 'pcov enabled as coverage driver', + 'PCOV enabled as coverage driver', os_version, - 'success' + 'success', + 'Set Coverage Driver' ); // version is not supported break; case '5.6': case '7.0': script += await utils.log( - 'pcov requires php 7.1 or newer', + 'PCOV requires PHP 7.1 or newer', os_version, - 'warning' + 'warning', + 'Set Coverage Driver' ); break; } break; //xdebug case 'xdebug': - script += await addExtension('xdebug', version, os_version); + script += await addExtension( + 'xdebug', + version, + os_version, + 'Set Coverage Driver' + ); script += await utils.log( 'Xdebug enabled as coverage driver', os_version, - 'success' + 'success', + 'Set Coverage Driver' ); break; // unknown coverage driver diff --git a/src/utils.ts b/src/utils.ts index b2bffb8f..866f0a80 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -157,13 +157,22 @@ export async function INIArray(ini_values_csv: string): Promise> { export async function log( message: string, os_version: string, - log_type: string + log_type: string, + prefix = '' ): Promise { const unix_color: any = { error: '31', success: '32', warning: '33' }; + switch (prefix) { + case '': + prefix = prefix; + break; + default: + prefix = prefix + ': '; + break; + } switch (os_version) { case 'win32': const color: any = { @@ -171,13 +180,24 @@ export async function log( success: 'green', warning: 'yellow' }; - return "Write-Host '" + message + "' -ForegroundColor " + color[log_type]; + return ( + "Write-Host '" + + prefix + + message + + "' -ForegroundColor " + + color[log_type] + ); case 'linux': case 'darwin': default: return ( - 'echo "\\033[' + unix_color[log_type] + ';1m' + message + '\\033[0m"' + 'echo "\\033[' + + unix_color[log_type] + + ';1m' + + prefix + + message + + '\\033[0m"' ); } }