From e201830bdd5eba128fc26dba5fbfe1893489f167 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 | 7 +++++-- __tests__/tools.test.ts | 6 ++++++ dist/index.js | 25 ++++++++++--------------- src/tools.ts | 33 +++++++++++++-------------------- 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index 4421ec27..d4fb6b89 100644 --- a/README.md +++ b/README.md @@ -85,8 +85,9 @@ with: tools: php-cs-fixer, phpunit ``` -To setup a particular version of a tool, specify it in the form `tool:version`. -Latest stable version of `composer` is setup by default and accepts `v1`, `v2`, `snapshot` and `preview` as versions. +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 uses: shivammathur/setup-php@v1 @@ -95,6 +96,8 @@ with: 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`. + Version for other tools should be in `semver` format and a valid release of the tool. ```yaml diff --git a/__tests__/tools.test.ts b/__tests__/tools.test.ts index 295699cc..faddc7d1 100644 --- a/__tests__/tools.test.ts +++ b/__tests__/tools.test.ts @@ -296,9 +296,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 c8def310..81df151b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1919,22 +1919,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 e46a5471..0f79a8fd 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -307,27 +307,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`; } }