Skip to content

Commit cd65450

Browse files
author
Danny McCormick
authored
Merge pull request #1 from actions/features/installer
Add installer
2 parents 95028d6 + 90fdf52 commit cd65450

File tree

5 files changed

+323
-6
lines changed

5 files changed

+323
-6
lines changed

__tests__/installer.test.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,65 @@ process.env['RUNNER_TOOLSDIRECTORY'] = toolDir;
1010
process.env['RUNNER_TEMPDIRECTORY'] = tempDir;
1111
import * as installer from '../src/installer';
1212

13+
const IS_WINDOWS = process.platform === 'win32';
14+
1315
describe('installer tests', () => {
14-
beforeAll(() => {});
1516
beforeAll(async () => {
1617
await io.rmRF(toolDir);
1718
await io.rmRF(tempDir);
19+
}, 100000);
20+
21+
afterAll(async () => {
22+
try {
23+
await io.rmRF(toolDir);
24+
await io.rmRF(tempDir);
25+
} catch {
26+
console.log('Failed to remove test directories');
27+
}
28+
}, 100000);
29+
30+
it('Acquires version of go if no matching version is installed', async () => {
31+
await installer.getGo('1.10');
32+
const goDir = path.join(toolDir, 'go', '1.10.0', os.arch());
33+
34+
expect(fs.existsSync(`${goDir}.complete`)).toBe(true);
35+
if (IS_WINDOWS) {
36+
expect(fs.existsSync(path.join(goDir, 'bin', 'go.exe'))).toBe(true);
37+
} else {
38+
expect(fs.existsSync(path.join(goDir, 'bin', 'go'))).toBe(true);
39+
}
40+
}, 100000);
41+
42+
it('Throws if no location contains correct go version', async () => {
43+
let thrown = false;
44+
try {
45+
await installer.getGo('1000.0');
46+
} catch {
47+
thrown = true;
48+
}
49+
expect(thrown).toBe(true);
50+
});
51+
52+
it('Uses version of go installed in cache', async () => {
53+
const goDir: string = path.join(toolDir, 'go', '250.0.0', os.arch());
54+
await io.mkdirP(goDir);
55+
fs.writeFileSync(`${goDir}.complete`, 'hello');
56+
// This will throw if it doesn't find it in the cache (because no such version exists)
57+
await installer.getGo('250.0');
58+
return;
1859
});
1960

20-
it('TODO - Add tests', async () => {});
61+
it('Doesnt use version of go that was only partially installed in cache', async () => {
62+
const goDir: string = path.join(toolDir, 'go', '251.0.0', os.arch());
63+
await io.mkdirP(goDir);
64+
let thrown = false;
65+
try {
66+
// This will throw if it doesn't find it in the cache (because no such version exists)
67+
await installer.getGo('251.0');
68+
} catch {
69+
thrown = true;
70+
}
71+
expect(thrown).toBe(true);
72+
return;
73+
});
2174
});

lib/installer.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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 (goPath) {
114+
core.exportVariable('GOPATH', goPath);
115+
}
116+
if (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+
}

lib/setup-go.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
1616
};
1717
Object.defineProperty(exports, "__esModule", { value: true });
1818
const core = __importStar(require("@actions/core"));
19-
// import * as installer from './installer';
19+
const installer = __importStar(require("./installer"));
2020
function run() {
2121
return __awaiter(this, void 0, void 0, function* () {
2222
try {
@@ -26,7 +26,7 @@ function run() {
2626
//
2727
const version = core.getInput('version');
2828
if (version) {
29-
// await installer.getGo(version);
29+
yield installer.getGo(version);
3030
}
3131
// TODO: setup proxy from runner proxy config
3232
// TODO: problem matchers registered

src/installer.ts

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

src/setup-go.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as core from '@actions/core';
2-
// import * as installer from './installer';
2+
import * as installer from './installer';
33

44
async function run() {
55
try {
@@ -9,7 +9,7 @@ async function run() {
99
//
1010
const version = core.getInput('version');
1111
if (version) {
12-
// await installer.getGo(version);
12+
await installer.getGo(version);
1313
}
1414

1515
// TODO: setup proxy from runner proxy config

0 commit comments

Comments
 (0)