setup-php/src/utils.ts
2020-03-14 07:53:43 +05:30

258 lines
5.0 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import * as core from '@actions/core';
/**
* Function to get inputs from both with and env annotations.
*
* @param name
* @param mandatory
*/
export async function getInput(
name: string,
mandatory: boolean
): Promise<string> {
const input = process.env[name];
switch (input) {
case '':
case undefined:
return core.getInput(name, {required: mandatory});
default:
return input;
}
}
/**
* Async foreach loop
*
* @author https://github.com/Atinux
* @param array
* @param callback
*/
export async function asyncForEach(
array: Array<string>,
callback: (
element: string,
index: number,
array: Array<string>
) => Promise<void>
): Promise<void> {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
/**
* 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
* @param os_version
*/
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'
);
}
}
/**
* Read the scripts
*
* @param filename
*/
export async function readScript(filename: string): Promise<string> {
return fs.readFileSync(
path.join(__dirname, '../src/scripts/' + filename),
'utf8'
);
}
/**
* Write final script which runs
*
* @param filename
* @param script
*/
export async function writeScript(
filename: string,
script: string
): Promise<string> {
const runner_dir: string = await getInput('RUNNER_TOOL_CACHE', false);
const script_path: string = path.join(runner_dir, filename);
fs.writeFileSync(script_path, script, {mode: 0o755});
return script_path;
}
/**
* Function to break extension csv into an array
*
* @param extension_csv
*/
export async function extensionArray(
extension_csv: string
): Promise<Array<string>> {
switch (extension_csv) {
case '':
case ' ':
return [];
default:
return extension_csv
.split(',')
.map(function(extension: string) {
return extension
.trim()
.toLowerCase()
.replace('php-', '')
.replace('php_', '');
})
.filter(Boolean);
}
}
/**
* Function to break csv into an array
*
* @param values_csv
* @constructor
*/
export async function CSVArray(values_csv: string): Promise<Array<string>> {
switch (values_csv) {
case '':
case ' ':
return [];
default:
return values_csv
.split(',')
.map(function(value: string) {
return value.trim();
})
.filter(Boolean);
}
}
/**
* Function to get prefix required to load an extension.
*
* @param extension
*/
export async function getExtensionPrefix(extension: string): Promise<string> {
const zend: Array<string> = ['xdebug', 'opcache', 'ioncube', 'eaccelerator'];
switch (zend.indexOf(extension)) {
case 0:
case 1:
return 'zend_extension';
case -1:
default:
return 'extension';
}
}
/**
* 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'
);
}
}