mirror of
https://github.com/actions/setup-java.git
synced 2025-04-22 02:46:46 +00:00
feat: added support to pom.xml
This commit is contained in:
parent
e11351903a
commit
2fe433d4cd
3 changed files with 133 additions and 5 deletions
|
@ -47,7 +47,7 @@ async function run() {
|
|||
.toString()
|
||||
.trim();
|
||||
|
||||
const version = getVersionFromFileContent(content, distributionName);
|
||||
const version = getVersionFromFileContent(versionFile, content, distributionName);
|
||||
core.debug(`Parsed version from file '${version}'`);
|
||||
|
||||
if (!version) {
|
||||
|
|
91
src/util.ts
91
src/util.ts
|
@ -101,13 +101,19 @@ export function isCacheFeatureAvailable(): boolean {
|
|||
}
|
||||
|
||||
export function getVersionFromFileContent(
|
||||
fileName: string,
|
||||
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)
|
||||
: '';
|
||||
let fileContent = null;
|
||||
if (fileName.includes('.java-version')) {
|
||||
fileContent = parseJavaVersionFile(content)
|
||||
} else if (fileName.includes('pom.xml')){
|
||||
fileContent = parsePomXmlFile(content)
|
||||
} else {
|
||||
throw new Error(`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`)
|
||||
}
|
||||
|
||||
if (!fileContent) {
|
||||
return null;
|
||||
}
|
||||
|
@ -133,7 +139,84 @@ export function getVersionFromFileContent(
|
|||
return version.toString();
|
||||
}
|
||||
|
||||
function parseJavaVersionFile(content: 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;
|
||||
}
|
||||
|
||||
return fileContent
|
||||
}
|
||||
|
||||
function parsePomXmlFile(content: string): string | null {
|
||||
const parser = new DOMParser();
|
||||
const xmlDoc = parser.parseFromString(content, "text/xml");
|
||||
const versionDefinitionTypes = [
|
||||
getByMavenCompilerSource,
|
||||
getByMavenCompilerRelease,
|
||||
getBySpringBootSpecification,
|
||||
]
|
||||
const versionFound = versionDefinitionTypes.filter(function(definitionType){
|
||||
const version = definitionType(xmlDoc)
|
||||
|
||||
return version !== null
|
||||
})
|
||||
|
||||
|
||||
return versionFound?.at(0)?.toString() ?? null;
|
||||
}
|
||||
|
||||
function getByMavenCompilerSource(xmlDoc: Document): string | null {
|
||||
const possibleTags = [
|
||||
"maven.compiler.source",
|
||||
"source"
|
||||
]
|
||||
|
||||
const tagFound = possibleTags.filter(function(tag) {
|
||||
const version = getVersionByTagName(xmlDoc, tag)
|
||||
|
||||
return version !== null
|
||||
})
|
||||
|
||||
return tagFound?.at(0)?.toString() ?? null
|
||||
|
||||
}
|
||||
|
||||
function getByMavenCompilerRelease(xmlDoc: Document): string | null {
|
||||
const possibleTags = [
|
||||
"maven.compiler.release",
|
||||
"release"
|
||||
]
|
||||
|
||||
const tagFound = possibleTags.filter(function(tag) {
|
||||
const version = getVersionByTagName(xmlDoc, tag)
|
||||
|
||||
return version !== null
|
||||
})
|
||||
|
||||
return tagFound?.at(0)?.toString() ?? null
|
||||
}
|
||||
|
||||
function getBySpringBootSpecification(xmlDoc: Document): string | null {
|
||||
return getVersionByTagName(xmlDoc, "java.version")
|
||||
}
|
||||
|
||||
function getVersionByTagName(xmlDoc: Document, tagName: string): string | null {
|
||||
const element = xmlDoc.getElementsByTagName(tagName)
|
||||
|
||||
if (element.length < 1) {
|
||||
return null
|
||||
}
|
||||
|
||||
return element[0].textContent
|
||||
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue