From d878c911270ccb9fdeb39ece922c78fd1a1a1fb5 Mon Sep 17 00:00:00 2001 From: augustomelo <4723788+augustomelo@users.noreply.github.com> Date: Sat, 27 Apr 2024 11:24:16 +0100 Subject: [PATCH] refactor: applied code review --- dist/cleanup/index.js | 40 ++++++++++++++++---------------- dist/setup/index.js | 42 +++++++++++++++++----------------- src/setup-java.ts | 4 ++-- src/util.ts | 53 +++++++++++++++++++++++-------------------- 4 files changed, 71 insertions(+), 68 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 799fbb70..5758feb2 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -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 = /(?(?<=(^|\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 node - if (n.node.nodeName !== "source") { + if (n.node.nodeName !== 'source') { return false; } if (n.node.childNodes.length !== 1) { return false; } // Must be within - if (n.up().node.nodeName !== "configuration") { + if (n.up().node.nodeName !== 'configuration') { return false; } // Which must be inside - 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; diff --git a/dist/setup/index.js b/dist/setup/index.js index fbed6b0b..e72a2897 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -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 = /(?(?<=(^|\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 node - if (n.node.nodeName !== "source") { + if (n.node.nodeName !== 'source') { return false; } if (n.node.childNodes.length !== 1) { return false; } // Must be within - if (n.up().node.nodeName !== "configuration") { + if (n.up().node.nodeName !== 'configuration') { return false; } // Which must be inside - 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; diff --git a/src/setup-java.ts b/src/setup-java.ts index df90e6ef..95929a2b 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -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) { diff --git a/src/util.ts b/src/util.ts index 08d2410e..93f2ad43 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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 node - if (n.node.nodeName !== "source") { + if (n.node.nodeName !== 'source') { return false; } if (n.node.childNodes.length !== 1) { return false; } // Must be within - if (n.up().node.nodeName !== "configuration") { + if (n.up().node.nodeName !== 'configuration') { return false; } // Which must be inside - 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; }