mirror of
https://github.com/actions/setup-java.git
synced 2025-04-22 02:46:46 +00:00
feat: finished implementation and added e2e tests
This commit is contained in:
parent
2fe433d4cd
commit
c2ea5167ff
6 changed files with 45157 additions and 4871 deletions
119
src/util.ts
119
src/util.ts
|
@ -1,12 +1,15 @@
|
|||
import os from 'os';
|
||||
import os, { version } from 'os';
|
||||
import path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import * as semver from 'semver';
|
||||
import * as cache from '@actions/cache';
|
||||
import * as core from '@actions/core';
|
||||
|
||||
import { create as xmlCreate } from 'xmlbuilder2';
|
||||
import { select } from 'xpath';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import { INPUT_JOB_STATUS, DISTRIBUTIONS_ONLY_MAJOR_VERSION } from './constants';
|
||||
import { XMLBuilder } from 'xmlbuilder2/lib/interfaces';
|
||||
|
||||
export function getTempDir() {
|
||||
let tempDirectory = process.env['RUNNER_TEMP'] || os.tmpdir();
|
||||
|
@ -106,12 +109,16 @@ export function getVersionFromFileContent(
|
|||
distributionName: string
|
||||
): string | null {
|
||||
let fileContent = null;
|
||||
|
||||
core.debug(`Getting version from: '${fileName}'`);
|
||||
if (fileName.includes('.java-version')) {
|
||||
fileContent = parseJavaVersionFile(content)
|
||||
} else if (fileName.includes('pom.xml')){
|
||||
fileContent = parsePomXmlFile(content)
|
||||
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'`)
|
||||
throw new Error(
|
||||
`File ${fileName} not supported, files supported: '.java-version' and 'pom.xml'`
|
||||
);
|
||||
}
|
||||
|
||||
if (!fileContent) {
|
||||
|
@ -148,75 +155,59 @@ function parseJavaVersionFile(content: string): string | null {
|
|||
return null;
|
||||
}
|
||||
|
||||
return fileContent
|
||||
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)
|
||||
function parsePomXmlFile(xmlString: string): string | null {
|
||||
const xmlDoc = xmlCreate(xmlString);
|
||||
const versionDefinitionTypes = [getByMavenCompilerSpecification, getBySpringBootSpecification];
|
||||
|
||||
return version !== null
|
||||
})
|
||||
for (var definitionType of versionDefinitionTypes) {
|
||||
var version = definitionType(xmlDoc);
|
||||
|
||||
|
||||
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
|
||||
if (version !== null) {
|
||||
return version;
|
||||
}
|
||||
}
|
||||
|
||||
return element[0].textContent
|
||||
return null;
|
||||
}
|
||||
|
||||
function getByMavenCompilerSpecification(xmlDoc: XMLBuilder): string | null {
|
||||
const possibleTags = [
|
||||
'//properties/maven.compiler.source',
|
||||
'//configuration/source',
|
||||
'//properties/maven.compiler.release',
|
||||
'//configuration/release'
|
||||
];
|
||||
|
||||
for (var tag of possibleTags) {
|
||||
const version = getVersionByTagName(xmlDoc, tag);
|
||||
|
||||
if (version !== null) {
|
||||
return version;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getBySpringBootSpecification(xmlDoc: XMLBuilder): string | null {
|
||||
return getVersionByTagName(xmlDoc, '//properties/java.version');
|
||||
}
|
||||
|
||||
function getVersionByTagName(xmlDoc: XMLBuilder, xpathQuery: string): string | null {
|
||||
const element = select(`string(${xpathQuery})`, xmlDoc.node as any);
|
||||
|
||||
core.debug(`Found: '${element}' using xpath query: '${xpathQuery}'`);
|
||||
if (element == undefined || element.length == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return element.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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue