Handle semver (#8)

* Handle semver

* Clean up
This commit is contained in:
Danny McCormick
2019-07-18 16:00:58 -04:00
committed by GitHub
parent fa3b0ff712
commit b6dbbd5232
5 changed files with 172 additions and 51 deletions

View File

@ -22,6 +22,7 @@ const exec = __importStar(require("@actions/exec"));
const tc = __importStar(require("@actions/tool-cache"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const semver = __importStar(require("semver"));
const httpm = __importStar(require("typed-rest-client/HttpClient"));
const IS_WINDOWS = process.platform === 'win32';
if (!tempDirectory) {
@ -50,7 +51,12 @@ function getJava(version, arch, jdkFile) {
let compressedFileExtension = '';
if (!jdkFile) {
core.debug('Downloading Jdk from Azul');
jdkFile = yield downloadJava(version);
let http = new httpm.HttpClient('setup-java');
let contents = yield (yield http.get('https://static.azul.com/zulu/bin/')).readBody();
let refs = contents.match(/<a href.*\">/gi) || [];
const downloadInfo = getDownloadInfo(refs, version);
jdkFile = yield tc.downloadTool(downloadInfo.url);
version = downloadInfo.version;
compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
}
else {
@ -60,7 +66,7 @@ function getJava(version, arch, jdkFile) {
let tempDir = path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000));
const jdkDir = yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir);
core.debug(`jdk extracted to ${jdkDir}`);
toolPath = yield tc.cacheDir(jdkDir, 'Java', normalizeVersion(version), arch);
toolPath = yield tc.cacheDir(jdkDir, 'Java', getCacheVersionString(version), arch);
}
let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch;
core.exportVariable('JAVA_HOME', toolPath);
@ -69,7 +75,7 @@ function getJava(version, arch, jdkFile) {
});
}
exports.getJava = getJava;
function normalizeVersion(version) {
function getCacheVersionString(version) {
const versionArray = version.split('.');
const major = versionArray[0];
const minor = versionArray.length > 1 ? versionArray[1] : '0';
@ -156,32 +162,70 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder, extension) {
}
});
}
function downloadJava(version) {
return __awaiter(this, void 0, void 0, function* () {
let filterString = '';
if (IS_WINDOWS) {
filterString = `jdk${version}-win_x64.zip`;
function getDownloadInfo(refs, version) {
version = normalizeVersion(version);
let extension = '';
if (IS_WINDOWS) {
extension = `-win_x64.zip`;
}
else {
if (process.platform === 'darwin') {
extension = `-macosx_x64.tar.gz`;
}
else {
if (process.platform === 'darwin') {
filterString = `jdk${version}-macosx_x64.tar.gz`;
}
else {
filterString = `jdk${version}-linux_x64.tar.gz`;
}
extension = `-linux_x64.tar.gz`;
}
let http = new httpm.HttpClient('setup-java');
let contents = yield (yield http.get('https://static.azul.com/zulu/bin/')).readBody();
let refs = contents.match(/<a href.*\">/gi) || [];
refs = refs.filter(val => {
if (val.indexOf(filterString) > -1) {
return true;
}
});
if (refs.length == 0) {
throw new Error(`No valid download found for version ${version}. Check https://static.azul.com/zulu/bin/ for a list of valid versions or download your own jdk file and add the jdkFile argument`);
}
// Maps version to url
let versionMap = new Map();
// Filter by platform
refs.forEach(ref => {
if (ref.indexOf(extension) < 0) {
return;
}
// If we haven't returned, means we're looking at the correct platform
let versions = ref.match(/jdk.*-/gi) || [];
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('jdk'.length, versions[0].length - 1);
if (semver.satisfies(refVersion, version)) {
versionMap.set(refVersion, 'https://static.azul.com/zulu/bin/' +
ref.slice('<a href="'.length, ref.length - '">'.length));
}
const fileName = refs[0].slice('<a href="'.length, refs[0].length - '">'.length);
return yield tc.downloadTool(`https://static.azul.com/zulu/bin/${fileName}`);
});
// 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}. 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) {
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');
}
}
// Add trailing .x if it is missing
if (version.split('.').length != 3) {
if (version[version.length - 1] != 'x') {
version = version + '.x';
}
}
return version;
}