Files
setup-java/src/distributions/liberica/installer.ts
T
Bruno BorgesandCopilot App ef0b922255 Fix Liberica Java 25 release resolution (#1313)
* Fix Liberica multi-component patch resolution

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

* Fix Liberica patch version ordering

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
2026-11-04 16:26:15 -05:00

256 lines
7.9 KiB
TypeScript

import {JavaBase} from '../base-installer.js';
import {
JavaDownloadRelease,
JavaInstallerOptions,
JavaInstallerResults
} from '../base-models.js';
import semver from 'semver';
import {
cacheJdkDir,
extractJdkFile,
getDownloadArchiveExtension,
renameWinArchive
} from '../../util.js';
import * as core from '@actions/core';
import {ArchitectureOptions, LibericaVersion, OsVersions} from './models.js';
import {isAlpineLinux} from '../platform-types.js';
import fs from 'fs';
import path from 'path';
const supportedPlatform = `'linux', 'linux-musl', 'macos', 'solaris', 'windows'`;
const supportedArchitectures = `'x86', 'x64', 'armv7', 'aarch64', 'ppc64le'`;
export class LibericaDistributions extends JavaBase {
constructor(installerOptions: JavaInstallerOptions) {
super('Liberica', installerOptions);
}
protected async downloadTool(
javaRelease: JavaDownloadRelease
): Promise<JavaInstallerResults> {
core.info(
`Downloading Java ${javaRelease.version} (${this.distribution}) from ${javaRelease.url} ...`
);
let javaArchivePath = await this.downloadAndVerify(javaRelease);
core.info(`Extracting Java archive...`);
const extension = getDownloadArchiveExtension();
if (process.platform === 'win32') {
javaArchivePath = renameWinArchive(javaArchivePath);
}
const extractedJavaPath = await extractJdkFile(javaArchivePath, extension);
const archiveName = fs.readdirSync(extractedJavaPath)[0];
const archivePath = path.join(extractedJavaPath, archiveName);
const javaPath = await cacheJdkDir(
archivePath,
this.toolcacheFolderName,
this.getToolcacheVersionName(javaRelease.version),
this.architecture
);
return {version: javaRelease.version, path: javaPath};
}
protected async findPackageForDownload(
range: string
): Promise<JavaDownloadRelease> {
const availableVersionsRaw = await this.getAvailableVersions();
const availableVersions = availableVersionsRaw.map(item => ({
url: item.downloadUrl,
version: this.convertVersionToSemver(item)
}));
const satisfiedVersion = availableVersions
.filter(item => this.isVersionSatisfied(range, item.version))
.sort((a, b) => -this.compareVersions(a.version, b.version))[0];
if (!satisfiedVersion) {
const availableVersionStrings = availableVersions.map(
item => item.version
);
throw this.createVersionNotFoundError(range, availableVersionStrings);
}
return satisfiedVersion;
}
protected normalizeVersion(version: string) {
const normalized = super.normalizeVersion(version);
if (/^\d+\.\d+\.\d+(?:\.\d+)+(?:-ea)?$/.test(version)) {
// Keep patch-only requests distinct from exact SDKMAN build requests.
normalized.version = version.replace(/-ea$/, '');
}
return normalized;
}
protected isVersionSatisfied(range: string, version: string): boolean {
const patchRequest = range.match(/^(\d+\.\d+\.\d+)((?:\.\d+)+)$/);
if (patchRequest) {
const candidate = semver.parse(version);
if (!candidate || !semver.eq(patchRequest[1], candidate)) {
return false;
}
const patch =
candidate.build.length > 1
? candidate.build.slice(0, -1).join('.')
: '0';
return patch === patchRequest[2].slice(1);
}
return super.isVersionSatisfied(range, version);
}
protected compareVersions(a: string, b: string): number {
const mainComparison = semver.compare(a, b);
if (mainComparison !== 0) {
return mainComparison;
}
const getPatchAndBuild = (version: string) => {
const build = new semver.SemVer(version).build;
return build.length > 1
? {patch: build.slice(0, -1), build: build[build.length - 1]}
: {patch: [], build: build[0] ?? '0'};
};
const first = getPatchAndBuild(a);
const second = getPatchAndBuild(b);
const compareNumericIdentifiers = (left: string, right: string) =>
semver.compareBuild(`0.0.0+${left}`, `0.0.0+${right}`);
for (
let index = 0;
index < Math.max(first.patch.length, second.patch.length);
index++
) {
const patchComparison = compareNumericIdentifiers(
first.patch[index] ?? '0',
second.patch[index] ?? '0'
);
if (patchComparison !== 0) {
return patchComparison;
}
}
return compareNumericIdentifiers(first.build, second.build);
}
private async getAvailableVersions(): Promise<LibericaVersion[]> {
if (core.isDebug()) {
console.time('Retrieving available versions for Liberica took'); // eslint-disable-line no-console
}
const url = this.prepareAvailableVersionsUrl();
core.debug(`Gathering available versions from '${url}'`);
const availableVersions =
(await this.http.getJson<LibericaVersion[]>(url)).result ?? [];
if (core.isDebug()) {
core.startGroup('Print information about available versions');
console.timeEnd('Retrieving available versions for Liberica took'); // eslint-disable-line no-console
core.debug(`Available versions: [${availableVersions.length}]`);
core.debug(availableVersions.map(item => item.version).join(', '));
core.endGroup();
}
return availableVersions;
}
private prepareAvailableVersionsUrl() {
const urlOptions = {
os: this.getPlatformOption(),
'bundle-type': this.getBundleType(),
...this.getArchitectureOptions(),
'build-type': this.stable ? 'all' : 'ea',
'installation-type': 'archive',
fields:
'downloadUrl,version,featureVersion,interimVersion,updateVersion,buildVersion'
};
const searchParams = new URLSearchParams(urlOptions).toString();
return `https://api.bell-sw.com/v1/liberica/releases?${searchParams}`;
}
private getBundleType(): string {
const [bundleType, feature] = this.packageType.split('+');
if (feature?.includes('fx')) {
return bundleType + '-full';
}
return bundleType;
}
private getArchitectureOptions(): ArchitectureOptions {
const arch = this.distributionArchitecture();
switch (arch) {
case 'x86':
return {bitness: '32', arch: 'x86'};
case 'x64':
return {bitness: '64', arch: 'x86'};
case 'armv7':
return {bitness: '32', arch: 'arm'};
case 'aarch64':
return {bitness: '64', arch: 'arm'};
case 'ppc64le':
return {bitness: '64', arch: 'ppc'};
default:
throw new Error(
`Architecture '${this.architecture}' is not supported. Supported architectures: ${supportedArchitectures}`
);
}
}
private getPlatformOption(
platform: NodeJS.Platform = process.platform
): OsVersions {
switch (platform) {
case 'darwin':
return 'macos';
case 'win32':
case 'cygwin':
return 'windows';
case 'linux':
return isAlpineLinux(platform) ? 'linux-musl' : 'linux';
case 'sunos':
return 'solaris';
default:
throw new Error(
`Platform '${platform}' is not supported. Supported platforms: ${supportedPlatform}`
);
}
}
private convertVersionToSemver(version: LibericaVersion): string {
const {featureVersion, interimVersion, updateVersion} = version;
const buildVersion = version.version.split('+')[1] || version.buildVersion;
const mainVersion = [featureVersion, interimVersion, updateVersion].join(
'.'
);
const patchVersion = version.version
.split('+')[0]
.split('.')
.slice(3)
.join('.');
if (patchVersion) {
return `${mainVersion}+${patchVersion}.${buildVersion}`;
}
if (buildVersion != 0) {
return `${mainVersion}+${buildVersion}`;
}
return mainVersion;
}
protected distributionArchitecture(): string {
const arch = super.distributionArchitecture();
switch (arch) {
case 'arm':
return 'armv7';
default:
return arch;
}
}
}