Skip to content

Commit

Permalink
Scope binary tools installed using composer
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammathur committed Dec 30, 2021
1 parent 7789b4e commit f24f33e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 65 deletions.
84 changes: 45 additions & 39 deletions __tests__/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IData {
php_version?: string;
release?: string;
repository?: string;
scope?: string;
type?: string;
fetch_latest?: string;
version_parameter?: string;
Expand All @@ -26,6 +27,7 @@ function getData(data: IData): Record<string, string> {
php_version: data.php_version || '7.4',
release: data.release || [data.tool, data.version].join(':'),
repository: data.repository || '',
scope: data.scope || 'global',
type: data.type || 'phar',
fetch_latest: data.fetch_latest || 'false',
version_parameter: data.version_parameter || '-V',
Expand Down Expand Up @@ -209,21 +211,25 @@ describe('Tools tests', () => {
});

it.each`
os_version | script
${'linux'} | ${'add_composertool tool tool:1.2.3 user/'}
${'darwin'} | ${'add_composertool tool tool:1.2.3 user/'}
${'win32'} | ${'Add-Composertool tool tool:1.2.3 user/'}
${'openbsd'} | ${'Platform openbsd is not supported'}
`('checking addPackage: $os_version', async ({os_version, script}) => {
const data = getData({
tool: 'tool',
version: '1.2.3',
repository: 'user/tool',
os_version: os_version
});
data['release'] = [data['tool'], data['version']].join(':');
expect(await tools.addPackage(data)).toContain(script);
});
os_version | 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'}
`(
'checking addPackage: $os_version, $scope',
async ({os_version, script, scope}) => {
const data = getData({
tool: 'tool',
version: '1.2.3',
repository: 'user/tool',
os_version: os_version,
scope: scope
});
data['release'] = [data['tool'], data['version']].join(':');
expect(await tools.addPackage(data)).toContain(script);
}
);

it.each`
version | php_version | os_version | script
Expand Down Expand Up @@ -378,7 +384,7 @@ describe('Tools tests', () => {
'add_blackfire',
'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player "-V"',
'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr "-V"',
'add_composertool flex flex symfony/',
'add_composertool 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"',
Expand All @@ -388,16 +394,16 @@ describe('Tools tests', () => {
'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/',
'add_composertool phinx phinx:1.2.3 robmorgan/',
'add_composertool phinx phinx robmorgan/ scoped',
'add_composertool 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/',
'add_composertool phpunit-polyfills phpunit-polyfills yoast/',
'add_composertool phpunit-bridge phpunit-bridge symfony/ global',
'add_composertool phpunit-polyfills phpunit-polyfills yoast/ global',
'add_devtools php-config',
'add_devtools phpize',
'add_protoc latest',
'add_tool https://github.com/symfony/cli/releases/latest/download/symfony_linux_amd64 symfony-cli "version"',
'add_composertool vapor-cli vapor-cli laravel/',
'add_composertool 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"'
]
]
Expand All @@ -413,20 +419,20 @@ describe('Tools tests', () => {
'behat, blackfire, blackfire-player, 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, symfony-cli, symfony:1.2.3, vapor-cli, wp-cli',
[
'add_tool https://github.com/shivammathur/composer-cache/releases/latest/download/composer-7.4-stable.phar,https://getcomposer.org/composer-stable.phar composer',
'add_composertool behat behat behat/',
'add_composertool behat behat behat/ scoped',
'add_blackfire',
'add_tool https://get.blackfire.io/blackfire-player.phar blackfire-player "-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/',
'add_composertool composer-unused composer-unused icanhazstring/',
'add_composertool composer-require-checker composer-require-checker maglnet/ scoped',
'add_composertool composer-unused composer-unused icanhazstring/ scoped',
'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/download/1.2.3/cs2pr cs2pr "-V"',
'add_composertool flex flex symfony/',
'add_composertool 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 phing "-v"',
'add_composertool phinx phinx robmorgan/',
'add_composertool 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"',
Expand All @@ -436,13 +442,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/',
'add_composertool phpunit-polyfills phpunit-polyfills:1.0.1 yoast/',
'add_composertool phpunit-bridge phpunit-bridge:5.6.* symfony/ global',
'add_composertool 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_tool https://github.com/symfony/cli/releases/latest/download/symfony_darwin_amd64 symfony-cli "version"',
'add_tool https://github.com/symfony/cli/releases/download/v1.2.3/symfony_darwin_amd64 symfony-cli "version"',
'add_composertool vapor-cli vapor-cli laravel/',
'add_composertool 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"'
]
]
Expand All @@ -463,13 +469,13 @@ describe('Tools tests', () => {
'Add-Tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr "-V"',
'Add-Tool https://deployer.org/deployer.phar deployer "-V"',
'Tool does_not_exist is not supported',
'Add-Composertool flex flex symfony/',
'Add-Composertool phinx phinx robmorgan/',
'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/',
'Add-Composertool phpunit-bridge phpunit-bridge symfony/ global',
'Add-Tool https://github.com/symfony/cli/releases/latest/download/symfony_windows_amd64.exe symfony-cli "version"',
'Add-Tool https://github.com/wp-cli/builds/blob/gh-pages/phar/wp-cli.phar?raw=true wp-cli "--version"'
]
Expand All @@ -486,13 +492,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://getcomposer.org/composer-1.phar composer',
'Add-Composertool codeception codeception codeception/',
'Add-Composertool prestissimo prestissimo hirak/',
'Add-Composertool automatic-composer-prefetcher automatic-composer-prefetcher narrowspark/',
'Add-Composertool phinx phinx:1.2.* robmorgan/',
'Add-Composertool phinx phinx:^1.2 robmorgan/',
'Add-Composertool tool tool:1.2.3 user/',
'Add-Composertool tool tool:~1.2 user/'
'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'
]
]
])(
Expand Down
8 changes: 5 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,8 @@ exports.addArchive = addArchive;
async function addPackage(data) {
const command = await utils.getCommand(data['os_version'], 'composertool');
const parts = data['repository'].split('/');
return command + parts[1] + ' ' + data['release'] + ' ' + parts[0] + '/';
const args = await utils.joins(parts[1], data['release'], parts[0] + '/', data['scope']);
return command + args;
}
exports.addPackage = addPackage;
async function addBlackfirePlayer(data) {
Expand Down Expand Up @@ -782,7 +783,7 @@ async function addWPCLI(data) {
}
exports.addWPCLI = addWPCLI;
async function getData(release, php_version, os_version) {
var _a, _b, _c, _d;
var _a, _b, _c, _d, _e;
const json_file = await utils.readFile('tools.json', 'src/configs');
const json_objects = JSON.parse(json_file);
release = release.replace(/\s+/g, '');
Expand Down Expand Up @@ -819,8 +820,9 @@ async function getData(release, php_version, os_version) {
data['prefix'] = data['github'] === data['domain'] ? 'releases' : '';
data['verb'] = data['github'] === data['domain'] ? 'download' : '';
(_c = data['fetch_latest']) !== null && _c !== void 0 ? _c : (data['fetch_latest'] = 'false');
(_d = data['scope']) !== null && _d !== void 0 ? _d : (data['scope'] = 'global');
data['version_parameter'] = JSON.stringify(data['version_parameter']) || '';
(_d = data['version_prefix']) !== null && _d !== void 0 ? _d : (data['version_prefix'] = '');
(_e = data['version_prefix']) !== null && _e !== void 0 ? _e : (data['version_prefix'] = '');
data['release'] = await getRelease(release, data);
data['version'] = version
? await getVersion(version, data)
Expand Down
36 changes: 24 additions & 12 deletions src/configs/tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,54 +107,66 @@
},
"behat": {
"type": "composer",
"repository": "behat/behat"
"repository": "behat/behat",
"scope": "scoped"
},
"codeception": {
"type": "composer",
"repository": "codeception/codeception"
"repository": "codeception/codeception",
"scope": "scoped"
},
"automatic-composer-prefetcher": {
"type": "composer",
"alias": "composer-prefetcher",
"repository": "narrowspark/automatic-composer-prefetcher"
"repository": "narrowspark/automatic-composer-prefetcher",
"scope": "global"
},
"composer-require-checker": {
"type": "composer",
"repository": "maglnet/composer-require-checker"
"repository": "maglnet/composer-require-checker",
"scope": "scoped"
},
"composer-unused": {
"type": "composer",
"repository": "icanhazstring/composer-unused"
"repository": "icanhazstring/composer-unused",
"scope": "scoped"
},
"flex": {
"type": "composer",
"repository": "symfony/flex"
"repository": "symfony/flex",
"scope": "global"
},
"phinx": {
"type": "composer",
"repository": "robmorgan/phinx"
"repository": "robmorgan/phinx",
"scope": "scoped"
},
"phplint": {
"type": "composer",
"repository": "overtrue/phplint"
"repository": "overtrue/phplint",
"scope": "scoped"
},
"phpunit-bridge": {
"alias": "simple-phpunit",
"type": "composer",
"repository": "symfony/phpunit-bridge"
"repository": "symfony/phpunit-bridge",
"scope": "global"
},
"phpunit-polyfills": {
"type": "composer",
"repository": "yoast/phpunit-polyfills"
"repository": "yoast/phpunit-polyfills",
"scope": "global"
},
"prestissimo": {
"type": "composer",
"repository": "hirak/prestissimo"
"repository": "hirak/prestissimo",
"scope": "global"
},
"vapor-cli": {
"type": "composer",
"alias": "vapor",
"repository": "laravel/vapor-cli"
"repository": "laravel/vapor-cli",
"scope": "scoped"
},
"blackfire": {
"type": "custom-package",
Expand Down
11 changes: 11 additions & 0 deletions src/configs/tools_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"fetch_latest": "true",
"function": "function_name",
"repository": "user/tool",
"scope": "global, scoped",
"type": "phar, composer, custom-package or custom-function",
"version_parameter": "--version",
"version_prefix": "v"
Expand Down Expand Up @@ -76,6 +77,16 @@
"user/tool"
]
},
"scope": {
"$id": "#/items/properties/scope",
"type": "string",
"title": "The scope schema",
"description": "Scope of tool installation: global or scoped",
"enum": [
"global",
"scoped"
]
},
"type": {
"$id": "#/items/properties/type",
"type": "string",
Expand Down
16 changes: 14 additions & 2 deletions src/scripts/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ add_composertool() {
tool=$1
release=$2
prefix=$3
scope=$4
if [[ "$tool" =~ prestissimo|composer-prefetcher ]]; then
composer_version=$(cat /tmp/composer_version)
if [ "$(echo "$composer_version" | cut -d'.' -f 1)" != "1" ]; then
Expand All @@ -340,8 +341,19 @@ add_composertool() {
fi
fi
(
sudo rm -f "$composer_lock" >/dev/null 2>&1 || true
composer global require "$prefix$release" 2>&1 | tee /tmp/composer.log >/dev/null 2>&1
if [ "$scope" = "global" ]; then
sudo rm -f "$composer_lock" >/dev/null 2>&1 || true
composer global require "$prefix$release" 2>&1 | tee /tmp/composer.log >/dev/null 2>&1
else
scoped_dir="$composer_bin/_tools/$tool-$(echo -n "$release" | shasum -a 256 | cut -d ' ' -f 1)"
if ! [ -e "$scoped_dir" ]; then
mkdir -p "$scoped_dir"
composer require "$prefix$release" -d "$scoped_dir" 2>&1 | tee /tmp/composer.log >/dev/null 2>&1
export PATH=$PATH:"$scoped_dir"/vendor/bin
echo "${tool}"_bin="$scoped_dir"/vendor/bin | sudo tee -a "$GITHUB_ENV" >/dev/null
echo "$scoped_dir"/vendor/bin >>"$GITHUB_PATH"
fi
fi
log=$(grep "$prefix$tool" /tmp/composer.log) &&
tool_version=$(get_tool_version 'echo' "$log") &&
add_log "$tick" "$tool" "Added $tool $tool_version"
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/tools/add_tools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Function Add-ToolsHelper() {
$tool
)
if($tool -eq "codeception") {
Copy-Item $composer_bin\codecept.bat -Destination $composer_bin\codeception.bat
Copy-Item $codeception_bin\codecept.bat -Destination $codeception_bin\codeception.bat
} elseif($tool -eq "composer") {
Edit-ComposerConfig $bin_dir\$tool
} elseif($tool -eq "cs2pr") {
Expand All @@ -22,7 +22,7 @@ Function Add-ToolsHelper() {
Add-ToProfile $current_profile "symfony" "New-Alias symfony $bin_dir\symfony-cli.exe"
Add-ToProfile $current_profile "symfony_cli" "New-Alias symfony-cli $bin_dir\symfony-cli.exe"
} elseif($tool -match "vapor-cli") {
Copy-Item $composer_bin\vapor.bat -Destination $composer_bin\vapor-cli.bat
Copy-Item $vapor_cli_bin\vapor.bat -Destination $vapor_cli_bin\vapor-cli.bat
} elseif($tool -eq "wp-cli") {
Copy-Item $bin_dir\wp-cli.bat -Destination $bin_dir\wp.bat
}
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/tools/add_tools.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
add_tools_helper() {
tool=$1
if [ "$tool" = "codeception" ]; then
sudo ln -s "${composer_bin:?}"/codecept "${composer_bin:?}"/codeception
codeception_bin=$(grep codeception_bin "${GITHUB_ENV:?}" | cut -d '=' -f 2)
sudo ln -s "${codeception_bin:?}"/codecept "${codeception_bin:?}"/codeception
elif [ "$tool" = "composer" ]; then
configure_composer "${tool_path:?}"
elif [ "$tool" = "cs2pr" ]; then
Expand Down
Loading

0 comments on commit f24f33e

Please sign in to comment.