mirror of
https://github.com/shivammathur/setup-php.git
synced 2024-11-22 20:01:06 +07:00
Add support to specify version in \d.x notation
This commit is contained in:
parent
d0f1a91dfd
commit
5520fdb61a
@ -8,6 +8,12 @@ jest.mock('@actions/core', () => ({
|
||||
})
|
||||
}));
|
||||
|
||||
jest.spyOn(utils, 'fetch').mockImplementation(
|
||||
async (url): Promise<string> => {
|
||||
return `{ "latest": "8.0", "5.x": "5.6", "url": "${url}" }`;
|
||||
}
|
||||
);
|
||||
|
||||
async function cleanup(path: string): Promise<void> {
|
||||
fs.unlink(path, error => {
|
||||
if (error) {
|
||||
@ -32,6 +38,20 @@ describe('Utils tests', () => {
|
||||
}).rejects.toThrow('Input required and not supplied: DoesNotExist');
|
||||
});
|
||||
|
||||
it('checking fetch', async () => {
|
||||
expect(await utils.fetch('test_url')).toBe(
|
||||
'{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }'
|
||||
);
|
||||
});
|
||||
|
||||
it('checking parseVersion', async () => {
|
||||
expect(await utils.parseVersion('latest')).toBe('8.0');
|
||||
expect(await utils.parseVersion('7')).toBe('7.0');
|
||||
expect(await utils.parseVersion('7.4')).toBe('7.4');
|
||||
expect(await utils.parseVersion('5.x')).toBe('5.6');
|
||||
expect(await utils.parseVersion('4.x')).toBe(undefined);
|
||||
});
|
||||
|
||||
it('checking asyncForEach', async () => {
|
||||
const array: Array<string> = ['a', 'b', 'c'];
|
||||
let concat = '';
|
||||
|
58
dist/index.js
vendored
58
dist/index.js
vendored
@ -1054,8 +1054,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.getInput = exports.readEnv = void 0;
|
||||
exports.suppressOutput = exports.getExtensionPrefix = exports.CSVArray = exports.extensionArray = exports.writeScript = exports.readScript = exports.addLog = exports.stepLog = exports.log = exports.color = exports.asyncForEach = exports.parseVersion = exports.fetch = exports.getInput = exports.readEnv = void 0;
|
||||
const fs = __importStar(__webpack_require__(747));
|
||||
const https = __importStar(__webpack_require__(211));
|
||||
const path = __importStar(__webpack_require__(622));
|
||||
const core = __importStar(__webpack_require__(470));
|
||||
/**
|
||||
@ -1094,6 +1095,44 @@ async function getInput(name, mandatory) {
|
||||
}
|
||||
}
|
||||
exports.getInput = getInput;
|
||||
/**
|
||||
* Function to fetch an URL
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
async function fetch(url) {
|
||||
const fetch_promise = new Promise(resolve => {
|
||||
const req = https.get(url, (res) => {
|
||||
res.setEncoding('utf8');
|
||||
let body = '';
|
||||
res.on('data', chunk => (body += chunk));
|
||||
res.on('end', () => resolve(body));
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
return await fetch_promise;
|
||||
}
|
||||
exports.fetch = fetch;
|
||||
/**
|
||||
* Function to parse PHP version.
|
||||
*
|
||||
* @param version
|
||||
*/
|
||||
async function parseVersion(version) {
|
||||
const manifest = 'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json';
|
||||
switch (true) {
|
||||
case /^(latest|\d+\.x)$/.test(version):
|
||||
return JSON.parse(await fetch(manifest))[version];
|
||||
default:
|
||||
switch (true) {
|
||||
case version.length > 1:
|
||||
return version.slice(0, 3);
|
||||
default:
|
||||
return version + '.0';
|
||||
}
|
||||
}
|
||||
}
|
||||
exports.parseVersion = parseVersion;
|
||||
/**
|
||||
* Async foreach loop
|
||||
*
|
||||
@ -1285,6 +1324,13 @@ async function suppressOutput(os_version) {
|
||||
exports.suppressOutput = suppressOutput;
|
||||
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 211:
|
||||
/***/ (function(module) {
|
||||
|
||||
module.exports = require("https");
|
||||
|
||||
/***/ }),
|
||||
|
||||
/***/ 357:
|
||||
@ -2418,13 +2464,13 @@ exports.build = build;
|
||||
async function run() {
|
||||
try {
|
||||
core.warning('setup-php v1 is deprecated.\nPlease upgrade to v2 - https://github.com/shivammathur/setup-php/wiki/Switch-to-v2');
|
||||
const os_version = process.platform;
|
||||
let version = await utils.getInput('php-version', true);
|
||||
version = version.length > 1 ? version.slice(0, 3) : version + '.0';
|
||||
const version = await utils.parseVersion(await utils.getInput('php-version', true));
|
||||
if (version == '8.1') {
|
||||
core.setFailed('PHP 8.1 is not supported on setup-php v1.\nPlease upgrade to v2 - https://github.com/shivammathur/setup-php/wiki/Switch-to-v2');
|
||||
return;
|
||||
}
|
||||
if (version) {
|
||||
const os_version = process.platform;
|
||||
// check the os version and run the respective script
|
||||
let script_path = '';
|
||||
switch (os_version) {
|
||||
@ -2439,6 +2485,10 @@ async function run() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
core.setFailed('Unable to get the PHP version');
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
6
src/configs/php-versions.json
Normal file
6
src/configs/php-versions.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"latest": "8.0",
|
||||
"5.x": "5.6",
|
||||
"7.x": "7.4",
|
||||
"8.x": "8.0"
|
||||
}
|
@ -58,15 +58,17 @@ export async function run(): Promise<void> {
|
||||
core.warning(
|
||||
'setup-php v1 is deprecated.\nPlease upgrade to v2 - https://github.com/shivammathur/setup-php/wiki/Switch-to-v2'
|
||||
);
|
||||
const os_version: string = process.platform;
|
||||
let version: string = await utils.getInput('php-version', true);
|
||||
version = version.length > 1 ? version.slice(0, 3) : version + '.0';
|
||||
const version: string = await utils.parseVersion(
|
||||
await utils.getInput('php-version', true)
|
||||
);
|
||||
if (version == '8.1') {
|
||||
core.setFailed(
|
||||
'PHP 8.1 is not supported on setup-php v1.\nPlease upgrade to v2 - https://github.com/shivammathur/setup-php/wiki/Switch-to-v2'
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (version) {
|
||||
const os_version: string = process.platform;
|
||||
// check the os version and run the respective script
|
||||
let script_path = '';
|
||||
switch (os_version) {
|
||||
@ -80,6 +82,9 @@ export async function run(): Promise<void> {
|
||||
await exec('pwsh ' + script_path + ' ' + version + ' ' + __dirname);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
core.setFailed('Unable to get the PHP version');
|
||||
}
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
|
41
src/utils.ts
41
src/utils.ts
@ -1,4 +1,6 @@
|
||||
import {IncomingMessage} from 'http';
|
||||
import * as fs from 'fs';
|
||||
import * as https from 'https';
|
||||
import * as path from 'path';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
@ -41,6 +43,45 @@ export async function getInput(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to fetch an URL
|
||||
*
|
||||
* @param url
|
||||
*/
|
||||
export async function fetch(url: string): Promise<string> {
|
||||
const fetch_promise: Promise<string> = new Promise(resolve => {
|
||||
const req = https.get(url, (res: IncomingMessage) => {
|
||||
res.setEncoding('utf8');
|
||||
let body = '';
|
||||
res.on('data', chunk => (body += chunk));
|
||||
res.on('end', () => resolve(body));
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
return await fetch_promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to parse PHP version.
|
||||
*
|
||||
* @param version
|
||||
*/
|
||||
export async function parseVersion(version: string): Promise<string> {
|
||||
const manifest =
|
||||
'https://raw.githubusercontent.com/shivammathur/setup-php/develop/src/configs/php-versions.json';
|
||||
switch (true) {
|
||||
case /^(latest|\d+\.x)$/.test(version):
|
||||
return JSON.parse(await fetch(manifest))[version];
|
||||
default:
|
||||
switch (true) {
|
||||
case version.length > 1:
|
||||
return version.slice(0, 3);
|
||||
default:
|
||||
return version + '.0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Async foreach loop
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user