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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 172 additions and 51 deletions

@ -14,7 +14,7 @@ Basic:
actions: actions:
- uses: actions/setup-java@latest - uses: actions/setup-java@latest
with: with:
version: 9.0.4 // The JDK version to make available on the path. Use a whole version, such as 9.0.4, not a semver version version: 9.0.4 // The JDK version to make available on the path. Takes a whole or semver Jdk version, or 1.x syntax (e.g. 1.8 => Jdk 8.x)
architecture: x64 // (x64 or x86) - defaults to x64 architecture: x64 // (x64 or x86) - defaults to x64
- run: java -cp java HelloWorldApp - run: java -cp java HelloWorldApp
``` ```
@ -36,7 +36,7 @@ jobs:
build: build:
strategy: strategy:
matrix: matrix:
java: [ 6.0.119, 9.0.4, 12.0.2 ] java: [ 1.6, 9.0.x, 12.0.2 ]
name: Java ${{ matrix.java }} sample name: Java ${{ matrix.java }} sample
actions: actions:
- name: Setup java - name: Setup java

@ -77,6 +77,22 @@ describe('installer tests', () => {
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true); expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000); }, 100000);
it('Downloads java with 1.x syntax', async () => {
await installer.getJava('1.10', 'x64', '');
const JavaDir = path.join(toolDir, 'Java', '10.0.2', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000);
it('Downloads java with normal semver syntax', async () => {
await installer.getJava('9.0.x', 'x64', '');
const JavaDir = path.join(toolDir, 'Java', '9.0.7', 'x64');
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
}, 100000);
it('Throws if invalid directory to jdk', async () => { it('Throws if invalid directory to jdk', async () => {
let thrown = false; let thrown = false;
try { try {

@ -3,7 +3,7 @@ description: 'Setup your runner with Java'
author: 'GitHub' author: 'GitHub'
inputs: inputs:
version: version:
description: 'The JDK version to make available on the path. Use a whole version, such as 9.0.4' description: 'The JDK version to make available on the path. Takes a whole or semver Jdk version, or 1.x syntax (e.g. 1.8 => Jdk 8.x)'
required: true required: true
architecture: architecture:
description: 'The architecture (x86, x64) of the JDK.' description: 'The architecture (x86, x64) of the JDK.'

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

@ -6,6 +6,7 @@ import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
import * as fs from 'fs'; import * as fs from 'fs';
import * as path from 'path'; import * as path from 'path';
import * as semver from 'semver';
import * as httpm from 'typed-rest-client/HttpClient'; import * as httpm from 'typed-rest-client/HttpClient';
const IS_WINDOWS = process.platform === 'win32'; const IS_WINDOWS = process.platform === 'win32';
@ -38,7 +39,16 @@ export async function getJava(
let compressedFileExtension = ''; let compressedFileExtension = '';
if (!jdkFile) { if (!jdkFile) {
core.debug('Downloading Jdk from Azul'); core.debug('Downloading Jdk from Azul');
jdkFile = await downloadJava(version); let http: httpm.HttpClient = new httpm.HttpClient('setup-java');
let contents = await (await http.get(
'https://static.azul.com/zulu/bin/'
)).readBody();
let refs = contents.match(/<a href.*\">/gi) || [];
const downloadInfo = getDownloadInfo(refs, version);
jdkFile = await tc.downloadTool(downloadInfo.url);
version = downloadInfo.version;
compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz'; compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
} else { } else {
core.debug('Retrieving Jdk from local path'); core.debug('Retrieving Jdk from local path');
@ -57,7 +67,7 @@ export async function getJava(
toolPath = await tc.cacheDir( toolPath = await tc.cacheDir(
jdkDir, jdkDir,
'Java', 'Java',
normalizeVersion(version), getCacheVersionString(version),
arch arch
); );
} }
@ -68,7 +78,7 @@ export async function getJava(
core.addPath(path.join(toolPath, 'bin')); core.addPath(path.join(toolPath, 'bin'));
} }
function normalizeVersion(version: string) { function getCacheVersionString(version: string) {
const versionArray = version.split('.'); const versionArray = version.split('.');
const major = versionArray[0]; const major = versionArray[0];
const minor = versionArray.length > 1 ? versionArray[1] : '0'; const minor = versionArray.length > 1 ? versionArray[1] : '0';
@ -161,37 +171,88 @@ async function unzipJavaDownload(
} }
} }
async function downloadJava(version: string): Promise<string> { function getDownloadInfo(
let filterString = ''; refs: string[],
version: string
): {version: string; url: string} {
version = normalizeVersion(version);
let extension = '';
if (IS_WINDOWS) { if (IS_WINDOWS) {
filterString = `jdk${version}-win_x64.zip`; extension = `-win_x64.zip`;
} else { } else {
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
filterString = `jdk${version}-macosx_x64.tar.gz`; extension = `-macosx_x64.tar.gz`;
} else { } else {
filterString = `jdk${version}-linux_x64.tar.gz`; extension = `-linux_x64.tar.gz`;
} }
} }
let http: httpm.HttpClient = new httpm.HttpClient('setup-java');
let contents = await (await http.get( // Maps version to url
'https://static.azul.com/zulu/bin/' let versionMap = new Map();
)).readBody();
let refs = contents.match(/<a href.*\">/gi) || []; // Filter by platform
refs = refs.filter(val => { refs.forEach(ref => {
if (val.indexOf(filterString) > -1) { if (ref.indexOf(extension) < 0) {
return true; 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)
);
} }
}); });
if (refs.length == 0) { // 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( 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` `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`
); );
} }
const fileName = refs[0].slice( return {version: curVersion, url: curUrl};
'<a href="'.length, }
refs[0].length - '">'.length
); function normalizeVersion(version: string): string {
return await tc.downloadTool(`https://static.azul.com/zulu/bin/${fileName}`); 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;
} }