1
0
Files
setup-java/src/setup-java.ts
T

144 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-12-13 12:45:14 +01:00
import fs from 'fs';
2019-07-10 10:54:25 -04:00
import * as core from '@actions/core';
2019-11-15 16:01:13 -08:00
import * as auth from './auth';
import {
getBooleanInput,
isCacheFeatureAvailable,
getVersionFromFileContent
} from './util';
2022-01-16 17:33:29 +01:00
import * as toolchains from './toolchains';
2020-07-15 19:53:39 -06:00
import * as constants from './constants';
import {restore} from './cache';
2019-07-11 22:57:54 -04:00
import * as path from 'path';
import {getJavaDistribution} from './distributions/distribution-factory';
import {JavaInstallerOptions} from './distributions/base-models';
2019-07-10 10:54:25 -04:00
async function run() {
2019-07-10 23:11:48 -04:00
try {
2022-12-13 12:45:14 +01:00
const versions = core.getMultilineInput(constants.INPUT_JAVA_VERSION);
const distributionName = core.getInput(constants.INPUT_DISTRIBUTION, {
required: true
});
2022-12-13 12:45:14 +01:00
const versionFile = core.getInput(constants.INPUT_JAVA_VERSION_FILE);
const architecture = core.getInput(constants.INPUT_ARCHITECTURE);
const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE);
const jdkFile = core.getInput(constants.INPUT_JDK_FILE);
const cache = core.getInput(constants.INPUT_CACHE);
const cacheDependencyPath = core.getInput(
constants.INPUT_CACHE_DEPENDENCY_PATH
);
const checkLatest = getBooleanInput(constants.INPUT_CHECK_LATEST, false);
2022-01-16 17:33:29 +01:00
let toolchainIds = core.getMultilineInput(constants.INPUT_MVN_TOOLCHAIN_ID);
2022-12-13 12:45:14 +01:00
core.startGroup('Installed distributions');
2022-01-16 17:33:29 +01:00
if (versions.length !== toolchainIds.length) {
toolchainIds = [];
}
2022-12-13 12:45:14 +01:00
if (!versions.length && !versionFile) {
throw new Error('java-version or java-version-file input expected');
}
const installerInputsOptions: installerInputsOptions = {
architecture,
packageType,
checkLatest,
distributionName,
jdkFile,
toolchainIds
};
if (!versions.length) {
core.debug(
'java-version input is empty, looking for java-version-file input'
);
const content = fs.readFileSync(versionFile).toString().trim();
2022-12-13 12:45:14 +01:00
const version = getVersionFromFileContent(content, distributionName);
core.debug(`Parsed version from file '${version}'`);
if (!version) {
throw new Error(
`No supported version was found in file ${versionFile}`
);
2022-09-08 15:26:54 +02:00
}
2022-12-13 12:45:14 +01:00
await installVersion(version, installerInputsOptions);
}
for (const [index, version] of versions.entries()) {
await installVersion(version, installerInputsOptions, index);
2019-08-13 16:24:39 -04:00
}
2022-09-08 15:26:54 +02:00
core.endGroup();
2020-05-02 04:33:15 -07:00
const matchersPath = path.join(__dirname, '..', '..', '.github');
2020-07-15 19:53:39 -06:00
core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
await auth.configureAuthentication();
2022-04-01 00:39:57 +05:30
if (cache && isCacheFeatureAvailable()) {
await restore(cache, cacheDependencyPath);
}
2019-07-10 23:11:48 -04:00
} catch (error) {
core.setFailed(error.message);
}
2019-07-10 10:54:25 -04:00
}
run();
2022-12-13 12:45:14 +01:00
async function installVersion(
version: string,
options: installerInputsOptions,
toolchainId = 0
) {
2022-12-13 12:45:14 +01:00
const {
distributionName,
jdkFile,
architecture,
packageType,
checkLatest,
toolchainIds
} = options;
const installerOptions: JavaInstallerOptions = {
architecture,
packageType,
checkLatest,
version
};
const distribution = getJavaDistribution(
distributionName,
installerOptions,
jdkFile
);
2022-12-13 12:45:14 +01:00
if (!distribution) {
throw new Error(
`No supported distribution was found for input ${distributionName}`
);
2022-12-13 12:45:14 +01:00
}
const result = await distribution.setupJava();
await toolchains.configureToolchains(
version,
distributionName,
result.path,
toolchainIds[toolchainId]
);
core.info('');
core.info('Java configuration:');
core.info(` Distribution: ${distributionName}`);
core.info(` Version: ${result.version}`);
core.info(` Path: ${result.path}`);
core.info('');
}
interface installerInputsOptions {
architecture: string;
packageType: string;
checkLatest: boolean;
distributionName: string;
jdkFile: string;
toolchainIds: Array<string>;
}