Add support for .tool-versions format in php-version-file

This commit is contained in:
Shivam Mathur 2024-12-23 15:27:22 +05:30
parent 84f76b1fc9
commit b6d8115f12
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
3 changed files with 14 additions and 2 deletions

View File

@ -285,6 +285,14 @@ describe('Utils tests', () => {
delete process.env['php-version-file']; delete process.env['php-version-file'];
delete process.env['php-version']; delete process.env['php-version'];
existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('ruby 1.2.3\nphp 8.4.2\nnode 20.1.2');
expect(await utils.readPHPVersion()).toBe('8.4.2');
existsSync.mockReturnValue(true);
readFileSync.mockReturnValue('setup-php');
expect(await utils.readPHPVersion()).toBe('setup-php');
existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true); existsSync.mockReturnValueOnce(false).mockReturnValueOnce(true);
readFileSync.mockReturnValue( readFileSync.mockReturnValue(
'{ "platform-overrides": { "php": "7.3.25" } }' '{ "platform-overrides": { "php": "7.3.25" } }'

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@ -436,7 +436,11 @@ export async function readPHPVersion(): Promise<string> {
const versionFile = const versionFile =
(await getInput('php-version-file', false)) || '.php-version'; (await getInput('php-version-file', false)) || '.php-version';
if (fs.existsSync(versionFile)) { if (fs.existsSync(versionFile)) {
return fs.readFileSync(versionFile, 'utf8').replace(/[\r\n]/g, ''); const contents: string = fs.readFileSync(versionFile, 'utf8');
const match: RegExpMatchArray | null = contents.match(
/^(?:php\s)?(\d+\.\d+\.\d+)$/m
);
return match ? match[1] : contents.trim();
} else if (versionFile !== '.php-version') { } else if (versionFile !== '.php-version') {
throw new Error(`Could not find '${versionFile}' file.`); throw new Error(`Could not find '${versionFile}' file.`);
} }