FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Infer distribution from asdf .tool-versions vendor prefix (#1084) · actions/setup-java@4db08ef · GitHub

Commit 4db08ef

Browse files
authored
Infer distribution from asdf .tool-versions vendor prefix (#1084)
* Infer distribution from asdf .tool-versions vendor prefix asdf-java encodes the JDK vendor as a prefix on the version string in .tool-versions (e.g. `java temurin-17.0.3+7`). Capture that prefix and map it to a setup-java distribution, mirroring the existing .sdkmanrc behavior. Unknown prefixes warn and fall back to the distribution input. Fixes #1081 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 7dbccc6 commit 4db08ef

5 files changed

Lines changed: 253 additions & 4 deletions

File tree

‎__tests__/util.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,72 @@ describe('getVersionFromFileContent', () => {
296296
);
297297
});
298298
});
299+
300+
describe('.tool-versions', () => {
301+
it.each([
302+
['java temurin-17.0.3+7', '17.0.3+7', 'temurin'],
303+
['java temurin-jre-17.0.3+7', '17.0.3+7', 'temurin'],
304+
['java adoptopenjdk-11.0.16+8', '11.0.16+8', 'temurin'],
305+
['java adoptopenjdk-openj9-11.0.16+8', '11.0.16+8', 'temurin'],
306+
['java zulu-11.56.19', '11.56.19', 'zulu'],
307+
['java corretto-17.0.13.11.1', '17', 'corretto'], // corretto -> major only
308+
['java liberica-11.0.15+10', '11.0.15+10', 'liberica'],
309+
['java microsoft-11.0.13.8.1', '11.0.13', 'microsoft'],
310+
['java semeru-openj9-11.0.25+9', '11.0.25+9', 'semeru'],
311+
['java ibm-openj9-11.0.25+9', '11.0.25+9', 'semeru'],
312+
['java dragonwell-17.0.13.0.13+11', '17.0.13', 'dragonwell'],
313+
['java graalvm-22.3.0+java17', '22.3.0+java17', 'graalvm'],
314+
['java graalvm-community-22.3.0', '22.3.0', 'graalvm-community'],
315+
['java oracle-graalvm-21.0.5', '21.0.5', 'graalvm'],
316+
['java oracle-21.0.5', '21.0.5', 'oracle'],
317+
['java sapmachine-21.0.5', '21.0.5', 'sapmachine'],
318+
['java kona-17.0.13', '17.0.13', 'kona'],
319+
['java jetbrains-21.0.5', '21.0.5', 'jetbrains']
320+
])(
321+
'parsing %s should return version %s and distribution %s',
322+
(content: string, expectedVersion: string, expectedDist: string) => {
323+
const actual = getVersionFromFileContent(
324+
content,
325+
'openjdk',
326+
'.tool-versions'
327+
);
328+
expect(actual?.version).toBe(expectedVersion);
329+
expect(actual?.distribution).toBe(expectedDist);
330+
}
331+
);
332+
333+
it.each([
334+
['java 17.0.7', '17.0.7'],
335+
['java 17', '17'],
336+
['java 1.8', '8'],
337+
['java 21-ea', '21-ea']
338+
])(
339+
'parsing prefix-less %s should return version %s and no distribution',
340+
(content: string, expectedVersion: string) => {
341+
const actual = getVersionFromFileContent(
342+
content,
343+
'temurin',
344+
'.tool-versions'
345+
);
346+
expect(actual?.version).toBe(expectedVersion);
347+
expect(actual?.distribution).toBeUndefined();
348+
}
349+
);
350+
351+
it('should warn and return undefined distribution for unsupported vendor', () => {
352+
const warnSpy = jest.spyOn(core, 'warning');
353+
const actual = getVersionFromFileContent(
354+
'java openjdk-17.0.7',
355+
'temurin',
356+
'.tool-versions'
357+
);
358+
expect(actual?.version).toBe('17.0.7');
359+
expect(actual?.distribution).toBeUndefined();
360+
expect(warnSpy).toHaveBeenCalledWith(
361+
expect.stringContaining('Unknown asdf distribution identifier')
362+
);
363+
});
364+
});
299365
});
300366

301367
describe('isGhes', () => {

‎dist/cleanup/index.js‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95966,8 +95966,10 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
9596695966
}
9596795967
const versionFileName = getFileName(versionFile);
9596895968
if (versionFileName == '.tool-versions') {
95969+
// Capture an optional asdf-java vendor prefix (e.g. `temurin-`, `corretto-`)
95970+
// in the `distribution` group so it can be mapped to a setup-java distribution.
9596995971
javaVersionRegExp =
95970-
/^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
95972+
/^java\s+(?:(?<distribution>\S*)-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
9597195973
}
9597295974
else if (versionFileName == '.sdkmanrc') {
9597395975
// Match both version and optional distribution identifier
@@ -95987,6 +95989,14 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
9598795989
extractedDistribution = mapSdkmanDistribution(sdkmanDist);
9598895990
core.debug(`Parsed distribution '${extractedDistribution}' from SDKMAN identifier '${sdkmanDist}'`);
9598995991
}
95992+
// Extract distribution from asdf .tool-versions file
95993+
if (versionFileName == '.tool-versions' && match?.groups?.distribution) {
95994+
const asdfDist = match.groups.distribution;
95995+
extractedDistribution = mapAsdfDistribution(asdfDist);
95996+
if (extractedDistribution) {
95997+
core.debug(`Parsed distribution '${extractedDistribution}' from asdf identifier '${asdfDist}'`);
95998+
}
95999+
}
9599096000
core.debug(`Parsed version '${capturedVersion}' from file '${versionFileName}'`);
9599196001
if (!capturedVersion) {
9599296002
return null;
@@ -96035,6 +96045,43 @@ function mapSdkmanDistribution(sdkmanDist) {
9603596045
}
9603696046
return mapped;
9603796047
}
96048+
// Map asdf-java (.tool-versions) vendor identifiers to setup-java distribution names.
96049+
// asdf-java encodes the vendor as a prefix on the version string, e.g.
96050+
// `java temurin-17.0.3+7` or `java semeru-openj9-11.0.25+9`. Packaging variants
96051+
// (`-jre`, `-musl`, `-openj9`, `-crac`, `-javafx`, ...) are collapsed onto the
96052+
// base vendor since setup-java does not distinguish them here.
96053+
function mapAsdfDistribution(asdfDist) {
96054+
const normalized = asdfDist.toLowerCase();
96055+
// Multi-segment vendors that map to a distinct setup-java distribution.
96056+
if (normalized.startsWith('graalvm-community')) {
96057+
return 'graalvm-community';
96058+
}
96059+
if (normalized.startsWith('oracle-graalvm')) {
96060+
return 'graalvm';
96061+
}
96062+
const baseVendor = normalized.split('-')[0];
96063+
const distributionMap = {
96064+
temurin: 'temurin',
96065+
adoptopenjdk: 'temurin',
96066+
zulu: 'zulu',
96067+
corretto: 'corretto',
96068+
liberica: 'liberica',
96069+
microsoft: 'microsoft',
96070+
semeru: 'semeru',
96071+
ibm: 'semeru',
96072+
dragonwell: 'dragonwell',
96073+
graalvm: 'graalvm',
96074+
oracle: 'oracle',
96075+
sapmachine: 'sapmachine',
96076+
kona: 'kona',
96077+
jetbrains: 'jetbrains'
96078+
};
96079+
const mapped = distributionMap[baseVendor];
96080+
if (!mapped) {
96081+
core.warning(`Unknown asdf distribution identifier '${asdfDist}'. Please specify the distribution explicitly.`);
96082+
}
96083+
return mapped;
96084+
}
9603896085
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
9603996086
function avoidOldNotation(content) {
9604096087
return content.startsWith('1.') ? content.substring(2) : content;

‎dist/setup/index.js‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126958,8 +126958,10 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
126958126958
}
126959126959
const versionFileName = getFileName(versionFile);
126960126960
if (versionFileName == '.tool-versions') {
126961+
// Capture an optional asdf-java vendor prefix (e.g. `temurin-`, `corretto-`)
126962+
// in the `distribution` group so it can be mapped to a setup-java distribution.
126961126963
javaVersionRegExp =
126962-
/^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
126964+
/^java\s+(?:(?<distribution>\S*)-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
126963126965
}
126964126966
else if (versionFileName == '.sdkmanrc') {
126965126967
// Match both version and optional distribution identifier
@@ -126979,6 +126981,14 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
126979126981
extractedDistribution = mapSdkmanDistribution(sdkmanDist);
126980126982
core_debug(`Parsed distribution '${extractedDistribution}' from SDKMAN identifier '${sdkmanDist}'`);
126981126983
}
126984+
// Extract distribution from asdf .tool-versions file
126985+
if (versionFileName == '.tool-versions' && match?.groups?.distribution) {
126986+
const asdfDist = match.groups.distribution;
126987+
extractedDistribution = mapAsdfDistribution(asdfDist);
126988+
if (extractedDistribution) {
126989+
core_debug(`Parsed distribution '${extractedDistribution}' from asdf identifier '${asdfDist}'`);
126990+
}
126991+
}
126982126992
core_debug(`Parsed version '${capturedVersion}' from file '${versionFileName}'`);
126983126993
if (!capturedVersion) {
126984126994
return null;
@@ -127027,6 +127037,43 @@ function mapSdkmanDistribution(sdkmanDist) {
127027127037
}
127028127038
return mapped;
127029127039
}
127040+
// Map asdf-java (.tool-versions) vendor identifiers to setup-java distribution names.
127041+
// asdf-java encodes the vendor as a prefix on the version string, e.g.
127042+
// `java temurin-17.0.3+7` or `java semeru-openj9-11.0.25+9`. Packaging variants
127043+
// (`-jre`, `-musl`, `-openj9`, `-crac`, `-javafx`, ...) are collapsed onto the
127044+
// base vendor since setup-java does not distinguish them here.
127045+
function mapAsdfDistribution(asdfDist) {
127046+
const normalized = asdfDist.toLowerCase();
127047+
// Multi-segment vendors that map to a distinct setup-java distribution.
127048+
if (normalized.startsWith('graalvm-community')) {
127049+
return 'graalvm-community';
127050+
}
127051+
if (normalized.startsWith('oracle-graalvm')) {
127052+
return 'graalvm';
127053+
}
127054+
const baseVendor = normalized.split('-')[0];
127055+
const distributionMap = {
127056+
temurin: 'temurin',
127057+
adoptopenjdk: 'temurin',
127058+
zulu: 'zulu',
127059+
corretto: 'corretto',
127060+
liberica: 'liberica',
127061+
microsoft: 'microsoft',
127062+
semeru: 'semeru',
127063+
ibm: 'semeru',
127064+
dragonwell: 'dragonwell',
127065+
graalvm: 'graalvm',
127066+
oracle: 'oracle',
127067+
sapmachine: 'sapmachine',
127068+
kona: 'kona',
127069+
jetbrains: 'jetbrains'
127070+
};
127071+
const mapped = distributionMap[baseVendor];
127072+
if (!mapped) {
127073+
warning(`Unknown asdf distribution identifier '${asdfDist}'. Please specify the distribution explicitly.`);
127074+
}
127075+
return mapped;
127076+
}
127030127077
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
127031127078
function avoidOldNotation(content) {
127032127079
return content.startsWith('1.') ? content.substring(2) : content;

‎docs/advanced-usage.md‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,27 @@ steps:
780780

781781
Supported files are `.java-version`, `.tool-versions` and `.sdkmanrc`.
782782
* In `.java-version` file, only the version should be specified (e.g., 17.0.7). The `.java-version` file recognizes all variants of the version description according to [jenv](https://github.com/jenv/jenv).
783-
* In `.tool-versions` file, java version should be preceded by the java keyword (e.g., java 17.0.7). The `.tool-versions` file supports version specifications in accordance with [asdf](https://github.com/asdf-vm/asdf) standards, adhering to Semantic Versioning ([semver](https://semver.org/)).
783+
* In `.tool-versions` file, java version should be preceded by the java keyword (e.g., java 17.0.7). The `.tool-versions` file supports version specifications in accordance with [asdf](https://github.com/asdf-vm/asdf) standards, adhering to Semantic Versioning ([semver](https://semver.org/)). When the entry includes an [asdf-java](https://github.com/halcyon/asdf-java) vendor prefix (e.g. `java temurin-17.0.3+7`), setup-java can infer the `distribution` input automatically. Unrecognized vendor prefixes require setting `distribution` explicitly.
784+
785+
Supported asdf-java vendor prefix mappings (packaging variants such as `-jre`, `-musl`, `-openj9`, `-crac`, `-javafx` are collapsed onto the base vendor):
786+
787+
| asdf-java vendor prefix | setup-java distribution |
788+
| ----------------------- | ----------------------- |
789+
| `temurin` | `temurin` |
790+
| `adoptopenjdk` | `temurin` |
791+
| `zulu` | `zulu` |
792+
| `corretto` | `corretto` |
793+
| `liberica` | `liberica` |
794+
| `microsoft` | `microsoft` |
795+
| `semeru`, `ibm` | `semeru` |
796+
| `dragonwell` | `dragonwell` |
797+
| `graalvm`, `oracle-graalvm` | `graalvm` |
798+
| `graalvm-community` | `graalvm-community` |
799+
| `oracle` | `oracle` |
800+
| `sapmachine` | `sapmachine` |
801+
| `kona` | `kona` |
802+
| `jetbrains` | `jetbrains` |
803+
784804
* In `.sdkmanrc` file, java version should be preceded by the `java=` prefix (e.g., `java=17.0.7-tem`). When a recognized SDKMAN distribution suffix is present, setup-java can infer the `distribution` input automatically. Unrecognized suffixes require setting `distribution` explicitly. The `.sdkmanrc` file supports version specifications in accordance with [file format](https://sdkman.io/usage#env-command), see [Sdkman! documentation](https://sdkman.io/jdks) for more information.
785805

786806
Supported SDKMAN suffix mappings:
@@ -816,6 +836,19 @@ steps:
816836
java=17.0.7-tem
817837
```
818838
839+
**Example step using `asdf`** (distribution inferred from `.tool-versions`):
840+
```yml
841+
- name: Setup java
842+
uses: actions/setup-java@v5
843+
with:
844+
java-version-file: '.tool-versions'
845+
```
846+
847+
**Example `.tool-versions`**:
848+
```
849+
java temurin-17.0.7+7
850+
```
851+
819852
Valid entry options (does not apply to `.sdkmanrc`):
820853
```
821854
major versions: 8, 11, 16, 17, 21

‎src/util.ts‎

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,10 @@ export function getVersionFromFileContent(
149149

150150
const versionFileName = getFileName(versionFile);
151151
if (versionFileName == '.tool-versions') {
152+
// Capture an optional asdf-java vendor prefix (e.g. `temurin-`, `corretto-`)
153+
// in the `distribution` group so it can be mapped to a setup-java distribution.
152154
javaVersionRegExp =
153-
/^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
155+
/^java\s+(?:(?<distribution>\S*)-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
154156
} else if (versionFileName == '.sdkmanrc') {
155157
// Match both version and optional distribution identifier
156158
javaVersionRegExp =
@@ -173,6 +175,17 @@ export function getVersionFromFileContent(
173175
);
174176
}
175177

178+
// Extract distribution from asdf .tool-versions file
179+
if (versionFileName == '.tool-versions' && match?.groups?.distribution) {
180+
const asdfDist = match.groups.distribution;
181+
extractedDistribution = mapAsdfDistribution(asdfDist);
182+
if (extractedDistribution) {
183+
core.debug(
184+
`Parsed distribution '${extractedDistribution}' from asdf identifier '${asdfDist}'`
185+
);
186+
}
187+
}
188+
176189
core.debug(
177190
`Parsed version '${capturedVersion}' from file '${versionFileName}'`
178191
);
@@ -238,6 +251,49 @@ function mapSdkmanDistribution(sdkmanDist: string): string | undefined {
238251
return mapped;
239252
}
240253

254+
// Map asdf-java (.tool-versions) vendor identifiers to setup-java distribution names.
255+
// asdf-java encodes the vendor as a prefix on the version string, e.g.
256+
// `java temurin-17.0.3+7` or `java semeru-openj9-11.0.25+9`. Packaging variants
257+
// (`-jre`, `-musl`, `-openj9`, `-crac`, `-javafx`, ...) are collapsed onto the
258+
// base vendor since setup-java does not distinguish them here.
259+
function mapAsdfDistribution(asdfDist: string): string | undefined {
260+
const normalized = asdfDist.toLowerCase();
261+
262+
// Multi-segment vendors that map to a distinct setup-java distribution.
263+
if (normalized.startsWith('graalvm-community')) {
264+
return 'graalvm-community';
265+
}
266+
if (normalized.startsWith('oracle-graalvm')) {
267+
return 'graalvm';
268+
}
269+
270+
const baseVendor = normalized.split('-')[0];
271+
const distributionMap: Record<string, string> = {
272+
temurin: 'temurin',
273+
adoptopenjdk: 'temurin',
274+
zulu: 'zulu',
275+
corretto: 'corretto',
276+
liberica: 'liberica',
277+
microsoft: 'microsoft',
278+
semeru: 'semeru',
279+
ibm: 'semeru',
280+
dragonwell: 'dragonwell',
281+
graalvm: 'graalvm',
282+
oracle: 'oracle',
283+
sapmachine: 'sapmachine',
284+
kona: 'kona',
285+
jetbrains: 'jetbrains'
286+
};
287+
288+
const mapped = distributionMap[baseVendor];
289+
if (!mapped) {
290+
core.warning(
291+
`Unknown asdf distribution identifier '${asdfDist}'. Please specify the distribution explicitly.`
292+
);
293+
}
294+
return mapped;
295+
}
296+
241297
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
242298
function avoidOldNotation(content: string): string {
243299
return content.startsWith('1.') ? content.substring(2) : content;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL