Initial attempt

This commit is contained in:
Danny McCormick 2019-07-10 11:55:10 -04:00
parent 06d666cd11
commit 8ec3a6392c
5 changed files with 445 additions and 5 deletions

View File

@ -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'

211
lib/installer.js Normal file
View File

@ -0,0 +1,211 @@
"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"));
if (!tempDirectory) {
let baseLocation;
if (process.platform === 'win32') {
// 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');
}
const IS_WINDOWS = process.platform === 'win32';
function getJava(versionSpec, arch, jdkFile) {
return __awaiter(this, void 0, void 0, function* () {
let toolPath = tc.find('Java', versionSpec);
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));
yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir);
toolPath = yield tc.cacheDir(tempDir, 'Java', versionSpec, arch);
}
let extendedJavaHome = 'JAVA_HOME_' + versionSpec + '_' + 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 getSevenZipLocation() {
return __awaiter(this, void 0, void 0, function* () {
if (IS_WINDOWS) {
return path.join(__dirname, '7zip/7z.exe');
}
else {
return yield io.which('7z', true);
}
});
}
function isTar(file) {
const name = file.toLowerCase();
// standard gnu-tar extension formats with recognized auto compression formats
// https://www.gnu.org/software/tar/manual/html_section/tar_69.html
return (name.endsWith('.tar') || // no compression
name.endsWith('.tar.gz') || // gzip
name.endsWith('.tgz') || // gzip
name.endsWith('.taz') || // gzip
name.endsWith('.tar.z') || // compress
name.endsWith('.tar.bz2') || // bzip2
name.endsWith('.tz2') || // bzip2
name.endsWith('.tbz2') || // bzip2
name.endsWith('.tbz') || // bzip2
name.endsWith('.tar.lz') || // lzip
name.endsWith('.tar.lzma') || // lzma
name.endsWith('.tlz') || // lzma
name.endsWith('.tar.lzo') || // lzop
name.endsWith('.tar.xz') || // xz
name.endsWith('.txz')); // xz
}
function sevenZipExtract(file, destinationFolder) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`Using 7zip to extract ${file}`);
yield tc.extract7z(file, destinationFolder, yield getSevenZipLocation());
});
}
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 (IS_WINDOWS) {
if ('.tar' === fileEnding) {
// a simple tar
yield sevenZipExtract(file, destinationFolder);
}
else if (isTar(file)) {
// a compressed tar, e.g. 'fullFilePath/test.tar.gz'
// e.g. 'fullFilePath/test.tar.gz' --> 'test.tar.gz'
const shortFileName = path.basename(file);
// e.g. 'destinationFolder/_test.tar.gz_'
const tempFolder = path.normalize(destinationFolder + path.sep + '_' + shortFileName + '_');
// 0 create temp folder
yield io.mkdirP(tempFolder);
// 1 extract compressed tar
yield sevenZipExtract(file, tempFolder);
const tempTar = tempFolder + path.sep + fs.readdirSync(tempFolder)[0]; // should be only one
// 2 expand extracted tar
yield sevenZipExtract(tempTar, destinationFolder);
// 3 cleanup temp folder
yield io.rmRF(tempFolder);
}
else {
// use sevenZip
yield sevenZipExtract(file, destinationFolder);
}
}
else {
// not windows
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 sevenZipExtract(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 = process.platform.match(/^win/i)
? 'unpack200.exe'
: 'unpack200';
const args = process.platform.match(/^win/i) ? '-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* () {
let initialDirectoriesList;
let finalDirectoriesList;
let jdkDirectory;
// 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);
jdkDirectory = fs.readdirSync(tempDirectory)[0];
yield unpackJars(jdkDirectory, path.join(jdkDirectory, 'bin'));
return jdkDirectory;
}
else {
throw new Error(`Jdk argument ${jdkFile} is not a file`);
}
});
}

View File

@ -16,10 +16,18 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const installer = __importStar(require("./installer"));
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);
}
catch (error) {
core.setFailed(error.message);
}
});
}
run();

206
src/installer.ts Normal file
View File

@ -0,0 +1,206 @@
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
import * as core from '@actions/core';
import * as io from '@actions/io';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import * as fs from 'fs';
import * as path from 'path';
if (!tempDirectory) {
let baseLocation;
if (process.platform === 'win32') {
// 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');
}
const IS_WINDOWS = process.platform === 'win32';
export async function getJava(
versionSpec: string,
arch: string,
jdkFile: string
): Promise<void> {
let toolPath = tc.find('Java', versionSpec);
if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`);
} else {
core.debug('Retrieving Jdk from local path');
const compressedFileExtension = getFileEnding(jdkFile);
let tempDir: string = path.join(
tempDirectory,
'temp_' + Math.floor(Math.random() * 2000000000)
);
await unzipJavaDownload(jdkFile, compressedFileExtension, tempDir);
toolPath = await tc.cacheDir(tempDir, 'Java', versionSpec, arch);
}
let extendedJavaHome = 'JAVA_HOME_' + versionSpec + '_' + arch;
core.exportVariable('JAVA_HOME', toolPath);
core.exportVariable(extendedJavaHome, toolPath);
core.addPath(path.join(toolPath, 'bin'));
}
function getFileEnding(file: string): string {
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;
}
async function getSevenZipLocation(): Promise<string> {
if (IS_WINDOWS) {
return path.join(__dirname, '7zip/7z.exe');
} else {
return await io.which('7z', true);
}
}
function isTar(file: string): boolean {
const name = file.toLowerCase();
// standard gnu-tar extension formats with recognized auto compression formats
// https://www.gnu.org/software/tar/manual/html_section/tar_69.html
return (
name.endsWith('.tar') || // no compression
name.endsWith('.tar.gz') || // gzip
name.endsWith('.tgz') || // gzip
name.endsWith('.taz') || // gzip
name.endsWith('.tar.z') || // compress
name.endsWith('.tar.bz2') || // bzip2
name.endsWith('.tz2') || // bzip2
name.endsWith('.tbz2') || // bzip2
name.endsWith('.tbz') || // bzip2
name.endsWith('.tar.lz') || // lzip
name.endsWith('.tar.lzma') || // lzma
name.endsWith('.tlz') || // lzma
name.endsWith('.tar.lzo') || // lzop
name.endsWith('.tar.xz') || // xz
name.endsWith('.txz')
); // xz
}
async function sevenZipExtract(file: string, destinationFolder: string) {
console.log(`Using 7zip to extract ${file}`);
await tc.extract7z(file, destinationFolder, await getSevenZipLocation());
}
async function extractFiles(
file: string,
fileEnding: string,
destinationFolder: string
): Promise<void> {
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 (IS_WINDOWS) {
if ('.tar' === fileEnding) {
// a simple tar
await sevenZipExtract(file, destinationFolder);
} else if (isTar(file)) {
// a compressed tar, e.g. 'fullFilePath/test.tar.gz'
// e.g. 'fullFilePath/test.tar.gz' --> 'test.tar.gz'
const shortFileName = path.basename(file);
// e.g. 'destinationFolder/_test.tar.gz_'
const tempFolder = path.normalize(
destinationFolder + path.sep + '_' + shortFileName + '_'
);
// 0 create temp folder
await io.mkdirP(tempFolder);
// 1 extract compressed tar
await sevenZipExtract(file, tempFolder);
const tempTar = tempFolder + path.sep + fs.readdirSync(tempFolder)[0]; // should be only one
// 2 expand extracted tar
await sevenZipExtract(tempTar, destinationFolder);
// 3 cleanup temp folder
await io.rmRF(tempFolder);
} else {
// use sevenZip
await sevenZipExtract(file, destinationFolder);
}
} else {
// not windows
if ('.tar' === fileEnding || '.tar.gz' === fileEnding) {
await tc.extractTar(file, destinationFolder);
} else if ('.zip' === fileEnding) {
await tc.extractZip(file, destinationFolder);
} else {
// fall through and use sevenZip
await sevenZipExtract(file, destinationFolder);
}
}
}
// This method recursively finds all .pack files under fsPath and unpacks them with the unpack200 tool
async function unpackJars(fsPath: string, javaBinPath: string) {
if (fs.existsSync(fsPath)) {
if (fs.lstatSync(fsPath).isDirectory()) {
for (const file in fs.readdirSync(fsPath)) {
const curPath = path.join(fsPath, file);
await unpackJars(curPath, javaBinPath);
}
} else if (path.extname(fsPath).toLowerCase() === '.pack') {
// Unpack the pack file synchonously
const p = path.parse(fsPath);
const toolName = process.platform.match(/^win/i)
? 'unpack200.exe'
: 'unpack200';
const args = process.platform.match(/^win/i) ? '-r -v -l ""' : '';
const name = path.join(p.dir, p.name);
await exec.exec(`"${path.join(javaBinPath, toolName)}"`, [
`${args} "${name}.pack" "${name}.jar"`
]);
}
}
}
async function unzipJavaDownload(
repoRoot: string,
fileEnding: string,
destinationFolder: string
): Promise<string> {
let initialDirectoriesList: string[];
let finalDirectoriesList: string[];
let jdkDirectory: string;
// Create the destination folder if it doesn't exist
await io.mkdirP(destinationFolder);
const jdkFile = path.normalize(repoRoot);
const stats = fs.statSync(jdkFile);
if (stats.isFile()) {
await extractFiles(jdkFile, fileEnding, destinationFolder);
jdkDirectory = fs.readdirSync(tempDirectory)[0];
await unpackJars(jdkDirectory, path.join(jdkDirectory, 'bin'));
return jdkDirectory;
} else {
throw new Error(`Jdk argument ${jdkFile} is not a file`);
}
}

View File

@ -1,8 +1,16 @@
import * as core from '@actions/core';
import * as installer from './installer';
async function run() {
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});
await installer.getJava(version, arch, jdkFile);
} catch (error) {
core.setFailed(error.message);
}
}
run();