Skip to content

Commit 0cf3ae5

Browse files
author
Danny McCormick
authored
Download from Azul if no Jdk file specified (#5)
* Download from zulu * Fix pathing * Fix * Fix * Fix * Update io * Tests and pathing * Add back husky * Update action.yml
1 parent 012e076 commit 0cf3ae5

31 files changed

Lines changed: 385 additions & 314 deletions

__tests__/installer.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('installer tests', () => {
5151
}
5252
}, 100000);
5353

54-
it('Acquires version of Java if no matching version is installed', async () => {
54+
it('Installs version of Java from jdkFile if no matching version is installed', async () => {
5555
await installer.getJava('12', 'x64', javaFilePath);
5656
const JavaDir = path.join(toolDir, 'Java', '12.0.0', 'x64');
5757

@@ -69,6 +69,24 @@ describe('installer tests', () => {
6969
expect(thrown).toBe(true);
7070
});
7171

72+
it('Downloads java if no file given', async () => {
73+
await installer.getJava('8.0.102', 'x64', '');
74+
const JavaDir = path.join(toolDir, 'Java', '8.0.102', 'x64');
75+
76+
expect(fs.existsSync(`${JavaDir}.complete`)).toBe(true);
77+
expect(fs.existsSync(path.join(JavaDir, 'bin'))).toBe(true);
78+
}, 100000);
79+
80+
it('Throws if invalid directory to jdk', async () => {
81+
let thrown = false;
82+
try {
83+
await installer.getJava('1000', 'x64', 'bad path');
84+
} catch {
85+
thrown = true;
86+
}
87+
expect(thrown).toBe(true);
88+
});
89+
7290
it('Uses version of Java installed in cache', async () => {
7391
const JavaDir: string = path.join(toolDir, 'Java', '250.0.0', 'x64');
7492
await io.mkdirP(JavaDir);

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: 'Setup your runner with Java'
33
author: 'GitHub'
44
inputs:
55
version:
6-
description: 'A number that specifies the JDK version to make available on the path. Use a whole number version, such as 10'
6+
description: 'The JDK version to make available on the path. Use a whole version, such as 9.0.4'
77
required: true
88
architecture:
99
description: 'The architecture (x86, x64) of the JDK.'

lib/installer.js

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const exec = __importStar(require("@actions/exec"));
2222
const tc = __importStar(require("@actions/tool-cache"));
2323
const fs = __importStar(require("fs"));
2424
const path = __importStar(require("path"));
25+
const httpm = __importStar(require("typed-rest-client/HttpClient"));
2526
const IS_WINDOWS = process.platform === 'win32';
2627
if (!tempDirectory) {
2728
let baseLocation;
@@ -46,15 +47,20 @@ function getJava(version, arch, jdkFile) {
4647
core.debug(`Tool found in cache ${toolPath}`);
4748
}
4849
else {
50+
let compressedFileExtension = '';
4951
if (!jdkFile) {
50-
throw new Error(`Failed to find Java ${version} in the cache. Please specify a valid jdk file to install from instead.`);
52+
core.debug('Downloading Jdk from Azul');
53+
jdkFile = yield downloadJava(version);
54+
compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
5155
}
52-
core.debug('Retrieving Jdk from local path');
53-
const compressedFileExtension = getFileEnding(jdkFile);
56+
else {
57+
core.debug('Retrieving Jdk from local path');
58+
}
59+
compressedFileExtension = compressedFileExtension || getFileEnding(jdkFile);
5460
let tempDir = path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000));
5561
const jdkDir = yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir);
5662
core.debug(`jdk extracted to ${jdkDir}`);
57-
toolPath = yield tc.cacheDir(jdkDir, 'Java', `${version}.0.0`, arch);
63+
toolPath = yield tc.cacheDir(jdkDir, 'Java', normalizeVersion(version), arch);
5864
}
5965
let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch;
6066
core.exportVariable('JAVA_HOME', toolPath);
@@ -63,6 +69,13 @@ function getJava(version, arch, jdkFile) {
6369
});
6470
}
6571
exports.getJava = getJava;
72+
function normalizeVersion(version) {
73+
const versionArray = version.split('.');
74+
const major = versionArray[0];
75+
const minor = versionArray.length > 1 ? versionArray[1] : '0';
76+
const patch = versionArray.length > 2 ? versionArray[2] : '0';
77+
return `${major}.${minor}.${patch}`;
78+
}
6679
function getFileEnding(file) {
6780
let fileEnding = '';
6881
if (file.endsWith('.tar')) {
@@ -126,7 +139,7 @@ function unpackJars(fsPath, javaBinPath) {
126139
}
127140
});
128141
}
129-
function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) {
142+
function unzipJavaDownload(repoRoot, fileEnding, destinationFolder, extension) {
130143
return __awaiter(this, void 0, void 0, function* () {
131144
// Create the destination folder if it doesn't exist
132145
yield io.mkdirP(destinationFolder);
@@ -143,3 +156,32 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) {
143156
}
144157
});
145158
}
159+
function downloadJava(version) {
160+
return __awaiter(this, void 0, void 0, function* () {
161+
let filterString = '';
162+
if (IS_WINDOWS) {
163+
filterString = `jdk${version}-win_x64.zip`;
164+
}
165+
else {
166+
if (process.platform === 'darwin') {
167+
filterString = `jdk${version}-macosx_x64.tar.gz`;
168+
}
169+
else {
170+
filterString = `jdk${version}-linux_x64.tar.gz`;
171+
}
172+
}
173+
let http = new httpm.HttpClient('setup-java');
174+
let contents = yield (yield http.get('https://static.azul.com/zulu/bin/')).readBody();
175+
let refs = contents.match(/<a href.*\">/gi) || [];
176+
refs = refs.filter(val => {
177+
if (val.indexOf(filterString) > -1) {
178+
return true;
179+
}
180+
});
181+
if (refs.length == 0) {
182+
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`);
183+
}
184+
const fileName = refs[0].slice('<a href="'.length, refs[0].length - '">'.length);
185+
return yield tc.downloadTool(`https://static.azul.com/zulu/bin/${fileName}`);
186+
});
187+
}

node_modules/@actions/core/lib/core.d.ts

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/core/lib/core.js

Lines changed: 30 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/core/lib/core.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/core/package.json

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/exec/package.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/@actions/exit/LICENSE.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

node_modules/@actions/exit/README.md

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)