From 291b946ea5d2d6858065257aed50d8c574f196f1 Mon Sep 17 00:00:00 2001 From: Evgenii Korolevskii Date: Thu, 8 Dec 2022 15:32:14 +0100 Subject: [PATCH] fix review points --- src/setup-java.ts | 32 +------------------------------- src/util.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/setup-java.ts b/src/setup-java.ts index 7978f22b..916d184b 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -1,14 +1,13 @@ import fs from 'fs'; import * as core from '@actions/core'; import * as auth from './auth'; -import { getBooleanInput, isCacheFeatureAvailable } from './util'; +import { getBooleanInput, isCacheFeatureAvailable, getVersionFromFileContent, avoidOldNotation } from './util'; import * as toolchains from './toolchains'; import * as constants from './constants'; import { restore } from './cache'; import * as path from 'path'; import { getJavaDistribution } from './distributions/distribution-factory'; import { JavaInstallerOptions } from './distributions/base-models'; -import * as semver from 'semver'; async function run() { try { @@ -122,32 +121,3 @@ interface installerInputsOptions { toolchainIds: Array; } -function getVersionFromFileContent(content: string, distributionName: string): string | null { - const javaVersionRegExp = /(?(?<=(^|\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.startsWith('1.') ? content.substring(2) : content; -} diff --git a/src/util.ts b/src/util.ts index 10f18c5f..4ad22a1f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -6,7 +6,7 @@ import * as cache from '@actions/cache'; import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; -import { INPUT_JOB_STATUS } from './constants'; +import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants'; export function getTempDir() { let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir(); @@ -99,3 +99,34 @@ export function isCacheFeatureAvailable(): boolean { return true; } + + +export function getVersionFromFileContent(content: string, distributionName: string): string | null { + const javaVersionRegExp = /(?(?<=(^|\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 (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` +export function avoidOldNotation(content: string): string { + return content.startsWith('1.') ? content.substring(2) : content; +}