From e1a751b5d3f29a82de84c70000ff3bf470374565 Mon Sep 17 00:00:00 2001 From: AUGMELO Date: Sun, 8 Jan 2023 19:25:29 +0000 Subject: [PATCH] feat: added support to pom.xml --- .github/workflows/e2e-versions.yml | 45 +++++++++++++++ src/setup-java.ts | 2 +- src/util.ts | 91 ++++++++++++++++++++++++++++-- 3 files changed, 133 insertions(+), 5 deletions(-) diff --git a/.github/workflows/e2e-versions.yml b/.github/workflows/e2e-versions.yml index 47250cc..7ca6727 100644 --- a/.github/workflows/e2e-versions.yml +++ b/.github/workflows/e2e-versions.yml @@ -337,3 +337,48 @@ jobs: - name: Verify Java run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}" shell: bash + + setup-java-version-from-pom-spring-boot-specification: + name: ${{ matrix.distribution }} version from file 'openjdk64-11.0.2' - ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, windows-latest, ubuntu-latest] + distribution: ['adopt', 'zulu', 'liberica' ] + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Create .java-version file + shell: bash + run: | + echo "" > pom.xml + echo "" >> pom.xml + echo " 4.0.0" >> pom.xml + echo " com.test" >> pom.xml + echo " Test" >> pom.xml + echo " 0.0.1-SNAPSHOT" >> pom.xml + echo " Test" >> pom.xml + echo " " >> pom.xml + echo " 11" >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo " org.springframework.boot" >> pom.xml + echo " spring-boot-maven-plugin" >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo " " >> pom.xml + echo "" >> pom.xml + - name: setup-java + uses: ./ + id: setup-java + with: + distribution: ${{ matrix.distribution }} + java-version-file: 'pom.xml' + - name: Verify Java + run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}" + shell: bash diff --git a/src/setup-java.ts b/src/setup-java.ts index 1d6dc15..df90e6e 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -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) { diff --git a/src/util.ts b/src/util.ts index d3a09f6..967406c 100644 --- a/src/util.ts +++ b/src/util.ts @@ -101,13 +101,19 @@ export function isCacheFeatureAvailable(): boolean { } export function getVersionFromFileContent( + fileName: string, content: string, distributionName: string ): string | null { - const javaVersionRegExp = /(?(?<=(^|\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 = /(?(?<=(^|\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; } +