forked from actions/setup-java
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Oracle JDK (fixes actions#69)
- Loading branch information
Showing
6 changed files
with
338 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { OracleDistribution } from '../../src/distributions/oracle/installer'; | ||
import os from 'os'; | ||
import * as core from '@actions/core'; | ||
|
||
describe('findPackageForDownload', () => { | ||
let distribution: OracleDistribution; | ||
let spyDebug: jest.SpyInstance; | ||
|
||
beforeEach(() => { | ||
distribution = new OracleDistribution({ | ||
version: '', | ||
architecture: 'x64', | ||
packageType: 'jdk', | ||
checkLatest: false | ||
}); | ||
|
||
spyDebug = jest.spyOn(core, 'debug'); | ||
spyDebug.mockImplementation(() => {}); | ||
}); | ||
|
||
it.each([ | ||
[ | ||
'19', | ||
'19', | ||
'https://download.oracle.com/java/19/latest/jdk-19_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' | ||
], | ||
[ | ||
'19.0.1', | ||
'19.0.1', | ||
'https://download.oracle.com/java/19/archive/jdk-19.0.1_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' | ||
], | ||
[ | ||
'18.0.2.1', | ||
'18.0.2.1', | ||
'https://download.oracle.com/java/18/archive/jdk-18.0.2.1_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' | ||
], | ||
[ | ||
'17', | ||
'17', | ||
'https://download.oracle.com/java/17/latest/jdk-17_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' | ||
], | ||
[ | ||
'17.0.1', | ||
'17.0.1', | ||
'https://download.oracle.com/java/17/archive/jdk-17.0.1_{{OS_TYPE}}-x64_bin.{{ARCHIVE_TYPE}}' | ||
] | ||
])('version is %s -> %s', async (input, expectedVersion, expectedUrl) => { | ||
const result = await distribution['findPackageForDownload'](input); | ||
expect(result.version).toBe(expectedVersion); | ||
let os: string; | ||
let archive: string; | ||
switch (process.platform) { | ||
case 'darwin': | ||
os = 'macos'; | ||
archive = 'tar.gz'; | ||
break; | ||
case 'win32': | ||
os = 'windows'; | ||
archive = 'zip'; | ||
break; | ||
default: | ||
os = process.platform.toString(); | ||
archive = 'tar.gz'; | ||
break; | ||
} | ||
const url = expectedUrl.replace('{{OS_TYPE}}', os).replace('{{ARCHIVE_TYPE}}', archive); | ||
expect(result.url).toBe(url); | ||
}); | ||
|
||
it.each([ | ||
['amd64', 'x64'], | ||
['arm64', 'aarch64'] | ||
])( | ||
'defaults to os.arch(): %s mapped to distro arch: %s', | ||
async (osArch: string, distroArch: string) => { | ||
jest.spyOn(os, 'arch').mockReturnValue(osArch); | ||
jest.spyOn(os, 'platform').mockReturnValue('linux'); | ||
|
||
const version = '17'; | ||
const distro = new OracleDistribution({ | ||
version, | ||
architecture: '', // to get default value | ||
packageType: 'jdk', | ||
checkLatest: false | ||
}); | ||
|
||
const result = await distro['findPackageForDownload'](version); | ||
const expectedUrl = `https://download.oracle.com/java/17/latest/jdk-17_linux-${distroArch}_bin.tar.gz`; | ||
|
||
expect(result.url).toBe(expectedUrl); | ||
} | ||
); | ||
|
||
it('should throw an error', async () => { | ||
await expect(distribution['findPackageForDownload']('8')).rejects.toThrow( | ||
/Oracle JDK is only supported for JDK 17 and later/ | ||
); | ||
await expect(distribution['findPackageForDownload']('11')).rejects.toThrow( | ||
/Oracle JDK is only supported for JDK 17 and later/ | ||
); | ||
await expect(distribution['findPackageForDownload']('18')).rejects.toThrow( | ||
/Could not find Oracle JDK for SemVer */ | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import * as core from '@actions/core'; | ||
import * as tc from '@actions/tool-cache'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
|
||
import { JavaBase } from '../base-installer'; | ||
import { JavaDownloadRelease, JavaInstallerOptions, JavaInstallerResults } from '../base-models'; | ||
import { extractJdkFile, getDownloadArchiveExtension } from '../../util'; | ||
import { HttpCodes } from '@actions/http-client'; | ||
|
||
const ORACLE_DL_BASE = 'https://download.oracle.com/java' | ||
|
||
export class OracleDistribution extends JavaBase { | ||
constructor(installerOptions: JavaInstallerOptions) { | ||
super('Oracle', 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...`); | ||
let extension = getDownloadArchiveExtension(); | ||
|
||
let extractedJavaPath = await extractJdkFile(javaArchivePath, extension); | ||
|
||
const archiveName = fs.readdirSync(extractedJavaPath)[0]; | ||
const archivePath = path.join(extractedJavaPath, archiveName); | ||
const version = this.getToolcacheVersionName(javaRelease.version); | ||
|
||
let javaPath = await tc.cacheDir( | ||
archivePath, | ||
this.toolcacheFolderName, | ||
version, | ||
this.architecture | ||
); | ||
|
||
return { version: javaRelease.version, path: javaPath }; | ||
} | ||
|
||
protected async findPackageForDownload(range: string): Promise<JavaDownloadRelease> { | ||
const arch = this.distributionArchitecture(); | ||
if (arch !== 'x64' && arch !== 'aarch64') { | ||
throw new Error(`Unsupported architecture: ${this.architecture}`); | ||
} | ||
|
||
if (!this.stable) { | ||
throw new Error('Early access versions are not supported'); | ||
} | ||
|
||
if (this.packageType !== 'jdk') { | ||
throw new Error('Oracle JDK provides only the `jdk` package type'); | ||
} | ||
|
||
const platform = this.getPlatform() | ||
const extension = getDownloadArchiveExtension(); | ||
let major; | ||
let fileUrl; | ||
if (range.includes('.')) { | ||
major = range.split('.')[0] | ||
fileUrl = `${ORACLE_DL_BASE}/${major}/archive/jdk-${range}_${platform}-${arch}_bin.${extension}` | ||
} else { | ||
major = range | ||
fileUrl = `${ORACLE_DL_BASE}/${range}/latest/jdk-${range}_${platform}-${arch}_bin.${extension}` | ||
} | ||
|
||
if (parseInt(major) < 17) { | ||
throw new Error('Oracle JDK is only supported for JDK 17 and later') | ||
} | ||
|
||
const response = await this.http.head(fileUrl) | ||
|
||
if (response.message.statusCode === HttpCodes.NotFound) { | ||
throw new Error(`Could not find Oracle JDK for SemVer ${range}`); | ||
} | ||
|
||
if (response.message.statusCode !== HttpCodes.OK) { | ||
throw new Error(`Http request for Oracle JDK failed with status code: ${response.message.statusCode}`); | ||
} | ||
|
||
return { url: fileUrl, version: range } | ||
} | ||
|
||
private getPlatform(platform: NodeJS.Platform = process.platform): OsVersions { | ||
switch (platform) { | ||
case 'darwin': | ||
return 'macos'; | ||
case 'win32': | ||
case 'cygwin': | ||
return 'windows'; | ||
case 'linux': | ||
return 'linux'; | ||
default: | ||
throw new Error( | ||
`Platform '${platform}' is not supported. Supported platforms: 'linux', 'macos', 'windows'` | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type OsVersions = 'linux' | 'macos' | 'windows'; |