forked from actions/setup-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.ts
139 lines (116 loc) · 4.07 KB
/
installer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import * as tc from '@actions/tool-cache';
import * as path from 'path';
import * as semver from 'semver';
import * as httpm from '@actions/http-client';
import * as sys from './system';
import {debug} from '@actions/core';
export async function downloadGo(
versionSpec: string,
stable: boolean
): Promise<string | undefined> {
let toolPath: string | undefined;
try {
let match: IGoVersion | undefined = await findMatch(versionSpec, stable);
if (match) {
// download
debug(`match ${match.version}`);
let downloadUrl: string = `https://storage.googleapis.com/golang/${match.files[0].filename}`;
console.log(`Downloading from ${downloadUrl}`);
let downloadPath: string = await tc.downloadTool(downloadUrl);
debug(`downloaded to ${downloadPath}`);
// extract
console.log('Extracting ...');
let extPath: string =
sys.getPlatform() == 'windows'
? await tc.extractZip(downloadPath)
: await tc.extractTar(downloadPath);
debug(`extracted to ${extPath}`);
// extracts with a root folder that matches the fileName downloaded
const toolRoot = path.join(extPath, 'go');
toolPath = await tc.cacheDir(toolRoot, 'go', makeSemver(match.version));
}
} catch (error) {
throw new Error(`Failed to download version ${versionSpec}: ${error}`);
}
return toolPath;
}
export interface IGoVersionFile {
filename: string;
// darwin, linux, windows
os: string;
arch: string;
}
export interface IGoVersion {
version: string;
stable: boolean;
files: IGoVersionFile[];
}
export async function findMatch(
versionSpec: string,
stable: boolean
): Promise<IGoVersion | undefined> {
let archFilter = sys.getArch();
let platFilter = sys.getPlatform();
let result: IGoVersion | undefined;
let match: IGoVersion | undefined;
const dlUrl: string = 'https://goproxy.io/dl/?mode=json&include=all';
let candidates: IGoVersion[] | null = await module.exports.getVersions(dlUrl);
if (!candidates) {
throw new Error(`golang download url did not return results`);
}
let goFile: IGoVersionFile | undefined;
for (let i = 0; i < candidates.length; i++) {
let candidate: IGoVersion = 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: string[] = version.split('.');
if (parts.length == 2) {
version = version + '.0';
}
debug(`check ${version} satisfies ${versionSpec}`);
if (
semver.satisfies(version, versionSpec) &&
(!stable || candidate.stable === stable)
) {
goFile = candidate.files.find(file => {
debug(`${file.arch}===${archFilter} && ${file.os}===${platFilter}`);
return file.arch === archFilter && file.os === platFilter;
});
if (goFile) {
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 = <IGoVersion>Object.assign({}, match);
result.files = [goFile];
}
return result;
}
export async function getVersions(dlUrl: string): Promise<IGoVersion[] | null> {
// this returns versions descending so latest is first
let http: httpm.HttpClient = new httpm.HttpClient('setup-go');
return (await http.getJson<IGoVersion[]>(dlUrl)).result;
}
//
// 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
export function makeSemver(version: string): string {
version = version.replace('go', '');
version = version.replace('beta', '-beta').replace('rc', '-rc');
let parts = version.split('-');
let verPart: string = parts[0];
let prereleasePart = parts.length > 1 ? `-${parts[1]}` : '';
let verParts: string[] = verPart.split('.');
if (verParts.length == 2) {
verPart += '.0';
}
return `${verPart}${prereleasePart}`;
}