Skip to content

Commit

Permalink
rafactor: using xmlbuilder2 instead of regex
Browse files Browse the repository at this point in the history
  • Loading branch information
augustomelo committed Mar 4, 2024
1 parent 6abf828 commit a806815
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as core from '@actions/core';

import * as tc from '@actions/tool-cache';
import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants';
import { create } from 'xmlbuilder2';
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';

export function getTempDir() {
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
Expand Down Expand Up @@ -155,11 +157,11 @@ function parseJavaVersionFile(content: string): string | null {
return fileContent;
}

function parsePomXmlFile(xmlFile: string): string | null {
function parsePomXmlFile(xmlFileAsString: string): string | null {
const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];

for (var definitionType of versionDefinitionTypes) {
var version = definitionType(xmlFile);
var version = definitionType(create(xmlFileAsString));

if (version !== null) {
return version;
Expand All @@ -169,14 +171,14 @@ function parsePomXmlFile(xmlFile: string): string | null {
return null;
}

function getByMavenCompilerSpecification(xmlFile: string): string | null {
function getByMavenCompilerSpecification(xmlDoc: XMLBuilder): string | null {
const possibleTagsRegex = [
'<maven\.compiler\.source>(.*?)<\/maven\.compiler\.source>',
'<maven.compiler.release>(.*?)<\/maven.compiler.release>',
'maven.compiler.source',
'maven.compiler.release',
];

for (var tag of possibleTagsRegex) {
const version = getVersionByTagName(xmlFile, tag);
const version = getVersionByTagName(xmlDoc, tag);

if (version !== null) {
return version;
Expand All @@ -186,16 +188,16 @@ function getByMavenCompilerSpecification(xmlFile: string): string | null {
return null;
}

function getBySpringBootSpecification(xmlFile: string): string | null {
return getVersionByTagName(xmlFile, '<java.version>(.*?)<\/java.version>');
function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null {
return getVersionByTagName(xmlDoc, 'java.version');
}

function getVersionByTagName(xmlFile: string, regex: string): string | null {
const match = xmlFile.match(new RegExp(regex));
function getVersionByTagName(xmlDoc: XMLBuilder, tag: string): string | null {
const match = xmlDoc.find(n => n.node.nodeName === tag);

if (match) {
core.debug(`Found java version: '${match[1]}' using regex: '${regex}'`);
return match[1];
if (match !== undefined) {
core.debug(`Found java version: '${match.first().toString()}' using tag: '${tag}'`);
return match.first().toString();
} else {
return null;
}
Expand Down

0 comments on commit a806815

Please sign in to comment.