Skip to content

Commit

Permalink
Fix error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nanasess committed Mar 8, 2022
1 parent 7cd960c commit ceda18a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion __tests__/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as semver from 'semver';
import * as installer from '../src/installer';

jest.setTimeout(10000);
describe('example tests', () => {
it('testing', () => {
expect(true).toBe(true);
Expand Down
5 changes: 5 additions & 0 deletions lib/choco-install-php-windows.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Param(

choco install -y php --force --version $version --package-parameters='"/InstallDir:C:\tools\php"""'

Write-Host "`$LASTEXITCODE = $LASTEXITCODE"

cd c:\tools\php
copy php.ini-production php.ini
Write-Output extension_dir='C:/tools/php/ext' | Add-Content php.ini -Encoding Default
Expand All @@ -19,3 +21,6 @@ Write-Output extension=php_pdo_sqlite.dll | Add-Content php.ini -Encoding Defaul
Write-Output extension=php_pdo_mysql.dll | Add-Content php.ini -Encoding Default
Write-Output extension=php_pdo_pgsql.dll | Add-Content php.ini -Encoding Default
Write-Output extension=php_curl.dll | Add-Content php.ini -Encoding Default

Write-Host "`$LASTEXITCODE = $LASTEXITCODE"
exit $LASTEXITCODE
13 changes: 10 additions & 3 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ function installPhp(version) {
const installVersion = yield convertInstallVersion(version);
if (process.platform === 'linux') {
if (!hasPatchVersion(version) && hasAptVersion(version)) {
yield exec.exec(path.join(__dirname, 'apt-install-php-ubuntu.sh'), [
return yield exec.exec(path.join(__dirname, 'apt-install-php-ubuntu.sh'), [
new Number(version).toFixed(1)
]);
}
else {
yield exec.exec(path.join(__dirname, 'phpenv-install-php-ubuntu.sh'), [
return yield exec.exec(path.join(__dirname, 'phpenv-install-php-ubuntu.sh'), [
installVersion
]);
}
}
else if (process.platform === 'win32') {
yield exec.exec('powershell -File ' +
return yield exec.exec('powershell -File ' +
path.join(__dirname, 'choco-install-php-windows.ps1 -version ' + installVersion));
}
// Illegal process.platform
return -1;
});
}
exports.installPhp = installPhp;
Expand Down Expand Up @@ -92,6 +94,11 @@ function convertInstallVersion(version) {
case '8':
return version;
default:
// The last version of PHP7.3.x series in chocolatey is 7.3.30
// see https://community.chocolatey.org/packages/php/7.3.30
if (process.platform === 'win32' && version === '7.3') {
return '7.3.30';
}
const json = yield (0, node_fetch_1.default)(`${PHP_RELEASES_URL}&version=${version}`)
.then(response => response.json());
return json.version;
Expand Down
5 changes: 4 additions & 1 deletion lib/setup-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ function run() {
try {
const phpSpec = core.getInput('php-version', { required: true });
console.log(`##Installing PHP ${phpSpec}`);
yield (0, installer_1.installPhp)(phpSpec);
const exitCode = yield (0, installer_1.installPhp)(phpSpec);
if (exitCode !== 0) {
throw new Error(`An error occurred while installing PHP(Code: ${exitCode}`);
}
}
catch (error) {
if (error instanceof Error) {
Expand Down
16 changes: 12 additions & 4 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@ import fetch from 'node-fetch';

const PHP_RELEASES_URL = 'https://www.php.net/releases/index.php?json=true';

export async function installPhp(version: string) {
export async function installPhp(version: string): Promise<number> {
const installVersion = await convertInstallVersion(version);
if (process.platform === 'linux') {
if (!hasPatchVersion(version) && hasAptVersion(version)) {
await exec.exec(path.join(__dirname, 'apt-install-php-ubuntu.sh'), [
return await exec.exec(path.join(__dirname, 'apt-install-php-ubuntu.sh'), [
new Number(version).toFixed(1)
]);
} else {
await exec.exec(path.join(__dirname, 'phpenv-install-php-ubuntu.sh'), [
return await exec.exec(path.join(__dirname, 'phpenv-install-php-ubuntu.sh'), [
installVersion
]);
}
} else if (process.platform === 'win32') {
await exec.exec(
return await exec.exec(
'powershell -File ' +
path.join(
__dirname,
'choco-install-php-windows.ps1 -version ' + installVersion
)
);
}

// Illegal process.platform
return -1;
}
export function hasAptVersion(version: string): boolean {
if (hasPatchVersion(version)) return false;
Expand Down Expand Up @@ -57,6 +60,11 @@ export async function convertInstallVersion(version: string): Promise<string> {
case '8':
return version;
default:
// The last version of PHP7.3.x series in chocolatey is 7.3.30
// see https://community.chocolatey.org/packages/php/7.3.30
if (process.platform === 'win32' && version === '7.3') {
return '7.3.30';
}
const json = await fetch(`${PHP_RELEASES_URL}&version=${version}`)
.then(response => response.json()) as PHPReleaseJson;
return json.version;
Expand Down
5 changes: 4 additions & 1 deletion src/setup-php.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ async function run() {
try {
const phpSpec = core.getInput('php-version', {required: true});
console.log(`##Installing PHP ${phpSpec}`);
await installPhp(phpSpec);
const exitCode = await installPhp(phpSpec);
if (exitCode !== 0) {
throw new Error(`An error occurred while installing PHP(Code: ${exitCode}`);
}
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
Expand Down

0 comments on commit ceda18a

Please sign in to comment.