From 0cf6bc4d0e5e0bbea2008de77abfbc8f12e7a4ee Mon Sep 17 00:00:00 2001 From: Wataru Kurashima Date: Thu, 25 Aug 2022 13:42:53 +0900 Subject: [PATCH] read PHP version from composer.json --- __tests__/utils.test.ts | 18 ++++++++++++++++++ dist/index.js | 31 +++++++++++++++++++++++++++++-- src/install.ts | 4 +--- src/utils.ts | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 5 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 45b01b0d..50732785 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -52,6 +52,24 @@ describe('Utils tests', () => { expect(await utils.parseVersion('4.x')).toBe(undefined); }); + it('checking resolveVersionInput', async () => { + expect(utils.parsePhpVersionFile('{"require": {"php": "^8.1"}}')).toBe( + '8.1' + ); + expect(utils.parsePhpVersionFile('{"require": {"php": "8.1.1"}}')).toBe( + '8.1' + ); + expect( + utils.parsePhpVersionFile('{"config": {"platform": {"php": "8.1"}}}') + ).toBe('8.1'); + expect( + utils.parsePhpVersionFile('{"config": {"platform": {"php": "8.1.1"}}}') + ).toBe('8.1'); + expect(utils.parsePhpVersionFile('{"require": {"php": "^8.1|^8.0"}}')).toBe( + '8.1' + ); + }); + it('checking parseIniFile', async () => { expect(await utils.parseIniFile('production')).toBe('production'); expect(await utils.parseIniFile('development')).toBe('development'); diff --git a/dist/index.js b/dist/index.js index dbe3b8c5..d0aa2242 100644 --- a/dist/index.js +++ b/dist/index.js @@ -547,7 +547,7 @@ async function getScript(os) { const ini_values_csv = await utils.getInput('ini-values', false); const coverage_driver = await utils.getInput('coverage', false); const tools_csv = await utils.getInput('tools', false); - const version = await utils.parseVersion(await utils.getInput('php-version', true)); + const version = await utils.resolveVersionInput(); const ini_file = await utils.parseIniFile(await utils.getInput('ini-file', false)); let script = await utils.joins('.', script_path, version, ini_file); if (extension_csv) { @@ -1019,10 +1019,11 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.setVariable = exports.parseExtensionSource = exports.customPackage = exports.scriptTool = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseIniFile = exports.parseVersion = exports.getManifestURL = exports.getInput = exports.readEnv = void 0; +exports.setVariable = exports.parseExtensionSource = exports.customPackage = exports.scriptTool = exports.scriptExtension = exports.joins = exports.getCommand = exports.getUnsupportedLog = exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseIniFile = exports.parseVersion = exports.getManifestURL = exports.parsePhpVersionFile = exports.resolveVersionInput = exports.getInput = exports.readEnv = void 0; const path = __importStar(__nccwpck_require__(17)); const core = __importStar(__nccwpck_require__(186)); const fetch = __importStar(__nccwpck_require__(387)); +const fs = __nccwpck_require__(147); async function readEnv(property) { const property_lc = property.toLowerCase(); const property_uc = property.toUpperCase(); @@ -1049,6 +1050,32 @@ async function getInput(name, mandatory) { } } exports.getInput = getInput; +async function resolveVersionInput() { + let version = core.getInput('php-version'); + const version_file = core.getInput('php-version-file'); + if (version && version_file) { + core.info('Both php-version and php-version-file inputs are specified, only php-version will be used'); + } + if (version) { + return version; + } + if (version_file) { + const version_file_path = path.join(process.env.GITHUB_WORKSPACE, version_file); + if (!fs.existsSync(version_file_path)) { + throw new Error(`The specified php version file at: ${version_file_path} does not exist`); + } + version = parsePhpVersionFile(fs.readFileSync(version_file_path, 'utf8')); + } + return version; +} +exports.resolveVersionInput = resolveVersionInput; +function parsePhpVersionFile(contents) { + const json = JSON.parse(contents); + const version = json.config?.platform?.php ?? json.require?.php; + const match = version.match(/(\d+(\.\d+))/); + return match ? match[1] : ''; +} +exports.parsePhpVersionFile = parsePhpVersionFile; async function getManifestURL() { return 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json'; } diff --git a/src/install.ts b/src/install.ts index 4a09200d..269fc2de 100644 --- a/src/install.ts +++ b/src/install.ts @@ -23,9 +23,7 @@ export async function getScript(os: string): Promise { const ini_values_csv: string = await utils.getInput('ini-values', false); const coverage_driver: string = await utils.getInput('coverage', false); const tools_csv: string = await utils.getInput('tools', false); - const version: string = await utils.parseVersion( - await utils.getInput('php-version', true) - ); + const version: string = await utils.resolveVersionInput(); const ini_file: string = await utils.parseIniFile( await utils.getInput('ini-file', false) ); diff --git a/src/utils.ts b/src/utils.ts index 2a3b1ac2..206e5674 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,7 @@ import * as path from 'path'; import * as core from '@actions/core'; import * as fetch from './fetch'; +import fs = require('fs'); /** * Function to read environment variable and return a string value. @@ -44,6 +45,43 @@ export async function getInput( } } +export async function resolveVersionInput(): Promise { + let version = core.getInput('php-version'); + const version_file = core.getInput('php-version-file'); + + if (version && version_file) { + core.info( + 'Both php-version and php-version-file inputs are specified, only php-version will be used' + ); + } + + if (version) { + return version; + } + + if (version_file) { + const version_file_path = path.join( + process.env.GITHUB_WORKSPACE!, + version_file + ); + if (!fs.existsSync(version_file_path)) { + throw new Error( + `The specified php version file at: ${version_file_path} does not exist` + ); + } + version = parsePhpVersionFile(fs.readFileSync(version_file_path, 'utf8')); + } + + return version; +} + +export function parsePhpVersionFile(contents: string): string { + const json = JSON.parse(contents); + const version = json.config?.platform?.php ?? json.require?.php; + const match = version.match(/(\d+(\.\d+))/); + return match ? match[1] : ''; +} + /** Function to get manifest URL * */