Fix extension logs

Refactor utils.addCommand
This commit is contained in:
Shivam Mathur 2023-01-18 09:00:28 +05:30
parent 6db1a06425
commit bbc65d999a
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
10 changed files with 94 additions and 69 deletions

View File

@ -222,11 +222,11 @@ describe('Tools tests', () => {
});
it.each`
os | script | scope
${'linux'} | ${'add_composertool tool tool:1.2.3 user/ global'} | ${'global'}
${'darwin'} | ${'add_composertool tool tool:1.2.3 user/ scoped'} | ${'scoped'}
${'win32'} | ${'Add-Composertool tool tool:1.2.3 user/ scoped'} | ${'scoped'}
${'openbsd'} | ${'Platform openbsd is not supported'} | ${'global'}
os | script | scope
${'linux'} | ${'add_composer_tool tool tool:1.2.3 user/ global'} | ${'global'}
${'darwin'} | ${'add_composer_tool tool tool:1.2.3 user/ scoped'} | ${'scoped'}
${'win32'} | ${'Add-ComposerTool tool tool:1.2.3 user/ scoped'} | ${'scoped'}
${'openbsd'} | ${'Platform openbsd is not supported'} | ${'global'}
`('checking addPackage: $os, $scope', async ({os, script, scope}) => {
const data = getData({
tool: 'tool',
@ -380,27 +380,27 @@ describe('Tools tests', () => {
'add_tool https://get.blackfire.io/blackfire-player-v1.22.0.phar blackfire-player "-V"',
'add_tool https://github.com/bmitch/churn-php/releases/latest/download/churn.phar churn "-V"',
'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr "-V"',
'add_composertool flex flex symfony/ global',
'add_composer_tool flex flex symfony/ global',
'add_grpc_php_plugin latest',
'add_tool https://github.com/php-parallel-lint/PHP-Parallel-Lint/releases/latest/download/parallel-lint.phar parallel-lint "--version"',
'add_tool https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v3.2.1/php-cs-fixer.phar php-cs-fixer "-V"',
'add_tool https://github.com/phpDocumentor/phpDocumentor/releases/latest/download/phpDocumentor.phar phpDocumentor "--version"',
'add_composertool phplint phplint overtrue/',
'add_composer_tool phplint phplint overtrue/',
'add_tool https://github.com/phpstan/phpstan/releases/latest/download/phpstan.phar phpstan "-V"',
'add_tool https://phar.phpunit.de/phpunit.phar phpunit "--version"',
'add_pecl',
'add_tool https://www.phing.info/get/phing-latest.phar phing "-v"',
'add_composertool phinx phinx robmorgan/ scoped',
'add_composertool phinx phinx:1.2.3 robmorgan/ scoped',
'add_composer_tool phinx phinx robmorgan/ scoped',
'add_composer_tool phinx phinx:1.2.3 robmorgan/ scoped',
'add_tool https://github.com/phar-io/phive/releases/download/3.2.1/phive-3.2.1.phar phive "status"',
'add_composertool phpunit-bridge phpunit-bridge symfony/ global',
'add_composertool phpunit-polyfills phpunit-polyfills yoast/ global',
'add_composer_tool phpunit-bridge phpunit-bridge symfony/ global',
'add_composer_tool phpunit-polyfills phpunit-polyfills yoast/ global',
'add_tool https://github.com/laravel/pint/releases/latest/download/pint.phar pint "-V"',
'add_devtools php-config',
'add_devtools phpize',
'add_protoc latest',
'add_symfony latest',
'add_composertool vapor-cli vapor-cli laravel/ scoped',
'add_composer_tool vapor-cli vapor-cli laravel/ scoped',
'add_tool https://github.com/wp-cli/builds/blob/gh-pages/phar/wp-cli.phar?raw=true wp-cli "--version"'
]
]
@ -416,21 +416,21 @@ describe('Tools tests', () => {
'behat, blackfire, blackfire-player, churn, composer-normalize, composer-require-checker, composer-unused, cs2pr:1.2.3, flex, grpc_php_plugin:1.2.3, infection, phan, phan:1.2.3, phing:1.2.3, phinx, phive:1.2.3, php-config, phpcbf, phpcpd, phpcs, phpdoc, phpize, phpmd, phpspec, phpunit-bridge:5.6, phpunit-polyfills:1.0.1, protoc:v1.2.3, psalm, rector, symfony-cli, vapor-cli, wp-cli',
[
'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-7.4-stable.phar,https://dl.cloudsmith.io/public/shivammathur/composer-cache/raw/files/composer-7.4-stable.phar,https://getcomposer.org/composer-stable.phar composer',
'add_composertool behat behat behat/ scoped',
'add_composer_tool behat behat behat/ scoped',
'add_blackfire',
'add_tool https://get.blackfire.io/blackfire-player-v1.22.0.phar blackfire-player "-V"',
'add_tool https://github.com/bmitch/churn-php/releases/latest/download/churn.phar churn "-V"',
'add_tool https://github.com/ergebnis/composer-normalize/releases/latest/download/composer-normalize.phar composer-normalize "-V"',
'add_composertool composer-require-checker composer-require-checker maglnet/ scoped',
'add_composer_tool composer-require-checker composer-require-checker maglnet/ scoped',
'add_tool https://github.com/composer-unused/composer-unused/releases/latest/download/composer-unused.phar composer-unused "-V"',
'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/download/1.2.3/cs2pr cs2pr "-V"',
'add_composertool flex flex symfony/ global',
'add_composer_tool flex flex symfony/ global',
'add_grpc_php_plugin 1.2.3',
'add_tool https://github.com/infection/infection/releases/latest/download/infection.phar infection "-V"',
'add_tool https://github.com/phan/phan/releases/latest/download/phan.phar phan "-v"',
'add_tool https://github.com/phan/phan/releases/download/1.2.3/phan.phar phan "-v"',
'add_tool https://www.phing.info/get/phing-1.2.3.phar,https://github.com/phingofficial/phing/releases/download/1.2.3/phing-1.2.3.phar phing "-v"',
'add_composertool phinx phinx robmorgan/ scoped',
'add_composer_tool phinx phinx robmorgan/ scoped',
'add_tool https://github.com/phar-io/phive/releases/download/1.2.3/phive-1.2.3.phar phive',
'add_devtools php-config',
'add_tool https://github.com/squizlabs/PHP_CodeSniffer/releases/latest/download/phpcbf.phar phpcbf "--version"',
@ -440,13 +440,13 @@ describe('Tools tests', () => {
'add_devtools phpize',
'add_tool https://github.com/phpmd/phpmd/releases/latest/download/phpmd.phar phpmd "--version"',
'add_tool https://github.com/phpspec/phpspec/releases/latest/download/phpspec.phar phpspec "-V"',
'add_composertool phpunit-bridge phpunit-bridge:5.6.* symfony/ global',
'add_composertool phpunit-polyfills phpunit-polyfills:1.0.1 yoast/ global',
'add_composer_tool phpunit-bridge phpunit-bridge:5.6.* symfony/ global',
'add_composer_tool phpunit-polyfills phpunit-polyfills:1.0.1 yoast/ global',
'add_protoc 1.2.3',
'add_tool https://github.com/vimeo/psalm/releases/latest/download/psalm.phar psalm "-v"',
'add_composertool rector rector rector/ scoped',
'add_composer_tool rector rector rector/ scoped',
'add_symfony latest',
'add_composertool vapor-cli vapor-cli laravel/ scoped',
'add_composer_tool vapor-cli vapor-cli laravel/ scoped',
'add_tool https://github.com/wp-cli/builds/blob/gh-pages/phar/wp-cli.phar?raw=true wp-cli "--version"'
]
]
@ -468,13 +468,13 @@ describe('Tools tests', () => {
'Add-Tool https://github.com/bmitch/churn-php/releases/latest/download/churn.phar churn "-V"',
'Add-Tool https://deployer.org/deployer.phar deployer "-V"',
'Tool does_not_exist is not supported',
'Add-Composertool flex flex symfony/ global',
'Add-Composertool phinx phinx robmorgan/ scoped',
'Add-ComposerTool flex flex symfony/ global',
'Add-ComposerTool phinx phinx robmorgan/ scoped',
'Add-Tool https://github.com/phar-io/phive/releases/download/0.13.2/phive-0.13.2.phar phive "status"',
'php-config is not a windows tool',
'phpize is not a windows tool',
'Add-Tool https://github.com/phpmd/phpmd/releases/latest/download/phpmd.phar phpmd "--version"',
'Add-Composertool phpunit-bridge phpunit-bridge symfony/ global',
'Add-ComposerTool phpunit-bridge phpunit-bridge symfony/ global',
'Add-Symfony',
'Add-Tool https://github.com/wp-cli/builds/blob/gh-pages/phar/wp-cli.phar?raw=true wp-cli "--version"'
]
@ -491,13 +491,13 @@ describe('Tools tests', () => {
'composer:v1, codeception/codeception, prestissimo, hirak/prestissimo, composer-prefetcher, narrowspark/automatic-composer-prefetcher, phinx: 1.2, robmorgan/phinx: ^1.2, user/tool:1.2.3, user/tool:~1.2',
[
'Add-Tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-7.4-1.phar,https://dl.cloudsmith.io/public/shivammathur/composer-cache/raw/files/composer-7.4-1.phar,https://getcomposer.org/composer-1.phar composer',
'Add-Composertool codeception codeception codeception/ global',
'Add-Composertool prestissimo prestissimo hirak/ global',
'Add-Composertool automatic-composer-prefetcher automatic-composer-prefetcher narrowspark/ global',
'Add-Composertool phinx phinx:1.2.* robmorgan/ scoped',
'Add-Composertool phinx phinx:^1.2 robmorgan/ global',
'Add-Composertool tool tool:1.2.3 user/ global',
'Add-Composertool tool tool:~1.2 user/ global'
'Add-ComposerTool codeception codeception codeception/ global',
'Add-ComposerTool prestissimo prestissimo hirak/ global',
'Add-ComposerTool automatic-composer-prefetcher automatic-composer-prefetcher narrowspark/ global',
'Add-ComposerTool phinx phinx:1.2.* robmorgan/ scoped',
'Add-ComposerTool phinx phinx:^1.2 robmorgan/ global',
'Add-ComposerTool tool tool:1.2.3 user/ global',
'Add-ComposerTool tool tool:~1.2 user/ global'
]
]
])(

View File

@ -193,6 +193,7 @@ describe('Utils tests', () => {
expect(await utils.getCommand('linux', 'tool')).toBe('add_tool ');
expect(await utils.getCommand('darwin', 'tool')).toBe('add_tool ');
expect(await utils.getCommand('win32', 'tool')).toBe('Add-Tool ');
expect(await utils.getCommand('win32', 'tool_name')).toBe('Add-ToolName ');
expect(await utils.getCommand('openbsd', 'tool')).toContain(
'Platform openbsd is not supported'
);

27
dist/index.js vendored
View File

@ -127,19 +127,21 @@ async function checkXdebugError(extension, version) {
exports.checkXdebugError = checkXdebugError;
async function addCoverageXdebug(extension, version, os, pipe) {
let script = '\n';
let message = await checkXdebugError(extension, version);
let status = '$cross';
if (!message) {
const error = await checkXdebugError(extension, version);
if (!error) {
script +=
(await extensions.addExtension(':pcov:false', version, os, true)) + pipe;
extension = extension == 'xdebug3' ? 'xdebug' : extension;
script +=
(await extensions.addExtension(extension, version, os, true)) + pipe;
script += await utils.setVariable('xdebug_version', 'php -r "echo phpversion(\'xdebug\');"', os);
message = 'Xdebug $xdebug_version enabled as coverage driver';
status = '$tick';
script +=
(await utils.getCommand(os, 'extension_log')) +
'xdebug "Xdebug $xdebug_version enabled as coverage driver"';
}
else {
script += await utils.addLog('$cross', extension, error, os);
}
script += await utils.addLog(status, extension, message, os);
return script;
}
exports.addCoverageXdebug = addCoverageXdebug;
@ -154,7 +156,9 @@ async function addCoveragePCOV(version, os, pipe) {
(await extensions.addExtension('pcov', version, os, true)) + pipe;
script += (await config.addINIValues('pcov.enabled=1', os, true)) + '\n';
script += await utils.setVariable('pcov_version', 'php -r "echo phpversion(\'pcov\');"', os);
script += await utils.addLog('$tick', 'coverage: pcov', 'PCOV $pcov_version enabled as coverage driver', os);
script +=
(await utils.getCommand(os, 'extension_log')) +
'pcov "PCOV $pcov_version enabled as coverage driver"';
break;
case /5\.[3-6]|7\.0/.test(version):
script += await utils.addLog('$cross', 'pcov', 'PHP 7.1 or newer is required', os);
@ -752,7 +756,7 @@ async function addArchive(data) {
}
exports.addArchive = addArchive;
async function addPackage(data) {
const command = await utils.getCommand(data['os'], 'composertool');
const command = await utils.getCommand(data['os'], 'composer_tool');
const parts = data['repository'].split('/');
const args = await utils.joins(parts[1], data['release'], parts[0] + '/', data['scope']);
return command + args;
@ -1218,7 +1222,12 @@ async function getCommand(os, suffix) {
case 'darwin':
return 'add_' + suffix + ' ';
case 'win32':
return 'Add-' + suffix.charAt(0).toUpperCase() + suffix.slice(1) + ' ';
return ('Add-' +
suffix
.split('_')
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
.join('') +
' ');
default:
return await log('Platform ' + os + ' is not supported', os, 'error');
}

View File

@ -16,7 +16,7 @@ export async function checkXdebugError(
}
/**
* Function to setup Xdebug
* Function to set up Xdebug
*
* @param extension
* @param version
@ -30,9 +30,8 @@ export async function addCoverageXdebug(
pipe: string
): Promise<string> {
let script = '\n';
let message: string = await checkXdebugError(extension, version);
let status = '$cross';
if (!message) {
const error: string = await checkXdebugError(extension, version);
if (!error) {
script +=
(await extensions.addExtension(':pcov:false', version, os, true)) + pipe;
extension = extension == 'xdebug3' ? 'xdebug' : extension;
@ -43,15 +42,17 @@ export async function addCoverageXdebug(
'php -r "echo phpversion(\'xdebug\');"',
os
);
message = 'Xdebug $xdebug_version enabled as coverage driver';
status = '$tick';
script +=
(await utils.getCommand(os, 'extension_log')) +
'xdebug "Xdebug $xdebug_version enabled as coverage driver"';
} else {
script += await utils.addLog('$cross', extension, error, os);
}
script += await utils.addLog(status, extension, message, os);
return script;
}
/**
* Function to setup PCOV
* Function to set up PCOV
*
* @param version
* @param os
@ -76,14 +77,9 @@ export async function addCoveragePCOV(
'php -r "echo phpversion(\'pcov\');"',
os
);
// success
script += await utils.addLog(
'$tick',
'coverage: pcov',
'PCOV $pcov_version enabled as coverage driver',
os
);
// version is not supported
script +=
(await utils.getCommand(os, 'extension_log')) +
'pcov "PCOV $pcov_version enabled as coverage driver"';
break;
case /5\.[3-6]|7\.0/.test(version):

View File

@ -1,3 +1,20 @@
Function Add-ExtensionLog() {
Param (
[Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNull()]
$extension,
[Parameter(Position = 1, Mandatory = $true)]
[ValidateNotNull()]
$message
)
$extension_info = Get-PhpExtension -Path $php_dir | Where-Object { $_.Name -eq $extension -or $_.Handle -eq $extension }
if ($null -ne $extension_info -and ($extension_info.State -eq 'Enabled' -or $extension_info.State -eq 'Builtin')) {
Add-Log $tick $extension $message
} else {
Add-Log $cross $extension "Could not install $extension on PHP $( $installed.FullVersion )"
}
}
# Function to link dependencies to PHP directory.
Function Set-ExtensionPrerequisites
{

View File

@ -47,10 +47,5 @@ Function Add-Http() {
}
Add-Extension pecl_http >$null 2>&1
Repair-ICU
try {
php --ri "http" 2> $null | Out-Null
Add-Log $tick "http" "Installed and enabled"
} catch {
Add-Log $cross "http" "Could not install http on PHP $( $installed.FullVersion )"
}
Add-ExtensionLog http "Installed and enabled"
}

View File

@ -183,7 +183,7 @@ Function Add-Tool() {
}
}
Function Add-ComposertoolHelper() {
Function Add-ComposerToolHelper() {
Param (
[Parameter(Position = 0, Mandatory = $true)]
[string]
@ -234,7 +234,7 @@ Function Add-ComposertoolHelper() {
}
# Function to setup a tool using composer.
Function Add-Composertool() {
Function Add-ComposerTool() {
Param (
[Parameter(Position = 0, Mandatory = $true)]
[ValidateNotNull()]
@ -267,7 +267,7 @@ Function Add-Composertool() {
}
}
Enable-PhpExtension -Extension curl, mbstring, openssl -Path $php_dir
$log = Add-ComposertoolHelper $tool $release $prefix $scope $composer_args
$log = Add-ComposerToolHelper $tool $release $prefix $scope $composer_args
if(Test-Path $composer_bin\composer) {
Copy-Item -Path "$bin_dir\composer" -Destination "$composer_bin\composer" -Force
}

View File

@ -153,7 +153,7 @@ add_tool() {
}
# Function to setup a tool using composer in a different scope.
add_composertool_helper() {
add_composer_tool_helper() {
tool=$1
release=$2
prefix=$3
@ -184,7 +184,7 @@ add_composertool_helper() {
}
# Function to setup a tool using composer.
add_composertool() {
add_composer_tool() {
tool=$1
release=$2
prefix=$3
@ -199,7 +199,7 @@ add_composertool() {
return
fi
fi
add_composertool_helper "$tool" "$release" "$prefix" "$scope" "$composer_args"
add_composer_tool_helper "$tool" "$release" "$prefix" "$scope" "$composer_args"
tool_version=$(get_tool_version cat /tmp/composer.log)
([ -s /tmp/composer.log ] && add_log "$tick" "$tool" "Added $tool $tool_version"
) || add_log "$cross" "$tool" "Could not setup $tool"

View File

@ -201,7 +201,7 @@ export async function addArchive(data: RS): Promise<string> {
* @param data
*/
export async function addPackage(data: RS): Promise<string> {
const command = await utils.getCommand(data['os'], 'composertool');
const command = await utils.getCommand(data['os'], 'composer_tool');
const parts: string[] = data['repository'].split('/');
const args: string = await utils.joins(
parts[1],

View File

@ -320,7 +320,14 @@ export async function getCommand(os: string, suffix: string): Promise<string> {
case 'darwin':
return 'add_' + suffix + ' ';
case 'win32':
return 'Add-' + suffix.charAt(0).toUpperCase() + suffix.slice(1) + ' ';
return (
'Add-' +
suffix
.split('_')
.map((part: string) => part.charAt(0).toUpperCase() + part.slice(1))
.join('') +
' '
);
default:
return await log('Platform ' + os + ' is not supported', os, 'error');
}