Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added .tool-versions file support #606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c3eb4ca
Merge pull request #1 from actions/main
mahabaleshwars Mar 4, 2024
7abdf1c
added support for tool version file
mahabaleshwars Mar 4, 2024
a7f2ec4
testing with one regex
mahabaleshwars Mar 4, 2024
12fa539
working regex
mahabaleshwars Mar 4, 2024
3ac7f4d
Checked for the file extension
mahabaleshwars Mar 4, 2024
7ac1739
added e2e checks for tool version
mahabaleshwars Mar 4, 2024
c2eed4c
removed error warning
mahabaleshwars Mar 5, 2024
d6dce30
updated regex to support early version
mahabaleshwars Mar 7, 2024
15bd552
updated regex for early version support
mahabaleshwars Mar 7, 2024
091c706
updated regex for early version
mahabaleshwars Mar 7, 2024
23f648d
updated regex to accept early versions
mahabaleshwars Mar 8, 2024
108d337
added coreinfo to analyze
mahabaleshwars Mar 8, 2024
600bab6
updated the regex
mahabaleshwars Mar 8, 2024
7e1d245
updated regex
mahabaleshwars Mar 8, 2024
7c2ba30
new regex for early version
mahabaleshwars Mar 8, 2024
4963182
updated regex to match the new version file format
mahabaleshwars Mar 8, 2024
5633ed7
new regex
mahabaleshwars Mar 8, 2024
0bd564a
changed the regex
mahabaleshwars Mar 8, 2024
dc07897
redex updated
mahabaleshwars Mar 8, 2024
ac5ac98
used java version regex
mahabaleshwars Mar 8, 2024
00ea4e3
regex updated
mahabaleshwars Mar 8, 2024
3a44440
regex modified
mahabaleshwars Mar 8, 2024
ae1e7f4
regex updated
mahabaleshwars Mar 8, 2024
4ef246e
regex updated
mahabaleshwars Mar 8, 2024
8c268be
regex updated
mahabaleshwars Mar 8, 2024
c5831e8
updated regex to support early versions
mahabaleshwars Mar 8, 2024
0d65cda
Regex updated to support all java versions
mahabaleshwars Mar 8, 2024
4253d87
Documentation updated to add tool version description
mahabaleshwars Mar 11, 2024
33cfd28
Documentation updated for the tool version file
mahabaleshwars Mar 12, 2024
b71bea4
update the advanced doc and readme file to specify tool version changes
mahabaleshwars Mar 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 20 additions & 4 deletions .github/workflows/e2e-versions.yml
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -27,7 +27,7 @@ This action allows you to work with Java and Scala projects.

- `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.

- `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).
- `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).

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

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

## License

Expand Down
14 changes: 12 additions & 2 deletions dist/cleanup/index.js
Expand Up @@ -87887,9 +87887,19 @@ function isCacheFeatureAvailable() {
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getVersionFromFileContent(content, distributionName) {
function getVersionFromFileContent(content, distributionName, versionFile) {
var _a, _b, _c, _d, _e;
const javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
let javaVersionRegExp;
if (versionFile == '.tool-versions') {
javaVersionRegExp =
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m;
}
else if (versionFile == '.java-version') {
javaVersionRegExp = /(?<version>(?<=(^|\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
: '';
Expand Down
16 changes: 13 additions & 3 deletions dist/setup/index.js
Expand Up @@ -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}`);
Expand Down Expand Up @@ -125261,9 +125261,19 @@ function isCacheFeatureAvailable() {
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function getVersionFromFileContent(content, distributionName) {
function getVersionFromFileContent(content, distributionName, versionFile) {
var _a, _b, _c, _d, _e;
const javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
let javaVersionRegExp;
if (versionFile == '.tool-versions') {
javaVersionRegExp =
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m;
Copy link

@rtyley rtyley Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this regex will not accept full java versions commonly generated in asdf using latest - eg:

asdf local java latest:corretto-21

...will generate a file with this line:

java corretto-21.0.2.13.1

The regex doesn't recognise this, so setup-java will fail with this message:

Error: No supported version was found in file .tool-versions

If we play around with the regex, we can see that we have to edit the string down to java corretto-21.0.2 in order to get it to work:

image

}
else if (versionFile == '.java-version') {
javaVersionRegExp = /(?<version>(?<=(^|\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
: '';
Expand Down
15 changes: 11 additions & 4 deletions docs/advanced-usage.md
Expand Up @@ -525,14 +525,21 @@ steps:
something_other
```

## Java-version file
If the `java-version-file` input is specified, the action will try to extract the version from the file and install it.
Action is able to recognize all variants of the version description according to [jenv](https://github.com/jenv/jenv).
## Java version file
If the `java-version-file` input is specified, the action will extract the version from the file and install it.

Supported files are .java-version and .tool-versions.
In .java-version file, only the version should be specified, e.g., 17.0.7.
In .tool-versions file, java version should be preceded by the java keyword, e.g., java 17.0.7.
.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).

If both java-version and java-version-file inputs are provided, the java-version input will be used.

Valid entry options:
```
major versions: 8, 11, 16, 17, 21
more specific versions: 1.8.0.2, 17.0, 11.0, 11.0.4, 8.0.232, 8.0.282+8
early access (EA) versions: 15-ea, 15.0.0-ea, 15.0.0-ea.2, 15.0.0+2-ea

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

15.0.2-sa.2, 15.2.0+2-sa

early access (EA) versions: 15-ea, 15.0.0-ea
versions with specified distribution: openjdk64-11.0.2
```
If the file contains multiple versions, only the first one will be recognized.
6 changes: 5 additions & 1 deletion src/setup-java.ts
Expand Up @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions src/util.ts
Expand Up @@ -115,9 +115,19 @@ export function isCacheFeatureAvailable(): boolean {

export function getVersionFromFileContent(
content: string,
distributionName: string
distributionName: string,
versionFile: string
): string | null {
const javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
let javaVersionRegExp: RegExp;
if (versionFile == '.tool-versions') {
javaVersionRegExp =
/^(java\s+)(?:\S*-)?v?(?<version>(\d+)(\.\d+)?(\.\d+)?(\+\d+)?(-ea(\.\d+)?)?)$/m;
} else if (versionFile == '.java-version') {
javaVersionRegExp = /(?<version>(?<=(^|\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)
: '';
Expand Down