Skip to content

Commit 5ad8d5e

Browse files
v-mahabaleshwarsShegox
authored andcommitted
Added .tool-versions file support (actions#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 f12b132 commit 5ad8d5e

7 files changed

Lines changed: 75 additions & 17 deletions

File tree

‎.github/workflows/e2e-versions.yml‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,19 +292,23 @@ jobs:
292292
matrix:
293293
os: [macos-latest, windows-latest, ubuntu-latest]
294294
distribution: ['temurin', 'microsoft', 'corretto']
295+
java-version-file: ['.java-version', '.tool-versions']
295296
steps:
296297
- name: Checkout
297298
uses: actions/checkout@v4
298299
- name: Create .java-version file
299300
shell: bash
300301
run: echo "8" > .java-version
302+
- name: Create .tool-versions file
303+
shell: bash
304+
run: echo "java 8" > .tool-versions
301305
- name: setup-java
302306
uses: ./
303307
id: setup-java
304308
with:
305309
distribution: ${{ matrix.distribution }}
306310
java-version: 11
307-
java-version-file: '.java-version'
311+
java-version-file: ${{matrix.java-version-file }}
308312
- name: Verify Java
309313
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
310314
shell: bash
@@ -317,18 +321,22 @@ jobs:
317321
matrix:
318322
os: [macos-latest, windows-latest, ubuntu-latest]
319323
distribution: ['temurin', 'zulu', 'liberica', 'microsoft', 'corretto']
324+
java-version-file: ['.java-version', '.tool-versions']
320325
steps:
321326
- name: Checkout
322327
uses: actions/checkout@v4
323328
- name: Create .java-version file
324329
shell: bash
325330
run: echo "11" > .java-version
331+
- name: Create .tool-versions file
332+
shell: bash
333+
run: echo "java 11" > .tool-versions
326334
- name: setup-java
327335
uses: ./
328336
id: setup-java
329337
with:
330338
distribution: ${{ matrix.distribution }}
331-
java-version-file: '.java-version'
339+
java-version-file: ${{matrix.java-version-file }}
332340
- name: Verify Java
333341
run: bash __tests__/verify-java.sh "11" "${{ steps.setup-java.outputs.path }}"
334342
shell: bash
@@ -341,18 +349,22 @@ jobs:
341349
matrix:
342350
os: [macos-latest, windows-latest, ubuntu-latest]
343351
distribution: ['adopt', 'adopt-openj9', 'zulu']
352+
java-version-file: ['.java-version', '.tool-versions']
344353
steps:
345354
- name: Checkout
346355
uses: actions/checkout@v4
347356
- name: Create .java-version file
348357
shell: bash
349358
run: echo "11.0.2" > .java-version
359+
- name: Create .tool-versions file
360+
shell: bash
361+
run: echo "java 11.0.2" > .tool-versions
350362
- name: setup-java
351363
uses: ./
352364
id: setup-java
353365
with:
354366
distribution: ${{ matrix.distribution }}
355-
java-version-file: '.java-version'
367+
java-version-file: ${{matrix.java-version-file }}
356368
- name: Verify Java
357369
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}"
358370
shell: bash
@@ -365,18 +377,22 @@ jobs:
365377
matrix:
366378
os: [macos-latest, windows-latest, ubuntu-latest]
367379
distribution: ['adopt', 'zulu', 'liberica']
380+
java-version-file: ['.java-version', '.tool-versions']
368381
steps:
369382
- name: Checkout
370383
uses: actions/checkout@v4
371384
- name: Create .java-version file
372385
shell: bash
373386
run: echo "openjdk64-11.0.2" > .java-version
387+
- name: Create .tool-versions file
388+
shell: bash
389+
run: echo "java openjdk64-11.0.2" > .tool-versions
374390
- name: setup-java
375391
uses: ./
376392
id: setup-java
377393
with:
378394
distribution: ${{ matrix.distribution }}
379-
java-version-file: '.java-version'
395+
java-version-file: ${{matrix.java-version-file }}
380396
- name: Verify Java
381397
run: bash __tests__/verify-java.sh "11.0.2" "${{ steps.setup-java.outputs.path }}"
382398
shell: bash

‎README.md‎

Lines changed: 2 additions & 1 deletion
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

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

272273
## License
273274

‎dist/cleanup/index.js‎

Lines changed: 12 additions & 2 deletions
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‎

Lines changed: 13 additions & 3 deletions
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‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,21 @@ steps:
538538
something_other
539539
```
540540

541-
## Java-version file
542-
If the `java-version-file` input is specified, the action will try to extract the version from the file and install it.
543-
Action is able to recognize all variants of the version description according to [jenv](https://github.com/jenv/jenv).
541+
## Java version file
542+
If the `java-version-file` input is specified, the action will extract the version from the file and install it.
543+
544+
Supported files are .java-version and .tool-versions.
545+
In .java-version file, only the version should be specified, e.g., 17.0.7.
546+
In .tool-versions file, java version should be preceded by the java keyword, e.g., java 17.0.7.
547+
.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).
548+
549+
If both java-version and java-version-file inputs are provided, the java-version input will be used.
550+
544551
Valid entry options:
545552
```
546553
major versions: 8, 11, 16, 17, 21
547554
more specific versions: 1.8.0.2, 17.0, 11.0, 11.0.4, 8.0.232, 8.0.282+8
548-
early access (EA) versions: 15-ea, 15.0.0-ea, 15.0.0-ea.2, 15.0.0+2-ea
555+
early access (EA) versions: 15-ea, 15.0.0-ea
549556
versions with specified distribution: openjdk64-11.0.2
550557
```
551558
If the file contains multiple versions, only the first one will be recognized.

‎src/setup-java.ts‎

Lines changed: 5 additions & 1 deletion
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‎

Lines changed: 12 additions & 2 deletions
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)