|
| 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 | +// Load tempDirectory before it gets wiped by tool-cache |
| 19 | +let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; |
| 20 | +const core = __importStar(require("@actions/core")); |
| 21 | +const tc = __importStar(require("@actions/tool-cache")); |
| 22 | +const os = __importStar(require("os")); |
| 23 | +const path = __importStar(require("path")); |
| 24 | +const util = __importStar(require("util")); |
| 25 | +let osPlat = os.platform(); |
| 26 | +let osArch = os.arch(); |
| 27 | +if (!tempDirectory) { |
| 28 | + let baseLocation; |
| 29 | + if (process.platform === 'win32') { |
| 30 | + // On windows use the USERPROFILE env variable |
| 31 | + baseLocation = process.env['USERPROFILE'] || 'C:\\'; |
| 32 | + } |
| 33 | + else { |
| 34 | + if (process.platform === 'darwin') { |
| 35 | + baseLocation = '/Users'; |
| 36 | + } |
| 37 | + else { |
| 38 | + baseLocation = '/home'; |
| 39 | + } |
| 40 | + } |
| 41 | + tempDirectory = path.join(baseLocation, 'actions', 'temp'); |
| 42 | +} |
| 43 | +function getGo(version) { |
| 44 | + return __awaiter(this, void 0, void 0, function* () { |
| 45 | + // check cache |
| 46 | + let toolPath; |
| 47 | + toolPath = tc.find('go', normalizeVersion(version)); |
| 48 | + if (!toolPath) { |
| 49 | + // download, extract, cache |
| 50 | + toolPath = yield acquireGo(version); |
| 51 | + core.debug('Go tool is cached under ' + toolPath); |
| 52 | + } |
| 53 | + setGoEnvironmentVariables(toolPath); |
| 54 | + toolPath = path.join(toolPath, 'bin'); |
| 55 | + // |
| 56 | + // prepend the tools path. instructs the agent to prepend for future tasks |
| 57 | + // |
| 58 | + core.addPath(toolPath); |
| 59 | + }); |
| 60 | +} |
| 61 | +exports.getGo = getGo; |
| 62 | +function acquireGo(version) { |
| 63 | + return __awaiter(this, void 0, void 0, function* () { |
| 64 | + // |
| 65 | + // Download - a tool installer intimately knows how to get the tool (and construct urls) |
| 66 | + // |
| 67 | + let fileName = getFileName(version); |
| 68 | + let downloadUrl = getDownloadUrl(fileName); |
| 69 | + let downloadPath = null; |
| 70 | + try { |
| 71 | + downloadPath = yield tc.downloadTool(downloadUrl); |
| 72 | + } |
| 73 | + catch (error) { |
| 74 | + core.debug(error); |
| 75 | + throw `Failed to download version ${version}: ${error}`; |
| 76 | + } |
| 77 | + // |
| 78 | + // Extract |
| 79 | + // |
| 80 | + let extPath = tempDirectory; |
| 81 | + if (!extPath) { |
| 82 | + throw new Error('Temp directory not set'); |
| 83 | + } |
| 84 | + if (osPlat == 'win32') { |
| 85 | + extPath = yield tc.extractZip(downloadPath); |
| 86 | + } |
| 87 | + else { |
| 88 | + extPath = yield tc.extractTar(downloadPath); |
| 89 | + } |
| 90 | + // |
| 91 | + // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded |
| 92 | + // |
| 93 | + const toolRoot = path.join(extPath, 'go'); |
| 94 | + version = normalizeVersion(version); |
| 95 | + return yield tc.cacheDir(toolRoot, 'go', version); |
| 96 | + }); |
| 97 | +} |
| 98 | +function getFileName(version) { |
| 99 | + const platform = osPlat == 'win32' ? 'windows' : osPlat; |
| 100 | + const arch = osArch == 'x64' ? 'amd64' : '386'; |
| 101 | + const ext = osPlat == 'win32' ? 'zip' : 'tar.gz'; |
| 102 | + const filename = util.format('go%s.%s-%s.%s', version, platform, arch, ext); |
| 103 | + return filename; |
| 104 | +} |
| 105 | +function getDownloadUrl(filename) { |
| 106 | + return util.format('https://storage.googleapis.com/golang/%s', filename); |
| 107 | +} |
| 108 | +function setGoEnvironmentVariables(goRoot) { |
| 109 | + core.exportVariable('GOROOT', goRoot); |
| 110 | + const goPath = process.env['GOPATH'] || ''; |
| 111 | + const goBin = process.env['GOBIN'] || ''; |
| 112 | + // set GOPATH and GOBIN as user value |
| 113 | + if (!util.isNullOrUndefined(goPath)) { |
| 114 | + core.exportVariable('GOPATH', goPath); |
| 115 | + } |
| 116 | + if (!util.isNullOrUndefined(goBin)) { |
| 117 | + core.exportVariable('GOBIN', goBin); |
| 118 | + } |
| 119 | +} |
| 120 | +// This function is required to convert the version 1.10 to 1.10.0. |
| 121 | +// Because caching utility accept only sementic version, |
| 122 | +// which have patch number as well. |
| 123 | +function normalizeVersion(version) { |
| 124 | + const versionPart = version.split('.'); |
| 125 | + if (versionPart[1] == null) { |
| 126 | + //append minor and patch version if not available |
| 127 | + return version.concat('.0.0'); |
| 128 | + } |
| 129 | + else if (versionPart[2] == null) { |
| 130 | + //append patch version if not available |
| 131 | + return version.concat('.0'); |
| 132 | + } |
| 133 | + return version; |
| 134 | +} |
0 commit comments