1
0
Fork 0
mirror of https://github.com/actions/setup-go synced 2024-12-22 06:12:42 +00:00

Merge pull request #1 from actions/features/installer

Add installer
This commit is contained in:
Danny McCormick 2019-06-26 21:01:54 -04:00 committed by GitHub
commit cd65450961
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 323 additions and 6 deletions

View file

@ -10,12 +10,65 @@ process.env['RUNNER_TOOLSDIRECTORY'] = toolDir;
process.env['RUNNER_TEMPDIRECTORY'] = tempDir;
import * as installer from '../src/installer';
const IS_WINDOWS = process.platform === 'win32';
describe('installer tests', () => {
beforeAll(() => {});
beforeAll(async () => {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
}, 100000);
afterAll(async () => {
try {
await io.rmRF(toolDir);
await io.rmRF(tempDir);
} catch {
console.log('Failed to remove test directories');
}
}, 100000);
it('Acquires version of go if no matching version is installed', async () => {
await installer.getGo('1.10');
const goDir = path.join(toolDir, 'go', '1.10.0', os.arch());
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
if (IS_WINDOWS) {
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
} else {
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
}
}, 100000);
it('Throws if no location contains correct go version', async () => {
let thrown = false;
try {
await installer.getGo('1000.0');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
});
it('TODO - Add tests', async () => {});
it('Uses version of go installed in cache', async () => {
const goDir: string = path.join(toolDir, 'go', '250.0.0', os.arch());
await io.mkdirP(goDir);
fs.writeFileSync(`${goDir}.complete`, 'hello');
// This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getGo('250.0');
return;
});
it('Doesnt use version of go that was only partially installed in cache', async () => {
const goDir: string = path.join(toolDir, 'go', '251.0.0', os.arch());
await io.mkdirP(goDir);
let thrown = false;
try {
// This will throw if it doesn't find it in the cache (because no such version exists)
await installer.getGo('251.0');
} catch {
thrown = true;
}
expect(thrown).toBe(true);
return;
});
});

134
lib/installer.js Normal file
View file

@ -0,0 +1,134 @@
"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"));
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* () {
// 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 if (versionPart[2] == null) {
//append patch version if not available
return version.concat('.0');
}
return version;
}

View file

@ -16,7 +16,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
// import * as installer from './installer';
const installer = __importStar(require("./installer"));
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
@ -26,7 +26,7 @@ function run() {
//
const version = core.getInput('version');
if (version) {
// await installer.getGo(version);
yield installer.getGo(version);
}
// TODO: setup proxy from runner proxy config
// TODO: problem matchers registered

130
src/installer.ts Normal file
View file

@ -0,0 +1,130 @@
// Load tempDirectory before it gets wiped by tool-cache
let tempDirectory = process.env['RUNNER_TEMPDIRECTORY'] || '';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
let osPlat: string = os.platform();
let osArch: string = 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');
}
export async function getGo(version: string) {
// check cache
let toolPath: string;
toolPath = tc.find('go', normalizeVersion(version));
if (!toolPath) {
// download, extract, cache
toolPath = await 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);
}
async function acquireGo(version: string): Promise<string> {
//
// Download - a tool installer intimately knows how to get the tool (and construct urls)
//
let fileName: string = getFileName(version);
let downloadUrl: string = getDownloadUrl(fileName);
let downloadPath: string | null = null;
try {
downloadPath = await tc.downloadTool(downloadUrl);
} catch (error) {
core.debug(error);
throw `Failed to download version ${version}: ${error}`;
}
//
// Extract
//
let extPath: string = tempDirectory;
if (!extPath) {
throw new Error('Temp directory not set');
}
if (osPlat == 'win32') {
extPath = await tc.extractZip(downloadPath);
} else {
extPath = await 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 await tc.cacheDir(toolRoot, 'go', version);
}
function getFileName(version: string): string {
const platform: string = osPlat == 'win32' ? 'windows' : osPlat;
const arch: string = osArch == 'x64' ? 'amd64' : '386';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
const filename: string = util.format(
'go%s.%s-%s.%s',
version,
platform,
arch,
ext
);
return filename;
}
function getDownloadUrl(filename: string): string {
return util.format('https://storage.googleapis.com/golang/%s', filename);
}
function setGoEnvironmentVariables(goRoot: string) {
core.exportVariable('GOROOT', goRoot);
const goPath: string = process.env['GOPATH'] || '';
const goBin: string = 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: string): string {
const versionPart = version.split('.');
if (versionPart[1] == null) {
//append minor and patch version if not available
return version.concat('.0.0');
} else if (versionPart[2] == null) {
//append patch version if not available
return version.concat('.0');
}
return version;
}

View file

@ -1,5 +1,5 @@
import * as core from '@actions/core';
// import * as installer from './installer';
import * as installer from './installer';
async function run() {
try {
@ -9,7 +9,7 @@ async function run() {
//
const version = core.getInput('version');
if (version) {
// await installer.getGo(version);
await installer.getGo(version);
}
// TODO: setup proxy from runner proxy config