import { IncomingMessage , OutgoingHttpHeaders } from 'http' ; import * as fs from 'fs' ; import * as https from 'https' ; import * as path from 'path' ; import * as url from 'url' ; import * as core from '@actions/core' ; param property export async function readEnv ( property ) { const property_lc = property . toLowerCase ( ) ; const property_uc = property . toUpperCase ( ) ; return ( process . env [ property ] || process . env [ property_lc ] || process . env [ property_uc ] || process . env [ property_lc . replace ( '_' , '-' ) ] || process . env [ property_uc . replace ( '_' , '-' ) ] || '' ) ; } param name param mandatory export async function getInput ( name , mandatory ) { const input = core . getInput ( name ) ; const env_input = await readEnv ( name ) ; switch ( true ) { case input != '' : return input ; case input == '' && env_input != '' : return env_input ; case input == '' && env_input == '' && mandatory : throw new Error ( `Input required and not supplied: ${ name }` ) ; default : return '' ; } } param input_url param auth_token export async function fetch ( input_url , auth_token ? ) { const fetch_promise = new Promise ( resolve => { const url_object = new url . URL ( input_url ) ; const headers = { 'User-Agent' : `Mozilla/5.0 (${ process . platform } ${ process . arch }) setup-php` } ; if ( auth_token ) { headers . authorization = 'Bearer ' + auth_token ; } const options = { hostname : url_object . hostname , path : url_object . pathname , headers : headers } ; const req = https . get ( options , ( res ) => { if ( res . statusCode != 200 ) { resolve ( { error : `${ res . statusCode }: ${ res . statusMessage }` } ) ; } else { let body = '' ; res . setEncoding ( 'utf8' ) ; res . on ( 'data' , chunk => ( body += chunk ) ) ; res . on ( 'end' , ( ) => resolve ( { data : `${ body }` } ) ) ; } } ) ; req . end ( ) ; } ) ; return await fetch_promise ; } /** Function to get manifest URL
 *
 */ export async function getManifestURL ( ) { return 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json' ; } param version export async function parseVersion ( version ) { const manifest = await getManifestURL ( ) ; switch ( true ) { case /^(latest|nightly|\d+\.x)$/ . test ( version ) : return JSON . parse ( ( await fetch ( manifest ) ) [ 'data' ] ) [ version ] ; default : switch ( true ) { case version . length > 1 : return version . slice ( 0 , 3 ) ; default : return version + '.0' ; } } } author https://github.com/Atinux
 *  param array param callback export async function asyncForEach ( array , callback ) { for ( let index = 0 ; index < array . length ; index ++ ) { await callback ( array [ index ] , index , array ) ; } } param type export async function color ( type ) { switch ( type ) { case 'error' : return '31' ; default : case 'success' : return '32' ; case 'warning' : return '33' ; } } param message param os_version param log_type export async function log ( message , os_version , log_type ) { 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"' ) ; } } param message param os_version export async function stepLog ( message , os_version ) { 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' ) ; } } param mark param subject param message param os_version export async function addLog ( mark , subject , message , os_version ) { 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' ) ; } } param filename param directory export async function readFile ( filename , directory ) { return fs . readFileSync ( path . join ( __dirname , '../' + directory , filename ) , 'utf8' ) ; } param filename param script export async function writeScript ( filename , script ) { const runner_dir = await getInput ( 'RUNNER_TOOL_CACHE' , false ) ; const script_path = path . join ( runner_dir , filename ) ; fs . writeFileSync ( script_path , script , { mode : 0o755 } ) ; return script_path ; } param extension_csv export async function extensionArray ( extension_csv ) { switch ( extension_csv ) { case '' : case ' ' : return [ ] ; default : return [ extension_csv . match ( /(^|,\s?)none(\s?,|$)/ ) ? 'none' : '' , ... extension_csv . split ( ',' ) . map ( function ( extension ) { if ( /.+-.+\/.+@.+/ . test ( extension ) ) { return extension ; } return extension . trim ( ) . toLowerCase ( ) . replace ( /^(:)?(php[-_]|none|zend )|(-[^-]*)-/ , '$1$3' ) ; } ) ] . filter ( Boolean ) ; } } param values_csv constructor export async function CSVArray ( values_csv ) { switch ( values_csv ) { case '' : case ' ' : return [ ] ; default : return values_csv . split ( /,(?=(?:(?:[^"']*["']){2})*[^"']*$)/ ) . map ( function ( value ) { return value . trim ( ) . replace ( /^["']|["']$|(?<==)["']/g , '' ) . replace ( /=(((?!E_).)*[?{}|&~![()^]+((?!E_).)+)/ , "='$1'" ) ; } ) . filter ( Boolean ) ; } } param extension export async function getExtensionPrefix ( extension ) { switch ( true ) { default : return 'extension' ; case /xdebug([2-3])?$|opcache|ioncube|eaccelerator/ . test ( extension ) : return 'zend_extension' ; } } param os_version export async function suppressOutput ( os_version ) { 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' ) ; } } param extension param version param os_version export async function getUnsupportedLog ( extension , version , os_version ) { return ( '\n' + ( await addLog ( '$cross' , extension , [ extension , 'is not supported on PHP' , version ] . join ( ' ' ) , os_version ) ) + '\n' ) ; } param os_version param suffix export async function getCommand ( os_version , suffix ) { 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' ) ; } } param str export async function joins ( ... str ) { return [ ... str ] . join ( ' ' ) ; } param os_version export async function scriptExtension ( os_version ) { 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' ) ; } } param os_version export async function scriptTool ( os_version ) { switch ( os_version ) { case 'win32' : return 'pwsh' ; case 'linux' : case 'darwin' : return 'bash' ; default : return await log ( 'Platform ' + os_version + ' is not supported' , os_version , 'error' ) ; } } param pkg param type param version param os_version export async function customPackage ( pkg , type , version , os_version ) { const pkg_name = pkg . replace ( /\d+|(pdo|pecl)[_-]/ , '' ) ; const script_extension = await scriptExtension ( os_version ) ; const script = path . join ( __dirname , '../src/scripts/' + type + '/' + pkg_name + script_extension ) ; const command = await getCommand ( os_version , pkg_name ) ; return '\n. ' + script + '\n' + command + version ; } param extension param prefix export async function parseExtensionSource ( extension , prefix ) { const regex = /(\w+)-(.+:\/\/.+(?:[.:].+)+\/)?([\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 ) ; }