Refactor to use setup-php
This commit is contained in:
13
src/cache.ts
13
src/cache.ts
@@ -1,6 +1,7 @@
|
||||
import {exec} from '@actions/exec';
|
||||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
import * as spu from 'setup-php/lib/utils';
|
||||
import * as path from 'path';
|
||||
import * as utils from './utils';
|
||||
|
||||
@@ -17,7 +18,7 @@ export async function handleDependencies(
|
||||
if (!/^5.[3-5]$/.test(version) && /linux|darwin/.test(process.platform)) {
|
||||
const cache_key: string = (await utils.getOutput('key')) + '-deps';
|
||||
const cache_dir: string = path.join(
|
||||
await utils.readEnv('RUNNER_TOOL_CACHE'),
|
||||
await spu.readEnv('RUNNER_TOOL_CACHE'),
|
||||
'deps'
|
||||
);
|
||||
const cache_hit: string | undefined = await cache.restoreCache(
|
||||
@@ -37,17 +38,17 @@ export async function handleDependencies(
|
||||
*/
|
||||
export async function run(): Promise<void> {
|
||||
try {
|
||||
const version: string = await utils.parseVersion(
|
||||
await utils.getInput('php-version', true)
|
||||
const version: string = await spu.parseVersion(
|
||||
await spu.getInput('php-version', true)
|
||||
);
|
||||
const extensions: string = await utils.filterExtensions(
|
||||
await utils.getInput('extensions', true)
|
||||
await spu.getInput('extensions', true)
|
||||
);
|
||||
const key: string = await utils.getInput('key', true);
|
||||
const key: string = await spu.getInput('key', true);
|
||||
await exec(await utils.scriptCall('data', extensions, version, key));
|
||||
await handleDependencies(extensions, version);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
core.setFailed((error as Error).message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
62
src/utils.ts
62
src/utils.ts
@@ -1,75 +1,17 @@
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
/**
|
||||
* Function to read environment variable and return a string value.
|
||||
*
|
||||
* @param property
|
||||
*/
|
||||
export async function readEnv(property: string): Promise<string> {
|
||||
const value = process.env[property];
|
||||
switch (value) {
|
||||
case undefined:
|
||||
return '';
|
||||
default:
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 = 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 '';
|
||||
}
|
||||
}
|
||||
import * as spu from 'setup-php/lib/utils';
|
||||
|
||||
/**
|
||||
* Function to get outputs
|
||||
*/
|
||||
export async function getOutput(output: string): Promise<string> {
|
||||
return fs.readFileSync(
|
||||
path.join(await readEnv('RUNNER_TEMP'), output),
|
||||
path.join(await spu.readEnv('RUNNER_TEMP'), output),
|
||||
'utf8'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to parse PHP version.
|
||||
*
|
||||
* @param version
|
||||
*/
|
||||
export async function parseVersion(version: string): Promise<string> {
|
||||
switch (version) {
|
||||
case 'latest':
|
||||
return '7.4';
|
||||
default:
|
||||
switch (true) {
|
||||
case version.length > 1:
|
||||
return version.slice(0, 3);
|
||||
default:
|
||||
return version + '.0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to filter extensions
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user