Add support for cs2pr and refactor tools code

This commit is contained in:
Shivam Mathur 2020-02-02 23:51:44 +05:30
parent 3454287b87
commit d3760be2cc
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
6 changed files with 70 additions and 28 deletions

View File

@ -96,13 +96,13 @@ describe('Tools tests', () => {
it('checking getUri', async () => { it('checking getUri', async () => {
expect( expect(
await tools.getUri('tool', 'latest', 'releases', '', 'download') await tools.getUri('tool', '.phar', 'latest', 'releases', '', 'download')
).toBe('releases/latest/download/tool.phar'); ).toBe('releases/latest/download/tool.phar');
expect( 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'); ).toBe('releases/download/1.2.3/tool.phar');
expect( 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'); ).toBe('releases/download/v1.2.3/tool.phar');
}); });
@ -325,13 +325,16 @@ describe('Tools tests', () => {
it('checking addTools on linux', async () => { it('checking addTools on linux', async () => {
const script: string = await tools.addTools( 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', '7.4',
'linux' 'linux'
); );
expect(script).toContain( expect(script).toContain(
'add_tool https://github.com/composer/composer/releases/latest/download/composer.phar composer' '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( expect(script).toContain(
'add_tool https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/latest/download/php-cs-fixer.phar php-cs-fixer' '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 () => { it('checking addTools on darwin', async () => {
const script: string = await tools.addTools( 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', '7.4',
'darwin' 'darwin'
); );
expect(script).toContain( expect(script).toContain(
'add_tool https://github.com/composer/composer/releases/latest/download/composer.phar composer' '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( expect(script).toContain(
'add_tool https://github.com/squizlabs/PHP_CodeSniffer/releases/latest/download/phpcs.phar phpcs' '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 () => { it('checking addTools on windows', async () => {
const script: string = await tools.addTools( 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', '7.4',
'win32' 'win32'
); );
expect(script).toContain( expect(script).toContain(
'Add-Tool https://github.com/composer/composer/releases/latest/download/composer.phar composer' '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( expect(script).toContain(
'Add-Tool https://deployer.org/deployer.phar deployer' 'Add-Tool https://deployer.org/deployer.phar deployer'
); );

17
dist/index.js vendored
View File

@ -1684,13 +1684,15 @@ exports.parseTool = parseTool;
* @param version_prefix * @param version_prefix
* @param verb * @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* () { return __awaiter(this, void 0, void 0, function* () {
switch (version) { switch (version) {
case 'latest': case 'latest':
return [prefix, version, verb, tool + '.phar'].filter(Boolean).join('/'); return [prefix, version, verb, tool + extension]
.filter(Boolean)
.join('/');
default: default:
return [prefix, verb, version_prefix + version, tool + '.phar'] return [prefix, verb, version_prefix + version, tool + extension]
.filter(Boolean) .filter(Boolean)
.join('/'); .join('/');
} }
@ -1940,12 +1942,17 @@ function addTools(tools_csv, php_version, os_version) {
const tool = tool_data.name; const tool = tool_data.name;
const version = tool_data.version; const version = tool_data.version;
const github = 'https://github.com/'; const github = 'https://github.com/';
let uri = yield getUri(tool, version, 'releases', '', 'download'); let uri = yield getUri(tool, '.phar', version, 'releases', '', 'download');
script += '\n'; script += '\n';
let url = ''; let url = '';
switch (tool) { 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': 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; url = github + 'FriendsOfPHP/PHP-CS-Fixer/' + uri;
script += yield addArchive(tool, version, url, os_version); script += yield addArchive(tool, version, url, os_version);
break; break;

View File

@ -87,21 +87,26 @@ add_tool() {
composer -q global config process-timeout 0 composer -q global config process-timeout 0
add_log "$tick" "$tool" "Added" add_log "$tick" "$tool" "Added"
else else
if [ ! -e /usr/local/bin/"$tool" ]; then tool_path=/usr/local/bin/"$tool"
rm -rf /usr/local/bin/"${tool:?}" if [ ! -e "$tool_path" ]; then
rm -rf "$tool_path"
fi 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 if [ "$status_code" = "200" ]; then
sudo chmod a+x /usr/local/bin/"$tool" sudo chmod a+x "$tool_path"
add_log "$tick" "$tool" "Added"
else
add_log "$cross" "$tool" "Could not setup $tool"
fi
fi
if [ "$tool" = "phive" ]; then if [ "$tool" = "phive" ]; then
add_extension curl >/dev/null 2>&1 add_extension curl >/dev/null 2>&1
add_extension mbstring >/dev/null 2>&1 add_extension mbstring >/dev/null 2>&1
add_extension xml >/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 fi
} }

View File

@ -140,14 +140,17 @@ update_extension() {
add_tool() { add_tool() {
url=$1 url=$1
tool=$2 tool=$2
if [ ! -e /usr/local/bin/"$tool" ]; then tool_path=/usr/local/bin/"$tool"
rm -rf /usr/local/bin/"${tool:?}" if [ ! -e "$tool_path" ]; then
rm -rf "$tool_path"
fi 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 if [ "$status_code" = "200" ]; then
sudo chmod a+x /usr/local/bin/"$tool" sudo chmod a+x "$tool_path"
if [ "$tool" = "composer" ]; then if [ "$tool" = "composer" ]; then
composer -q global config process-timeout 0 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 elif [ "$tool" = "phive" ]; then
add_extension curl >/dev/null 2>&1 add_extension curl >/dev/null 2>&1
add_extension mbstring >/dev/null 2>&1 add_extension mbstring >/dev/null 2>&1

View File

@ -114,6 +114,9 @@ Function Add-Tool() {
Add-Extension mbstring >$null 2>&1 Add-Extension mbstring >$null 2>&1
Add-Extension xml >$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)) { if (((Get-ChildItem -Path $php_dir/* | Where-Object Name -Match "^$tool(.exe|.phar)*$").Count -gt 0)) {
Add-Log $tick $tool "Added" Add-Log $tick $tool "Added"
} else { } else {

View File

@ -116,6 +116,7 @@ export async function parseTool(
*/ */
export async function getUri( export async function getUri(
tool: string, tool: string,
extension: string,
version: string, version: string,
prefix: string, prefix: string,
version_prefix: string, version_prefix: string,
@ -123,9 +124,11 @@ export async function getUri(
): Promise<string> { ): Promise<string> {
switch (version) { switch (version) {
case 'latest': case 'latest':
return [prefix, version, verb, tool + '.phar'].filter(Boolean).join('/'); return [prefix, version, verb, tool + extension]
.filter(Boolean)
.join('/');
default: default:
return [prefix, verb, version_prefix + version, tool + '.phar'] return [prefix, verb, version_prefix + version, tool + extension]
.filter(Boolean) .filter(Boolean)
.join('/'); .join('/');
} }
@ -404,12 +407,24 @@ export async function addTools(
const tool: string = tool_data.name; const tool: string = tool_data.name;
const version: string = tool_data.version; const version: string = tool_data.version;
const github = 'https://github.com/'; 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'; script += '\n';
let url = ''; let url = '';
switch (tool) { 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': 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; url = github + 'FriendsOfPHP/PHP-CS-Fixer/' + uri;
script += await addArchive(tool, version, url, os_version); script += await addArchive(tool, version, url, os_version);
break; break;