From 925ede25899cf1b223e68ee8512c111ab68b5c23 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Fri, 30 Oct 2020 18:29:39 +0530 Subject: [PATCH] Refactor composer setup --- README.md | 4 +++- __tests__/tools.test.ts | 6 ++++++ dist/index.js | 25 ++++++++++--------------- src/tools.ts | 33 +++++++++++++-------------------- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index e4160e86..29238638 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,7 @@ These tools can be setup globally using the `tools` input. tools: php-cs-fixer, phpunit ``` -- To set up a particular version of a tool, specify it in the form `tool:version`. The latest stable version of `composer` is set up by default. You can set up the required version by specifying `v1`, `v2`, `snapshot` or `preview` as version. +- To set up a particular version of a tool, specify it in the form `tool:version`. The latest stable version of `composer` is set up by default. You can set up the required `composer` version by specifying `v1`, `v2`, `snapshot` or `preview` as versions or the exact version in semver format. ```yaml - name: Setup PHP with composer v2 @@ -203,6 +203,8 @@ These tools can be setup globally using the `tools` input. tools: composer:v2 ``` +- If you have specified composer plugins `prestissimo` or `composer-prefetcher` in tools, the latest stable version of `composer v1` will be setup. Unless some of your packages require `composer v1`, it is recommended to drop `prestissimo` and use `composer v2`. + - The latest versions of both agent `blackfire-agent` and client `blackfire` are setup when `blackfire` is specified in tools input. Please refer to the [official documentation](https://blackfire.io/docs/integrations/ci/github-actions "Blackfire.io documentation for GitHub Actions") for using `blackfire` with GitHub Actions. - Version for other tools should be in `semver` format and a valid release of the tool. This is useful for installing tools for older versions of PHP. For example to set up `PHPUnit` on `PHP 7.2`. diff --git a/__tests__/tools.test.ts b/__tests__/tools.test.ts index 015caf08..1a371458 100644 --- a/__tests__/tools.test.ts +++ b/__tests__/tools.test.ts @@ -278,9 +278,15 @@ describe('Tools tests', () => { expect(await tools.getComposerUrl('1.7.2')).toContain( 'https://github.com/composer/composer/releases/download/1.7.2/composer.phar' ); + expect(await tools.getComposerUrl('1.7.2')).toContain( + 'https://getcomposer.org/composer-1.7.2.phar' + ); expect(await tools.getComposerUrl('2.0.0-RC2')).toContain( 'https://github.com/composer/composer/releases/download/2.0.0-RC2/composer.phar' ); + expect(await tools.getComposerUrl('2.0.0-RC2')).toContain( + 'https://getcomposer.org/composer-2.0.0-RC2.phar' + ); expect(await tools.getComposerUrl('wrong')).toContain( 'https://getcomposer.org/composer-stable.phar' ); diff --git a/dist/index.js b/dist/index.js index a13b9c05..8cc3b12d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2047,22 +2047,17 @@ exports.addComposer = addComposer; * @param version */ async function getComposerUrl(version) { - const cache_url = 'https://github.com/shivammathur/composer-cache/releases/latest/download/composer-' + - version.replace('latest', 'stable') + - '.phar,'; - switch (version) { - case 'snapshot': - return cache_url + 'https://getcomposer.org/composer.phar'; - case 'preview': - case '1': - case '2': - return (cache_url + 'https://getcomposer.org/composer-' + version + '.phar'); + let cache_url = `https://github.com/shivammathur/composer-cache/releases/latest/download/composer-${version.replace('latest', 'stable')}.phar`; + switch (true) { + case /^snapshot$/.test(version): + return `${cache_url},https://getcomposer.org/composer.phar`; + case /^preview$|^[1-2]$/.test(version): + return `${cache_url},https://getcomposer.org/composer-${version}.phar`; + case /^\d+\.\d+\.\d+[\w-]*$/.test(version): + cache_url = `https://github.com/composer/composer/releases/download/${version}/composer.phar`; + return `${cache_url},https://getcomposer.org/composer-${version}.phar`; default: - if (/^\d+\.\d+\.\d+[\w-]*$/.test(version)) { - return (cache_url + - `https://github.com/composer/composer/releases/download/${version}/composer.phar`); - } - return cache_url + 'https://getcomposer.org/composer-stable.phar'; + return `${cache_url},https://getcomposer.org/composer-stable.phar`; } } exports.getComposerUrl = getComposerUrl; diff --git a/src/tools.ts b/src/tools.ts index 5914099b..1a17502c 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -330,27 +330,20 @@ export async function addComposer(tools_list: string[]): Promise { * @param version */ export async function getComposerUrl(version: string): Promise { - const cache_url = - 'https://github.com/shivammathur/composer-cache/releases/latest/download/composer-' + - version.replace('latest', 'stable') + - '.phar,'; - switch (version) { - case 'snapshot': - return cache_url + 'https://getcomposer.org/composer.phar'; - case 'preview': - case '1': - case '2': - return ( - cache_url + 'https://getcomposer.org/composer-' + version + '.phar' - ); + let cache_url = `https://github.com/shivammathur/composer-cache/releases/latest/download/composer-${version.replace( + 'latest', + 'stable' + )}.phar`; + switch (true) { + case /^snapshot$/.test(version): + return `${cache_url},https://getcomposer.org/composer.phar`; + case /^preview$|^[1-2]$/.test(version): + return `${cache_url},https://getcomposer.org/composer-${version}.phar`; + case /^\d+\.\d+\.\d+[\w-]*$/.test(version): + cache_url = `https://github.com/composer/composer/releases/download/${version}/composer.phar`; + return `${cache_url},https://getcomposer.org/composer-${version}.phar`; default: - if (/^\d+\.\d+\.\d+[\w-]*$/.test(version)) { - return ( - cache_url + - `https://github.com/composer/composer/releases/download/${version}/composer.phar` - ); - } - return cache_url + 'https://getcomposer.org/composer-stable.phar'; + return `${cache_url},https://getcomposer.org/composer-stable.phar`; } }