mirror of
https://github.com/actions/setup-java.git
synced 2025-04-19 09:26:46 +00:00
Add findPackageForDownload
Support only major versions Fix typo in folder name
This commit is contained in:
parent
852fac3790
commit
f2f8c11ba1
4 changed files with 157 additions and 85 deletions
|
@ -1,6 +1,6 @@
|
|||
import { HttpClient } from '@actions/http-client';
|
||||
|
||||
import { CorettoDistribution } from '../../src/distributions/coretto/installer';
|
||||
import { CorettoDistribution } from '../../src/distributions/corretto/installer';
|
||||
import { JavaInstallerOptions } from '../../src/distributions/base-models';
|
||||
|
||||
describe('getAvailableVersions', () => {
|
||||
|
@ -9,7 +9,7 @@ describe('getAvailableVersions', () => {
|
|||
afterEach(() => {});
|
||||
|
||||
it('load available versions', async () => {
|
||||
const distribution = new CorettoDistribution('coretto', {
|
||||
const distribution = new CorettoDistribution({
|
||||
version: '11',
|
||||
architecture: 'x64',
|
||||
packageType: 'jdk',
|
||||
|
@ -18,6 +18,18 @@ describe('getAvailableVersions', () => {
|
|||
|
||||
const availableVersions = await distribution['getAvailableVersions']();
|
||||
expect(availableVersions).not.toBeNull();
|
||||
expect(availableVersions.length).toBe(24);
|
||||
expect(availableVersions.length).toBe(6);
|
||||
});
|
||||
|
||||
it('find package for download', async () => {
|
||||
const distribution = new CorettoDistribution({
|
||||
version: '11',
|
||||
architecture: 'x64',
|
||||
packageType: 'jdk',
|
||||
checkLatest: false
|
||||
});
|
||||
|
||||
const availableVersion = await distribution['findPackageForDownload']('15');
|
||||
expect(availableVersion).not.toBeNull();
|
||||
});
|
||||
});
|
|
@ -1,82 +0,0 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import { JavaBase } from '../base-installer';
|
||||
import { JavaDownloadRelease, JavaInstallerResults } from '../base-models';
|
||||
import { ICorrettoAllAvailableVersions, ICorettoAvailableVersions } from './models';
|
||||
|
||||
export class CorettoDistribution extends JavaBase {
|
||||
protected downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
protected findPackageForDownload(range: string): Promise<JavaDownloadRelease> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
private async getAvailableVersions(): Promise<ICorettoAvailableVersions[]> {
|
||||
const platform = this.getPlatformOption();
|
||||
const arch = this.architecture;
|
||||
const imageType = this.packageType;
|
||||
|
||||
console.time('coretto-retrieve-available-versions');
|
||||
|
||||
const availableVersionsUrl =
|
||||
'https://corretto.github.io/corretto-downloads/latest_links/indexmap_with_checksum.json';
|
||||
const fetchResult = await this.http.getJson<ICorrettoAllAvailableVersions>(
|
||||
availableVersionsUrl
|
||||
);
|
||||
if (!fetchResult.result) {
|
||||
throw Error(`Could not fetch latest corretto versions from ${availableVersionsUrl}`);
|
||||
}
|
||||
const availableVersions: ICorettoAvailableVersions[] = [];
|
||||
const eligbleVersions = fetchResult.result[platform][arch][imageType];
|
||||
for (const version in eligbleVersions) {
|
||||
const availableVersion = eligbleVersions[version];
|
||||
for (const fileType in availableVersion) {
|
||||
const availableVersionDetails = availableVersion[fileType];
|
||||
const correttoVersion = this.getCorettoVersionr(availableVersionDetails.resource);
|
||||
|
||||
availableVersions.push({
|
||||
checksum: availableVersionDetails.checksum,
|
||||
checksum_sha256: availableVersionDetails.checksum_sha256,
|
||||
fileType,
|
||||
resource: availableVersionDetails.resource,
|
||||
version: version,
|
||||
correttoVersion
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (core.isDebug()) {
|
||||
core.startGroup('Print information about available versions');
|
||||
console.timeEnd('coretto-retrieve-available-versions');
|
||||
console.log(`Available versions: [${availableVersions.length}]`);
|
||||
console.log(
|
||||
availableVersions.map(item => `${item.version}: ${item.correttoVersion}`).join(', ')
|
||||
);
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
return availableVersions;
|
||||
}
|
||||
|
||||
private getPlatformOption(): string {
|
||||
// Coretto has its own platform names so we need to map them
|
||||
switch (process.platform) {
|
||||
case 'darwin':
|
||||
return 'mac';
|
||||
case 'win32':
|
||||
return 'windows';
|
||||
default:
|
||||
return process.platform;
|
||||
}
|
||||
}
|
||||
|
||||
private getCorettoVersionr(resource: string): string {
|
||||
const regex = /(\d+.+)\//;
|
||||
const match = regex.exec(resource);
|
||||
if (match === null) {
|
||||
throw Error(`Could not parse corretto version from ${resource}`);
|
||||
}
|
||||
return match[1];
|
||||
}
|
||||
}
|
141
src/distributions/corretto/installer.ts
Normal file
141
src/distributions/corretto/installer.ts
Normal file
|
@ -0,0 +1,141 @@
|
|||
import * as core from '@actions/core';
|
||||
import * as tc from '@actions/tool-cache';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { extractJdkFile, getDownloadArchiveExtension } from '../../util';
|
||||
import { JavaBase } from '../base-installer';
|
||||
import { JavaDownloadRelease, JavaInstallerOptions, JavaInstallerResults } from '../base-models';
|
||||
import { ICorrettoAllAvailableVersions, ICorettoAvailableVersions } from './models';
|
||||
|
||||
export class CorettoDistribution extends JavaBase {
|
||||
constructor(installerOptions: JavaInstallerOptions) {
|
||||
super('Corretto', installerOptions);
|
||||
}
|
||||
|
||||
protected async downloadTool(javaRelease: JavaDownloadRelease): Promise<JavaInstallerResults> {
|
||||
core.info(
|
||||
`Downloading Java ${javaRelease.version} (${this.distribution}) from ${javaRelease.url} ...`
|
||||
);
|
||||
const javaArchivePath = await tc.downloadTool(javaRelease.url);
|
||||
|
||||
core.info(`Extracting Java archive...`);
|
||||
|
||||
const extractedJavaPath = await extractJdkFile(javaArchivePath, getDownloadArchiveExtension());
|
||||
|
||||
const archiveName = fs.readdirSync(extractedJavaPath)[0];
|
||||
const archivePath = path.join(extractedJavaPath, archiveName);
|
||||
const version = this.getToolcacheVersionName(javaRelease.version);
|
||||
|
||||
const javaPath = await tc.cacheDir(
|
||||
archivePath,
|
||||
this.toolcacheFolderName,
|
||||
version,
|
||||
this.architecture
|
||||
);
|
||||
|
||||
return { version: javaRelease.version, path: javaPath };
|
||||
}
|
||||
|
||||
protected async findPackageForDownload(version: string): Promise<JavaDownloadRelease> {
|
||||
if (!this.stable) {
|
||||
throw new Error('Early access versions are not supported');
|
||||
}
|
||||
if (version.includes('.')) {
|
||||
throw new Error('Only major versions are supported');
|
||||
}
|
||||
const availableVersions = await this.getAvailableVersions();
|
||||
const matchingVersions = availableVersions
|
||||
.filter(item => item.version == version)
|
||||
.map(item => {
|
||||
return {
|
||||
version: item.correttoVersion,
|
||||
url: item.downloadLink
|
||||
} as JavaDownloadRelease;
|
||||
});
|
||||
|
||||
const resolvedVersion = matchingVersions.length > 0 ? matchingVersions[0] : null;
|
||||
if (!resolvedVersion) {
|
||||
const availableOptions = availableVersions.map(item => item.version).join(', ');
|
||||
const availableOptionsMessage = availableOptions
|
||||
? `\nAvailable versions: ${availableOptions}`
|
||||
: '';
|
||||
throw new Error(
|
||||
`Could not find satisfied version for SemVer '${version}'. ${availableOptionsMessage}`
|
||||
);
|
||||
}
|
||||
return resolvedVersion;
|
||||
}
|
||||
|
||||
private async getAvailableVersions(): Promise<ICorettoAvailableVersions[]> {
|
||||
const platform = this.getPlatformOption();
|
||||
const arch = this.architecture;
|
||||
const imageType = this.packageType;
|
||||
|
||||
console.time('coretto-retrieve-available-versions');
|
||||
|
||||
const availableVersionsUrl =
|
||||
'https://corretto.github.io/corretto-downloads/latest_links/indexmap_with_checksum.json';
|
||||
const fetchResult = await this.http.getJson<ICorrettoAllAvailableVersions>(
|
||||
availableVersionsUrl
|
||||
);
|
||||
if (!fetchResult.result) {
|
||||
throw Error(`Could not fetch latest corretto versions from ${availableVersionsUrl}`);
|
||||
}
|
||||
const availableVersions: ICorettoAvailableVersions[] = [];
|
||||
const eligbleVersions = fetchResult.result[platform][arch][imageType];
|
||||
for (const version in eligbleVersions) {
|
||||
const availableVersion = eligbleVersions[version];
|
||||
for (const fileType in availableVersion) {
|
||||
const skipNonExtractableBinaries = fileType != getDownloadArchiveExtension();
|
||||
if (skipNonExtractableBinaries) {
|
||||
continue;
|
||||
}
|
||||
const availableVersionDetails = availableVersion[fileType];
|
||||
const correttoVersion = this.getCorettoVersion(availableVersionDetails.resource);
|
||||
|
||||
availableVersions.push({
|
||||
checksum: availableVersionDetails.checksum,
|
||||
checksum_sha256: availableVersionDetails.checksum_sha256,
|
||||
fileType,
|
||||
resource: availableVersionDetails.resource,
|
||||
downloadLink: `https://corretto.aws${availableVersionDetails.resource}`,
|
||||
version: version,
|
||||
correttoVersion
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (core.isDebug()) {
|
||||
core.startGroup('Print information about available versions');
|
||||
console.timeEnd('coretto-retrieve-available-versions');
|
||||
console.log(`Available versions: [${availableVersions.length}]`);
|
||||
console.log(
|
||||
availableVersions.map(item => `${item.version}: ${item.correttoVersion}`).join(', ')
|
||||
);
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
return availableVersions;
|
||||
}
|
||||
|
||||
private getPlatformOption(): string {
|
||||
// Coretto has its own platform names so we need to map them
|
||||
switch (process.platform) {
|
||||
case 'darwin':
|
||||
return 'mac';
|
||||
case 'win32':
|
||||
return 'windows';
|
||||
default:
|
||||
return process.platform;
|
||||
}
|
||||
}
|
||||
|
||||
private getCorettoVersion(resource: string): string {
|
||||
const regex = /(\d+.+)\//;
|
||||
const match = regex.exec(resource);
|
||||
if (match === null) {
|
||||
throw Error(`Could not parse corretto version from ${resource}`);
|
||||
}
|
||||
return match[1];
|
||||
}
|
||||
}
|
|
@ -20,5 +20,6 @@ export interface ICorettoAvailableVersions {
|
|||
checksum: string;
|
||||
checksum_sha256: string;
|
||||
resource: string;
|
||||
downloadLink: string;
|
||||
correttoVersion: string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue