Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/matcher #3

Closed
wants to merge 18 commits into from
14 changes: 14 additions & 0 deletions .github/java.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"problemMatcher": [
{
"owner": "java",
"pattern": [
{
"regexp": "^Exception in thread \"(.*)\" (.*): (.*)$",
"code": 2,
"message": 3
}
]
}
]
}
98 changes: 98 additions & 0 deletions __tests__/installer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import io = require('@actions/io');
import fs = require('fs');
import path = require('path');
import child_process = require('child_process');

const toolDir = path.join(__dirname, 'runner', 'tools');
const tempDir = path.join(__dirname, 'runner', 'temp');
const javaDir = path.join(__dirname, 'runner', 'java');

process.env['RUNNER_TOOLSDIRECTORY'] = toolDir;
process.env['RUNNER_TEMPDIRECTORY'] = tempDir;
import * as installer from '../src/installer';

let javaFilePath = '';
let javaUrl = '';
if (process.platform === 'win32') {
javaFilePath = path.join(javaDir, 'java_win.zip');
javaUrl =
'https://download.java.net/java/GA/jdk12/33/GPL/openjdk-12_windows-x64_bin.zip';
} else if (process.platform === 'darwin') {
javaFilePath = path.join(javaDir, 'java_mac.tar.gz');
javaUrl =
'https://download.java.net/java/GA/jdk12/33/GPL/openjdk-12_osx-x64_bin.tar.gz';
} else {
javaFilePath = path.join(javaDir, 'java_linux.tar.gz');
javaUrl =
'https://download.java.net/java/GA/jdk12/33/GPL/openjdk-12_linux-x64_bin.tar.gz';
}

describe('installer tests', () => {
beforeAll(async () => {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
if (!fs.existsSync(`${javaFilePath}.complete`)) {
// Download java
await io.mkdirP(javaDir);

console.log('Downloading java');
child_process.execSync(`curl "${javaUrl}" > "${javaFilePath}"`);
// Write complete file so we know it was successful
fs.writeFileSync(`${javaFilePath}.complete`, 'content');
}
}, 300000);

afterAll(async () => {
try {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
} catch {
console.log('Failed to remove test directories');
}
}, 100000);

it('Acquires version of Java if no matching version is installed', async () => {
await installer.getJava('12', 'x64', javaFilePath);
const JavaDir = path.join(toolDir, 'Java', '12.0.0', '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);
fs.writeFileSync(`${JavaDir}.complete`, 'hello');
// This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getJava(
'250',
'x64',
'path shouldnt matter, found in cache'
);
return;
});

it('Doesnt use version of Java that was only partially installed in cache', async () => {
const JavaDir: string = path.join(toolDir, 'Java', '251.0.0', 'x64');
await io.mkdirP(JavaDir);
let thrown = false;
try {
// This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getJava('251', 'x64', 'bad path');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
return;
});
});
3 changes: 0 additions & 3 deletions __tests__/setup-java.test.ts

This file was deleted.

9 changes: 8 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ description: 'Setup your runner with Java'
author: 'GitHub'
inputs:
version:
description: 'Version of JDK to use'
description: 'A number that specifies the JDK version to make available on the path. Use a whole number version, such as 10'
required: true
architecture:
description: 'The architecture (x86, x64) of the JDK.'
required: true
jdkFile:
description: 'Path to where the compressed JDK is located. The path could be in your source repository or a local path on the agent.'
required: true
runs:
using: 'node12'
main: 'lib/setup-java.js'
142 changes: 142 additions & 0 deletions lib/installer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
const core = __importStar(require("@actions/core"));
const io = __importStar(require("@actions/io"));
const exec = __importStar(require("@actions/exec"));
const tc = __importStar(require("@actions/tool-cache"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const IS_WINDOWS = process.platform === 'win32';
if (!tempDirectory) {
let baseLocation;
if (IS_WINDOWS) {
// On windows use the USERPROFILE env variable
baseLocation = process.env['USERPROFILE'] || 'C:\\';
}
else {
if (process.platform === 'darwin') {
baseLocation = '/Users';
}
else {
baseLocation = '/home';
}
}
tempDirectory = path.join(baseLocation, 'actions', 'temp');
}
function getJava(version, arch, jdkFile) {
return __awaiter(this, void 0, void 0, function* () {
let toolPath = tc.find('Java', version);
if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`);
}
else {
core.debug('Retrieving Jdk from local path');
const 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);
}
let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch;
core.exportVariable('JAVA_HOME', toolPath);
core.exportVariable(extendedJavaHome, toolPath);
core.addPath(path.join(toolPath, 'bin'));
});
}
exports.getJava = getJava;
function getFileEnding(file) {
let fileEnding = '';
if (file.endsWith('.tar')) {
fileEnding = '.tar';
}
else if (file.endsWith('.tar.gz')) {
fileEnding = '.tar.gz';
}
else if (file.endsWith('.zip')) {
fileEnding = '.zip';
}
else if (file.endsWith('.7z')) {
fileEnding = '.7z';
}
else {
throw new Error(`${file} has an unsupported file extension`);
}
return fileEnding;
}
function extractFiles(file, fileEnding, destinationFolder) {
return __awaiter(this, void 0, void 0, function* () {
const stats = fs.statSync(file);
if (!stats) {
throw new Error(`Failed to extract ${file} - it doesn't exist`);
}
else if (stats.isDirectory()) {
throw new Error(`Failed to extract ${file} - it is a directory`);
}
if ('.tar' === fileEnding || '.tar.gz' === fileEnding) {
yield tc.extractTar(file, destinationFolder);
}
else if ('.zip' === fileEnding) {
yield tc.extractZip(file, destinationFolder);
}
else {
// fall through and use sevenZip
yield tc.extract7z(file, destinationFolder);
}
});
}
// This method recursively finds all .pack files under fsPath and unpacks them with the unpack200 tool
function unpackJars(fsPath, javaBinPath) {
return __awaiter(this, void 0, void 0, function* () {
if (fs.existsSync(fsPath)) {
if (fs.lstatSync(fsPath).isDirectory()) {
for (const file in fs.readdirSync(fsPath)) {
const curPath = path.join(fsPath, file);
yield unpackJars(curPath, javaBinPath);
}
}
else if (path.extname(fsPath).toLowerCase() === '.pack') {
// Unpack the pack file synchonously
const p = path.parse(fsPath);
const toolName = IS_WINDOWS ? 'unpack200.exe' : 'unpack200';
const args = IS_WINDOWS ? '-r -v -l ""' : '';
const name = path.join(p.dir, p.name);
yield exec.exec(`"${path.join(javaBinPath, toolName)}"`, [
`${args} "${name}.pack" "${name}.jar"`
]);
}
}
});
}
function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) {
return __awaiter(this, void 0, void 0, function* () {
// Create the destination folder if it doesn't exist
yield io.mkdirP(destinationFolder);
const jdkFile = path.normalize(repoRoot);
const stats = fs.statSync(jdkFile);
if (stats.isFile()) {
yield extractFiles(jdkFile, fileEnding, destinationFolder);
const jdkDirectory = path.join(destinationFolder, fs.readdirSync(destinationFolder)[0]);
yield unpackJars(jdkDirectory, path.join(jdkDirectory, 'bin'));
return jdkDirectory;
}
else {
throw new Error(`Jdk argument ${jdkFile} is not a file`);
}
});
}
15 changes: 13 additions & 2 deletions lib/setup-java.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const installer = __importStar(require("./installer"));
const path = __importStar(require("path"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
const myInput = core.getInput('myInput');
core.debug(`Hello ${myInput}`);
try {
const version = core.getInput('version', { required: true });
const arch = core.getInput('architecture', { required: true });
const jdkFile = core.getInput('jdkFile', { required: true });
yield installer.getJava(version, arch, jdkFile);
const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();
Loading