From eb3673a1220680ef0157186d3a35893eb7560276 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 4 Sep 2019 01:36:31 +0200 Subject: [PATCH 1/4] Set GOPATH to the default directory if unset --- lib/installer.js | 422 ++++++++++++++++++++++++----------------------- src/installer.ts | 19 ++- 2 files changed, 234 insertions(+), 207 deletions(-) diff --git a/lib/installer.js b/lib/installer.js index b585d9ea3..4ee1e1d19 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -1,205 +1,217 @@ -"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 }); -// Load tempDirectory before it gets wiped by tool-cache -let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; -const core = __importStar(require("@actions/core")); -const tc = __importStar(require("@actions/tool-cache")); -const os = __importStar(require("os")); -const path = __importStar(require("path")); -const util = __importStar(require("util")); -const semver = __importStar(require("semver")); -const restm = __importStar(require("typed-rest-client/RestClient")); -let osPlat = os.platform(); -let osArch = os.arch(); -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'); -} -function getGo(version) { - return __awaiter(this, void 0, void 0, function* () { - const selected = yield determineVersion(version); - if (selected) { - version = selected; - } - // check cache - let toolPath; - toolPath = tc.find('go', normalizeVersion(version)); - if (!toolPath) { - // download, extract, cache - toolPath = yield acquireGo(version); - core.debug('Go tool is cached under ' + toolPath); - } - setGoEnvironmentVariables(toolPath); - toolPath = path.join(toolPath, 'bin'); - // - // prepend the tools path. instructs the agent to prepend for future tasks - // - core.addPath(toolPath); - }); -} -exports.getGo = getGo; -function acquireGo(version) { - return __awaiter(this, void 0, void 0, function* () { - // - // Download - a tool installer intimately knows how to get the tool (and construct urls) - // - let fileName = getFileName(version); - let downloadUrl = getDownloadUrl(fileName); - let downloadPath = null; - try { - downloadPath = yield tc.downloadTool(downloadUrl); - } - catch (error) { - core.debug(error); - throw `Failed to download version ${version}: ${error}`; - } - // - // Extract - // - let extPath = tempDirectory; - if (!extPath) { - throw new Error('Temp directory not set'); - } - if (osPlat == 'win32') { - extPath = yield tc.extractZip(downloadPath); - } - else { - extPath = yield tc.extractTar(downloadPath); - } - // - // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded - // - const toolRoot = path.join(extPath, 'go'); - version = normalizeVersion(version); - return yield tc.cacheDir(toolRoot, 'go', version); - }); -} -function getFileName(version) { - const platform = osPlat == 'win32' ? 'windows' : osPlat; - const arch = osArch == 'x64' ? 'amd64' : '386'; - const ext = osPlat == 'win32' ? 'zip' : 'tar.gz'; - const filename = util.format('go%s.%s-%s.%s', version, platform, arch, ext); - return filename; -} -function getDownloadUrl(filename) { - return util.format('https://storage.googleapis.com/golang/%s', filename); -} -function setGoEnvironmentVariables(goRoot) { - core.exportVariable('GOROOT', goRoot); - const goPath = process.env['GOPATH'] || ''; - const goBin = process.env['GOBIN'] || ''; - // set GOPATH and GOBIN as user value - if (goPath) { - core.exportVariable('GOPATH', goPath); - } - if (goBin) { - core.exportVariable('GOBIN', goBin); - } -} -// This function is required to convert the version 1.10 to 1.10.0. -// Because caching utility accept only sementic version, -// which have patch number as well. -function normalizeVersion(version) { - const versionPart = version.split('.'); - if (versionPart[1] == null) { - //append minor and patch version if not available - return version.concat('.0.0'); - } - else { - // handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 - if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) { - versionPart[1] = versionPart[1] - .replace('beta', '.0-beta') - .replace('rc', '.0-rc'); - return versionPart.join('.'); - } - } - if (versionPart[2] == null) { - //append patch version if not available - return version.concat('.0'); - } - else { - // handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 - if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) { - versionPart[2] = versionPart[2] - .replace('beta', '-beta') - .replace('rc', '-rc'); - return versionPart.join('.'); - } - } - return version; -} -function determineVersion(version) { - return __awaiter(this, void 0, void 0, function* () { - if (!version.endsWith('.x')) { - const versionPart = version.split('.'); - if (versionPart[1] == null || versionPart[2] == null) { - return yield getLatestVersion(version.concat('.x')); - } - else { - return version; - } - } - return yield getLatestVersion(version); - }); -} -function getLatestVersion(version) { - return __awaiter(this, void 0, void 0, function* () { - // clean .x syntax: 1.10.x -> 1.10 - const trimmedVersion = version.slice(0, version.length - 2); - const versions = yield getPossibleVersions(trimmedVersion); - core.debug(`evaluating ${versions.length} versions`); - if (version.length === 0) { - throw new Error('unable to get latest version'); - } - core.debug(`matched: ${versions[0]}`); - return versions[0]; - }); -} -function getAvailableVersions() { - return __awaiter(this, void 0, void 0, function* () { - let rest = new restm.RestClient('setup-go'); - let tags = (yield rest.get('https://api.github.com/repos/golang/go/git/refs/tags')).result || []; - return tags - .filter(tag => tag.ref.match(/go\d+\.[\w\.]+/g)) - .map(tag => tag.ref.replace('refs/tags/go', '')); - }); -} -function getPossibleVersions(version) { - return __awaiter(this, void 0, void 0, function* () { - const versions = yield getAvailableVersions(); - const possibleVersions = versions.filter(v => v.startsWith(version)); - const versionMap = new Map(); - possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v)); - return Array.from(versionMap.keys()) - .sort(semver.rcompare) - .map(v => versionMap.get(v)); - }); -} +"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 }); +// Load tempDirectory before it gets wiped by tool-cache +let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; +const core = __importStar(require("@actions/core")); +const tc = __importStar(require("@actions/tool-cache")); +const os = __importStar(require("os")); +const path = __importStar(require("path")); +const util = __importStar(require("util")); +const semver = __importStar(require("semver")); +const restm = __importStar(require("typed-rest-client/RestClient")); +let osPlat = os.platform(); +let osArch = os.arch(); +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'); +} +function getGo(version) { + return __awaiter(this, void 0, void 0, function* () { + const selected = yield determineVersion(version); + if (selected) { + version = selected; + } + // check cache + let toolPath; + toolPath = tc.find('go', normalizeVersion(version)); + if (!toolPath) { + // download, extract, cache + toolPath = yield acquireGo(version); + core.debug('Go tool is cached under ' + toolPath); + } + setGoEnvironmentVariables(toolPath); + toolPath = path.join(toolPath, 'bin'); + // + // prepend the tools path. instructs the agent to prepend for future tasks + // + core.addPath(toolPath); + }); +} +exports.getGo = getGo; +function acquireGo(version) { + return __awaiter(this, void 0, void 0, function* () { + // + // Download - a tool installer intimately knows how to get the tool (and construct urls) + // + let fileName = getFileName(version); + let downloadUrl = getDownloadUrl(fileName); + let downloadPath = null; + try { + downloadPath = yield tc.downloadTool(downloadUrl); + } + catch (error) { + core.debug(error); + throw `Failed to download version ${version}: ${error}`; + } + // + // Extract + // + let extPath = tempDirectory; + if (!extPath) { + throw new Error('Temp directory not set'); + } + if (osPlat == 'win32') { + extPath = yield tc.extractZip(downloadPath); + } + else { + extPath = yield tc.extractTar(downloadPath); + } + // + // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded + // + const toolRoot = path.join(extPath, 'go'); + version = normalizeVersion(version); + return yield tc.cacheDir(toolRoot, 'go', version); + }); +} +function getFileName(version) { + const platform = osPlat == 'win32' ? 'windows' : osPlat; + const arch = osArch == 'x64' ? 'amd64' : '386'; + const ext = osPlat == 'win32' ? 'zip' : 'tar.gz'; + const filename = util.format('go%s.%s-%s.%s', version, platform, arch, ext); + return filename; +} +function getDownloadUrl(filename) { + return util.format('https://storage.googleapis.com/golang/%s', filename); +} +function setGoEnvironmentVariables(goRoot) { + core.exportVariable('GOROOT', goRoot); + const goPath = getGoPath(); + const goBin = process.env['GOBIN'] || ''; + // set GOPATH and GOBIN if defined + if (goPath) { + core.exportVariable('GOPATH', goPath); + core.addPath(path.join(goPath, 'bin')); + } + if (goBin) { + core.exportVariable('GOBIN', goBin); + } +} +// Get GOPATH as user value or as defined by https://golang.org/doc/code.html#GOPATH +function getGoPath() { + const home = process.env['HOME'] || ''; + const goPath = process.env['GOPATH'] || ''; + if (goPath) { + return goPath; + } else if (home) { + return path.join(home, 'go'); + } + return ''; +} +// This function is required to convert the version 1.10 to 1.10.0. +// Because caching utility accept only sementic version, +// which have patch number as well. +function normalizeVersion(version) { + const versionPart = version.split('.'); + if (versionPart[1] == null) { + //append minor and patch version if not available + return version.concat('.0.0'); + } + else { + // handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 + if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) { + versionPart[1] = versionPart[1] + .replace('beta', '.0-beta') + .replace('rc', '.0-rc'); + return versionPart.join('.'); + } + } + if (versionPart[2] == null) { + //append patch version if not available + return version.concat('.0'); + } + else { + // handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 + if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) { + versionPart[2] = versionPart[2] + .replace('beta', '-beta') + .replace('rc', '-rc'); + return versionPart.join('.'); + } + } + return version; +} +function determineVersion(version) { + return __awaiter(this, void 0, void 0, function* () { + if (!version.endsWith('.x')) { + const versionPart = version.split('.'); + if (versionPart[1] == null || versionPart[2] == null) { + return yield getLatestVersion(version.concat('.x')); + } + else { + return version; + } + } + return yield getLatestVersion(version); + }); +} +function getLatestVersion(version) { + return __awaiter(this, void 0, void 0, function* () { + // clean .x syntax: 1.10.x -> 1.10 + const trimmedVersion = version.slice(0, version.length - 2); + const versions = yield getPossibleVersions(trimmedVersion); + core.debug(`evaluating ${versions.length} versions`); + if (version.length === 0) { + throw new Error('unable to get latest version'); + } + core.debug(`matched: ${versions[0]}`); + return versions[0]; + }); +} +function getAvailableVersions() { + return __awaiter(this, void 0, void 0, function* () { + let rest = new restm.RestClient('setup-go'); + let tags = (yield rest.get('https://api.github.com/repos/golang/go/git/refs/tags')).result || []; + return tags + .filter(tag => tag.ref.match(/go\d+\.[\w\.]+/g)) + .map(tag => tag.ref.replace('refs/tags/go', '')); + }); +} +function getPossibleVersions(version) { + return __awaiter(this, void 0, void 0, function* () { + const versions = yield getAvailableVersions(); + const possibleVersions = versions.filter(v => v.startsWith(version)); + const versionMap = new Map(); + possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v)); + return Array.from(versionMap.keys()) + .sort(semver.rcompare) + .map(v => versionMap.get(v)); + }); +} diff --git a/src/installer.ts b/src/installer.ts index acafe9f78..7e19cfbd3 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -109,18 +109,33 @@ function getDownloadUrl(filename: string): string { function setGoEnvironmentVariables(goRoot: string) { core.exportVariable('GOROOT', goRoot); - const goPath: string = process.env['GOPATH'] || ''; - const goBin: string = process.env['GOBIN'] || ''; + const goPath = getGoPath(); + const goBin = process.env['GOBIN'] || ''; // set GOPATH and GOBIN as user value if (goPath) { core.exportVariable('GOPATH', goPath); + core.addPath(path.join(goPath, 'bin')); } if (goBin) { core.exportVariable('GOBIN', goBin); } } +// Get GOPATH as user value or as defined by https://golang.org/doc/code.html#GOPATH +function getGoPath(): string { + const home: string = process.env['HOME'] || ''; + const goPath: string = process.env['GOPATH'] || ''; + + if (goPath) { + return goPath; + } else if (home) { + return path.join(home, 'go'); + } + + return ''; +} + // This function is required to convert the version 1.10 to 1.10.0. // Because caching utility accept only sementic version, // which have patch number as well. From 033c63db2c0e4d337206d3c88670900e3c5e89b9 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 4 Sep 2019 01:55:28 +0200 Subject: [PATCH 2/4] Fix EOL --- lib/installer.js | 434 +++++++++++++++++++++++------------------------ 1 file changed, 217 insertions(+), 217 deletions(-) diff --git a/lib/installer.js b/lib/installer.js index 4ee1e1d19..1282c17b0 100644 --- a/lib/installer.js +++ b/lib/installer.js @@ -1,217 +1,217 @@ -"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 }); -// Load tempDirectory before it gets wiped by tool-cache -let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; -const core = __importStar(require("@actions/core")); -const tc = __importStar(require("@actions/tool-cache")); -const os = __importStar(require("os")); -const path = __importStar(require("path")); -const util = __importStar(require("util")); -const semver = __importStar(require("semver")); -const restm = __importStar(require("typed-rest-client/RestClient")); -let osPlat = os.platform(); -let osArch = os.arch(); -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'); -} -function getGo(version) { - return __awaiter(this, void 0, void 0, function* () { - const selected = yield determineVersion(version); - if (selected) { - version = selected; - } - // check cache - let toolPath; - toolPath = tc.find('go', normalizeVersion(version)); - if (!toolPath) { - // download, extract, cache - toolPath = yield acquireGo(version); - core.debug('Go tool is cached under ' + toolPath); - } - setGoEnvironmentVariables(toolPath); - toolPath = path.join(toolPath, 'bin'); - // - // prepend the tools path. instructs the agent to prepend for future tasks - // - core.addPath(toolPath); - }); -} -exports.getGo = getGo; -function acquireGo(version) { - return __awaiter(this, void 0, void 0, function* () { - // - // Download - a tool installer intimately knows how to get the tool (and construct urls) - // - let fileName = getFileName(version); - let downloadUrl = getDownloadUrl(fileName); - let downloadPath = null; - try { - downloadPath = yield tc.downloadTool(downloadUrl); - } - catch (error) { - core.debug(error); - throw `Failed to download version ${version}: ${error}`; - } - // - // Extract - // - let extPath = tempDirectory; - if (!extPath) { - throw new Error('Temp directory not set'); - } - if (osPlat == 'win32') { - extPath = yield tc.extractZip(downloadPath); - } - else { - extPath = yield tc.extractTar(downloadPath); - } - // - // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded - // - const toolRoot = path.join(extPath, 'go'); - version = normalizeVersion(version); - return yield tc.cacheDir(toolRoot, 'go', version); - }); -} -function getFileName(version) { - const platform = osPlat == 'win32' ? 'windows' : osPlat; - const arch = osArch == 'x64' ? 'amd64' : '386'; - const ext = osPlat == 'win32' ? 'zip' : 'tar.gz'; - const filename = util.format('go%s.%s-%s.%s', version, platform, arch, ext); - return filename; -} -function getDownloadUrl(filename) { - return util.format('https://storage.googleapis.com/golang/%s', filename); -} -function setGoEnvironmentVariables(goRoot) { - core.exportVariable('GOROOT', goRoot); - const goPath = getGoPath(); - const goBin = process.env['GOBIN'] || ''; - // set GOPATH and GOBIN if defined - if (goPath) { - core.exportVariable('GOPATH', goPath); - core.addPath(path.join(goPath, 'bin')); - } - if (goBin) { - core.exportVariable('GOBIN', goBin); - } -} -// Get GOPATH as user value or as defined by https://golang.org/doc/code.html#GOPATH -function getGoPath() { - const home = process.env['HOME'] || ''; - const goPath = process.env['GOPATH'] || ''; - if (goPath) { - return goPath; - } else if (home) { - return path.join(home, 'go'); - } - return ''; -} -// This function is required to convert the version 1.10 to 1.10.0. -// Because caching utility accept only sementic version, -// which have patch number as well. -function normalizeVersion(version) { - const versionPart = version.split('.'); - if (versionPart[1] == null) { - //append minor and patch version if not available - return version.concat('.0.0'); - } - else { - // handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 - if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) { - versionPart[1] = versionPart[1] - .replace('beta', '.0-beta') - .replace('rc', '.0-rc'); - return versionPart.join('.'); - } - } - if (versionPart[2] == null) { - //append patch version if not available - return version.concat('.0'); - } - else { - // handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 - if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) { - versionPart[2] = versionPart[2] - .replace('beta', '-beta') - .replace('rc', '-rc'); - return versionPart.join('.'); - } - } - return version; -} -function determineVersion(version) { - return __awaiter(this, void 0, void 0, function* () { - if (!version.endsWith('.x')) { - const versionPart = version.split('.'); - if (versionPart[1] == null || versionPart[2] == null) { - return yield getLatestVersion(version.concat('.x')); - } - else { - return version; - } - } - return yield getLatestVersion(version); - }); -} -function getLatestVersion(version) { - return __awaiter(this, void 0, void 0, function* () { - // clean .x syntax: 1.10.x -> 1.10 - const trimmedVersion = version.slice(0, version.length - 2); - const versions = yield getPossibleVersions(trimmedVersion); - core.debug(`evaluating ${versions.length} versions`); - if (version.length === 0) { - throw new Error('unable to get latest version'); - } - core.debug(`matched: ${versions[0]}`); - return versions[0]; - }); -} -function getAvailableVersions() { - return __awaiter(this, void 0, void 0, function* () { - let rest = new restm.RestClient('setup-go'); - let tags = (yield rest.get('https://api.github.com/repos/golang/go/git/refs/tags')).result || []; - return tags - .filter(tag => tag.ref.match(/go\d+\.[\w\.]+/g)) - .map(tag => tag.ref.replace('refs/tags/go', '')); - }); -} -function getPossibleVersions(version) { - return __awaiter(this, void 0, void 0, function* () { - const versions = yield getAvailableVersions(); - const possibleVersions = versions.filter(v => v.startsWith(version)); - const versionMap = new Map(); - possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v)); - return Array.from(versionMap.keys()) - .sort(semver.rcompare) - .map(v => versionMap.get(v)); - }); -} +"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 }); +// Load tempDirectory before it gets wiped by tool-cache +let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || ''; +const core = __importStar(require("@actions/core")); +const tc = __importStar(require("@actions/tool-cache")); +const os = __importStar(require("os")); +const path = __importStar(require("path")); +const util = __importStar(require("util")); +const semver = __importStar(require("semver")); +const restm = __importStar(require("typed-rest-client/RestClient")); +let osPlat = os.platform(); +let osArch = os.arch(); +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'); +} +function getGo(version) { + return __awaiter(this, void 0, void 0, function* () { + const selected = yield determineVersion(version); + if (selected) { + version = selected; + } + // check cache + let toolPath; + toolPath = tc.find('go', normalizeVersion(version)); + if (!toolPath) { + // download, extract, cache + toolPath = yield acquireGo(version); + core.debug('Go tool is cached under ' + toolPath); + } + setGoEnvironmentVariables(toolPath); + toolPath = path.join(toolPath, 'bin'); + // + // prepend the tools path. instructs the agent to prepend for future tasks + // + core.addPath(toolPath); + }); +} +exports.getGo = getGo; +function acquireGo(version) { + return __awaiter(this, void 0, void 0, function* () { + // + // Download - a tool installer intimately knows how to get the tool (and construct urls) + // + let fileName = getFileName(version); + let downloadUrl = getDownloadUrl(fileName); + let downloadPath = null; + try { + downloadPath = yield tc.downloadTool(downloadUrl); + } + catch (error) { + core.debug(error); + throw `Failed to download version ${version}: ${error}`; + } + // + // Extract + // + let extPath = tempDirectory; + if (!extPath) { + throw new Error('Temp directory not set'); + } + if (osPlat == 'win32') { + extPath = yield tc.extractZip(downloadPath); + } + else { + extPath = yield tc.extractTar(downloadPath); + } + // + // Install into the local tool cache - node extracts with a root folder that matches the fileName downloaded + // + const toolRoot = path.join(extPath, 'go'); + version = normalizeVersion(version); + return yield tc.cacheDir(toolRoot, 'go', version); + }); +} +function getFileName(version) { + const platform = osPlat == 'win32' ? 'windows' : osPlat; + const arch = osArch == 'x64' ? 'amd64' : '386'; + const ext = osPlat == 'win32' ? 'zip' : 'tar.gz'; + const filename = util.format('go%s.%s-%s.%s', version, platform, arch, ext); + return filename; +} +function getDownloadUrl(filename) { + return util.format('https://storage.googleapis.com/golang/%s', filename); +} +function setGoEnvironmentVariables(goRoot) { + core.exportVariable('GOROOT', goRoot); + const goPath = getGoPath(); + const goBin = process.env['GOBIN'] || ''; + // set GOPATH and GOBIN if defined + if (goPath) { + core.exportVariable('GOPATH', goPath); + core.addPath(path.join(goPath, 'bin')); + } + if (goBin) { + core.exportVariable('GOBIN', goBin); + } +} +// Get GOPATH as user value or as defined by https://golang.org/doc/code.html#GOPATH +function getGoPath() { + const home = process.env['HOME'] || ''; + const goPath = process.env['GOPATH'] || ''; + if (goPath) { + return goPath; + } else if (home) { + return path.join(home, 'go'); + } + return ''; +} +// This function is required to convert the version 1.10 to 1.10.0. +// Because caching utility accept only sementic version, +// which have patch number as well. +function normalizeVersion(version) { + const versionPart = version.split('.'); + if (versionPart[1] == null) { + //append minor and patch version if not available + return version.concat('.0.0'); + } + else { + // handle beta and rc: 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 + if (versionPart[1].includes('beta') || versionPart[1].includes('rc')) { + versionPart[1] = versionPart[1] + .replace('beta', '.0-beta') + .replace('rc', '.0-rc'); + return versionPart.join('.'); + } + } + if (versionPart[2] == null) { + //append patch version if not available + return version.concat('.0'); + } + else { + // handle beta and rc: 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 + if (versionPart[2].includes('beta') || versionPart[2].includes('rc')) { + versionPart[2] = versionPart[2] + .replace('beta', '-beta') + .replace('rc', '-rc'); + return versionPart.join('.'); + } + } + return version; +} +function determineVersion(version) { + return __awaiter(this, void 0, void 0, function* () { + if (!version.endsWith('.x')) { + const versionPart = version.split('.'); + if (versionPart[1] == null || versionPart[2] == null) { + return yield getLatestVersion(version.concat('.x')); + } + else { + return version; + } + } + return yield getLatestVersion(version); + }); +} +function getLatestVersion(version) { + return __awaiter(this, void 0, void 0, function* () { + // clean .x syntax: 1.10.x -> 1.10 + const trimmedVersion = version.slice(0, version.length - 2); + const versions = yield getPossibleVersions(trimmedVersion); + core.debug(`evaluating ${versions.length} versions`); + if (version.length === 0) { + throw new Error('unable to get latest version'); + } + core.debug(`matched: ${versions[0]}`); + return versions[0]; + }); +} +function getAvailableVersions() { + return __awaiter(this, void 0, void 0, function* () { + let rest = new restm.RestClient('setup-go'); + let tags = (yield rest.get('https://api.github.com/repos/golang/go/git/refs/tags')).result || []; + return tags + .filter(tag => tag.ref.match(/go\d+\.[\w\.]+/g)) + .map(tag => tag.ref.replace('refs/tags/go', '')); + }); +} +function getPossibleVersions(version) { + return __awaiter(this, void 0, void 0, function* () { + const versions = yield getAvailableVersions(); + const possibleVersions = versions.filter(v => v.startsWith(version)); + const versionMap = new Map(); + possibleVersions.forEach(v => versionMap.set(normalizeVersion(v), v)); + return Array.from(versionMap.keys()) + .sort(semver.rcompare) + .map(v => versionMap.get(v)); + }); +} From f543d166383a27d20be44962ca905e43391522b3 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Wed, 4 Sep 2019 01:57:38 +0200 Subject: [PATCH 3/4] Fix def --- src/installer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/installer.ts b/src/installer.ts index 7e19cfbd3..0803be3e7 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -109,8 +109,8 @@ function getDownloadUrl(filename: string): string { function setGoEnvironmentVariables(goRoot: string) { core.exportVariable('GOROOT', goRoot); - const goPath = getGoPath(); - const goBin = process.env['GOBIN'] || ''; + const goPath: string = getGoPath(); + const goBin: string = process.env['GOBIN'] || ''; // set GOPATH and GOBIN as user value if (goPath) { From 7fb5bea2dd6044c13578b39d351de3694b3ba764 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 11 Feb 2020 16:33:09 +0100 Subject: [PATCH 4/4] Solve conflicts --- dist/index.js | 474 +++++++++++++++++++++++++++----------------------- src/main.ts | 12 ++ src/system.ts | 17 +- 3 files changed, 281 insertions(+), 222 deletions(-) diff --git a/dist/index.js b/dist/index.js index 21783e64b..4238e836f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1246,10 +1246,10 @@ exports.debug = debug; // for test /***/ (function(__unusedmodule, exports, __webpack_require__) { "use strict"; - -Object.defineProperty(exports, "__esModule", { value: true }); -const main_1 = __webpack_require__(198); -main_1.run(); + +Object.defineProperty(exports, "__esModule", { value: true }); +const main_1 = __webpack_require__(198); +main_1.run(); /***/ }), @@ -1258,65 +1258,76 @@ main_1.run(); /***/ (function(__unusedmodule, exports, __webpack_require__) { "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 }); -const core = __importStar(__webpack_require__(470)); -const tc = __importStar(__webpack_require__(533)); -const installer = __importStar(__webpack_require__(749)); -const path = __importStar(__webpack_require__(622)); -function run() { - return __awaiter(this, void 0, void 0, function* () { - try { - // - // versionSpec is optional. If supplied, install / use from the tool cache - // If not supplied then problem matchers will still be setup. Useful for self-hosted. - // - let versionSpec = core.getInput('go-version'); - // stable will be true unless false is the exact input - // since getting unstable versions should be explicit - let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; - console.log(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`); - if (versionSpec) { - let installDir = tc.find('go', versionSpec); - if (!installDir) { - console.log(`A version satisfying ${versionSpec} not found locally, attempting to download ...`); - installDir = yield installer.downloadGo(versionSpec, stable); - console.log('Installed'); - } - if (installDir) { - core.exportVariable('GOROOT', installDir); - core.addPath(path.join(installDir, 'bin')); - console.log('Added go to the path'); - } - else { - throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`); - } - } - // add problem matchers - const matchersPath = path.join(__dirname, '..', 'matchers.json'); - console.log(`##[add-matcher]${matchersPath}`); - } - catch (error) { - core.setFailed(error.message); - } - }); -} -exports.run = run; + +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 }); +const core = __importStar(__webpack_require__(470)); +const tc = __importStar(__webpack_require__(533)); +const installer = __importStar(__webpack_require__(749)); +const path = __importStar(__webpack_require__(622)); +const system = __importStar(__webpack_require__(737)); +function run() { + return __awaiter(this, void 0, void 0, function* () { + try { + // + // versionSpec is optional. If supplied, install / use from the tool cache + // If not supplied then problem matchers will still be setup. Useful for self-hosted. + // + let versionSpec = core.getInput('go-version'); + // stable will be true unless false is the exact input + // since getting unstable versions should be explicit + let stable = (core.getInput('stable') || 'true').toUpperCase() === 'TRUE'; + console.log(`Setup go ${stable ? 'stable' : ''} version spec ${versionSpec}`); + if (versionSpec) { + let installDir = tc.find('go', versionSpec); + if (!installDir) { + console.log(`A version satisfying ${versionSpec} not found locally, attempting to download ...`); + installDir = yield installer.downloadGo(versionSpec, stable); + console.log('Installed'); + } + if (installDir) { + core.exportVariable('GOROOT', installDir); + core.addPath(path.join(installDir, 'bin')); + console.log('Added go to the path'); + // set GOPATH and GOBIN as user value + const goPath = system.getGoPath(); + if (goPath) { + core.exportVariable('GOPATH', goPath); + core.addPath(path.join(goPath, 'bin')); + } + const goBin = process.env['GOBIN'] || ''; + if (goBin) { + core.exportVariable('GOBIN', goBin); + } + } + else { + throw new Error(`Could not find a version that satisfied version spec: ${versionSpec}`); + } + } + // add problem matchers + const matchersPath = path.join(__dirname, '..', 'matchers.json'); + console.log(`##[add-matcher]${matchersPath}`); + } + catch (error) { + core.setFailed(error.message); + } + }); +} +exports.run = run; /***/ }), @@ -4509,40 +4520,61 @@ module.exports = bytesToUuid; /***/ (function(__unusedmodule, exports, __webpack_require__) { "use strict"; - -Object.defineProperty(exports, "__esModule", { value: true }); -let os = __webpack_require__(87); -function getPlatform() { - // darwin and linux match already - // freebsd not supported yet but future proofed. - // 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32' - let plat = os.platform(); - // wants 'darwin', 'freebsd', 'linux', 'windows' - if (plat === 'win32') { - plat = 'windows'; - } - return plat; -} -exports.getPlatform = getPlatform; -function getArch() { - // 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'. - let arch = os.arch(); - // wants amd64, 386, arm64, armv61, ppc641e, s390x - // currently not supported by runner but future proofed mapping - switch (arch) { - case 'x64': - arch = 'amd64'; - break; - // case 'ppc': - // arch = 'ppc64'; - // break; - case 'x32': - arch = '386'; - break; - } - return arch; -} -exports.getArch = getArch; + +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 os = __importStar(__webpack_require__(87)); +const path = __importStar(__webpack_require__(622)); +function getPlatform() { + // darwin and linux match already + // freebsd not supported yet but future proofed. + // 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', and 'win32' + let plat = os.platform(); + // wants 'darwin', 'freebsd', 'linux', 'windows' + if (plat === 'win32') { + plat = 'windows'; + } + return plat; +} +exports.getPlatform = getPlatform; +function getArch() { + // 'arm', 'arm64', 'ia32', 'mips', 'mipsel', 'ppc', 'ppc64', 's390', 's390x', 'x32', and 'x64'. + let arch = os.arch(); + // wants amd64, 386, arm64, armv61, ppc641e, s390x + // currently not supported by runner but future proofed mapping + switch (arch) { + case 'x64': + arch = 'amd64'; + break; + // case 'ppc': + // arch = 'ppc64'; + // break; + case 'x32': + arch = '386'; + break; + } + return arch; +} +exports.getArch = getArch; +// Get GOPATH as user value or as defined by https://golang.org/doc/code.html#GOPATH +function getGoPath() { + const home = process.env['HOME'] || ''; + const goPath = process.env['GOPATH'] || ''; + if (goPath) { + return goPath; + } + else if (home) { + return path.join(home, 'go'); + } + return ''; +} +exports.getGoPath = getGoPath; /***/ }), @@ -4558,130 +4590,130 @@ module.exports = require("fs"); /***/ (function(module, exports, __webpack_require__) { "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 }); -const tc = __importStar(__webpack_require__(533)); -const path = __importStar(__webpack_require__(622)); -const semver = __importStar(__webpack_require__(280)); -const httpm = __importStar(__webpack_require__(539)); -const sys = __importStar(__webpack_require__(737)); -const core_1 = __webpack_require__(470); -function downloadGo(versionSpec, stable) { - return __awaiter(this, void 0, void 0, function* () { - let toolPath; - try { - let match = yield findMatch(versionSpec, stable); - if (match) { - // download - core_1.debug(`match ${match.version}`); - let downloadUrl = `https://storage.googleapis.com/golang/${match.files[0].filename}`; - console.log(`Downloading from ${downloadUrl}`); - let downloadPath = yield tc.downloadTool(downloadUrl); - core_1.debug(`downloaded to ${downloadPath}`); - // extract - console.log('Extracting ...'); - let extPath = sys.getPlatform() == 'windows' - ? yield tc.extractZip(downloadPath) - : yield tc.extractTar(downloadPath); - core_1.debug(`extracted to ${extPath}`); - // extracts with a root folder that matches the fileName downloaded - const toolRoot = path.join(extPath, 'go'); - toolPath = yield tc.cacheDir(toolRoot, 'go', versionSpec); - } - } - catch (error) { - throw new Error(`Failed to download version ${versionSpec}: ${error}`); - } - return toolPath; - }); -} -exports.downloadGo = downloadGo; -function findMatch(versionSpec, stable) { - return __awaiter(this, void 0, void 0, function* () { - let archFilter = sys.getArch(); - let platFilter = sys.getPlatform(); - let result; - let match; - const dlUrl = 'https://golang.org/dl/?mode=json&include=all'; - let candidates = yield module.exports.getVersions(dlUrl); - if (!candidates) { - throw new Error(`golang download url did not return results`); - } - let goFile; - for (let i = 0; i < candidates.length; i++) { - let candidate = candidates[i]; - let version = makeSemver(candidate.version); - // 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0 - // since a semver of 1.13 would match latest 1.13 - let parts = version.split('.'); - if (parts.length == 2) { - version = version + '.0'; - } - core_1.debug(`check ${version} satisfies ${versionSpec}`); - if (semver.satisfies(version, versionSpec) && - (!stable || candidate.stable === stable)) { - goFile = candidate.files.find(file => { - core_1.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`); - return file.arch === archFilter && file.os === platFilter; - }); - if (goFile) { - core_1.debug(`matched ${candidate.version}`); - match = candidate; - break; - } - } - } - if (match && goFile) { - // clone since we're mutating the file list to be only the file that matches - result = Object.assign({}, match); - result.files = [goFile]; - } - return result; - }); -} -exports.findMatch = findMatch; -function getVersions(dlUrl) { - return __awaiter(this, void 0, void 0, function* () { - // this returns versions descending so latest is first - let http = new httpm.HttpClient('setup-go'); - return (yield http.getJson(dlUrl)).result; - }); -} -exports.getVersions = getVersions; -// -// Convert the go version syntax into semver for semver matching -// 1.13.1 => 1.13.1 -// 1.13 => 1.13.0 -// 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 -// 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 -function makeSemver(version) { - version = version.replace('go', ''); - version = version.replace('beta', '-beta').replace('rc', '-rc'); - let parts = version.split('-'); - let verPart = parts[0]; - let prereleasePart = parts.length > 1 ? `-${parts[1]}` : ''; - let verParts = verPart.split('.'); - if (verParts.length == 2) { - verPart += '.0'; - } - return `${verPart}${prereleasePart}`; -} -exports.makeSemver = makeSemver; + +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 }); +const tc = __importStar(__webpack_require__(533)); +const path = __importStar(__webpack_require__(622)); +const semver = __importStar(__webpack_require__(280)); +const httpm = __importStar(__webpack_require__(539)); +const sys = __importStar(__webpack_require__(737)); +const core_1 = __webpack_require__(470); +function downloadGo(versionSpec, stable) { + return __awaiter(this, void 0, void 0, function* () { + let toolPath; + try { + let match = yield findMatch(versionSpec, stable); + if (match) { + // download + core_1.debug(`match ${match.version}`); + let downloadUrl = `https://storage.googleapis.com/golang/${match.files[0].filename}`; + console.log(`Downloading from ${downloadUrl}`); + let downloadPath = yield tc.downloadTool(downloadUrl); + core_1.debug(`downloaded to ${downloadPath}`); + // extract + console.log('Extracting ...'); + let extPath = sys.getPlatform() == 'windows' + ? yield tc.extractZip(downloadPath) + : yield tc.extractTar(downloadPath); + core_1.debug(`extracted to ${extPath}`); + // extracts with a root folder that matches the fileName downloaded + const toolRoot = path.join(extPath, 'go'); + toolPath = yield tc.cacheDir(toolRoot, 'go', versionSpec); + } + } + catch (error) { + throw new Error(`Failed to download version ${versionSpec}: ${error}`); + } + return toolPath; + }); +} +exports.downloadGo = downloadGo; +function findMatch(versionSpec, stable) { + return __awaiter(this, void 0, void 0, function* () { + let archFilter = sys.getArch(); + let platFilter = sys.getPlatform(); + let result; + let match; + const dlUrl = 'https://golang.org/dl/?mode=json&include=all'; + let candidates = yield module.exports.getVersions(dlUrl); + if (!candidates) { + throw new Error(`golang download url did not return results`); + } + let goFile; + for (let i = 0; i < candidates.length; i++) { + let candidate = candidates[i]; + let version = makeSemver(candidate.version); + // 1.13.0 is advertised as 1.13 preventing being able to match exactly 1.13.0 + // since a semver of 1.13 would match latest 1.13 + let parts = version.split('.'); + if (parts.length == 2) { + version = version + '.0'; + } + core_1.debug(`check ${version} satisfies ${versionSpec}`); + if (semver.satisfies(version, versionSpec) && + (!stable || candidate.stable === stable)) { + goFile = candidate.files.find(file => { + core_1.debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`); + return file.arch === archFilter && file.os === platFilter; + }); + if (goFile) { + core_1.debug(`matched ${candidate.version}`); + match = candidate; + break; + } + } + } + if (match && goFile) { + // clone since we're mutating the file list to be only the file that matches + result = Object.assign({}, match); + result.files = [goFile]; + } + return result; + }); +} +exports.findMatch = findMatch; +function getVersions(dlUrl) { + return __awaiter(this, void 0, void 0, function* () { + // this returns versions descending so latest is first + let http = new httpm.HttpClient('setup-go'); + return (yield http.getJson(dlUrl)).result; + }); +} +exports.getVersions = getVersions; +// +// Convert the go version syntax into semver for semver matching +// 1.13.1 => 1.13.1 +// 1.13 => 1.13.0 +// 1.10beta1 => 1.10.0-beta1, 1.10rc1 => 1.10.0-rc1 +// 1.8.5beta1 => 1.8.5-beta1, 1.8.5rc1 => 1.8.5-rc1 +function makeSemver(version) { + version = version.replace('go', ''); + version = version.replace('beta', '-beta').replace('rc', '-rc'); + let parts = version.split('-'); + let verPart = parts[0]; + let prereleasePart = parts.length > 1 ? `-${parts[1]}` : ''; + let verParts = verPart.split('.'); + if (verParts.length == 2) { + verPart += '.0'; + } + return `${verPart}${prereleasePart}`; +} +exports.makeSemver = makeSemver; /***/ }), diff --git a/src/main.ts b/src/main.ts index 132be9bd2..30856e38d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,6 +2,7 @@ import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; import * as installer from './installer'; import * as path from 'path'; +import * as system from './system'; export async function run() { try { @@ -34,6 +35,17 @@ export async function run() { core.exportVariable('GOROOT', installDir); core.addPath(path.join(installDir, 'bin')); console.log('Added go to the path'); + + // set GOPATH and GOBIN as user value + const goPath: string = system.getGoPath(); + if (goPath) { + core.exportVariable('GOPATH', goPath); + core.addPath(path.join(goPath, 'bin')); + } + const goBin: string = process.env['GOBIN'] || ''; + if (goBin) { + core.exportVariable('GOBIN', goBin); + } } else { throw new Error( `Could not find a version that satisfied version spec: ${versionSpec}` diff --git a/src/system.ts b/src/system.ts index 8dfad42fd..992760cd9 100644 --- a/src/system.ts +++ b/src/system.ts @@ -1,4 +1,5 @@ -let os = require('os'); +import * as os from 'os'; +import * as path from 'path'; export function getPlatform(): string { // darwin and linux match already @@ -35,3 +36,17 @@ export function getArch(): string { return arch; } + +// Get GOPATH as user value or as defined by https://golang.org/doc/code.html#GOPATH +export function getGoPath(): string { + const home: string = process.env['HOME'] || ''; + const goPath: string = process.env['GOPATH'] || ''; + + if (goPath) { + return goPath; + } else if (home) { + return path.join(home, 'go'); + } + + return ''; +}