refactor: applied code review

This commit is contained in:
augustomelo 2024-04-27 11:24:16 +01:00
parent f5e27775fa
commit d878c91127
4 changed files with 71 additions and 68 deletions

40
dist/cleanup/index.js vendored
View File

@ -103695,7 +103695,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getVersionFromFileContent = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
exports.getVersionFromFile = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017));
const fs = __importStar(__nccwpck_require__(7147));
@ -103791,24 +103791,24 @@ function isCacheFeatureAvailable() {
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getVersionFromFileContent(fileName, content, distributionName) {
function getVersionFromFile(fileName, content, distributionName) {
var _a;
let fileContent = null;
let parsedVersion = null;
core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content);
parsedVersion = parseJavaVersionFile(content);
}
else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content);
parsedVersion = parsePomXmlFile(content);
}
else {
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`);
}
if (!fileContent) {
if (!parsedVersion) {
return null;
}
core.debug(`Version from file '${fileContent}'`);
const tentativeVersion = avoidOldNotation(fileContent);
core.debug(`Version from file '${parsedVersion}'`);
const tentativeVersion = avoidOldNotation(parsedVersion);
const rawVersion = tentativeVersion.split('-')[0];
let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion);
core.debug(`Range version from file is '${version}'`);
@ -103821,7 +103821,7 @@ function getVersionFromFileContent(fileName, content, distributionName) {
}
return version.toString();
}
exports.getVersionFromFileContent = getVersionFromFileContent;
exports.getVersionFromFile = getVersionFromFile;
function parseJavaVersionFile(content) {
var _a, _b, _c, _d;
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
@ -103849,11 +103849,8 @@ function parsePomXmlFile(xmlFileAsString) {
return null;
}
function getByMavenProperties(xmlDoc) {
const possibleTagsRegex = [
'maven.compiler.source',
'maven.compiler.release',
];
for (var tag of possibleTagsRegex) {
const versionTags = ['maven.compiler.source', 'maven.compiler.release'];
for (const tag of versionTags) {
const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) {
return version;
@ -103878,29 +103875,32 @@ function getByMavenCompilerPluginConfig(xmlDoc) {
var _a;
const source = xmlDoc.find(n => {
// Find <source> node
if (n.node.nodeName !== "source") {
if (n.node.nodeName !== 'source') {
return false;
}
if (n.node.childNodes.length !== 1) {
return false;
}
// Must be within <configuration>
if (n.up().node.nodeName !== "configuration") {
if (n.up().node.nodeName !== 'configuration') {
return false;
}
// Which must be inside <plugin>
if (n.up().up().node.nodeName !== "plugin") {
if (n.up().up().node.nodeName !== 'plugin') {
return false;
}
// Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n.up().up().some(c => {
if (c.node.nodeName !== "artifactId") {
const isCompilerPlugin = n
.up()
.up()
.some(c => {
if (c.node.nodeName !== 'artifactId') {
return false;
}
if (c.node.childNodes.length !== 1) {
return false;
}
return c.first().toString() === "maven-compiler-plugin";
return c.first().toString() === 'maven-compiler-plugin';
}, false, true);
if (!isCompilerPlugin) {
return false;

42
dist/setup/index.js vendored
View File

@ -105104,7 +105104,7 @@ function run() {
.readFileSync(versionFile)
.toString()
.trim();
const version = util_1.getVersionFromFileContent(versionFile, content, distributionName);
const version = util_1.getVersionFromFile(versionFile, content, distributionName);
core.debug(`Parsed version from file '${version}'`);
if (!version) {
throw new Error(`No supported version was found in file ${versionFile}`);
@ -105352,7 +105352,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getVersionFromFileContent = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
exports.getVersionFromFile = exports.isCacheFeatureAvailable = exports.isGhes = exports.isJobStatusSuccess = exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0;
const os_1 = __importDefault(__nccwpck_require__(2037));
const path_1 = __importDefault(__nccwpck_require__(1017));
const fs = __importStar(__nccwpck_require__(7147));
@ -105448,24 +105448,24 @@ function isCacheFeatureAvailable() {
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getVersionFromFileContent(fileName, content, distributionName) {
function getVersionFromFile(fileName, content, distributionName) {
var _a;
let fileContent = null;
let parsedVersion = null;
core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content);
parsedVersion = parseJavaVersionFile(content);
}
else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content);
parsedVersion = parsePomXmlFile(content);
}
else {
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`);
}
if (!fileContent) {
if (!parsedVersion) {
return null;
}
core.debug(`Version from file '${fileContent}'`);
const tentativeVersion = avoidOldNotation(fileContent);
core.debug(`Version from file '${parsedVersion}'`);
const tentativeVersion = avoidOldNotation(parsedVersion);
const rawVersion = tentativeVersion.split('-')[0];
let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion);
core.debug(`Range version from file is '${version}'`);
@ -105478,7 +105478,7 @@ function getVersionFromFileContent(fileName, content, distributionName) {
}
return version.toString();
}
exports.getVersionFromFileContent = getVersionFromFileContent;
exports.getVersionFromFile = getVersionFromFile;
function parseJavaVersionFile(content) {
var _a, _b, _c, _d;
const javaVersionRegExp = /(?<version>(?<=(^|\s|\-))(\d+\S*))(\s|$)/;
@ -105506,11 +105506,8 @@ function parsePomXmlFile(xmlFileAsString) {
return null;
}
function getByMavenProperties(xmlDoc) {
const possibleTagsRegex = [
'maven.compiler.source',
'maven.compiler.release',
];
for (var tag of possibleTagsRegex) {
const versionTags = ['maven.compiler.source', 'maven.compiler.release'];
for (const tag of versionTags) {
const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) {
return version;
@ -105535,29 +105532,32 @@ function getByMavenCompilerPluginConfig(xmlDoc) {
var _a;
const source = xmlDoc.find(n => {
// Find <source> node
if (n.node.nodeName !== "source") {
if (n.node.nodeName !== 'source') {
return false;
}
if (n.node.childNodes.length !== 1) {
return false;
}
// Must be within <configuration>
if (n.up().node.nodeName !== "configuration") {
if (n.up().node.nodeName !== 'configuration') {
return false;
}
// Which must be inside <plugin>
if (n.up().up().node.nodeName !== "plugin") {
if (n.up().up().node.nodeName !== 'plugin') {
return false;
}
// Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n.up().up().some(c => {
if (c.node.nodeName !== "artifactId") {
const isCompilerPlugin = n
.up()
.up()
.some(c => {
if (c.node.nodeName !== 'artifactId') {
return false;
}
if (c.node.childNodes.length !== 1) {
return false;
}
return c.first().toString() === "maven-compiler-plugin";
return c.first().toString() === 'maven-compiler-plugin';
}, false, true);
if (!isCompilerPlugin) {
return false;

View File

@ -1,7 +1,7 @@
import fs from 'fs';
import * as core from '@actions/core';
import * as auth from './auth';
import { getBooleanInput, isCacheFeatureAvailable, getVersionFromFileContent } from './util';
import { getBooleanInput, isCacheFeatureAvailable, getVersionFromFile } from './util';
import * as toolchains from './toolchains';
import * as constants from './constants';
import { restore } from './cache';
@ -47,7 +47,7 @@ async function run() {
.toString()
.trim();
const version = getVersionFromFileContent(versionFile, content, distributionName);
const version = getVersionFromFile(versionFile, content, distributionName);
core.debug(`Parsed version from file '${version}'`);
if (!version) {

View File

@ -102,31 +102,31 @@ export function isCacheFeatureAvailable(): boolean {
return false;
}
export function getVersionFromFileContent(
export function getVersionFromFile(
fileName: string,
content: string,
distributionName: string
): string | null {
let fileContent = null;
let parsedVersion = null;
core.debug(`Getting version from: '${fileName}'`);
if (fileName.includes('.java-version')) {
fileContent = parseJavaVersionFile(content);
parsedVersion = parseJavaVersionFile(content);
} else if (fileName.includes('pom.xml')) {
fileContent = parsePomXmlFile(content);
parsedVersion = parsePomXmlFile(content);
} else {
throw new Error(
`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`
);
}
if (!fileContent) {
if (!parsedVersion) {
return null;
}
core.debug(`Version from file '${fileContent}'`);
core.debug(`Version from file '${parsedVersion}'`);
const tentativeVersion = avoidOldNotation(fileContent);
const tentativeVersion = avoidOldNotation(parsedVersion);
const rawVersion = tentativeVersion.split('-')[0];
let version = semver.validRange(rawVersion) ? tentativeVersion : semver.coerce(tentativeVersion);
@ -177,12 +177,9 @@ function parsePomXmlFile(xmlFileAsString: string): string | null {
}
function getByMavenProperties(xmlDoc: XMLBuilder): string | null {
const possibleTagsRegex = [
'maven.compiler.source',
'maven.compiler.release',
];
const versionTags = ['maven.compiler.source', 'maven.compiler.release'];
for (var tag of possibleTagsRegex) {
for (const tag of versionTags) {
const version = getVersionByTagName(xmlDoc, tag);
if (version !== null) {
@ -206,36 +203,42 @@ function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): string | null {
} else {
return null;
}
}
function getByMavenCompilerPluginConfig(xmlDoc: XMLBuilder): string | null {
const source = xmlDoc.find(n => {
// Find <source> node
if (n.node.nodeName !== "source") {
if (n.node.nodeName !== 'source') {
return false;
}
if (n.node.childNodes.length !== 1) {
return false;
}
// Must be within <configuration>
if (n.up().node.nodeName !== "configuration") {
if (n.up().node.nodeName !== 'configuration') {
return false;
}
// Which must be inside <plugin>
if (n.up().up().node.nodeName !== "plugin") {
if (n.up().up().node.nodeName !== 'plugin') {
return false;
}
// Make sure the plugin is maven-compiler-plugin
const isCompilerPlugin = n.up().up().some(c => {
if (c.node.nodeName !== "artifactId") {
return false;
}
if (c.node.childNodes.length !== 1) {
return false;
}
return c.first().toString() === "maven-compiler-plugin";
}, false, true);
const isCompilerPlugin = n
.up()
.up()
.some(
c => {
if (c.node.nodeName !== 'artifactId') {
return false;
}
if (c.node.childNodes.length !== 1) {
return false;
}
return c.first().toString() === 'maven-compiler-plugin';
},
false,
true
);
if (!isCompilerPlugin) {
return false;
}