diff --git a/.github/workflows/e2e-versions.yml b/.github/workflows/e2e-versions.yml index 1f9d6e77..8b9e1119 100644 --- a/.github/workflows/e2e-versions.yml +++ b/.github/workflows/e2e-versions.yml @@ -288,19 +288,23 @@ jobs: matrix: os: [macos-latest, windows-latest, ubuntu-latest] distribution: ['temurin', 'microsoft', 'corretto'] + java-version-file: ['.java-version', '.tool-versions'] steps: - name: Checkout uses: actions/checkout@v4 - name: Create .java-version file shell: bash run: echo "8" > .java-version + - name: Create .tool-versions file + shell: bash + run: echo "java 8" > .tool-versions - name: setup-java uses: ./ id: setup-java with: distribution: ${{ matrix.distribution }} java-version: 11 - java-version-file: '.java-version' + java-version-file: ${{matrix.java-version-file }} - name: Verify Java run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}" shell: bash @@ -313,18 +317,22 @@ jobs: matrix: os: [macos-latest, windows-latest, ubuntu-latest] distribution: ['temurin', 'zulu', 'liberica', 'microsoft', 'corretto'] + java-version-file: ['.java-version', '.tool-versions'] steps: - name: Checkout uses: actions/checkout@v4 - name: Create .java-version file shell: bash run: echo "11" > .java-version + - name: Create .tool-versions file + shell: bash + run: echo "java 11" > .tool-versions - name: setup-java uses: ./ id: setup-java with: distribution: ${{ matrix.distribution }} - java-version-file: '.java-version' + java-version-file: ${{matrix.java-version-file }} - name: Verify Java run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}" shell: bash @@ -337,18 +345,22 @@ jobs: matrix: os: [macos-latest, windows-latest, ubuntu-latest] distribution: ['adopt', 'adopt-openj9', 'zulu'] + java-version-file: ['.java-version', '.tool-versions'] steps: - name: Checkout uses: actions/checkout@v4 - name: Create .java-version file shell: bash run: echo "11.0.2" > .java-version + - name: Create .tool-versions file + shell: bash + run: echo "java 11.0.2" > .tool-versions - name: setup-java uses: ./ id: setup-java with: distribution: ${{ matrix.distribution }} - java-version-file: '.java-version' + java-version-file: ${{matrix.java-version-file }} - name: Verify Java run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}" shell: bash @@ -361,18 +373,22 @@ jobs: matrix: os: [macos-latest, windows-latest, ubuntu-latest] distribution: ['adopt', 'zulu', 'liberica'] + java-version-file: ['.java-version', '.tool-versions'] steps: - name: Checkout uses: actions/checkout@v4 - name: Create .java-version file shell: bash run: echo "openjdk64-11.0.2" > .java-version + - name: Create .tool-versions file + shell: bash + run: echo "java openjdk64-11.0.2" > .tool-versions - name: setup-java uses: ./ id: setup-java with: distribution: ${{ matrix.distribution }} - java-version-file: '.java-version' + java-version-file: ${{matrix.java-version-file }} - name: Verify Java run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}" shell: bash diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 971673a5..f8989c28 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -87887,9 +87887,18 @@ function isCacheFeatureAvailable() { return false; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; -function getVersionFromFileContent(content, distributionName) { +function getVersionFromFileContent(content, distributionName, versionFile) { var _a, _b, _c, _d, _e; - const javaVersionRegExp = /(?(?<=(^|\s|-))(\d+\S*))(\s|$)/; + let javaVersionRegExp; + if (versionFile == '.tool-versions') { + javaVersionRegExp = /^java\s+(?:\S+-)?v?(?[^\s]+)$/m; + } + else if (versionFile == '.java-version') { + javaVersionRegExp = /(?(?<=(^|\s|-))(\d+\S*))(\s|$)/; + } + else { + throw new Error('Invalid version file'); + } const fileContent = ((_b = (_a = content.match(javaVersionRegExp)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.version) ? (_d = (_c = content.match(javaVersionRegExp)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.version : ''; diff --git a/dist/setup/index.js b/dist/setup/index.js index f8c70dab..bcc80163 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -124907,7 +124907,7 @@ function run() { if (!versions.length) { core.debug('java-version input is empty, looking for java-version-file input'); const content = fs_1.default.readFileSync(versionFile).toString().trim(); - const version = (0, util_1.getVersionFromFileContent)(content, distributionName); + const version = (0, util_1.getVersionFromFileContent)(content, distributionName, versionFile); core.debug(`Parsed version from file '${version}'`); if (!version) { throw new Error(`No supported version was found in file ${versionFile}`); @@ -125261,9 +125261,18 @@ function isCacheFeatureAvailable() { return false; } exports.isCacheFeatureAvailable = isCacheFeatureAvailable; -function getVersionFromFileContent(content, distributionName) { +function getVersionFromFileContent(content, distributionName, versionFile) { var _a, _b, _c, _d, _e; - const javaVersionRegExp = /(?(?<=(^|\s|-))(\d+\S*))(\s|$)/; + let javaVersionRegExp; + if (versionFile == '.tool-versions') { + javaVersionRegExp = /^java\s+(?:\S+-)?v?(?[^\s]+)$/m; + } + else if (versionFile == '.java-version') { + javaVersionRegExp = /(?(?<=(^|\s|-))(\d+\S*))(\s|$)/; + } + else { + throw new Error('Invalid version file'); + } const fileContent = ((_b = (_a = content.match(javaVersionRegExp)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.version) ? (_d = (_c = content.match(javaVersionRegExp)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.version : ''; diff --git a/src/setup-java.ts b/src/setup-java.ts index 35a315df..73baf33a 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -55,7 +55,11 @@ async function run() { ); const content = fs.readFileSync(versionFile).toString().trim(); - const version = getVersionFromFileContent(content, distributionName); + const version = getVersionFromFileContent( + content, + distributionName, + versionFile + ); core.debug(`Parsed version from file '${version}'`); if (!version) { diff --git a/src/util.ts b/src/util.ts index 94be2347..4f4616e4 100644 --- a/src/util.ts +++ b/src/util.ts @@ -115,9 +115,18 @@ export function isCacheFeatureAvailable(): boolean { export function getVersionFromFileContent( content: string, - distributionName: string + distributionName: string, + versionFile: string ): string | null { - const javaVersionRegExp = /(?(?<=(^|\s|-))(\d+\S*))(\s|$)/; + let javaVersionRegExp: RegExp; + if (versionFile == '.tool-versions') { + javaVersionRegExp = /^java\s+(?:\S+-)?v?(?[^\s]+)$/m; + } else if (versionFile == '.java-version') { + javaVersionRegExp = /(?(?<=(^|\s|-))(\d+\S*))(\s|$)/; + } else { + throw new Error('Invalid version file'); + } + const fileContent = content.match(javaVersionRegExp)?.groups?.version ? (content.match(javaVersionRegExp)?.groups?.version as string) : '';