change version match logic

This commit is contained in:
Evgenii Korolevskii 2022-12-08 10:40:03 +01:00
parent 5a85bfb40d
commit 43f5042dba
2 changed files with 98 additions and 54 deletions

View file

@ -27,3 +27,5 @@ export const MVN_SETTINGS_FILE = 'settings.xml';
export const MVN_TOOLCHAINS_FILE = 'toolchains.xml'; export const MVN_TOOLCHAINS_FILE = 'toolchains.xml';
export const INPUT_MVN_TOOLCHAIN_ID = 'mvn-toolchain-id'; export const INPUT_MVN_TOOLCHAIN_ID = 'mvn-toolchain-id';
export const INPUT_MVN_TOOLCHAIN_VENDOR = 'mvn-toolchain-vendor'; export const INPUT_MVN_TOOLCHAIN_VENDOR = 'mvn-toolchain-vendor';
export const DISTRIBUTIONS_ONLY_MAJOR_VERSION = ['corretto']

View file

@ -8,6 +8,7 @@ import { restore } from './cache';
import * as path from 'path'; import * as path from 'path';
import { getJavaDistribution } from './distributions/distribution-factory'; import { getJavaDistribution } from './distributions/distribution-factory';
import { JavaInstallerOptions } from './distributions/base-models'; import { JavaInstallerOptions } from './distributions/base-models';
import * as semver from 'semver';
async function run() { async function run() {
try { try {
@ -31,34 +32,33 @@ async function run() {
throw new Error('Java-version or java-version-file input expected'); throw new Error('Java-version or java-version-file input expected');
} }
const installerInputsOptions: installerInputsOptions = {
architecture,
packageType,
checkLatest,
distributionName,
jdkFile,
toolchainIds
};
if (!versions.length) { if (!versions.length) {
core.debug('Java-version input is empty, looking for java-version-file input'); core.debug('Java-version input is empty, looking for java-version-file input');
const contents = fs const content = fs
.readFileSync(versionFile) .readFileSync(versionFile)
.toString() .toString()
.trim(); .trim();
const semverRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
let version = contents.match(semverRegExp)?.groups?.version const version = getVersionFromFileContent(content, distributionName)
? (contents.match(semverRegExp)?.groups?.version as string)
: ''; if (!version) {
let installed = false; throw new Error(`No supported version was found in file ${versionFile}`);
while (!installed && version) {
try {
core.debug(`Trying to install version ${version}`);
await installVersion(version);
installed = true;
} catch (error) {
core.debug(`${error.toString()}`);
version = getHigherVersion(version);
}
}
if (!installed) {
throw new Error("Сan't install appropriate version from .java-version file");
} }
await installVersion(version as string, installerInputsOptions);
} }
for (const [index, version] of versions.entries()) { for (const [index, version] of versions.entries()) {
await installVersion(version, index); await installVersion(version, installerInputsOptions, index);
} }
core.endGroup(); core.endGroup();
const matchersPath = path.join(__dirname, '..', '..', '.github'); const matchersPath = path.join(__dirname, '..', '..', '.github');
@ -68,13 +68,28 @@ async function run() {
if (cache && isCacheFeatureAvailable()) { if (cache && isCacheFeatureAvailable()) {
await restore(cache); await restore(cache);
} }
} catch (error) {
core.setFailed(error.message);
}
}
run();
async function installVersion(version: string, options: installerInputsOptions, toolchainId = 0) {
const {
distributionName,
jdkFile,
architecture,
packageType,
checkLatest,
toolchainIds
} = options;
async function installVersion(version: string, toolchainId = 0) {
const installerOptions: JavaInstallerOptions = { const installerOptions: JavaInstallerOptions = {
architecture, architecture,
packageType, packageType,
version, checkLatest,
checkLatest version
}; };
const distribution = getJavaDistribution(distributionName, installerOptions, jdkFile); const distribution = getJavaDistribution(distributionName, installerOptions, jdkFile);
@ -96,16 +111,43 @@ async function run() {
core.info(` Version: ${result.version}`); core.info(` Version: ${result.version}`);
core.info(` Path: ${result.path}`); core.info(` Path: ${result.path}`);
core.info(''); core.info('');
}
function getHigherVersion(version: string) {
return version.split('-')[0] === version
? version.substring(0, version.lastIndexOf('.'))
: version.split('-')[0];
}
} catch (error) {
core.setFailed(error.message);
}
} }
run(); interface installerInputsOptions {
architecture: string;
packageType: string;
checkLatest: boolean;
distributionName: string;
jdkFile: string;
toolchainIds: Array<string>;
}
function getVersionFromFileContent(content: string, distributionName: string): string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
const fileContent = content.match(javaVersionRegExp)?.groups?.version
? (content.match(javaVersionRegExp)?.groups?.version as string)
: '';
if (!fileContent) {
return null
}
const tentativeVersion = avoidOldNotation(fileContent);
let version = semver.validRange(tentativeVersion)
? tentativeVersion
: semver.coerce(tentativeVersion);
if (!version) {
return null
}
if (constants.DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(distributionName)) {
version = semver.major(version).toString();
}
return version.toString();
}
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
function avoidOldNotation(content: string): string {
return content.substring(0, 2) === '1.' ? content.substring(2) : content;
}