Skip to content

Commit 5896cec

Browse files
Added .tool-versions file support (#606)
* added support for tool version file * testing with one regex * working regex * Checked for the file extension * added e2e checks for tool version * removed error warning * updated regex to support early version * updated regex for early version support * updated regex for early version * updated regex to accept early versions * added coreinfo to analyze * updated the regex * updated regex * new regex for early version * updated regex to match the new version file format * new regex * changed the regex * redex updated * used java version regex * regex updated * regex modified * regex updated * regex updated * regex updated * updated regex to support early versions * Regex updated to support all java versions * Documentation updated to add tool version description * Documentation updated for the tool version file * update the advanced doc and readme file to specify tool version changes
1 parent 80ae3c2 commit 5896cec

File tree

7 files changed

+75
-17
lines changed

7 files changed

+75
-17
lines changed

.github/workflows/e2e-versions.yml

+20-4
Original file line numberDiff line numberDiff line change
@@ -288,19 +288,23 @@ jobs:
288288
matrix:
289289
os: [macos-latest, windows-latest, ubuntu-latest]
290290
distribution: ['temurin', 'microsoft', 'corretto']
291+
java-version-file: ['.java-version', '.tool-versions']
291292
steps:
292293
- name: Checkout
293294
uses: actions/checkout@v4
294295
- name: Create .java-version file
295296
shell: bash
296297
run: echo "8" > .java-version
298+
- name: Create .tool-versions file
299+
shell: bash
300+
run: echo "java 8" > .tool-versions
297301
- name: setup-java
298302
uses: ./
299303
id: setup-java
300304
with:
301305
distribution: ${{ matrix.distribution }}
302306
java-version: 11
303-
java-version-file: '.java-version'
307+
java-version-file: ${{matrix.java-version-file }}
304308
- name: Verify Java
305309
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
306310
shell: bash
@@ -313,18 +317,22 @@ jobs:
313317
matrix:
314318
os: [macos-latest, windows-latest, ubuntu-latest]
315319
distribution: ['temurin', 'zulu', 'liberica', 'microsoft', 'corretto']
320+
java-version-file: ['.java-version', '.tool-versions']
316321
steps:
317322
- name: Checkout
318323
uses: actions/checkout@v4
319324
- name: Create .java-version file
320325
shell: bash
321326
run: echo "11" > .java-version
327+
- name: Create .tool-versions file
328+
shell: bash
329+
run: echo "java 11" > .tool-versions
322330
- name: setup-java
323331
uses: ./
324332
id: setup-java
325333
with:
326334
distribution: ${{ matrix.distribution }}
327-
java-version-file: '.java-version'
335+
java-version-file: ${{matrix.java-version-file }}
328336
- name: Verify Java
329337
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
330338
shell: bash
@@ -337,18 +345,22 @@ jobs:
337345
matrix:
338346
os: [macos-latest, windows-latest, ubuntu-latest]
339347
distribution: ['adopt', 'adopt-openj9', 'zulu']
348+
java-version-file: ['.java-version', '.tool-versions']
340349
steps:
341350
- name: Checkout
342351
uses: actions/checkout@v4
343352
- name: Create .java-version file
344353
shell: bash
345354
run: echo "11.0.2" > .java-version
355+
- name: Create .tool-versions file
356+
shell: bash
357+
run: echo "java 11.0.2" > .tool-versions
346358
- name: setup-java
347359
uses: ./
348360
id: setup-java
349361
with:
350362
distribution: ${{ matrix.distribution }}
351-
java-version-file: '.java-version'
363+
java-version-file: ${{matrix.java-version-file }}
352364
- name: Verify Java
353365
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}"
354366
shell: bash
@@ -361,18 +373,22 @@ jobs:
361373
matrix:
362374
os: [macos-latest, windows-latest, ubuntu-latest]
363375
distribution: ['adopt', 'zulu', 'liberica']
376+
java-version-file: ['.java-version', '.tool-versions']
364377
steps:
365378
- name: Checkout
366379
uses: actions/checkout@v4
367380
- name: Create .java-version file
368381
shell: bash
369382
run: echo "openjdk64-11.0.2" > .java-version
383+
- name: Create .tool-versions file
384+
shell: bash
385+
run: echo "java openjdk64-11.0.2" > .tool-versions
370386
- name: setup-java
371387
uses: ./
372388
id: setup-java
373389
with:
374390
distribution: ${{ matrix.distribution }}
375-
java-version-file: '.java-version'
391+
java-version-file: ${{matrix.java-version-file }}
376392
- name: Verify Java
377393
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}"
378394
shell: bash

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ This action allows you to work with Java and Scala projects.
2727

2828
- `java-version`: The Java version that is going to be set up. Takes a whole or [semver](#supported-version-syntax) Java version. If not specified, the action will expect `java-version-file` input to be specified.
2929

30-
- `java-version-file`: The path to the `.java-version` file. See more details in [about `.java-version` file](docs/advanced-usage.md#Java-version-file).
30+
- `java-version-file`: The path to a file containing java version. Supported file types are `.java-version` and `.tool-versions`. See more details in [about .java-version-file](docs/advanced-usage.md#Java-version-file).
3131

3232
- `distribution`: _(required)_ Java [distribution](#supported-distributions).
3333

@@ -266,6 +266,7 @@ In the example above multiple JDKs are installed for the same job. The result af
266266
- [Publishing using Gradle](docs/advanced-usage.md#Publishing-using-Gradle)
267267
- [Hosted Tool Cache](docs/advanced-usage.md#Hosted-Tool-Cache)
268268
- [Modifying Maven Toolchains](docs/advanced-usage.md#Modifying-Maven-Toolchains)
269+
- [Java Version File](docs/advanced-usage.md#Java-version-file)
269270

270271
## License
271272

dist/cleanup/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -88352,9 +88352,19 @@ function isCacheFeatureAvailable() {
8835288352
return false;
8835388353
}
8835488354
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
88355-
function getVersionFromFileContent(content, distributionName) {
88355+
function getVersionFromFileContent(content, distributionName, versionFile) {
8835688356
var _a, _b, _c, _d, _e;
88357-
const javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
88357+
let javaVersionRegExp;
88358+
if (versionFile == '.tool-versions') {
88359+
javaVersionRegExp =
88360+
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m;
88361+
}
88362+
else if (versionFile == '.java-version') {
88363+
javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
88364+
}
88365+
else {
88366+
throw new Error('Invalid version file');
88367+
}
8835888368
const fileContent = ((_b = (_a = content.match(javaVersionRegExp)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.version)
8835988369
? (_d = (_c = content.match(javaVersionRegExp)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.version
8836088370
: '';

dist/setup/index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -125372,7 +125372,7 @@ function run() {
125372125372
if (!versions.length) {
125373125373
core.debug('java-version input is empty, looking for java-version-file input');
125374125374
const content = fs_1.default.readFileSync(versionFile).toString().trim();
125375-
const version = (0, util_1.getVersionFromFileContent)(content, distributionName);
125375+
const version = (0, util_1.getVersionFromFileContent)(content, distributionName, versionFile);
125376125376
core.debug(`Parsed version from file '${version}'`);
125377125377
if (!version) {
125378125378
throw new Error(`No supported version was found in file ${versionFile}`);
@@ -125726,9 +125726,19 @@ function isCacheFeatureAvailable() {
125726125726
return false;
125727125727
}
125728125728
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
125729-
function getVersionFromFileContent(content, distributionName) {
125729+
function getVersionFromFileContent(content, distributionName, versionFile) {
125730125730
var _a, _b, _c, _d, _e;
125731-
const javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
125731+
let javaVersionRegExp;
125732+
if (versionFile == '.tool-versions') {
125733+
javaVersionRegExp =
125734+
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m;
125735+
}
125736+
else if (versionFile == '.java-version') {
125737+
javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
125738+
}
125739+
else {
125740+
throw new Error('Invalid version file');
125741+
}
125732125742
const fileContent = ((_b = (_a = content.match(javaVersionRegExp)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.version)
125733125743
? (_d = (_c = content.match(javaVersionRegExp)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.version
125734125744
: '';

docs/advanced-usage.md

+11-4
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,21 @@ steps:
525525
something_other
526526
```
527527

528-
## Java-version file
529-
If the `java-version-file` input is specified, the action will try to extract the version from the file and install it.
530-
Action is able to recognize all variants of the version description according to [jenv](https://github.com/jenv/jenv).
528+
## Java version file
529+
If the `java-version-file` input is specified, the action will extract the version from the file and install it.
530+
531+
Supported files are .java-version and .tool-versions.
532+
In .java-version file, only the version should be specified, e.g., 17.0.7.
533+
In .tool-versions file, java version should be preceded by the java keyword, e.g., java 17.0.7.
534+
.java-version recognizes all variants of the version description according to [jenv](https://github.com/jenv/jenv) and .tool-version recognizes all variants of the version description according to [asdf](https://github.com/asdf-vm/asdf).
535+
536+
If both java-version and java-version-file inputs are provided, the java-version input will be used.
537+
531538
Valid entry options:
532539
```
533540
major versions: 8, 11, 16, 17, 21
534541
more specific versions: 1.8.0.2, 17.0, 11.0, 11.0.4, 8.0.232, 8.0.282+8
535-
early access (EA) versions: 15-ea, 15.0.0-ea, 15.0.0-ea.2, 15.0.0+2-ea
542+
early access (EA) versions: 15-ea, 15.0.0-ea
536543
versions with specified distribution: openjdk64-11.0.2
537544
```
538545
If the file contains multiple versions, only the first one will be recognized.

src/setup-java.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ async function run() {
5555
);
5656
const content = fs.readFileSync(versionFile).toString().trim();
5757

58-
const version = getVersionFromFileContent(content, distributionName);
58+
const version = getVersionFromFileContent(
59+
content,
60+
distributionName,
61+
versionFile
62+
);
5963
core.debug(`Parsed version from file '${version}'`);
6064

6165
if (!version) {

src/util.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,19 @@ export function isCacheFeatureAvailable(): boolean {
115115

116116
export function getVersionFromFileContent(
117117
content: string,
118-
distributionName: string
118+
distributionName: string,
119+
versionFile: string
119120
): string | null {
120-
const javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
121+
let javaVersionRegExp: RegExp;
122+
if (versionFile == '.tool-versions') {
123+
javaVersionRegExp =
124+
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m;
125+
} else if (versionFile == '.java-version') {
126+
javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
127+
} else {
128+
throw new Error('Invalid version file');
129+
}
130+
121131
const fileContent = content.match(javaVersionRegExp)?.groups?.version
122132
? (content.match(javaVersionRegExp)?.groups?.version as string)
123133
: '';

0 commit comments

Comments
 (0)