From d3760be2ccb00aac79d97ea008397ad2b697cf0e Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sun, 2 Feb 2020 23:51:44 +0530 Subject: [PATCH] Add support for cs2pr and refactor tools code --- __tests__/tools.test.ts | 21 +++++++++++++++------ dist/index.js | 17 ++++++++++++----- src/scripts/darwin.sh | 23 ++++++++++++++--------- src/scripts/linux.sh | 11 +++++++---- src/scripts/win32.ps1 | 3 +++ src/tools.ts | 23 +++++++++++++++++++---- 6 files changed, 70 insertions(+), 28 deletions(-) diff --git a/__tests__/tools.test.ts b/__tests__/tools.test.ts index b2f134ea..6c7394dd 100644 --- a/__tests__/tools.test.ts +++ b/__tests__/tools.test.ts @@ -96,13 +96,13 @@ describe('Tools tests', () => { it('checking getUri', async () => { expect( - await tools.getUri('tool', 'latest', 'releases', '', 'download') + await tools.getUri('tool', '.phar', 'latest', 'releases', '', 'download') ).toBe('releases/latest/download/tool.phar'); expect( - await tools.getUri('tool', '1.2.3', 'releases', '', 'download') + await tools.getUri('tool', '.phar', '1.2.3', 'releases', '', 'download') ).toBe('releases/download/1.2.3/tool.phar'); expect( - await tools.getUri('tool', '1.2.3', 'releases', 'v', 'download') + await tools.getUri('tool', '.phar', '1.2.3', 'releases', 'v', 'download') ).toBe('releases/download/v1.2.3/tool.phar'); }); @@ -325,13 +325,16 @@ describe('Tools tests', () => { it('checking addTools on linux', async () => { const script: string = await tools.addTools( - 'php-cs-fixer, phpstan, phpunit, pecl, phinx, phinx:1.2.3, phive, php-config, phpize, symfony', + 'cs2pr, php-cs-fixer, phpstan, phpunit, pecl, phinx, phinx:1.2.3, phive, php-config, phpize, symfony', '7.4', 'linux' ); expect(script).toContain( 'add_tool https://github.com/composer/composer/releases/latest/download/composer.phar composer' ); + expect(script).toContain( + 'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr' + ); expect(script).toContain( 'add_tool https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/latest/download/php-cs-fixer.phar php-cs-fixer' ); @@ -356,13 +359,16 @@ describe('Tools tests', () => { }); it('checking addTools on darwin', async () => { const script: string = await tools.addTools( - 'phpcs, phpcbf, phpcpd, phpmd, psalm, phinx, phive:1.2.3, composer-prefetcher:1.2.3, phpize, php-config, symfony, symfony:1.2.3', + 'phpcs, phpcbf, phpcpd, phpmd, psalm, phinx, phive:1.2.3, cs2pr:1.2.3, composer-prefetcher:1.2.3, phpize, php-config, symfony, symfony:1.2.3', '7.4', 'darwin' ); expect(script).toContain( 'add_tool https://github.com/composer/composer/releases/latest/download/composer.phar composer' ); + expect(script).toContain( + 'add_tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/download/1.2.3/cs2pr cs2pr' + ); expect(script).toContain( 'add_tool https://github.com/squizlabs/PHP_CodeSniffer/releases/latest/download/phpcs.phar phpcs' ); @@ -396,13 +402,16 @@ describe('Tools tests', () => { }); it('checking addTools on windows', async () => { const script: string = await tools.addTools( - 'codeception, deployer, prestissimo, phpmd, phinx, phive:0.13.2, php-config, phpize, symfony, does_not_exit', + 'codeception, cs2pr, deployer, prestissimo, phpmd, phinx, phive:0.13.2, php-config, phpize, symfony, does_not_exit', '7.4', 'win32' ); expect(script).toContain( 'Add-Tool https://github.com/composer/composer/releases/latest/download/composer.phar composer' ); + expect(script).toContain( + 'Add-Tool https://github.com/staabm/annotate-pull-request-from-checkstyle/releases/latest/download/cs2pr cs2pr' + ); expect(script).toContain( 'Add-Tool https://deployer.org/deployer.phar deployer' ); diff --git a/dist/index.js b/dist/index.js index 9da436c5..5525fcea 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1684,13 +1684,15 @@ exports.parseTool = parseTool; * @param version_prefix * @param verb */ -function getUri(tool, version, prefix, version_prefix, verb) { +function getUri(tool, extension, version, prefix, version_prefix, verb) { return __awaiter(this, void 0, void 0, function* () { switch (version) { case 'latest': - return [prefix, version, verb, tool + '.phar'].filter(Boolean).join('/'); + return [prefix, version, verb, tool + extension] + .filter(Boolean) + .join('/'); default: - return [prefix, verb, version_prefix + version, tool + '.phar'] + return [prefix, verb, version_prefix + version, tool + extension] .filter(Boolean) .join('/'); } @@ -1940,12 +1942,17 @@ function addTools(tools_csv, php_version, os_version) { const tool = tool_data.name; const version = tool_data.version; const github = 'https://github.com/'; - let uri = yield getUri(tool, version, 'releases', '', 'download'); + let uri = yield getUri(tool, '.phar', version, 'releases', '', 'download'); script += '\n'; let url = ''; switch (tool) { + case 'cs2pr': + uri = yield getUri(tool, '', version, 'releases', '', 'download'); + url = github + 'staabm/annotate-pull-request-from-checkstyle/' + uri; + script += yield addArchive(tool, version, url, os_version); + break; case 'php-cs-fixer': - uri = yield getUri(tool, version, 'releases', 'v', 'download'); + uri = yield getUri(tool, '.phar', version, 'releases', 'v', 'download'); url = github + 'FriendsOfPHP/PHP-CS-Fixer/' + uri; script += yield addArchive(tool, version, url, os_version); break; diff --git a/src/scripts/darwin.sh b/src/scripts/darwin.sh index de04fd30..d2972565 100644 --- a/src/scripts/darwin.sh +++ b/src/scripts/darwin.sh @@ -87,22 +87,27 @@ add_tool() { composer -q global config process-timeout 0 add_log "$tick" "$tool" "Added" else - if [ ! -e /usr/local/bin/"$tool" ]; then - rm -rf /usr/local/bin/"${tool:?}" + tool_path=/usr/local/bin/"$tool" + if [ ! -e "$tool_path" ]; then + rm -rf "$tool_path" fi - status_code=$(sudo curl -s -w "%{http_code}" -o /usr/local/bin/"$tool" -L "$url") + + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" -L "$url") if [ "$status_code" = "200" ]; then - sudo chmod a+x /usr/local/bin/"$tool" + sudo chmod a+x "$tool_path" + if [ "$tool" = "phive" ]; then + add_extension curl >/dev/null 2>&1 + add_extension mbstring >/dev/null 2>&1 + add_extension xml >/dev/null 2>&1 + elif [ "$tool" = "cs2pr" ]; then + sudo sed -i '' 's/exit(9)/exit(0)/' "$tool_path" + tr -d '\r' < "$tool_path" | sudo tee "$tool_path" >/dev/null 2>&1 + fi add_log "$tick" "$tool" "Added" else add_log "$cross" "$tool" "Could not setup $tool" fi fi - if [ "$tool" = "phive" ]; then - add_extension curl >/dev/null 2>&1 - add_extension mbstring >/dev/null 2>&1 - add_extension xml >/dev/null 2>&1 - fi } # Function to add a tool using composer diff --git a/src/scripts/linux.sh b/src/scripts/linux.sh index 0f0894b9..e75e88f5 100644 --- a/src/scripts/linux.sh +++ b/src/scripts/linux.sh @@ -140,14 +140,17 @@ update_extension() { add_tool() { url=$1 tool=$2 - if [ ! -e /usr/local/bin/"$tool" ]; then - rm -rf /usr/local/bin/"${tool:?}" + tool_path=/usr/local/bin/"$tool" + if [ ! -e "$tool_path" ]; then + rm -rf "$tool_path" fi - status_code=$(sudo curl -s -w "%{http_code}" -o /usr/local/bin/"$tool" -L "$url") + status_code=$(sudo curl -s -w "%{http_code}" -o "$tool_path" -L "$url") if [ "$status_code" = "200" ]; then - sudo chmod a+x /usr/local/bin/"$tool" + sudo chmod a+x "$tool_path" if [ "$tool" = "composer" ]; then composer -q global config process-timeout 0 + elif [ "$tool" = "cs2pr" ]; then + sudo sed -i 's/\r$//; s/exit(9)/exit(0)/' "$tool_path" elif [ "$tool" = "phive" ]; then add_extension curl >/dev/null 2>&1 add_extension mbstring >/dev/null 2>&1 diff --git a/src/scripts/win32.ps1 b/src/scripts/win32.ps1 index ab31bd77..082fbe94 100644 --- a/src/scripts/win32.ps1 +++ b/src/scripts/win32.ps1 @@ -114,6 +114,9 @@ Function Add-Tool() { Add-Extension mbstring >$null 2>&1 Add-Extension xml >$null 2>&1 } + if($tool -eq "cs2pr") { + (Get-Content $php_dir/cs2pr).replace('exit(9)', 'exit(0)') | Set-Content $php_dir/cs2pr + } if (((Get-ChildItem -Path $php_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) { Add-Log $tick $tool "Added" } else { diff --git a/src/tools.ts b/src/tools.ts index 7275ea91..0b816983 100644 --- a/src/tools.ts +++ b/src/tools.ts @@ -116,6 +116,7 @@ export async function parseTool( */ export async function getUri( tool: string, + extension: string, version: string, prefix: string, version_prefix: string, @@ -123,9 +124,11 @@ export async function getUri( ): Promise { switch (version) { case 'latest': - return [prefix, version, verb, tool + '.phar'].filter(Boolean).join('/'); + return [prefix, version, verb, tool + extension] + .filter(Boolean) + .join('/'); default: - return [prefix, verb, version_prefix + version, tool + '.phar'] + return [prefix, verb, version_prefix + version, tool + extension] .filter(Boolean) .join('/'); } @@ -404,12 +407,24 @@ export async function addTools( const tool: string = tool_data.name; const version: string = tool_data.version; const github = 'https://github.com/'; - let uri: string = await getUri(tool, version, 'releases', '', 'download'); + let uri: string = await getUri( + tool, + '.phar', + version, + 'releases', + '', + 'download' + ); script += '\n'; let url = ''; switch (tool) { + case 'cs2pr': + uri = await getUri(tool, '', version, 'releases', '', 'download'); + url = github + 'staabm/annotate-pull-request-from-checkstyle/' + uri; + script += await addArchive(tool, version, url, os_version); + break; case 'php-cs-fixer': - uri = await getUri(tool, version, 'releases', 'v', 'download'); + uri = await getUri(tool, '.phar', version, 'releases', 'v', 'download'); url = github + 'FriendsOfPHP/PHP-CS-Fixer/' + uri; script += await addArchive(tool, version, url, os_version); break;