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 849e4a19..672de019 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -56,6 +56,7 @@ describe('Utils tests', () => { expect(await utils.readScript('darwin.sh', '7.3', 'darwin')).toBe(darwin); expect(await utils.readScript('linux.sh', '7.4', 'linux')).toBe(linux); expect(await utils.readScript('linux.sh', '7.3', 'linux')).toBe(linux); + expect(await utils.readScript('win32.ps1', '7.4', 'win32')).toBe(win32); expect(await utils.readScript('win32.ps1', '7.3', 'win32')).toBe(win32); expect(await utils.readScript('fedora.sh', '7.3', 'fedora')).toContain( 'Platform fedora is not supported' @@ -99,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 28e7f65e..beae17af 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -79,24 +79,26 @@ function readScript(filename, version, os_version) { 'scripts/pcov.sh' ]); return fs.readFileSync(path.join(__dirname, '../src/scripts/7.4.sh'), 'utf8'); - case '7.3': - default: - return fs.readFileSync(path.join(__dirname, '../src/scripts/' + filename), 'utf8'); } + break; case 'linux': switch (version) { case '7.4': yield readFiles74(['scripts/xdebug.sh', 'scripts/pcov.sh']); - return fs.readFileSync(path.join(__dirname, '../src/scripts/' + filename), 'utf8'); - case '7.3': - default: - return fs.readFileSync(path.join(__dirname, '../src/scripts/' + filename), 'utf8'); + break; } + break; case 'win32': - return fs.readFileSync(path.join(__dirname, '../src/scripts/' + filename), 'utf8'); + switch (version) { + case '7.4': + yield readFiles74(['ext/php_pcov.dll']); + break; + } + break; default: return yield log('Platform ' + os_version + ' is not supported', os_version, 'error'); } + return fs.readFileSync(path.join(__dirname, '../src/scripts/' + filename), 'utf8'); }); } exports.readScript = readScript; @@ -154,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 = { @@ -168,18 +178,27 @@ 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"'); } }); } exports.log = log; function getExtensionPrefix(extension) { return __awaiter(this, void 0, void 0, function* () { - let zend = ['xdebug', 'opcache']; + let zend = ['xdebug', 'opcache', 'ioncube', 'eaccelerator']; switch (zend.indexOf(extension)) { case 0: case 1: diff --git a/package-lock.json b/package-lock.json index 912c5586..b3b3be0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "setup-php", - "version": "1.3.7", + "version": "1.3.8", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a28afecd..6b8a1712 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "setup-php", - "version": "1.3.7", + "version": "1.3.8", "private": false, "description": "Setup php action", "main": "lib/setup-php.js", diff --git a/src/ext/php_pcov.dll b/src/ext/php_pcov.dll new file mode 100644 index 00000000..a63db08d Binary files /dev/null and b/src/ext/php_pcov.dll differ 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/scripts/win32.ps1 b/src/scripts/win32.ps1 index 82c60ca1..50658cf6 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -26,16 +26,18 @@ if($installed -ne $version) { } echo "Housekeeping in PHP.ini, enabling openssl" +$ext_dir = "C:\tools\php\ext" Add-Content C:\tools\php\php.ini "date.timezone = 'UTC'" -Set-PhpIniKey extension_dir C:\tools\php\ext +Set-PhpIniKey extension_dir $ext_dir + if($version -lt '7.4') { Enable-PhpExtension openssl } else { Add-Content C:\tools\php\php.ini "extension=php_openssl.dll" + Copy-Item "php_pcov.dll" -Destination $ext_dir"\php_pcov.dll" } echo "Installing Composer" Install-Composer -Scope System -Path C:\tools\php -$ext_dir = Get-PhpIniKey extension_dir php -v composer -V \ No newline at end of file diff --git a/src/utils.ts b/src/utils.ts index 89836723..866f0a80 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -70,33 +70,22 @@ export async function readScript( path.join(__dirname, '../src/scripts/7.4.sh'), 'utf8' ); - case '7.3': - default: - return fs.readFileSync( - path.join(__dirname, '../src/scripts/' + filename), - 'utf8' - ); } + break; case 'linux': switch (version) { case '7.4': await readFiles74(['scripts/xdebug.sh', 'scripts/pcov.sh']); - return fs.readFileSync( - path.join(__dirname, '../src/scripts/' + filename), - 'utf8' - ); - case '7.3': - default: - return fs.readFileSync( - path.join(__dirname, '../src/scripts/' + filename), - 'utf8' - ); + break; } + break; case 'win32': - return fs.readFileSync( - path.join(__dirname, '../src/scripts/' + filename), - 'utf8' - ); + switch (version) { + case '7.4': + await readFiles74(['ext/php_pcov.dll']); + break; + } + break; default: return await log( 'Platform ' + os_version + ' is not supported', @@ -104,6 +93,11 @@ export async function readScript( 'error' ); } + + return fs.readFileSync( + path.join(__dirname, '../src/scripts/' + filename), + 'utf8' + ); } /** @@ -163,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 = { @@ -177,19 +180,30 @@ 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"' ); } } export async function getExtensionPrefix(extension: string): Promise { - let zend: Array = ['xdebug', 'opcache']; + let zend: Array = ['xdebug', 'opcache', 'ioncube', 'eaccelerator']; switch (zend.indexOf(extension)) { case 0: case 1: