From 3455b4ca37f83916c55ae9219586780932008cf0 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Wed, 3 Jun 2020 14:51:22 +0530 Subject: [PATCH] Add support for updating composer --- __tests__/tools.test.ts | 55 +++++++++++++++++++++++++++++++++++++++++ dist/index.js | 44 +++++++++++++++++++++++++-------- src/tools.ts | 47 ++++++++++++++++++++++++++++------- 3 files changed, 127 insertions(+), 19 deletions(-) diff --git a/__tests__/tools.test.ts b/__tests__/tools.test.ts index 7185c883..9649ea5a 100644 --- a/__tests__/tools.test.ts +++ b/__tests__/tools.test.ts @@ -243,6 +243,41 @@ describe('Tools tests', () => { expect( await tools.addComposer(['a', 'b', 'composer:1.2.3']) ).toStrictEqual(['composer', 'a', 'b']); + expect( + await tools.addComposer(['a', 'b', 'composer:snapshot']) + ).toStrictEqual(['composer:snapshot', 'a', 'b']); + expect( + await tools.addComposer(['a', 'b', 'composer:preview']) + ).toStrictEqual(['composer:preview', 'a', 'b']); + expect( + await tools.addComposer(['a', 'b', 'c', 'composer:1']) + ).toStrictEqual(['composer:1', 'a', 'b', 'c']); + expect( + await tools.addComposer(['a', 'b', 'c', 'composer:2']) + ).toStrictEqual(['composer:2', 'a', 'b', 'c']); + expect( + await tools.addComposer(['a', 'b', 'c', 'composer:v1']) + ).toStrictEqual(['composer:1', 'a', 'b', 'c']); + expect( + await tools.addComposer(['a', 'b', 'c', 'composer:v2']) + ).toStrictEqual(['composer:2', 'a', 'b', 'c']); + }); + + it('checking updateComposer', async () => { + expect(await tools.updateComposer('latest', 'linux')).toContain(''); + expect(await tools.updateComposer('stable', 'win32')).toContain(''); + expect(await tools.updateComposer('snapshot', 'darwin')).toContain( + '\ncomposer self-update --snapshot' + ); + expect(await tools.updateComposer('preview', 'linux')).toContain( + '\ncomposer self-update --preview' + ); + expect(await tools.updateComposer('1', 'win32')).toContain( + '\ncomposer self-update --1' + ); + expect(await tools.updateComposer('2', 'darwin')).toContain( + '\ncomposer self-update --2' + ); }); it('checking getSymfonyUri', async () => { @@ -485,4 +520,24 @@ describe('Tools tests', () => { 'Add-Composertool composer-prefetcher composer-prefetcher narrowspark/automatic-' ); }); + it('checking composer setup', async () => { + let script: string = await tools.addTools( + 'composer, composer:v1', + '7.4', + 'linux' + ); + expect(script).toContain( + 'add_tool https://getcomposer.org/composer-stable.phar composer' + ); + expect(script).toContain('composer self-update --1'); + + script = await tools.addTools('composer:preview', '7.4', 'linux'); + expect(script).toContain('composer self-update --preview'); + script = await tools.addTools( + 'composer:v1, composer:preview, composer:snapshot', + '7.4', + 'linux' + ); + expect(script).toContain('composer self-update --snapshot'); + }); }); diff --git a/dist/index.js b/dist/index.js index e869411a..d0cbf493 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1603,7 +1603,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.addTools = exports.addPackage = exports.addDevTools = exports.addArchive = exports.getCleanedToolsList = exports.addComposer = exports.getSymfonyUri = exports.getDeployerUrl = exports.getPharUrl = exports.addPhive = exports.getCodeceptionUri = exports.getCodeceptionUriBuilder = exports.getUri = exports.parseTool = exports.getToolVersion = exports.getCommand = void 0; +exports.addTools = exports.addPackage = exports.addDevTools = exports.addArchive = exports.getCleanedToolsList = exports.updateComposer = exports.addComposer = exports.getSymfonyUri = exports.getDeployerUrl = exports.getPharUrl = exports.addPhive = exports.getCodeceptionUri = exports.getCodeceptionUriBuilder = exports.getUri = exports.parseTool = exports.getToolVersion = exports.getCommand = void 0; const utils = __importStar(__webpack_require__(163)); /** * Function to get command to setup tools @@ -1631,8 +1631,10 @@ exports.getCommand = getCommand; async function getToolVersion(version) { // semver_regex - https://semver.org/ const semver_regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; + const composer_regex = /^stable$|^preview$|^snapshot$|^v?[1|2]$/; version = version.replace(/[><=^]*/, ''); switch (true) { + case composer_regex.test(version): case semver_regex.test(version): return version; default: @@ -1843,19 +1845,42 @@ exports.getSymfonyUri = getSymfonyUri; * @param tools_list */ async function addComposer(tools_list) { - const regex = /^composer($|:.*)/; - const composer = tools_list.filter(tool => regex.test(tool))[0]; - switch (composer) { + const regex_any = /^composer($|:.*)/; + const regex_valid = /^composer:?($|preview$|snapshot$|v?[1-2]$)/; + const matches = tools_list.filter(tool => regex_valid.test(tool)); + let composer = 'composer'; + tools_list = tools_list.filter(tool => !regex_any.test(tool)); + switch (matches[0]) { case undefined: break; default: - tools_list = tools_list.filter(tool => !regex.test(tool)); + composer = matches[matches.length - 1].replace(/v([1-2])/, '$1'); break; } - tools_list.unshift('composer'); + tools_list.unshift(composer); return tools_list; } exports.addComposer = addComposer; +/** + * Function to get script to update composer + * + * @param version + * @param os_version + */ +async function updateComposer(version, os_version) { + switch (version) { + case 'snapshot': + case 'preview': + case '1': + case '2': + return ('\ncomposer self-update --' + + version + + (await utils.suppressOutput(os_version))); + default: + return ''; + } +} +exports.updateComposer = updateComposer; /** * Function to get Tools list after cleanup * @@ -1970,11 +1995,10 @@ async function addTools(tools_csv, php_version, os_version) { script += await addArchive(tool, version, url, os_version); break; case 'composer': - // If RC is released as latest release, switch to getcomposer. - // Preferred source is GitHub as it is faster. - // url = github + 'composer/composer/releases/latest/download/composer.phar'; url = 'https://getcomposer.org/composer-stable.phar'; - script += await addArchive(tool, version, url, os_version); + script += + (await addArchive('composer', version, url, os_version)) + + (await updateComposer(version, os_version)); break; case 'codeception': url = diff --git a/src/tools.ts b/src/tools.ts index d745ac27..298eb02b 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -33,8 +33,10 @@ export async function getCommand( export async function getToolVersion(version: string): Promise { // semver_regex - https://semver.org/ const semver_regex = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; + const composer_regex = /^stable$|^preview$|^snapshot$|^v?[1|2]$/; version = version.replace(/[><=^]*/, ''); switch (true) { + case composer_regex.test(version): case semver_regex.test(version): return version; default: @@ -279,19 +281,47 @@ export async function getSymfonyUri( * @param tools_list */ export async function addComposer(tools_list: string[]): Promise { - const regex = /^composer($|:.*)/; - const composer: string = tools_list.filter(tool => regex.test(tool))[0]; - switch (composer) { + const regex_any = /^composer($|:.*)/; + const regex_valid = /^composer:?($|preview$|snapshot$|v?[1-2]$)/; + const matches: string[] = tools_list.filter(tool => regex_valid.test(tool)); + let composer = 'composer'; + tools_list = tools_list.filter(tool => !regex_any.test(tool)); + switch (matches[0]) { case undefined: break; default: - tools_list = tools_list.filter(tool => !regex.test(tool)); + composer = matches[matches.length - 1].replace(/v([1-2])/, '$1'); break; } - tools_list.unshift('composer'); + tools_list.unshift(composer); return tools_list; } +/** + * Function to get script to update composer + * + * @param version + * @param os_version + */ +export async function updateComposer( + version: string, + os_version: string +): Promise { + switch (version) { + case 'snapshot': + case 'preview': + case '1': + case '2': + return ( + '\ncomposer self-update --' + + version + + (await utils.suppressOutput(os_version)) + ); + default: + return ''; + } +} + /** * Function to get Tools list after cleanup * @@ -443,11 +473,10 @@ export async function addTools( script += await addArchive(tool, version, url, os_version); break; case 'composer': - // If RC is released as latest release, switch to getcomposer. - // Preferred source is GitHub as it is faster. - // url = github + 'composer/composer/releases/latest/download/composer.phar'; url = 'https://getcomposer.org/composer-stable.phar'; - script += await addArchive(tool, version, url, os_version); + script += + (await addArchive('composer', version, url, os_version)) + + (await updateComposer(version, os_version)); break; case 'codeception': url =