|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 4 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 5 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 6 | + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } |
| 7 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 8 | + }); |
| 9 | +}; |
| 10 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 11 | + if (mod && mod.__esModule) return mod; |
| 12 | + var result = {}; |
| 13 | + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; |
| 14 | + result["default"] = mod; |
| 15 | + return result; |
| 16 | +}; |
| 17 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 18 | +let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; |
| 19 | +const core = __importStar(require("@actions/core")); |
| 20 | +const io = __importStar(require("@actions/io")); |
| 21 | +const exec = __importStar(require("@actions/exec")); |
| 22 | +const tc = __importStar(require("@actions/tool-cache")); |
| 23 | +const fs = __importStar(require("fs")); |
| 24 | +const path = __importStar(require("path")); |
| 25 | +const IS_WINDOWS = process.platform === 'win32'; |
| 26 | +if (!tempDirectory) { |
| 27 | + let baseLocation; |
| 28 | + if (IS_WINDOWS) { |
| 29 | + // On windows use the USERPROFILE env variable |
| 30 | + baseLocation = process.env['USERPROFILE'] || 'C:\\'; |
| 31 | + } |
| 32 | + else { |
| 33 | + if (process.platform === 'darwin') { |
| 34 | + baseLocation = '/Users'; |
| 35 | + } |
| 36 | + else { |
| 37 | + baseLocation = '/home'; |
| 38 | + } |
| 39 | + } |
| 40 | + tempDirectory = path.join(baseLocation, 'actions', 'temp'); |
| 41 | +} |
| 42 | +function getJava(version, arch, jdkFile) { |
| 43 | + return __awaiter(this, void 0, void 0, function* () { |
| 44 | + let toolPath = tc.find('Java', version); |
| 45 | + if (toolPath) { |
| 46 | + core.debug(`Tool found in cache ${toolPath}`); |
| 47 | + } |
| 48 | + else { |
| 49 | + core.debug('Retrieving Jdk from local path'); |
| 50 | + const compressedFileExtension = getFileEnding(jdkFile); |
| 51 | + let tempDir = path.join(tempDirectory, 'temp_' + Math.floor(Math.random() * 2000000000)); |
| 52 | + const jdkDir = yield unzipJavaDownload(jdkFile, compressedFileExtension, tempDir); |
| 53 | + core.debug(`jdk extracted to ${jdkDir}`); |
| 54 | + toolPath = yield tc.cacheDir(jdkDir, 'Java', `${version}.0.0`, arch); |
| 55 | + } |
| 56 | + let extendedJavaHome = 'JAVA_HOME_' + version + '_' + arch; |
| 57 | + core.exportVariable('JAVA_HOME', toolPath); |
| 58 | + core.exportVariable(extendedJavaHome, toolPath); |
| 59 | + core.addPath(path.join(toolPath, 'bin')); |
| 60 | + }); |
| 61 | +} |
| 62 | +exports.getJava = getJava; |
| 63 | +function getFileEnding(file) { |
| 64 | + let fileEnding = ''; |
| 65 | + if (file.endsWith('.tar')) { |
| 66 | + fileEnding = '.tar'; |
| 67 | + } |
| 68 | + else if (file.endsWith('.tar.gz')) { |
| 69 | + fileEnding = '.tar.gz'; |
| 70 | + } |
| 71 | + else if (file.endsWith('.zip')) { |
| 72 | + fileEnding = '.zip'; |
| 73 | + } |
| 74 | + else if (file.endsWith('.7z')) { |
| 75 | + fileEnding = '.7z'; |
| 76 | + } |
| 77 | + else { |
| 78 | + throw new Error(`${file} has an unsupported file extension`); |
| 79 | + } |
| 80 | + return fileEnding; |
| 81 | +} |
| 82 | +function extractFiles(file, fileEnding, destinationFolder) { |
| 83 | + return __awaiter(this, void 0, void 0, function* () { |
| 84 | + const stats = fs.statSync(file); |
| 85 | + if (!stats) { |
| 86 | + throw new Error(`Failed to extract ${file} - it doesn't exist`); |
| 87 | + } |
| 88 | + else if (stats.isDirectory()) { |
| 89 | + throw new Error(`Failed to extract ${file} - it is a directory`); |
| 90 | + } |
| 91 | + if ('.tar' === fileEnding || '.tar.gz' === fileEnding) { |
| 92 | + yield tc.extractTar(file, destinationFolder); |
| 93 | + } |
| 94 | + else if ('.zip' === fileEnding) { |
| 95 | + yield tc.extractZip(file, destinationFolder); |
| 96 | + } |
| 97 | + else { |
| 98 | + // fall through and use sevenZip |
| 99 | + yield tc.extract7z(file, destinationFolder); |
| 100 | + } |
| 101 | + }); |
| 102 | +} |
| 103 | +// This method recursively finds all .pack files under fsPath and unpacks them with the unpack200 tool |
| 104 | +function unpackJars(fsPath, javaBinPath) { |
| 105 | + return __awaiter(this, void 0, void 0, function* () { |
| 106 | + if (fs.existsSync(fsPath)) { |
| 107 | + if (fs.lstatSync(fsPath).isDirectory()) { |
| 108 | + for (const file in fs.readdirSync(fsPath)) { |
| 109 | + const curPath = path.join(fsPath, file); |
| 110 | + yield unpackJars(curPath, javaBinPath); |
| 111 | + } |
| 112 | + } |
| 113 | + else if (path.extname(fsPath).toLowerCase() === '.pack') { |
| 114 | + // Unpack the pack file synchonously |
| 115 | + const p = path.parse(fsPath); |
| 116 | + const toolName = IS_WINDOWS ? 'unpack200.exe' : 'unpack200'; |
| 117 | + const args = IS_WINDOWS ? '-r -v -l ""' : ''; |
| 118 | + const name = path.join(p.dir, p.name); |
| 119 | + yield exec.exec(`"${path.join(javaBinPath, toolName)}"`, [ |
| 120 | + `${args} "${name}.pack" "${name}.jar"` |
| 121 | + ]); |
| 122 | + } |
| 123 | + } |
| 124 | + }); |
| 125 | +} |
| 126 | +function unzipJavaDownload(repoRoot, fileEnding, destinationFolder) { |
| 127 | + return __awaiter(this, void 0, void 0, function* () { |
| 128 | + // Create the destination folder if it doesn't exist |
| 129 | + yield io.mkdirP(destinationFolder); |
| 130 | + const jdkFile = path.normalize(repoRoot); |
| 131 | + const stats = fs.statSync(jdkFile); |
| 132 | + if (stats.isFile()) { |
| 133 | + yield extractFiles(jdkFile, fileEnding, destinationFolder); |
| 134 | + const jdkDirectory = path.join(destinationFolder, fs.readdirSync(destinationFolder)[0]); |
| 135 | + yield unpackJars(jdkDirectory, path.join(jdkDirectory, 'bin')); |
| 136 | + return jdkDirectory; |
| 137 | + } |
| 138 | + else { |
| 139 | + throw new Error(`Jdk argument ${jdkFile} is not a file`); |
| 140 | + } |
| 141 | + }); |
| 142 | +} |
0 commit comments