Skip to content

Commit d2b3ef8

Browse files
committed
feat: Add distribution detection support to .sdkmanrc file
Extends .sdkmanrc support to automatically detect Java distribution from SDKMAN identifiers (e.g., java=21.0.5-tem maps to temurin distribution). Makes distribution input optional when using .sdkmanrc with distribution suffix.
1 parent be666c2 commit d2b3ef8

File tree

8 files changed

+265
-84
lines changed

8 files changed

+265
-84
lines changed

__tests__/data/sdkman-java-versions.csv

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
7.0.352-zulu, 7.0.352
33
8.0.282-trava, 8.0.282
44
8.0.432-albba, 8.0.432
5-
8.0.432-amzn, 8.0.432
5+
8.0.432-amzn, 8
66
8.0.432-kona, 8.0.432
77
8.0.432-librca, 8.0.432
88
8.0.432-sem, 8.0.432
99
8.0.432-tem, 8.0.432
1010
8.0.432-zulu, 8.0.432
1111
8.0.432.fx-librca, 8.0.432
1212
8.0.432.fx-zulu, 8.0.432
13-
8.0.442-amzn, 8.0.442
13+
8.0.442-amzn, 8
1414
8.0.442-librca, 8.0.442
1515
8.0.442-tem, 8.0.442
1616
8.0.442-zulu, 8.0.442
@@ -19,7 +19,7 @@
1919
11.0.14.1-jbr, 11.0.14
2020
11.0.15-trava, 11.0.15
2121
11.0.25-albba, 11.0.25
22-
11.0.25-amzn, 11.0.25
22+
11.0.25-amzn, 11
2323
11.0.25-kona, 11.0.25
2424
11.0.25-librca, 11.0.25
2525
11.0.25-ms, 11.0.25
@@ -29,7 +29,7 @@
2929
11.0.25-zulu, 11.0.25
3030
11.0.25.fx-librca, 11.0.25
3131
11.0.25.fx-zulu, 11.0.25
32-
11.0.26-amzn, 11.0.26
32+
11.0.26-amzn, 11
3333
11.0.26-librca, 11.0.26
3434
11.0.26-ms, 11.0.26
3535
11.0.26-sapmchn, 11.0.26
@@ -40,7 +40,7 @@
4040
17.0.12-jbr, 17.0.12
4141
17.0.12-oracle, 17.0.12
4242
17.0.13-albba, 17.0.13
43-
17.0.13-amzn, 17.0.13
43+
17.0.13-amzn, 17
4444
17.0.13-kona, 17.0.13
4545
17.0.13-librca, 17.0.13
4646
17.0.13-ms, 17.0.13
@@ -52,7 +52,7 @@
5252
17.0.13.crac-zulu, 17.0.13
5353
17.0.13.fx-librca, 17.0.13
5454
17.0.13.fx-zulu, 17.0.13
55-
17.0.14-amzn, 17.0.14
55+
17.0.14-amzn, 17
5656
17.0.14-librca, 17.0.14
5757
17.0.14-ms, 17.0.14
5858
17.0.14-sapmchn, 17.0.14
@@ -62,7 +62,7 @@
6262
17.0.9-graalce, 17.0.9
6363
21.0.2-graalce, 21.0.2
6464
21.0.2-open, 21.0.2
65-
21.0.5-amzn, 21.0.5
65+
21.0.5-amzn, 21
6666
21.0.5-graal, 21.0.5
6767
21.0.5-jbr, 21.0.5
6868
21.0.5-kona, 21.0.5
@@ -77,7 +77,7 @@
7777
21.0.5.crac-zulu, 21.0.5
7878
21.0.5.fx-librca, 21.0.5
7979
21.0.5.fx-zulu, 21.0.5
80-
21.0.6-amzn, 21.0.6
80+
21.0.6-amzn, 21
8181
21.0.6-graal, 21.0.6
8282
21.0.6-librca, 21.0.6
8383
21.0.6-ms, 21.0.6
@@ -94,7 +94,7 @@
9494
22.3.5.r17-mandrel, 22.3.5
9595
22.3.5.r17-nik, 22.3.5
9696
23-open, 23
97-
23.0.1-amzn, 23.0.1
97+
23.0.1-amzn, 23
9898
23.0.1-graal, 23.0.1
9999
23.0.1-graalce, 23.0.1
100100
23.0.1-librca, 23.0.1
@@ -106,7 +106,7 @@
106106
23.0.1.crac-zulu, 23.0.1
107107
23.0.1.fx-librca, 23.0.1
108108
23.0.1.fx-zulu, 23.0.1
109-
23.0.2-amzn, 23.0.2
109+
23.0.2-amzn, 23
110110
23.0.2-graal, 23.0.2
111111
23.0.2-graalce, 23.0.2
112112
23.0.2-librca, 23.0.2

__tests__/util.test.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,41 @@ describe('convertVersionToSemver', () => {
8888
describe('getVersionFromFileContent', () => {
8989
describe('.sdkmanrc', () => {
9090
it.each([
91-
['java=11.0.20.1-tem', '11.0.20'],
92-
['java = 11.0.20.1-tem', '11.0.20'],
93-
['java=11.0.20.1-tem # a comment in sdkmanrc', '11.0.20'],
94-
['java=11.0.20.1-tem\n#java=21.0.20.1-tem\n', '11.0.20'], // choose first match
95-
['java=11.0.20.1-tem\njava=21.0.20.1-tem\n', '11.0.20'], // choose first match
96-
['#java=11.0.20.1-tem\njava=21.0.20.1-tem\n', '21.0.20'] // first one is 'commented' in .sdkmanrc
97-
])('parsing %s should return %s', (content: string, expected: string) => {
91+
['java=11.0.20.1-tem', '11.0.20', 'temurin'],
92+
['java = 11.0.20.1-tem', '11.0.20', 'temurin'],
93+
['java=11.0.20.1-tem # a comment in sdkmanrc', '11.0.20', 'temurin'],
94+
['java=11.0.20.1-tem\n#java=21.0.20.1-tem\n', '11.0.20', 'temurin'], // choose first match
95+
['java=11.0.20.1-tem\njava=21.0.20.1-tem\n', '11.0.20', 'temurin'], // choose first match
96+
['#java=11.0.20.1-tem\njava=21.0.20.1-tem\n', '21.0.20', 'temurin'], // first one is 'commented' in .sdkmanrc
97+
['java=21.0.5-zulu', '21.0.5', 'zulu'],
98+
['java=17.0.13-amzn', '17', 'corretto'],
99+
['java=21.0.5-graal', '21.0.5', 'graalvm'],
100+
['java=17.0.9-graalce', '17.0.9', 'graalvm'],
101+
['java=11.0.25-librca', '11.0.25', 'liberica'],
102+
['java=11.0.25-ms', '11.0.25', 'microsoft'],
103+
['java=21.0.5-oracle', '21.0.5', 'oracle'],
104+
['java=11.0.25-sapmchn', '11.0.25', 'sapmachine'],
105+
['java=21.0.5-jbr', '21.0.5', 'jetbrains'],
106+
['java=11.0.25-sem', '11.0.25', 'temurin'],
107+
['java=17.0.13-dragonwell', '17.0.13', 'dragonwell']
108+
])('parsing %s should return version %s and distribution %s', (content: string, expectedVersion: string, expectedDist: string) => {
98109
const actual = getVersionFromFileContent(content, 'openjdk', '.sdkmanrc');
99-
expect(actual).toBe(expected);
110+
expect(actual?.version).toBe(expectedVersion);
111+
expect(actual?.distribution).toBe(expectedDist);
112+
});
113+
114+
it('should warn and return undefined distribution for unknown identifier', () => {
115+
const warnSpy = jest.spyOn(require('@actions/core'), 'warning');
116+
const actual = getVersionFromFileContent('java=21.0.5-unknown', 'temurin', '.sdkmanrc');
117+
expect(actual?.version).toBe('21.0.5');
118+
expect(actual?.distribution).toBeUndefined();
119+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining('Unknown SDKMAN distribution identifier'));
120+
});
121+
122+
it('should return version without distribution when no suffix provided', () => {
123+
const actual = getVersionFromFileContent('java=11.0.20', 'temurin', '.sdkmanrc');
124+
expect(actual?.version).toBe('11.0.20');
125+
expect(actual?.distribution).toBeUndefined();
100126
});
101127

102128
describe('known versions', () => {
@@ -115,7 +141,7 @@ describe('getVersionFromFileContent', () => {
115141
'openjdk',
116142
'.sdkmanrc'
117143
);
118-
expect(actual).toBe(expected);
144+
expect(actual?.version).toBe(expected);
119145
}
120146
);
121147
});

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ inputs:
88
java-version-file:
99
description: 'The path to the `.java-version` file. See examples of supported syntax in README file'
1010
distribution:
11-
description: 'Java distribution. See the list of supported distributions in README file'
12-
required: true
11+
description: 'Java distribution. See the list of supported distributions in README file. When using .sdkmanrc with a distribution suffix (e.g., java=21.0.5-tem), this input is optional.'
12+
required: false
1313
java-package:
1414
description: 'The package type (jdk, jre, jdk+fx, jre+fx)'
1515
required: false

dist/cleanup/index.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49755,8 +49755,9 @@ function isCacheFeatureAvailable() {
4975549755
}
4975649756
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
4975749757
function getVersionFromFileContent(content, distributionName, versionFile) {
49758-
var _a, _b, _c, _d, _e;
49758+
var _a, _b, _c;
4975949759
let javaVersionRegExp;
49760+
let extractedDistribution;
4976049761
function getFileName(versionFile) {
4976149762
return path_1.default.basename(versionFile);
4976249763
}
@@ -49766,14 +49767,22 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
4976649767
/^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
4976749768
}
4976849769
else if (versionFileName == '.sdkmanrc') {
49769-
javaVersionRegExp = /^java\s*=\s*(?<version>[^-]+)/m;
49770+
// Match both version and optional distribution identifier
49771+
javaVersionRegExp = /^java\s*=\s*(?<version>[^-\s]+)(?:-(?<distribution>[a-z0-9]+))?/m;
4977049772
}
4977149773
else {
4977249774
javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
4977349775
}
49774-
const capturedVersion = ((_b = (_a = content.match(javaVersionRegExp)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.version)
49775-
? (_d = (_c = content.match(javaVersionRegExp)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.version
49776+
const match = content.match(javaVersionRegExp);
49777+
const capturedVersion = ((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.version)
49778+
? match.groups.version
4977649779
: '';
49780+
// Extract distribution from .sdkmanrc file
49781+
if (versionFileName == '.sdkmanrc' && ((_b = match === null || match === void 0 ? void 0 : match.groups) === null || _b === void 0 ? void 0 : _b.distribution)) {
49782+
const sdkmanDist = match.groups.distribution;
49783+
extractedDistribution = mapSdkmanDistribution(sdkmanDist);
49784+
core.debug(`Parsed distribution '${extractedDistribution}' from SDKMAN identifier '${sdkmanDist}'`);
49785+
}
4977749786
core.debug(`Parsed version '${capturedVersion}' from file '${versionFileName}'`);
4977849787
if (!capturedVersion) {
4977949788
return null;
@@ -49787,13 +49796,40 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
4978749796
if (!version) {
4978849797
return null;
4978949798
}
49790-
if (constants_1.DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(distributionName)) {
49791-
const coerceVersion = (_e = semver.coerce(version)) !== null && _e !== void 0 ? _e : version;
49799+
// Apply DISTRIBUTIONS_ONLY_MAJOR_VERSION logic whenever the effective distribution
49800+
// (either explicitly provided or extracted from the version file) is in the list.
49801+
if (constants_1.DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(extractedDistribution || distributionName)) {
49802+
const coerceVersion = (_c = semver.coerce(version)) !== null && _c !== void 0 ? _c : version;
4979249803
version = semver.major(coerceVersion).toString();
4979349804
}
49794-
return version.toString();
49805+
return {
49806+
version: version.toString(),
49807+
distribution: extractedDistribution
49808+
};
4979549809
}
4979649810
exports.getVersionFromFileContent = getVersionFromFileContent;
49811+
// Map SDKMAN distribution identifiers to setup-java distribution names
49812+
function mapSdkmanDistribution(sdkmanDist) {
49813+
const distributionMap = {
49814+
'tem': 'temurin',
49815+
'sem': 'temurin',
49816+
'zulu': 'zulu',
49817+
'amzn': 'corretto',
49818+
'graal': 'graalvm',
49819+
'graalce': 'graalvm',
49820+
'librca': 'liberica',
49821+
'ms': 'microsoft',
49822+
'oracle': 'oracle',
49823+
'sapmchn': 'sapmachine',
49824+
'jbr': 'jetbrains',
49825+
'dragonwell': 'dragonwell'
49826+
};
49827+
const mapped = distributionMap[sdkmanDist.toLowerCase()];
49828+
if (!mapped) {
49829+
core.warning(`Unknown SDKMAN distribution identifier '${sdkmanDist}'. Please specify the distribution explicitly.`);
49830+
}
49831+
return mapped;
49832+
}
4979749833
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
4979849834
function avoidOldNotation(content) {
4979949835
return content.startsWith('1.') ? content.substring(2) : content;

dist/setup/index.js

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87511,9 +87511,7 @@ function run() {
8751187511
return __awaiter(this, void 0, void 0, function* () {
8751287512
try {
8751387513
const versions = core.getMultilineInput(constants.INPUT_JAVA_VERSION);
87514-
const distributionName = core.getInput(constants.INPUT_DISTRIBUTION, {
87515-
required: true
87516-
});
87514+
let distributionName = core.getInput(constants.INPUT_DISTRIBUTION);
8751787515
const versionFile = core.getInput(constants.INPUT_JAVA_VERSION_FILE);
8751887516
const architecture = core.getInput(constants.INPUT_ARCHITECTURE);
8751987517
const packageType = core.getInput(constants.INPUT_JAVA_PACKAGE);
@@ -87529,26 +87527,48 @@ function run() {
8752987527
if (!versions.length && !versionFile) {
8753087528
throw new Error('java-version or java-version-file input expected');
8753187529
}
87532-
const installerInputsOptions = {
87533-
architecture,
87534-
packageType,
87535-
checkLatest,
87536-
distributionName,
87537-
jdkFile,
87538-
toolchainIds
87539-
};
8754087530
if (!versions.length) {
8754187531
core.debug('java-version input is empty, looking for java-version-file input');
8754287532
const content = fs_1.default.readFileSync(versionFile).toString().trim();
87543-
const version = (0, util_1.getVersionFromFileContent)(content, distributionName, versionFile);
87544-
core.debug(`Parsed version from file '${version}'`);
87545-
if (!version) {
87533+
const versionInfo = (0, util_1.getVersionFromFileContent)(content, distributionName, versionFile);
87534+
core.debug(`Parsed version from file '${versionInfo === null || versionInfo === void 0 ? void 0 : versionInfo.version}'`);
87535+
if (!versionInfo) {
8754687536
throw new Error(`No supported version was found in file ${versionFile}`);
8754787537
}
87548-
yield installVersion(version, installerInputsOptions);
87538+
// Use distribution from file if available, otherwise use the input
87539+
if (versionInfo.distribution) {
87540+
core.info(`Using distribution '${versionInfo.distribution}' from ${versionFile}`);
87541+
distributionName = versionInfo.distribution;
87542+
}
87543+
else if (!distributionName) {
87544+
throw new Error('distribution input is required when not specified in the version file');
87545+
}
87546+
const installerInputsOptions = {
87547+
architecture,
87548+
packageType,
87549+
checkLatest,
87550+
distributionName,
87551+
jdkFile,
87552+
toolchainIds
87553+
};
87554+
yield installVersion(versionInfo.version, installerInputsOptions);
8754987555
}
87550-
for (const [index, version] of versions.entries()) {
87551-
yield installVersion(version, installerInputsOptions, index);
87556+
else {
87557+
// When using java-version input, distribution is still required
87558+
if (!distributionName) {
87559+
throw new Error('distribution input is required');
87560+
}
87561+
const installerInputsOptions = {
87562+
architecture,
87563+
packageType,
87564+
checkLatest,
87565+
distributionName,
87566+
jdkFile,
87567+
toolchainIds
87568+
};
87569+
for (const [index, version] of versions.entries()) {
87570+
yield installVersion(version, installerInputsOptions, index);
87571+
}
8755287572
}
8755387573
core.endGroup();
8755487574
const matchersPath = path.join(__dirname, '..', '..', '.github');
@@ -87899,8 +87919,9 @@ function isCacheFeatureAvailable() {
8789987919
}
8790087920
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
8790187921
function getVersionFromFileContent(content, distributionName, versionFile) {
87902-
var _a, _b, _c, _d, _e;
87922+
var _a, _b, _c;
8790387923
let javaVersionRegExp;
87924+
let extractedDistribution;
8790487925
function getFileName(versionFile) {
8790587926
return path_1.default.basename(versionFile);
8790687927
}
@@ -87910,14 +87931,22 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
8791087931
/^java\s+(?:\S*-)?(?<version>\d+(?:\.\d+)*([+_.-](?:openj9[-._]?\d[\w.-]*|java\d+|jre[-_\w]*|OpenJDK\d+[\w_.-]*|[a-z0-9]+))*)/im;
8791187932
}
8791287933
else if (versionFileName == '.sdkmanrc') {
87913-
javaVersionRegExp = /^java\s*=\s*(?<version>[^-]+)/m;
87934+
// Match both version and optional distribution identifier
87935+
javaVersionRegExp = /^java\s*=\s*(?<version>[^-\s]+)(?:-(?<distribution>[a-z0-9]+))?/m;
8791487936
}
8791587937
else {
8791687938
javaVersionRegExp = /(?<version>(?<=(^|\s|-))(\d+\S*))(\s|$)/;
8791787939
}
87918-
const capturedVersion = ((_b = (_a = content.match(javaVersionRegExp)) === null || _a === void 0 ? void 0 : _a.groups) === null || _b === void 0 ? void 0 : _b.version)
87919-
? (_d = (_c = content.match(javaVersionRegExp)) === null || _c === void 0 ? void 0 : _c.groups) === null || _d === void 0 ? void 0 : _d.version
87940+
const match = content.match(javaVersionRegExp);
87941+
const capturedVersion = ((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.version)
87942+
? match.groups.version
8792087943
: '';
87944+
// Extract distribution from .sdkmanrc file
87945+
if (versionFileName == '.sdkmanrc' && ((_b = match === null || match === void 0 ? void 0 : match.groups) === null || _b === void 0 ? void 0 : _b.distribution)) {
87946+
const sdkmanDist = match.groups.distribution;
87947+
extractedDistribution = mapSdkmanDistribution(sdkmanDist);
87948+
core.debug(`Parsed distribution '${extractedDistribution}' from SDKMAN identifier '${sdkmanDist}'`);
87949+
}
8792187950
core.debug(`Parsed version '${capturedVersion}' from file '${versionFileName}'`);
8792287951
if (!capturedVersion) {
8792387952
return null;
@@ -87931,13 +87960,40 @@ function getVersionFromFileContent(content, distributionName, versionFile) {
8793187960
if (!version) {
8793287961
return null;
8793387962
}
87934-
if (constants_1.DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(distributionName)) {
87935-
const coerceVersion = (_e = semver.coerce(version)) !== null && _e !== void 0 ? _e : version;
87963+
// Apply DISTRIBUTIONS_ONLY_MAJOR_VERSION logic whenever the effective distribution
87964+
// (either explicitly provided or extracted from the version file) is in the list.
87965+
if (constants_1.DISTRIBUTIONS_ONLY_MAJOR_VERSION.includes(extractedDistribution || distributionName)) {
87966+
const coerceVersion = (_c = semver.coerce(version)) !== null && _c !== void 0 ? _c : version;
8793687967
version = semver.major(coerceVersion).toString();
8793787968
}
87938-
return version.toString();
87969+
return {
87970+
version: version.toString(),
87971+
distribution: extractedDistribution
87972+
};
8793987973
}
8794087974
exports.getVersionFromFileContent = getVersionFromFileContent;
87975+
// Map SDKMAN distribution identifiers to setup-java distribution names
87976+
function mapSdkmanDistribution(sdkmanDist) {
87977+
const distributionMap = {
87978+
'tem': 'temurin',
87979+
'sem': 'temurin',
87980+
'zulu': 'zulu',
87981+
'amzn': 'corretto',
87982+
'graal': 'graalvm',
87983+
'graalce': 'graalvm',
87984+
'librca': 'liberica',
87985+
'ms': 'microsoft',
87986+
'oracle': 'oracle',
87987+
'sapmchn': 'sapmachine',
87988+
'jbr': 'jetbrains',
87989+
'dragonwell': 'dragonwell'
87990+
};
87991+
const mapped = distributionMap[sdkmanDist.toLowerCase()];
87992+
if (!mapped) {
87993+
core.warning(`Unknown SDKMAN distribution identifier '${sdkmanDist}'. Please specify the distribution explicitly.`);
87994+
}
87995+
return mapped;
87996+
}
8794187997
// By convention, action expects version 8 in the format `8.*` instead of `1.8`
8794287998
function avoidOldNotation(content) {
8794387999
return content.startsWith('1.') ? content.substring(2) : content;

0 commit comments

Comments
 (0)