1
0
mirror of https://github.com/shivammathur/setup-php.git synced 2025-03-01 02:09:04 +07:00

494 lines
11 KiB
TypeScript
Raw Normal View History

2023-01-29 13:26:49 +08:00
import fs from 'fs';
2019-09-20 08:11:20 +05:30
import * as path from 'path';
import * as core from '@actions/core';
import * as fetch from './fetch';
2019-09-20 08:11:20 +05:30
2020-10-03 01:39:43 +05:30
/**
* Function to read environment variable and return a string value.
*
* @param property
*/
export async function readEnv(property: string): Promise<string> {
const property_lc: string = property.toLowerCase();
const property_uc: string = property.toUpperCase();
return (
process.env[property] ||
process.env[property_lc] ||
process.env[property_uc] ||
process.env[property_lc.replace('_', '-')] ||
process.env[property_uc.replace('_', '-')] ||
''
);
2020-10-03 01:39:43 +05:30
}
2019-10-11 23:09:05 +05:30
/**
* Function to get inputs from both with and env annotations.
*
* @param name
* @param mandatory
*/
2019-09-20 08:11:20 +05:30
export async function getInput(
name: string,
mandatory: boolean
): Promise<string> {
2020-10-03 01:39:43 +05:30
const input = core.getInput(name);
const env_input = await readEnv(name);
switch (true) {
case input != '':
return input;
2020-10-03 01:39:43 +05:30
case input == '' && env_input != '':
return env_input;
case input == '' && env_input == '' && mandatory:
throw new Error(`Input required and not supplied: ${name}`);
default:
return '';
2019-09-20 08:11:20 +05:30
}
}
2023-01-27 22:12:33 +01:00
/**
* Function to get manifest URL
*/
export async function getManifestURL(): Promise<string> {
return 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json';
}
2020-10-02 14:51:40 +05:30
/**
* Function to parse PHP version.
*
* @param version
*/
export async function parseVersion(version: string): Promise<string> {
switch (true) {
2021-09-06 11:58:31 +05:30
case /^(latest|nightly|\d+\.x)$/.test(version):
return JSON.parse((await fetch.fetch(await getManifestURL()))['data'])[
version
];
2020-10-02 14:51:40 +05:30
default:
switch (true) {
case version.length > 1:
return version.slice(0, 3);
default:
return version + '.0';
}
}
}
2022-01-25 13:43:49 +05:30
/**
* Function to parse ini file.
*
* @param ini_file
*/
export async function parseIniFile(ini_file: string): Promise<string> {
switch (true) {
case /^(production|development|none)$/.test(ini_file):
return ini_file;
case /php\.ini-(production|development)$/.test(ini_file):
return ini_file.split('-')[1];
default:
return 'production';
}
}
2019-09-20 08:11:20 +05:30
/**
* Async foreach loop
*
* @author https://github.com/Atinux
* @param array
* @param callback
*/
export async function asyncForEach(
2019-11-24 02:04:12 +05:30
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 08:11:20 +05:30
await callback(array[index], index, array);
}
}
2019-11-24 02:04:12 +05:30
/**
* 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
2022-01-29 05:29:58 +05:30
* @param os
2019-11-24 02:04:12 +05:30
* @param log_type
*/
export async function log(
message: string,
2022-01-29 05:29:58 +05:30
os: string,
2019-11-24 02:04:12 +05:30
log_type: string
): Promise<string> {
2022-01-29 05:29:58 +05:30
switch (os) {
2019-11-24 02:04:12 +05:30
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
2022-01-29 05:29:58 +05:30
* @param os
2019-11-24 02:04:12 +05:30
*/
2022-01-29 05:29:58 +05:30
export async function stepLog(message: string, os: string): Promise<string> {
switch (os) {
2019-11-24 02:04:12 +05:30
case 'win32':
return 'Step-Log "' + message + '"';
case 'linux':
case 'darwin':
return 'step_log "' + message + '"';
default:
2022-01-29 05:29:58 +05:30
return await log('Platform ' + os + ' is not supported', os, 'error');
2019-11-24 02:04:12 +05:30
}
}
/**
* Function to log a result
* @param mark
* @param subject
* @param message
2022-01-29 05:29:58 +05:30
* @param os
2019-11-24 02:04:12 +05:30
*/
export async function addLog(
mark: string,
subject: string,
message: string,
2022-01-29 05:29:58 +05:30
os: string
2019-11-24 02:04:12 +05:30
): Promise<string> {
2022-01-29 05:29:58 +05:30
switch (os) {
2019-11-24 02:04:12 +05:30
case 'win32':
return 'Add-Log "' + mark + '" "' + subject + '" "' + message + '"';
case 'linux':
case 'darwin':
return 'add_log "' + mark + '" "' + subject + '" "' + message + '"';
default:
2022-01-29 05:29:58 +05:30
return await log('Platform ' + os + ' is not supported', os, 'error');
2019-11-24 02:04:12 +05:30
}
}
2019-09-20 08:11:20 +05:30
/**
* Function to break extension csv into an array
2019-10-11 23:09:05 +05:30
*
2019-09-20 08:11:20 +05:30
* @param extension_csv
*/
export async function extensionArray(
extension_csv: string
): Promise<Array<string>> {
switch (extension_csv) {
case '':
case ' ':
return [];
default:
return [
extension_csv.match(/(^|,\s?)none(\s?,|$)/) ? 'none' : '',
...extension_csv
.split(',')
.map(function (extension: string) {
if (/.+-.+\/.+@.+/.test(extension)) {
return extension;
}
return extension
.trim()
.toLowerCase()
.replace(/^(:)?(php[-_]|none|zend )|(-[^-]*)-/, '$1$3');
})
].filter(Boolean);
}
2019-09-20 08:11:20 +05:30
}
/**
2019-12-27 06:56:49 +05:30
* Function to break csv into an array
2019-09-20 08:11:20 +05:30
*
2019-12-27 06:56:49 +05:30
* @param values_csv
2019-09-20 08:11:20 +05:30
* @constructor
*/
2019-12-27 06:56:49 +05:30
export async function CSVArray(values_csv: string): Promise<Array<string>> {
switch (values_csv) {
case '':
case ' ':
return [];
default:
2019-12-27 06:56:49 +05:30
return values_csv
.split(/,(?=(?:(?:[^"']*["']){2})*[^"']*$)/)
.map(function (value) {
return value
.trim()
.replace(/^["']|["']$|(?<==)["']/g, '')
2022-12-15 05:27:13 +05:30
.replace(/=(((?!E_).)*[?{}|&~![()^]+((?!E_).)+)/, "='$1'")
2023-06-12 05:40:08 +05:30
.replace(/=(.*?)(=.*)/, "='$1$2'")
.replace(/:\s*["'](.*?)/g, ':$1');
2019-12-27 06:56:49 +05:30
})
.filter(Boolean);
}
2019-09-20 08:11:20 +05:30
}
2019-10-11 23:09:05 +05:30
/**
* Function to get prefix required to load an extension.
*
* @param extension
*/
2019-09-28 08:49:11 +05:30
export async function getExtensionPrefix(extension: string): Promise<string> {
2020-12-24 18:20:42 +05:30
switch (true) {
2020-06-20 06:25:19 +05:30
default:
2019-09-28 08:49:11 +05:30
return 'extension';
2020-12-24 18:20:42 +05:30
case /xdebug([2-3])?$|opcache|ioncube|eaccelerator/.test(extension):
return 'zend_extension';
2019-09-28 08:49:11 +05:30
}
}
2019-10-17 01:41:13 +05:30
/**
* Function to get the suffix to suppress console output
*
2022-01-29 05:29:58 +05:30
* @param os
2019-10-17 01:41:13 +05:30
*/
2022-01-29 05:29:58 +05:30
export async function suppressOutput(os: string): Promise<string> {
switch (os) {
2019-10-17 01:41:13 +05:30
case 'win32':
return ' >$null 2>&1';
case 'linux':
case 'darwin':
return ' >/dev/null 2>&1';
default:
2022-01-29 05:29:58 +05:30
return await log('Platform ' + os + ' is not supported', os, 'error');
2019-10-17 01:41:13 +05:30
}
}
2020-07-24 10:28:48 +05:30
/**
* Function to get script to log unsupported extensions.
*
* @param extension
* @param version
2022-01-29 05:29:58 +05:30
* @param os
2020-07-24 10:28:48 +05:30
*/
export async function getUnsupportedLog(
extension: string,
version: string,
2022-01-29 05:29:58 +05:30
os: string
2020-07-24 10:28:48 +05:30
): Promise<string> {
return (
'\n' +
(await addLog(
'$cross',
extension,
[extension, 'is not supported on PHP', version].join(' '),
2022-01-29 05:29:58 +05:30
os
2020-07-24 10:28:48 +05:30
)) +
'\n'
);
}
2020-08-06 15:33:58 +05:30
/**
* Function to get command to setup tools
*
2022-01-29 05:29:58 +05:30
* @param os
2020-08-06 15:33:58 +05:30
* @param suffix
*/
2022-01-29 05:29:58 +05:30
export async function getCommand(os: string, suffix: string): Promise<string> {
switch (os) {
2020-08-06 15:33:58 +05:30
case 'linux':
case 'darwin':
return 'add_' + suffix + ' ';
case 'win32':
return (
'Add-' +
suffix
.split('_')
.map((part: string) => part.charAt(0).toUpperCase() + part.slice(1))
.join('') +
' '
);
2020-08-06 15:33:58 +05:30
default:
2022-01-29 05:29:58 +05:30
return await log('Platform ' + os + ' is not supported', os, 'error');
2020-08-06 15:33:58 +05:30
}
}
2020-07-24 10:28:48 +05:30
/**
* Function to join strings with space
*
* @param str
*/
export async function joins(...str: string[]): Promise<string> {
return [...str].join(' ');
}
/**
* Function to get script extensions
*
2022-01-29 05:29:58 +05:30
* @param os
*/
2022-01-29 05:29:58 +05:30
export async function scriptExtension(os: string): Promise<string> {
switch (os) {
case 'win32':
return '.ps1';
case 'linux':
case 'darwin':
return '.sh';
default:
2022-01-29 05:29:58 +05:30
return await log('Platform ' + os + ' is not supported', os, 'error');
}
}
2020-08-06 15:33:58 +05:30
2020-10-04 17:03:02 +05:30
/**
* Function to get script tool
*
2022-01-29 05:29:58 +05:30
* @param os
2020-10-04 17:03:02 +05:30
*/
2022-01-29 05:29:58 +05:30
export async function scriptTool(os: string): Promise<string> {
switch (os) {
2020-10-04 17:03:02 +05:30
case 'win32':
return 'pwsh ';
2020-10-04 17:03:02 +05:30
case 'linux':
case 'darwin':
return 'bash ';
2020-10-04 17:03:02 +05:30
default:
2022-01-29 05:29:58 +05:30
return await log('Platform ' + os + ' is not supported', os, 'error');
2020-10-04 17:03:02 +05:30
}
}
2020-08-06 15:33:58 +05:30
/**
* Function to get script to add tools with custom support.
*
* @param pkg
* @param type
* @param version
2022-01-29 05:29:58 +05:30
* @param os
2020-08-06 15:33:58 +05:30
*/
export async function customPackage(
pkg: string,
type: string,
version: string,
2022-01-29 05:29:58 +05:30
os: string
2020-08-06 15:33:58 +05:30
): Promise<string> {
const pkg_name: string = pkg.replace(/\d+|(pdo|pecl)[_-]/, '');
2022-01-29 05:29:58 +05:30
const script_extension: string = await scriptExtension(os);
2020-08-06 15:33:58 +05:30
const script: string = path.join(
__dirname,
'../src/scripts/' + type + '/' + pkg_name + script_extension
);
2022-01-29 05:29:58 +05:30
const command: string = await getCommand(os, pkg_name);
2020-08-06 15:33:58 +05:30
return '\n. ' + script + '\n' + command + version;
}
/**
* Function to extension input for installation from source.
*
* @param extension
* @param prefix
*/
export async function parseExtensionSource(
extension: string,
prefix: string
): Promise<string> {
// Groups: extension, domain url, org, repo, release
const regex =
/(\w+)-(\w+:\/\/.{1,253}(?:[.:][^:/\s]{2,63})+\/)?([\w.-]+)\/([\w.-]+)@(.+)/;
const matches = regex.exec(extension) as RegExpExecArray;
matches[2] = matches[2] ? matches[2].slice(0, -1) : 'https://github.com';
return await joins(
'\nadd_extension_from_source',
...matches.splice(1, matches.length),
prefix
);
}
2022-06-30 17:36:40 +05:30
2023-01-29 13:26:49 +08:00
/**
* Read php version from input or file
2023-01-29 13:26:49 +08:00
*/
export async function readPHPVersion(): Promise<string> {
2023-01-29 20:17:23 +05:30
const version = await getInput('php-version', false);
2023-01-29 13:26:49 +08:00
if (version) {
return version;
}
2023-01-29 20:17:23 +05:30
const versionFile =
(await getInput('php-version-file', false)) || '.php-version';
2023-01-29 13:26:49 +08:00
if (fs.existsSync(versionFile)) {
2023-04-02 20:25:49 +05:30
return fs.readFileSync(versionFile, 'utf8').replace(/[\r\n]/g, '');
2023-01-29 20:17:23 +05:30
} else if (versionFile !== '.php-version') {
throw new Error(`Could not find '${versionFile}' file.`);
2023-01-29 13:26:49 +08:00
}
const composerLock = 'composer.lock';
if (fs.existsSync(composerLock)) {
const lockFileContents = JSON.parse(fs.readFileSync(composerLock, 'utf8'));
if (
lockFileContents['platform-overrides'] &&
lockFileContents['platform-overrides']['php']
) {
return lockFileContents['platform-overrides']['php'];
}
}
const composerJson = 'composer.json';
if (fs.existsSync(composerJson)) {
const composerFileContents = JSON.parse(
fs.readFileSync(composerJson, 'utf8')
);
if (
composerFileContents['config'] &&
composerFileContents['config']['platform'] &&
composerFileContents['config']['platform']['php']
) {
return composerFileContents['config']['platform']['php'];
}
}
2023-01-29 20:17:23 +05:30
return 'latest';
2023-01-29 13:26:49 +08:00
}
2022-06-30 17:36:40 +05:30
/**
* Log to console
*
* @param variable
* @param command
* @param os
*/
export async function setVariable(
variable: string,
command: string,
os: string
): Promise<string> {
switch (os) {
case 'win32':
return '\n$' + variable + ' = ' + command + '\n';
case 'linux':
case 'darwin':
default:
return '\n' + variable + '="$(' + command + ')"\n';
}
}