|
| 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 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 11 | +const tasks = require("vsts-task-lib/task"); |
| 12 | +const tools = require("vsts-task-tool-lib/tool"); |
| 13 | +const RestClient_1 = require("typed-rest-client/RestClient"); |
| 14 | +const os = require("os"); |
| 15 | +const fs = require("fs"); |
| 16 | +const path = require("path"); |
| 17 | +//https://download.octopusdeploy.com/octopus-tools/4.35.0/OctopusTools.4.35.0.portable.zip |
| 18 | +//https://download.octopusdeploy.com/octopus-tools/4.35.0/OctopusTools.4.35.0.portable.tar.gz |
| 19 | +const toolName = "Octo"; |
| 20 | +var github = new RestClient_1.RestClient("OctoTFS/OctoInstaller", "https://api.github.com"); |
| 21 | +const octoLatestReleaseUrl = "https://api.github.com/repos/OctopusDeploy/OctopusClients/releases/latest"; |
| 22 | +const downloadUrl = (version) => `https://download.octopusdeploy.com/octopus-tools/${version}/OctopusTools.${version}.portable.tar.gz`; |
| 23 | +const resolveVersion = (version) => __awaiter(this, void 0, void 0, function* () { |
| 24 | + if (!version || version.toLowerCase() === "latest") { |
| 25 | + return github.get("repos/OctopusDeploy/OctopusClients/releases/latest").then(x => x.result.tag_name); |
| 26 | + } |
| 27 | + return Promise.resolve(version); |
| 28 | +}); |
| 29 | +function getLocalTool(version) { |
| 30 | + console.log("Checking local tool cache"); |
| 31 | + return tools.findLocalTool(toolName, version); |
| 32 | +} |
| 33 | +function run() { |
| 34 | + return __awaiter(this, void 0, void 0, function* () { |
| 35 | + let version = yield resolveVersion(tasks.getInput("version")); |
| 36 | + let toolPath = yield download(version); |
| 37 | + if (!process.env['PATH'].startsWith(path.dirname(toolPath))) { |
| 38 | + tools.prependPath(path.dirname(toolPath)); |
| 39 | + } |
| 40 | + }); |
| 41 | +} |
| 42 | +function download(version) { |
| 43 | + return __awaiter(this, void 0, void 0, function* () { |
| 44 | + var cachedToolPath = getLocalTool(version); |
| 45 | + if (!cachedToolPath) { |
| 46 | + try { |
| 47 | + let downloadPath = yield tools.downloadTool(downloadUrl(version)); |
| 48 | + let unzippedPath = yield tools.extractTar(downloadPath); |
| 49 | + cachedToolPath = yield tools.cacheDir(unzippedPath, toolName, version); |
| 50 | + } |
| 51 | + catch (exception) { |
| 52 | + throw new Error(`Failed to download octo tools from ${downloadUrl(version)}`); |
| 53 | + } |
| 54 | + } |
| 55 | + const octoPath = findOcto(cachedToolPath); |
| 56 | + if (!octoPath) { |
| 57 | + throw new Error("Octo wasn't found in tools directory"); |
| 58 | + } |
| 59 | + fs.chmod(octoPath, "777"); |
| 60 | + return octoPath; |
| 61 | + }); |
| 62 | +} |
| 63 | +function findOcto(rootFolder) { |
| 64 | + var octoPath = path.join(rootFolder, "*" + toolName + ".dll"); |
| 65 | + console.log(`Looking for ${octoPath}`); |
| 66 | + var allPaths = tasks.find(rootFolder); |
| 67 | + var matches = tasks.match(allPaths, octoPath, rootFolder); |
| 68 | + console.log(matches); |
| 69 | + return matches[0]; |
| 70 | +} |
| 71 | +function getExecutableExtention() { |
| 72 | + return os.type().match(/^Win/) ? ".exe" : ""; |
| 73 | +} |
| 74 | +function getTempDirectory() { |
| 75 | + return tasks.getVariable('agent.tempDirectory') || os.tmpdir(); |
| 76 | +} |
| 77 | +run().then(() => { |
| 78 | + tasks.setResult(tasks.TaskResult.Succeeded, ""); |
| 79 | +}, (reason) => { |
| 80 | + tasks.setResult(tasks.TaskResult.Failed, reason); |
| 81 | +}).catch((error) => { |
| 82 | + tasks.setResult(tasks.TaskResult.Failed, error); |
| 83 | +}); |
0 commit comments