setup-php/src/tools.ts

546 lines
15 KiB
TypeScript
Raw Normal View History

2019-12-27 08:26:49 +07:00
import * as utils from './utils';
2019-12-31 05:56:18 +07:00
/**
* Function to get tool version
*
* @param version
*/
export async function getToolVersion(version: string): Promise<string> {
// 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-]+)*))?$/;
2020-06-03 16:21:22 +07:00
const composer_regex = /^stable$|^preview$|^snapshot$|^v?[1|2]$/;
version = version.replace(/[><=^]*/, '');
switch (true) {
case version.charAt(0) == 'v':
return version.replace('v', '');
2020-06-03 16:21:22 +07:00
case composer_regex.test(version):
case semver_regex.test(version):
return version;
default:
return 'latest';
}
}
/**
* Function to parse tool:version
*
* @param release
*/
export async function parseTool(
release: string
): Promise<{name: string; version: string}> {
const parts: string[] = release.split(':');
const tool: string = parts[0];
const version: string | undefined = parts[1];
switch (true) {
case version === undefined:
return {
name: tool,
version: 'latest'
};
case /^[\w.-]+\/[\w.-]+$/.test(tool):
return {
name: tool,
version: version
};
default:
return {
name: tool,
version: await getToolVersion(parts[1])
};
}
}
/**
* Function to get the url of tool with the given version
*
2020-03-12 09:27:40 +07:00
* @param tool
* @param extension
* @param version
* @param prefix
* @param version_prefix
* @param verb
*/
export async function getUri(
tool: string,
extension: string,
version: string,
prefix: string,
version_prefix: string,
verb: string
): Promise<string> {
switch (version) {
case 'latest':
return [prefix, version, verb, tool + extension]
.filter(Boolean)
.join('/');
default:
return [prefix, verb, version_prefix + version, tool + extension]
.filter(Boolean)
.join('/');
}
}
2020-01-21 03:10:24 +07:00
/**
* Helper function to get script to setup phive
*
* @param version
2020-12-14 16:07:12 +07:00
* @param php_version
2020-01-21 03:10:24 +07:00
* @param os_version
*/
export async function addPhive(
version: string,
2020-12-14 16:07:12 +07:00
php_version: string,
2020-01-21 03:10:24 +07:00
os_version: string
): Promise<string> {
2020-12-14 16:07:12 +07:00
switch (true) {
case /5\.[3-5]/.test(php_version):
return await utils.addLog(
'$cross',
'phive',
'Phive is not supported on PHP ' + php_version,
os_version
);
case /5\.6|7\.0/.test(php_version):
version = version.replace('latest', '0.12.1');
break;
case /7\.1/.test(php_version):
version = version.replace('latest', '0.13.5');
break;
}
2020-01-21 03:10:24 +07:00
switch (version) {
case 'latest':
return (
2020-08-06 17:03:58 +07:00
(await utils.getCommand(os_version, 'tool')) +
2020-09-28 12:28:02 +07:00
'https://phar.io/releases/phive.phar phive status'
2020-01-21 03:10:24 +07:00
);
default:
return (
2020-08-06 17:03:58 +07:00
(await utils.getCommand(os_version, 'tool')) +
2020-01-21 03:10:24 +07:00
'https://github.com/phar-io/phive/releases/download/' +
version +
'/phive-' +
version +
2020-09-28 12:28:02 +07:00
'.phar phive status'
2020-01-21 03:10:24 +07:00
);
}
}
/**
* Function to get the phar url in domain/tool-version.phar format
*
2020-03-12 09:27:40 +07:00
* @param domain
* @param tool
* @param prefix
* @param version
*/
export async function getPharUrl(
domain: string,
tool: string,
prefix: string,
version: string
): Promise<string> {
switch (version) {
case 'latest':
return domain + '/' + tool + '.phar';
default:
return domain + '/' + tool + '-' + prefix + version + '.phar';
}
}
/**
* Function to get blackfire player url for a PHP version.
*
* @param version
* @param php_version
*/
export async function getBlackfirePlayerUrl(
version: string,
php_version: string
): Promise<string> {
switch (true) {
case /5\.[5-6]|7\.0/.test(php_version) && version == 'latest':
version = '1.9.3';
break;
default:
break;
}
return await getPharUrl(
'https://get.blackfire.io',
'blackfire-player',
'v',
version
);
}
/**
* Function to get the Deployer url
*
* @param version
*/
export async function getDeployerUrl(version: string): Promise<string> {
const deployer = 'https://deployer.org';
switch (version) {
case 'latest':
return deployer + '/deployer.phar';
default:
return deployer + '/releases/v' + version + '/deployer.phar';
}
}
2020-01-26 12:41:36 +07:00
/**
* Function to get the Deployer url
*
* @param version
* @param os_version
*/
export async function getSymfonyUri(
version: string,
os_version: string
): Promise<string> {
let filename = '';
switch (os_version) {
case 'linux':
case 'darwin':
filename = 'symfony_' + os_version + '_amd64';
break;
case 'win32':
filename = 'symfony_windows_amd64.exe';
break;
default:
return await utils.log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
switch (version) {
case 'latest':
return 'releases/latest/download/' + filename;
default:
return 'releases/download/v' + version + '/' + filename;
}
}
2020-02-22 11:36:14 +07:00
/**
* Function to get the WP-CLI url
*
* @param version
*/
export async function getWpCliUrl(version: string): Promise<string> {
switch (version) {
case 'latest':
2020-02-22 23:11:40 +07:00
return 'wp-cli/builds/blob/gh-pages/phar/wp-cli.phar?raw=true';
2020-02-22 11:36:14 +07:00
default:
2020-02-22 23:11:40 +07:00
return await getUri(
'wp-cli',
'-' + version + '.phar',
version,
'wp-cli/wp-cli/releases',
'v',
'download'
2020-02-22 11:36:14 +07:00
);
}
}
/**
* Function to add/move composer in the tools list
*
2020-03-12 09:27:40 +07:00
* @param tools_list
*/
export async function addComposer(tools_list: string[]): Promise<string[]> {
2020-06-03 16:21:22 +07:00
const regex_any = /^composer($|:.*)/;
const regex_valid =
/^composer:?($|preview$|snapshot$|v?[1-2]$|v?\d+\.\d+\.\d+[\w-]*$)/;
2020-06-03 16:21:22 +07:00
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 (true) {
case matches[0] == undefined:
break;
default:
2020-10-30 17:35:39 +07:00
composer = matches[matches.length - 1].replace(/v(\d\S*)/, '$1');
break;
}
2020-06-03 16:21:22 +07:00
tools_list.unshift(composer);
return tools_list;
}
2020-06-03 16:21:22 +07:00
/**
2020-07-07 18:06:32 +07:00
* Function to get composer URL for a given version
2020-06-03 16:21:22 +07:00
*
* @param version
*/
2020-07-07 18:06:32 +07:00
export async function getComposerUrl(version: string): Promise<string> {
2020-10-30 19:59:39 +07:00
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`;
2020-06-03 16:21:22 +07:00
default:
2020-10-30 19:59:39 +07:00
return `${cache_url},https://getcomposer.org/composer-stable.phar`;
2020-06-03 16:21:22 +07:00
}
}
/**
* Helper function to get script to setup a tool using a phar url
*
* @param tool
* @param url
* @param os_version
2020-09-28 12:28:02 +07:00
* @param ver_param
*/
export async function addArchive(
tool: string,
url: string,
2020-09-28 12:28:02 +07:00
os_version: string,
ver_param: string
): Promise<string> {
2020-09-28 12:28:02 +07:00
return (
(await utils.getCommand(os_version, 'tool')) +
(await utils.joins(url, tool, ver_param))
);
}
2020-01-17 14:28:28 +07:00
/**
* Function to get the script to setup php-config and phpize
*
* @param tool
* @param os_version
*/
export async function addDevTools(
tool: string,
os_version: string
): Promise<string> {
switch (os_version) {
case 'linux':
case 'darwin':
2020-09-28 12:28:02 +07:00
return 'add_devtools ' + tool;
2020-01-17 14:28:28 +07:00
case 'win32':
return await utils.addLog(
2020-10-17 05:43:36 +07:00
'$tick',
2020-01-17 14:28:28 +07:00
tool,
tool + ' is not a windows tool',
'win32'
);
default:
return await utils.log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
}
/**
* Helper function to get script to setup a tool using composer
*
* @param tool
* @param release
* @param prefix
* @param os_version
*/
export async function addPackage(
tool: string,
release: string,
prefix: string,
os_version: string
): Promise<string> {
2020-08-06 17:03:58 +07:00
const tool_command = await utils.getCommand(os_version, 'composertool');
return tool_command + tool + ' ' + release + ' ' + prefix;
}
2019-12-27 08:26:49 +07:00
/**
* Setup tools
*
2020-03-12 09:27:40 +07:00
* @param tools_csv
* @param php_version
2019-12-27 08:26:49 +07:00
* @param os_version
*/
export async function addTools(
tools_csv: string,
php_version: string,
2019-12-27 08:26:49 +07:00
os_version: string
): Promise<string> {
2021-04-21 18:41:24 +07:00
let script = '\n';
if (tools_csv === 'none') {
return '';
} else {
script += await utils.stepLog('Setup Tools', os_version);
}
const tools_list = await addComposer(await utils.CSVArray(tools_csv));
2020-05-08 07:11:00 +07:00
await utils.asyncForEach(tools_list, async function (release: string) {
const tool_data: {name: string; version: string} = await parseTool(release);
const tool: string = tool_data.name;
const version: string = tool_data.version;
const github = 'https://github.com/';
let uri: string = await getUri(
tool,
'.phar',
version,
'releases',
'',
'download'
);
2019-12-27 08:26:49 +07:00
script += '\n';
let url = '';
switch (true) {
case /^blackfire(-agent)?$/.test(tool):
script += await utils.customPackage(
'blackfire',
'tools',
version,
os_version
);
break;
case /^grpc_php_plugin$|^protoc$/.test(tool):
2020-08-06 17:03:58 +07:00
script += await utils.customPackage(tool, 'tools', version, os_version);
break;
case /^behat$|^codeception$|^phpspec$/.test(tool):
2020-10-05 10:41:15 +07:00
script += await addPackage(tool, release, tool + '/', os_version);
break;
case /^blackfire-player$/.test(tool):
url = await getBlackfirePlayerUrl(version, php_version);
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
break;
case /^composer$/.test(tool):
2020-07-19 17:18:00 +07:00
url = await getComposerUrl(version);
2020-09-28 12:28:02 +07:00
script += await addArchive('composer', url, os_version, version);
2020-07-19 17:18:00 +07:00
break;
case /^composer-normalize$/.test(tool):
uri = await getUri(tool, '.phar', version, 'releases', '', 'download');
url = github + 'ergebnis/composer-normalize/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
break;
case /^composer-prefetcher$/.test(tool):
2020-07-19 17:18:00 +07:00
script += await addPackage(
'automatic-' + tool,
2020-07-19 17:18:00 +07:00
release,
'narrowspark/',
2020-07-19 17:18:00 +07:00
os_version
);
break;
case /^composer-require-checker$/.test(tool):
uri = await getUri(tool, '.phar', version, 'releases', '', 'download');
url = github + 'maglnet/ComposerRequireChecker/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
break;
case /^composer-unused$/.test(tool):
2020-09-28 12:28:02 +07:00
script += await addPackage(tool, release, 'icanhazstring/', os_version);
break;
case /^cs2pr$/.test(tool):
uri = await getUri(tool, '', version, 'releases', '', 'download');
url = github + 'staabm/annotate-pull-request-from-checkstyle/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
break;
case /^deployer$/.test(tool):
2020-07-19 17:18:00 +07:00
url = await getDeployerUrl(version);
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
break;
case /^flex$/.test(tool):
2020-07-19 17:18:00 +07:00
script += await addPackage(tool, release, 'symfony/', os_version);
2019-12-27 08:26:49 +07:00
break;
case /^infection$/.test(tool):
2020-07-19 17:18:00 +07:00
url = github + 'infection/infection/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
2020-01-21 03:10:24 +07:00
break;
case /^pecl/.test(tool):
2020-08-06 17:03:58 +07:00
script += await utils.getCommand(os_version, 'pecl');
2020-07-19 17:18:00 +07:00
break;
case /^phan$/.test(tool):
2020-05-21 05:21:28 +07:00
url = github + 'phan/phan/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-v"');
2020-05-21 05:21:28 +07:00
break;
case /^phing$/.test(tool):
2020-08-24 13:41:17 +07:00
url = 'https://www.phing.info/get/phing-' + version + '.phar';
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-v"');
2020-08-24 13:41:17 +07:00
break;
case /^phinx$/.test(tool):
2020-07-19 17:18:00 +07:00
script += await addPackage(tool, release, 'robmorgan/', os_version);
break;
case /^phive$/.test(tool):
2020-12-14 16:07:12 +07:00
script += await addPhive(version, php_version, os_version);
2019-12-27 08:26:49 +07:00
break;
case /^php(-config|ize)$/.test(tool):
2020-07-19 17:18:00 +07:00
script += await addDevTools(tool, os_version);
2019-12-27 08:26:49 +07:00
break;
case /^php-cs-fixer$/.test(tool):
2020-07-19 17:18:00 +07:00
uri = await getUri(tool, '.phar', version, 'releases', 'v', 'download');
url = github + 'FriendsOfPHP/PHP-CS-Fixer/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
2019-12-28 02:12:00 +07:00
break;
case /^php(cbf|cs)$/.test(tool):
2020-07-19 17:18:00 +07:00
url = github + 'squizlabs/PHP_CodeSniffer/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"--version"');
2019-12-27 08:26:49 +07:00
break;
case /^php(cpd|unit)$/.test(tool):
url = await getPharUrl('https://phar.phpunit.de', tool, '', version);
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"--version"');
2019-12-27 08:26:49 +07:00
break;
case /^phplint$/.test(tool):
2020-06-24 06:00:50 +07:00
script += await addPackage(tool, release, 'overtrue/', os_version);
break;
case /^phpmd$/.test(tool):
2020-07-19 17:18:00 +07:00
url = github + 'phpmd/phpmd/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"--version"');
2019-12-27 08:26:49 +07:00
break;
case /^phpstan$/.test(tool):
2020-07-19 17:18:00 +07:00
url = github + 'phpstan/phpstan/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-V"');
break;
case /^prestissimo$/.test(tool):
2020-07-19 17:18:00 +07:00
script += await addPackage(tool, release, 'hirak/', os_version);
2019-12-27 08:26:49 +07:00
break;
case /^psalm$/.test(tool):
2020-07-19 17:18:00 +07:00
url = github + 'vimeo/psalm/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive(tool, url, os_version, '"-v"');
2020-01-17 14:28:28 +07:00
break;
case /^symfony(-cli)?$/.test(tool):
2020-01-26 12:41:36 +07:00
uri = await getSymfonyUri(version, os_version);
url = github + 'symfony/cli/' + uri;
2020-09-28 12:28:02 +07:00
script += await addArchive('symfony', url, os_version, 'version');
2020-01-26 12:41:36 +07:00
break;
case /^vapor(-cli)?$/.test(tool):
script += await addPackage(
'vapor-cli',
release,
'laravel/',
os_version
);
2020-07-19 17:18:00 +07:00
break;
case /^wp(-cli)?$/.test(tool):
2020-02-22 23:11:40 +07:00
url = github + (await getWpCliUrl(version));
script += await addArchive('wp-cli', url, os_version, '"--version"');
2020-02-22 11:36:14 +07:00
break;
2021-04-21 18:41:24 +07:00
case /^none$/.test(tool):
break;
case /^[\w.-]+\/[\w.-]+$/.test(tool):
script += await addPackage(
tool.split('/')[1],
release.split('/')[1].replace(/\s+/, ''),
tool.split('/')[0] + '/',
os_version
);
break;
2019-12-27 08:26:49 +07:00
default:
script += await utils.addLog(
'$cross',
tool,
2019-12-27 08:26:49 +07:00
'Tool ' + tool + ' is not supported',
os_version
2019-12-27 08:26:49 +07:00
);
break;
}
});
return script;
}