Unverified Commit 0cf3ae56 authored by Danny McCormick's avatar Danny McCormick Committed by GitHub
Browse files

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
parent 012e0762
Loading
Loading
Loading
Loading
+19 −1
Changes for __tests__/installer.test.ts: 19 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -51,7 +51,7 @@ describe('installer tests', () => {
    }
  }, 100000);

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

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

  it('Downloads java if no file given', async () => {
    await installer.getJava('8.0.102', 'x64', '');
    const JavaDir = path.join(toolDir, 'Java', '8.0.102', '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 () => {
    let thrown = false;
    try {
      await installer.getJava('1000', 'x64', 'bad path');
    } catch {
      thrown = true;
    }
    expect(thrown).toBe(true);
  });

  it('Uses version of Java installed in cache', async () => {
    const JavaDir: string = path.join(toolDir, 'Java', '250.0.0', 'x64');
    await io.mkdirP(JavaDir);
+1 −1
Changes for action.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ description: 'Setup your runner with Java'
author: 'GitHub'
inputs: 
  version:
    description: 'A number that specifies the JDK version to make available on the path. Use a whole number version, such as 10'
    description: 'The JDK version to make available on the path. Use a whole version, such as 9.0.4'
    required: true
  architecture:
    description: 'The architecture (x86, x64) of the JDK.'
+46 −4
Changes for lib/installer.js: 46 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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 httpm = __importStar(require("typed-rest-client/HttpClient"));
const IS_WINDOWS = process.platform === 'win32';
if (!tempDirectory) {
    let baseLocation;
@@ -46,15 +47,20 @@ function getJava(version, arch, jdkFile) {
            core.debug(`Tool found in cache ${toolPath}`);
        }
        else {
            let compressedFileExtension = '';
            if (!jdkFile) {
                throw new Error(`Failed to find Java ${version} in the cache. Please specify a valid jdk file to install from instead.`);
                core.debug('Downloading Jdk from Azul');
                jdkFile = yield downloadJava(version);
                compressedFileExtension = IS_WINDOWS ? '.zip' : '.tar.gz';
            }
            else {
                core.debug('Retrieving Jdk from local path');
            const compressedFileExtension = getFileEnding(jdkFile);
            }
            compressedFileExtension = compressedFileExtension || getFileEnding(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', `${version}.0.0`, arch);
            toolPath = yield tc.cacheDir(jdkDir, 'Java', normalizeVersion(version), arch);
        }
        let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch;
        core.exportVariable('JAVA_HOME', toolPath);
@@ -63,6 +69,13 @@ function getJava(version, arch, jdkFile) {
    });
}
exports.getJava = getJava;
function normalizeVersion(version) {
    const versionArray = version.split('.');
    const major = versionArray[0];
    const minor = versionArray.length > 1 ? versionArray[1] : '0';
    const patch = versionArray.length > 2 ? versionArray[2] : '0';
    return `${major}.${minor}.${patch}`;
}
function getFileEnding(file) {
    let fileEnding = '';
    if (file.endsWith('.tar')) {
@@ -126,7 +139,7 @@ function unpackJars(fsPath, javaBinPath) {
        }
    });
}
function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) {
function unzipJavaDownload(repoRoot, fileEnding, destinationFolder, extension) {
    return __awaiter(this, void 0, void 0, function* () {
        // Create the destination folder if it doesn't exist
        yield io.mkdirP(destinationFolder);
@@ -143,3 +156,32 @@ function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) {
        }
    });
}
function downloadJava(version) {
    return __awaiter(this, void 0, void 0, function* () {
        let filterString = '';
        if (IS_WINDOWS) {
            filterString = `jdk${version}-win_x64.zip`;
        }
        else {
            if (process.platform === 'darwin') {
                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();
        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`);
        }
        const fileName = refs[0].slice('<a href="'.length, refs[0].length - '">'.length);
        return yield tc.downloadTool(`https://static.azul.com/zulu/bin/${fileName}`);
    });
}
+24
Changes for node_modules/@actions/core/lib/core.d.ts: 24 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,23 @@ export interface InputOptions {
    /** Optional. Whether the input is required. If required and not present, will throw. Defaults to false */
    required?: boolean;
}
/**
 * The code to exit an action
 */
export declare enum ExitCode {
    /**
     * A code indicating that the action was successful
     */
    Success = 0,
    /**
     * A code indicating that the action was a failure
     */
    Failure = 1,
    /**
     * A code indicating that the action is complete, but neither succeeded nor failed
     */
    Neutral = 78
}
/**
 * sets env variable for this action and future actions in the job
 * @param name the name of the variable to set
@@ -30,6 +47,13 @@ export declare function addPath(inputPath: string): void;
 * @returns   string
 */
export declare function getInput(name: string, options?: InputOptions): string;
/**
 * Sets the value of an output.
 *
 * @param     name     name of the output to set
 * @param     value    value to store
 */
export declare function setOutput(name: string, value: string): void;
/**
 * Sets the action status to neutral
 */
+30 −3
Changes for node_modules/@actions/core/lib/core.js: 30 added lines, 3 removed lines.
Original line number Diff line number Diff line
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const exit_1 = require("@actions/exit");
const command_1 = require("./command");
const path = require("path");
/**
 * The code to exit an action
 */
var ExitCode;
(function (ExitCode) {
    /**
     * A code indicating that the action was successful
     */
    ExitCode[ExitCode["Success"] = 0] = "Success";
    /**
     * A code indicating that the action was a failure
     */
    ExitCode[ExitCode["Failure"] = 1] = "Failure";
    /**
     * A code indicating that the action is complete, but neither succeeded nor failed
     */
    ExitCode[ExitCode["Neutral"] = 78] = "Neutral";
})(ExitCode = exports.ExitCode || (exports.ExitCode = {}));
//-----------------------------------------------------------------------
// Variables
//-----------------------------------------------------------------------
@@ -50,6 +67,16 @@ function getInput(name, options) {
    return val.trim();
}
exports.getInput = getInput;
/**
 * Sets the value of an output.
 *
 * @param     name     name of the output to set
 * @param     value    value to store
 */
function setOutput(name, value) {
    command_1.issueCommand('set-output', { name }, value);
}
exports.setOutput = setOutput;
//-----------------------------------------------------------------------
// Results
//-----------------------------------------------------------------------
@@ -57,7 +84,7 @@ exports.getInput = getInput;
 * Sets the action status to neutral
 */
function setNeutral() {
    process.exitCode = exit_1.ExitCode.Neutral;
    process.exitCode = ExitCode.Neutral;
}
exports.setNeutral = setNeutral;
/**
@@ -66,7 +93,7 @@ exports.setNeutral = setNeutral;
 * @param message add error issue message
 */
function setFailed(message) {
    process.exitCode = exit_1.ExitCode.Failure;
    process.exitCode = ExitCode.Failure;
    error(message);
}
exports.setFailed = setFailed;
Loading