setup-php/src/utils.ts

365 lines
7.2 KiB
TypeScript
Raw Normal View History

2019-09-20 09:41:20 +07:00
import * as fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';
2019-10-12 00:39:05 +07:00
/**
* Function to get inputs from both with and env annotations.
*
* @param name
* @param mandatory
*/
2019-09-20 09:41:20 +07:00
export async function getInput(
name: string,
mandatory: boolean
): Promise<string> {
2019-11-24 03:34:12 +07:00
const input = process.env[name];
switch (input) {
case '':
case undefined:
return core.getInput(name, {required: mandatory});
default:
return input;
2019-09-20 09:41:20 +07:00
}
}
/**
* Async foreach loop
*
* @author https://github.com/Atinux
* @param array
* @param callback
*/
export async function asyncForEach(
2019-11-24 03:34:12 +07:00
array: Array<string>,
callback: (
element: string,
index: number,
array: Array<string>
) => Promise<void>
): Promise<void> {
for (let index = 0; index < array.length; index++) {
2019-09-20 09:41:20 +07:00
await callback(array[index], index, array);
}
}
2019-11-24 03:34:12 +07:00
/**
* Get color index
*
* @param type
*/
export async function color(type: string): Promise<string> {
switch (type) {
case 'error':
return '31';
default:
case 'success':
return '32';
case 'warning':
return '33';
}
}
/**
* Log to console
*
* @param message
* @param os_version
* @param log_type
*/
export async function log(
message: string,
os_version: string,
log_type: string
): Promise<string> {
switch (os_version) {
case 'win32':
return (
'printf "\\033[' +
(await color(log_type)) +
';1m' +
message +
' \\033[0m"'
);
case 'linux':
case 'darwin':
default:
return (
'echo "\\033[' + (await color(log_type)) + ';1m' + message + '\\033[0m"'
);
}
}
/**
* Function to log a step
*
* @param message
* @param os_version
*/
export async function stepLog(
message: string,
os_version: string
): Promise<string> {
switch (os_version) {
case 'win32':
return 'Step-Log "' + message + '"';
case 'linux':
case 'darwin':
return 'step_log "' + message + '"';
default:
return await log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
}
/**
* Function to log a result
* @param mark
* @param subject
* @param message
2020-03-12 09:27:40 +07:00
* @param os_version
2019-11-24 03:34:12 +07:00
*/
export async function addLog(
mark: string,
subject: string,
message: string,
os_version: string
): Promise<string> {
switch (os_version) {
case 'win32':
return 'Add-Log "' + mark + '" "' + subject + '" "' + message + '"';
case 'linux':
case 'darwin':
return 'add_log "' + mark + '" "' + subject + '" "' + message + '"';
default:
return await log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
}
2019-09-20 09:41:20 +07:00
/**
* Read the scripts
*
* @param filename
*/
2020-03-12 09:27:40 +07:00
export async function readScript(filename: string): Promise<string> {
2019-11-30 04:59:00 +07:00
return fs.readFileSync(
path.join(__dirname, '../src/scripts/' + filename),
'utf8'
);
2019-09-20 09:41:20 +07:00
}
/**
* Write final script which runs
*
* @param filename
* @param script
*/
export async function writeScript(
filename: string,
script: string
2019-10-27 07:12:49 +07:00
): Promise<string> {
2019-11-24 03:34:12 +07:00
const runner_dir: string = await getInput('RUNNER_TOOL_CACHE', false);
const script_path: string = path.join(runner_dir, filename);
2019-10-27 07:12:49 +07:00
fs.writeFileSync(script_path, script, {mode: 0o755});
return script_path;
2019-09-20 09:41:20 +07:00
}
/**
* Function to break extension csv into an array
2019-10-12 00:39:05 +07:00
*
2019-09-20 09:41:20 +07:00
* @param extension_csv
*/
export async function extensionArray(
extension_csv: string
): Promise<Array<string>> {
switch (extension_csv) {
case '':
case ' ':
return [];
default:
2019-12-27 08:26:49 +07:00
return extension_csv
.split(',')
2020-05-08 07:11:00 +07:00
.map(function (extension: string) {
2019-12-27 08:26:49 +07:00
return extension
.trim()
2020-03-12 06:17:52 +07:00
.toLowerCase()
2019-12-27 08:26:49 +07:00
.replace('php-', '')
.replace('php_', '');
})
.filter(Boolean);
}
2019-09-20 09:41:20 +07:00
}
/**
2019-12-27 08:26:49 +07:00
* Function to break csv into an array
2019-09-20 09:41:20 +07:00
*
2019-12-27 08:26:49 +07:00
* @param values_csv
2019-09-20 09:41:20 +07:00
* @constructor
*/
2019-12-27 08:26:49 +07:00
export async function CSVArray(values_csv: string): Promise<Array<string>> {
switch (values_csv) {
case '':
case ' ':
return [];
default:
2019-12-27 08:26:49 +07:00
return values_csv
.split(',')
2020-05-08 07:11:00 +07:00
.map(function (value: string) {
2019-12-27 08:26:49 +07:00
return value.trim();
})
.filter(Boolean);
}
2019-09-20 09:41:20 +07:00
}
2019-10-12 00:39:05 +07:00
/**
* Function to get prefix required to load an extension.
*
* @param extension
*/
2019-09-28 10:19:11 +07:00
export async function getExtensionPrefix(extension: string): Promise<string> {
2020-06-20 07:55:19 +07:00
const zend: Array<string> = [
'xdebug',
'xdebug3',
'opcache',
'ioncube',
'eaccelerator'
];
2019-09-28 10:19:11 +07:00
switch (zend.indexOf(extension)) {
2020-06-20 07:55:19 +07:00
default:
2019-09-28 10:19:11 +07:00
return 'zend_extension';
case -1:
return 'extension';
}
}
2019-10-17 03:11:13 +07:00
/**
* Function to get the suffix to suppress console output
*
* @param os_version
*/
export async function suppressOutput(os_version: string): Promise<string> {
switch (os_version) {
case 'win32':
return ' >$null 2>&1';
case 'linux':
case 'darwin':
return ' >/dev/null 2>&1';
default:
return await log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
}
2020-07-24 11:58:48 +07:00
/**
* Function to get script to log unsupported extensions.
*
* @param extension
* @param version
* @param os_version
*/
export async function getUnsupportedLog(
extension: string,
version: string,
os_version: string
): Promise<string> {
return (
'\n' +
(await addLog(
'$cross',
extension,
[extension, 'is not supported on PHP', version].join(' '),
os_version
)) +
'\n'
);
}
2020-08-06 17:03:58 +07:00
/**
* Function to get command to setup tools
*
* @param os_version
* @param suffix
*/
export async function getCommand(
os_version: string,
suffix: string
): Promise<string> {
switch (os_version) {
case 'linux':
case 'darwin':
return 'add_' + suffix + ' ';
case 'win32':
return 'Add-' + suffix.charAt(0).toUpperCase() + suffix.slice(1) + ' ';
default:
return await log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
}
2020-07-24 11:58:48 +07:00
/**
* Function to join strings with space
*
* @param str
*/
export async function joins(...str: string[]): Promise<string> {
return [...str].join(' ');
}
/**
* Function to get script extensions
*
* @param os_version
*/
export async function scriptExtension(os_version: string): Promise<string> {
switch (os_version) {
case 'win32':
return '.ps1';
case 'linux':
case 'darwin':
return '.sh';
default:
return await log(
'Platform ' + os_version + ' is not supported',
os_version,
'error'
);
}
}
2020-08-06 17:03:58 +07:00
/**
* Function to get script to add tools with custom support.
*
* @param pkg
* @param type
* @param version
* @param os_version
*/
export async function customPackage(
pkg: string,
type: string,
version: string,
os_version: string
): Promise<string> {
const pkg_name: string = pkg.replace(/\d+|pdo[_-]/, '');
const script_extension: string = await scriptExtension(os_version);
const script: string = path.join(
__dirname,
'../src/scripts/' + type + '/' + pkg_name + script_extension
);
const command: string = await getCommand(os_version, pkg_name);
return '\n. ' + script + '\n' + command + version;
}