gradle-build-action/lib/provision.js
Paul Merlin 6170f06e8d build
2019-09-23 12:20:06 +02:00

167 lines
6.8 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(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 });
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const httpm = __importStar(require("typed-rest-client/HttpClient"));
const unzip = __importStar(require("unzipper"));
const core = __importStar(require("@actions/core"));
const io = __importStar(require("@actions/io"));
const toolCache = __importStar(require("@actions/tool-cache"));
const gradlew = __importStar(require("./gradlew"));
const httpc = new httpm.HttpClient("eskatos/gradle-command-action");
const gradleVersionsBaseUrl = "https://services.gradle.org/versions";
function gradleVersion(gradleVersion) {
return __awaiter(this, void 0, void 0, function* () {
switch (gradleVersion) {
case "current":
return gradleCurrent();
case "rc":
return gradleReleaseCandidate();
case "nightly":
return gradleNightly();
case "release-nightly":
return gradleReleaseNightly();
default:
return gradle(gradleVersion);
}
});
}
exports.gradleVersion = gradleVersion;
function gradleCurrent() {
return __awaiter(this, void 0, void 0, function* () {
const json = yield gradleVersionDeclaration(`${gradleVersionsBaseUrl}/current`);
return provisionGradle(json.version, json.downloadUrl);
});
}
function gradleReleaseCandidate() {
return __awaiter(this, void 0, void 0, function* () {
const json = yield gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-candidate`);
if (json) {
return provisionGradle(json.version, json.downloadUrl);
}
return gradleCurrent();
});
}
function gradleNightly() {
return __awaiter(this, void 0, void 0, function* () {
const json = yield gradleVersionDeclaration(`${gradleVersionsBaseUrl}/nightly`);
return provisionGradle(json.version, json.downloadUrl);
});
}
function gradleReleaseNightly() {
return __awaiter(this, void 0, void 0, function* () {
const json = yield gradleVersionDeclaration(`${gradleVersionsBaseUrl}/release-nightly`);
return provisionGradle(json.version, json.downloadUrl);
});
}
function gradle(version) {
return __awaiter(this, void 0, void 0, function* () {
const declaration = yield findGradleVersionDeclaration(version);
if (!declaration) {
throw new Error(`Gradle version ${version} does not exists`);
}
return provisionGradle(declaration.version, declaration.downloadUrl);
});
}
function gradleVersionDeclaration(url) {
return __awaiter(this, void 0, void 0, function* () {
const json = yield httpGetJson(url);
return (json.version && json.version.length > 0) ? json : undefined;
});
}
function findGradleVersionDeclaration(version) {
return __awaiter(this, void 0, void 0, function* () {
const json = yield httpGetJson(`${gradleVersionsBaseUrl}/all`);
const found = json.find((entry) => {
return entry.version === version;
});
return found ? found : undefined;
});
}
function provisionGradle(version, url) {
return __awaiter(this, void 0, void 0, function* () {
const cachedInstall = toolCache.find("gradle", version);
if (cachedInstall.length > 0) {
const cachedExecutable = executableFrom(cachedInstall);
core.info(`Provisioned Gradle executable ${cachedExecutable}`);
return cachedExecutable;
}
const home = process.env["HOME"] || "";
const tmpdir = path.join(home, "gradle-provision-tmpdir");
const downloadsDir = path.join(tmpdir, "downloads");
const installsDir = path.join(tmpdir, "installs");
yield io.mkdirP(downloadsDir);
yield io.mkdirP(installsDir);
core.info(`Downloading ${url}`);
const downloadPath = path.join(downloadsDir, `gradle-${version}-bin.zip`);
yield httpDownload(url, downloadPath);
core.info(`Downloaded at ${downloadPath}, size ${fs.statSync(downloadPath).size}`);
yield extractZip(downloadPath, installsDir);
const installDir = path.join(installsDir, `gradle-${version}`);
core.info(`Extracted in ${installDir}`);
const executable = executableFrom(installDir);
fs.chmodSync(executable, "755");
core.info(`Provisioned Gradle executable ${executable}`);
toolCache.cacheDir(installDir, "gradle", version);
return executable;
});
}
function executableFrom(installDir) {
return path.join(installDir, "bin", `${gradlew.installScriptFilename()}`);
}
function httpGetJson(url) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield httpc.get(url);
const body = yield response.readBody();
return JSON.parse(body);
});
}
function httpDownload(url, path) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(function (resolve, reject) {
const writeStream = fs.createWriteStream(path);
httpc.get(url).then(response => {
response.message.pipe(writeStream)
.on("close", () => {
resolve();
})
.on("error", err => {
reject(err);
});
}).catch(reason => {
reject(reason);
});
});
});
}
function extractZip(zip, destination) {
return __awaiter(this, void 0, void 0, function* () {
return new Promise(function (resolve, reject) {
fs.createReadStream(zip)
.pipe(unzip.Extract({ "path": destination }))
.on("close", () => {
resolve();
})
.on("error", err => {
reject(err);
});
});
});
}