diff --git a/src/distro/adoptopenjdk.ts b/src/distro/adoptopenjdk.ts new file mode 100644 index 00000000..744f652d --- /dev/null +++ b/src/distro/adoptopenjdk.ts @@ -0,0 +1,23 @@ +import * as core from '@actions/core'; +import * as tc from '@actions/tool-cache'; + +export async function getJavaAdoptOpenJDK( + version: string, + javaPackage: string, + arch: string, + OS: string +) { + core.debug('Downloading JDK from AdoptOpenJDK'); + + const jdkFile = await tc.downloadTool( + `https://api.adoptopenjdk.net/v3/binary/latest/${normalize( + version + )}/ga/${OS}/${arch}/${javaPackage}/hotspot/normal/adoptopenjdk` + ); + return [jdkFile, version]; +} + +function normalize(version: string): string { + if (version == '1.8') return '8'; + return version; +} \ No newline at end of file diff --git a/src/distro/zulu.ts b/src/distro/zulu.ts new file mode 100644 index 00000000..c1cafa13 --- /dev/null +++ b/src/distro/zulu.ts @@ -0,0 +1,150 @@ +import * as core from '@actions/core'; +import * as tc from '@actions/tool-cache'; +import * as httpm from '@actions/http-client'; +import * as semver from 'semver'; + +const IS_WINDOWS = process.platform === 'win32'; + +export async function getJavaZulu(version: string, javaPackage: string) { + core.debug('Downloading JDK from Azul'); + const http = new httpm.HttpClient('setup-java', undefined, { + allowRetries: true, + maxRetries: 3 + }); + + const url = 'https://static.azul.com/zulu/bin/'; + + const response = await http.get(url); + const statusCode = response.message.statusCode || 0; + if (statusCode < 200 || statusCode > 299) { + let body = ''; + try { + body = await response.readBody(); + } catch (err) { + core.debug(`Unable to read body: ${err.message}`); + } + const message = `Unexpected HTTP status code '${response.message.statusCode}' when retrieving versions from '${url}'. ${body}`.trim(); + throw new Error(message); + } + + const contents = await response.readBody(); + const refs = contents.match(//gi) || []; + const downloadInfo = getDownloadInfo(refs, version, javaPackage); + const jdkFile = await tc.downloadTool(downloadInfo.url); + + version = downloadInfo.version; + return [jdkFile, version]; +} + +function getDownloadInfo( + refs: string[], + version: string, + javaPackage: string +): {version: string; url: string} { + version = normalizeVersion(version); + let extension = ''; + if (IS_WINDOWS) { + extension = `-win_x64.zip`; + } else { + if (process.platform === 'darwin') { + extension = `-macosx_x64.tar.gz`; + } else { + extension = `-linux_x64.tar.gz`; + } + } + + let pkgRegexp = new RegExp(''); + let pkgTypeLength = 0; + if (javaPackage === 'jdk') { + pkgRegexp = /jdk.*-/gi; + pkgTypeLength = 'jdk'.length; + } else if (javaPackage == 'jre') { + pkgRegexp = /jre.*-/gi; + pkgTypeLength = 'jre'.length; + } else if (javaPackage == 'jdk+fx') { + pkgRegexp = /fx-jdk.*-/gi; + pkgTypeLength = 'fx-jdk'.length; + } else { + throw new Error( + `package argument ${javaPackage} is not in [jdk | jre | jdk+fx]` + ); + } + + // Maps version to url + let versionMap = new Map(); + + // Filter by platform + refs.forEach(ref => { + if (!ref.endsWith(extension + '">')) { + return; + } + + // If we haven't returned, means we're looking at the correct platform + let versions = ref.match(pkgRegexp) || []; + if (versions.length > 1) { + throw new Error( + `Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}` + ); + } + if (versions.length == 0) { + return; + } + const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1); + + if (semver.satisfies(refVersion, version)) { + versionMap.set( + refVersion, + 'https://static.azul.com/zulu/bin/' + + ref.slice(''.length) + ); + } + }); + + // Choose the most recent satisfying version + let curVersion = '0.0.0'; + let curUrl = ''; + for (const entry of versionMap.entries()) { + const entryVersion = entry[0]; + const entryUrl = entry[1]; + if (semver.gt(entryVersion, curVersion)) { + curUrl = entryUrl; + curVersion = entryVersion; + } + } + + if (curUrl == '') { + throw new Error( + `No valid download found for version ${version} and package ${javaPackage}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument` + ); + } + + return {version: curVersion, url: curUrl}; +} + +function normalizeVersion(version: string): string { + if (version.slice(0, 2) === '1.') { + // Trim leading 1. for versions like 1.8 + version = version.slice(2); + if (!version) { + throw new Error('1. is not a valid version'); + } + } + + if (version.endsWith('-ea')) { + // convert e.g. 14-ea to 14.0.0-ea + if (version.indexOf('.') == -1) { + version = version.slice(0, version.length - 3) + '.0.0-ea'; + } + // match anything in -ea.X (semver won't do .x matching on pre-release versions) + if (version[0] >= '0' && version[0] <= '9') { + version = '>=' + version; + } + } else if (version.split('.').length < 3) { + // For non-ea versions, add trailing .x if it is missing + if (version[version.length - 1] != 'x') { + version = version + '.x'; + } + } + + return version; +} diff --git a/src/installer.ts b/src/installer.ts index 75ec8c11..11c55afc 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -1,19 +1,24 @@ import * as core from '@actions/core'; import * as io from '@actions/io'; import * as exec from '@actions/exec'; -import * as httpm from '@actions/http-client'; import * as tc from '@actions/tool-cache'; import * as fs from 'fs'; import * as path from 'path'; +<<<<<<< HEAD import * as semver from 'semver'; import * as util from './util'; +======= + +import {getJavaAdoptOpenJDK} from './distro/adoptopenjdk'; +import {getJavaZulu} from './distro/zulu'; +>>>>>>> break distro out into separate files const tempDirectory = util.getTempDir(); const IS_WINDOWS = util.isWindows(); export async function getJava( version: string, - vendor: string, + distro: string, arch: string, jdkFile: string, javaPackage: string @@ -26,13 +31,14 @@ export async function getJava( let compressedFileExtension = ''; if (!jdkFile) { - async function setupVendor(vendor: string) { - switch (vendor) { + async function setupVendor(distro: string) { + switch (distro) { case 'adoptopenjdk': [jdkFile, version] = await getJavaAdoptOpenJDK( version, javaPackage, - arch + arch, + OS ); break; case 'zulu': @@ -42,7 +48,7 @@ export async function getJava( return [jdkFile, version]; } - [jdkFile, version] = await setupVendor(vendor); + [jdkFile, version] = await setupVendor(distro); compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz'; } else { @@ -83,52 +89,6 @@ export async function getJava( core.setOutput('version', version); } -async function getJavaZulu(version: string, javaPackage: string) { - core.debug('Downloading JDK from Azul'); - const http = new httpm.HttpClient('setup-java', undefined, { - allowRetries: true, - maxRetries: 3 - }); - - const url = 'https://static.azul.com/zulu/bin/'; - - const response = await http.get(url); - const statusCode = response.message.statusCode || 0; - if (statusCode < 200 || statusCode > 299) { - let body = ''; - try { - body = await response.readBody(); - } catch (err) { - core.debug(`Unable to read body: ${err.message}`); - } - const message = `Unexpected HTTP status code '${response.message.statusCode}' when retrieving versions from '${url}'. ${body}`.trim(); - throw new Error(message); - } - - const contents = await response.readBody(); - const refs = contents.match(//gi) || []; - const downloadInfo = getDownloadInfo(refs, version, javaPackage); - const jdkFile = await tc.downloadTool(downloadInfo.url); - - version = downloadInfo.version; - return [jdkFile, version]; -} - -async function getJavaAdoptOpenJDK( - version: string, - javaPackage: string, - arch: string -) { - core.debug('Downloading JDK from AdoptOpenJDK'); - - const jdkFile = await tc.downloadTool( - `https://api.adoptopenjdk.net/v3/binary/latest/${normalizeAdoptOpenJDK( - version - )}/ga/${OS}/${arch}/${javaPackage}/hotspot/normal/adoptopenjdk` - ); - return [jdkFile, version]; -} - function getCacheVersionString(version: string) { const versionArray = version.split('.'); const major = versionArray[0]; @@ -221,121 +181,3 @@ async function unzipJavaDownload( throw new Error(`Jdk argument ${jdkFile} is not a file`); } } - -function getDownloadInfo( - refs: string[], - version: string, - javaPackage: string -): {version: string; url: string} { - version = normalizeVersion(version); - let extension = ''; - if (IS_WINDOWS) { - extension = `-win_x64.zip`; - } else { - if (process.platform === 'darwin') { - extension = `-macosx_x64.tar.gz`; - } else { - extension = `-linux_x64.tar.gz`; - } - } - - let pkgRegexp = new RegExp(''); - let pkgTypeLength = 0; - if (javaPackage === 'jdk') { - pkgRegexp = /jdk.*-/gi; - pkgTypeLength = 'jdk'.length; - } else if (javaPackage == 'jre') { - pkgRegexp = /jre.*-/gi; - pkgTypeLength = 'jre'.length; - } else if (javaPackage == 'jdk+fx') { - pkgRegexp = /fx-jdk.*-/gi; - pkgTypeLength = 'fx-jdk'.length; - } else { - throw new Error( - `package argument ${javaPackage} is not in [jdk | jre | jdk+fx]` - ); - } - - // Maps version to url - let versionMap = new Map(); - - // Filter by platform - refs.forEach(ref => { - if (!ref.endsWith(extension + '">')) { - return; - } - - // If we haven't returned, means we're looking at the correct platform - let versions = ref.match(pkgRegexp) || []; - if (versions.length > 1) { - throw new Error( - `Invalid ref received from https://static.azul.com/zulu/bin/: ${ref}` - ); - } - if (versions.length == 0) { - return; - } - const refVersion = versions[0].slice(pkgTypeLength, versions[0].length - 1); - - if (semver.satisfies(refVersion, version)) { - versionMap.set( - refVersion, - 'https://static.azul.com/zulu/bin/' + - ref.slice(''.length) - ); - } - }); - - // Choose the most recent satisfying version - let curVersion = '0.0.0'; - let curUrl = ''; - for (const entry of versionMap.entries()) { - const entryVersion = entry[0]; - const entryUrl = entry[1]; - if (semver.gt(entryVersion, curVersion)) { - curUrl = entryUrl; - curVersion = entryVersion; - } - } - - if (curUrl == '') { - throw new Error( - `No valid download found for version ${version} and package ${javaPackage}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument` - ); - } - - return {version: curVersion, url: curUrl}; -} - -function normalizeVersion(version: string): string { - if (version.slice(0, 2) === '1.') { - // Trim leading 1. for versions like 1.8 - version = version.slice(2); - if (!version) { - throw new Error('1. is not a valid version'); - } - } - - if (version.endsWith('-ea')) { - // convert e.g. 14-ea to 14.0.0-ea - if (version.indexOf('.') == -1) { - version = version.slice(0, version.length - 3) + '.0.0-ea'; - } - // match anything in -ea.X (semver won't do .x matching on pre-release versions) - if (version[0] >= '0' && version[0] <= '9') { - version = '>=' + version; - } - } else if (version.split('.').length < 3) { - // For non-ea versions, add trailing .x if it is missing - if (version[version.length - 1] != 'x') { - version = version + '.x'; - } - } - - return version; -} - -function normalizeAdoptOpenJDK(version: string): string { - if (version == '1.8') return '8'; - return version; -} diff --git a/src/setup-java.ts b/src/setup-java.ts index 49fea11a..8eab312f 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -11,14 +11,21 @@ async function run() { if (!version) { version = core.getInput(constants.INPUT_JAVA_VERSION, {required: true}); } +<<<<<<< HEAD const vendor = core.getInput('vendor', {required: true}); const arch = core.getInput(constants.INPUT_ARCHITECTURE, {required: true}); const javaPackage = core.getInput(constants.INPUT_JAVA_PACKAGE, { required: true }); const jdkFile = core.getInput(constants.INPUT_JDK_FILE, {required: false}); +======= + const distro = core.getInput('vendor', {required: true}); + const arch = core.getInput('architecture', {required: true}); + const javaPackage = core.getInput('java-package', {required: true}); + const jdkFile = core.getInput('jdkFile', {required: false}) || ''; +>>>>>>> break distro out into separate files - await installer.getJava(version, vendor, arch, jdkFile, javaPackage); + await installer.getJava(version, distro, arch, jdkFile, javaPackage); const matchersPath = path.join(__dirname, '..', '..', '.github'); core.info(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);