-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
499 lines (442 loc) · 17.2 KB
/
index.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import Cache from "@actions/cache";
import ChildProcess from "child_process";
import Core from "@actions/core";
import {promises as FS} from "fs";
import Glob from "@actions/glob";
import IO from "@actions/io";
import {Octokit} from "@octokit/rest";
import Path from "path";
import ToolCache from "@actions/tool-cache";
import URL from "url";
import Util from "util";
import {cmpTags} from "tag-cmp";
import fetch from "node-fetch";
const execFile = Util.promisify(ChildProcess.execFile);
async function run() {
try {
const params = {
"crystal": "latest",
"shards": "true",
};
for (const key of ["crystal", "shards", "arch"]) {
let value;
if ((value = Core.getInput(key))) {
params[key] = value;
}
}
if (params.crystal === "master") {
params.crystal = "nightly";
}
if (params.shards === "master") {
params.shards = "nightly";
}
params.path = Core.getInput("destination") || Path.join(
process.env["RUNNER_TEMP"], `crystal-${params.crystal.replace("branch:", "")}-${params.shards}-${params.arch}`,
);
Core.setOutput("path", params.path);
const func = {
[Linux]: installCrystalForLinux,
[Mac]: installCrystalForMac,
[Windows]: installCrystalForWindows,
}[getPlatform()];
if (!func) {
throw `Platform "${getPlatform()}" is not supported`;
}
const crystalPromise = func(params);
params.path += "-shards";
await maybeInstallShards(params, crystalPromise);
await crystalPromise;
const {stdout} = await subprocess(["crystal", "--version"]);
Core.info(stdout);
if (!Core.getInput("annotate") || Core.getBooleanInput("annotate")) {
const scriptDir = Path.dirname(URL.fileURLToPath(import.meta.url));
const matchersPath = Path.join(scriptDir, ".github");
Core.info(`::add-matcher::${Path.join(matchersPath, "crystal.json")}`);
Core.info(`::add-matcher::${Path.join(matchersPath, "crystal-spec.json")}`);
}
} catch (error) {
Core.setFailed(error);
process.exit(1);
}
}
const Linux = "Linux", Mac = "macOS", Windows = "Windows";
function getPlatform() {
const platform = process.env["INSTALL_CRYSTAL_PLATFORM"] || process.platform;
return {"linux": Linux, "darwin": Mac, "win32": Windows}[platform] || platform;
}
function getArch() {
return {"ia32": "x86", "x64": "x86_64"}[process.arch] || process.arch;
}
function checkArch(arch, allowed) {
if (!allowed.includes(arch)) {
throw `Architecture "${arch}" is not supported on ${getPlatform()}`;
}
}
const Latest = "latest";
const Nightly = "nightly";
const Any = "true";
const None = "false";
const NumericVersion = /^\d([.\d]*\d)?$/;
const BranchVersion = /^branch:(.+)$/;
function checkVersion(version, allowed, earliestAllowed = null) {
const numericVersion = NumericVersion.test(version) && version;
if (numericVersion && (!earliestAllowed || cmpTags(numericVersion, earliestAllowed) >= 0)) {
allowed[allowed.indexOf(NumericVersion)] = numericVersion;
}
const branchVersion = BranchVersion.test(version) && version;
if (branchVersion) {
allowed[allowed.indexOf(BranchVersion)] = branchVersion;
}
if (allowed.includes(version)) {
return version;
}
if ([Latest, Nightly, numericVersion, branchVersion].includes(version)) {
throw `Version "${version}" is not supported on ${getPlatform()}`;
}
throw `Version "${version}" is invalid`;
}
async function subprocess(command, options) {
Core.info("[command]" + command.join(" "));
const [file, ...args] = command;
try {
return await execFile(file, args, options);
} catch (error) {
Core.info(error.stdout);
Core.info("---");
throw error;
}
}
async function installCrystalForLinux({crystal, shards, arch = getArch(), path}) {
checkVersion(crystal, [Latest, Nightly, NumericVersion, BranchVersion]);
const filePatterns = {"x86_64": /-linux-x86_64\.tar\.gz$/, "x86": /-linux-i686\.tar\.gz$/};
checkArch(arch, Object.keys(filePatterns));
const packages = "libevent-dev libgmp-dev libpcre3-dev libssl-dev libxml2-dev libyaml-dev".split(" ");
if (crystal === Latest || crystal === Nightly || cmpTags(crystal, "1.8") >= 0) {
packages.push("libpcre2-dev");
}
const depsTask = installAptPackages(packages);
await installBinaryRelease({crystal, shards, filePattern: filePatterns[arch], path});
Core.info("Setting up environment for Crystal");
Core.addPath(Path.join(path, "bin"));
await FS.symlink("share/crystal/src", Path.join(path, "src"));
if (shards !== Any) {
try {
await FS.unlink(Path.join(path, "bin", "shards"));
} catch (e) {}
}
await depsTask;
}
async function installCrystalForMac({crystal, shards, arch = "x86_64", path}) {
checkVersion(crystal, [Latest, Nightly, NumericVersion, BranchVersion]);
if (crystal === Latest || crystal === Nightly || cmpTags(crystal, "1.2") >= 0) {
checkArch(arch, ["universal", "x86_64", "aarch64"]);
} else {
checkArch(arch, ["x86_64"]);
}
const filePattern = /-darwin-(universal|x86_64)\.tar\.gz$/;
await installBinaryRelease({crystal, shards, filePattern, path});
Core.info("Setting up environment for Crystal");
Core.addPath(Path.join(path, "embedded", "bin"));
Core.addPath(Path.join(path, "bin"));
if (shards !== Any) {
try {
await FS.unlink(Path.join(path, "embedded", "bin", "shards"));
} catch (e) {}
}
const {stdout} = await subprocess(["brew", "--prefix"]);
const homebrewPrefix = stdout.trim();
const globber = await Glob.create([
`${homebrewPrefix}/Cellar/openssl*/*/lib/pkgconfig`,
"/usr/local/opt/openssl/lib/pkgconfig",
].join("\n"));
let [pkgConfigPath] = await globber.glob();
if (process.env["PKG_CONFIG_PATH"]) {
pkgConfigPath += Path.delimiter + process.env["PKG_CONFIG_PATH"];
}
Core.exportVariable("PKG_CONFIG_PATH", pkgConfigPath);
}
async function installAptPackages(packages) {
Core.info("Installing package dependencies");
const command = [
"apt-get", "install", "-qy", "--no-install-recommends", "--no-upgrade", "--",
].concat(packages);
if (await IO.which("sudo")) {
command.unshift("sudo", "-n");
}
const {stdout} = await subprocess(command);
Core.startGroup("Finished installing package dependencies");
Core.info(stdout);
Core.endGroup();
}
async function installBinaryRelease({crystal, filePattern, path}) {
if (crystal === Nightly) {
await IO.mv(await downloadCrystalNightly(filePattern, "master"), path);
} else {
const version = BranchVersion.exec(crystal);
if (version) {
await IO.mv(await downloadCrystalNightly(filePattern, version[1]), path);
} else {
if (crystal === Latest) {
crystal = null;
}
await IO.mv(await downloadCrystalRelease(filePattern, crystal), path);
}
}
}
async function maybeInstallShards({shards, path, allowCache = true}, crystalPromise) {
const allowed = [Latest, Nightly, NumericVersion, Any, None];
let cached = false;
checkVersion(shards, allowed);
if (![Any, None].includes(shards)) {
cached = await installShards({shards, path, allowCache}, crystalPromise);
}
if (shards !== None) {
if (shards === Any) {
await crystalPromise;
}
let result = null;
try {
result = await subprocess(["shards", "--version"]);
} catch (error) {
if (!cached) {
throw error;
}
Core.warning(error);
Core.info("Will try to rebuild");
await crystalPromise;
await rebuildShards({path});
result = await subprocess(["shards", "--version"]);
}
const {stdout} = result;
const [ver] = stdout.match(/\d[^ ]+/);
if (shards === Any && ver) {
Core.setOutput("shards", "v" + ver);
}
Core.info(stdout);
}
}
async function installShards({shards, path}, crystalPromise) {
if (NumericVersion.test(shards)) {
shards = "v" + shards;
}
const ref = await findRef({name: "Shards", repo: RepoShards, version: shards});
Core.setOutput("shards", ref);
const cacheKey = `install-shards-v1-${ref}--${getArch()}-${getPlatform()}`;
let restored = null;
try {
Core.info(`Trying to restore cache: key '${cacheKey}'`);
restored = await Cache.restoreCache([path], cacheKey);
} catch (error) {
Core.warning(error.message);
}
if (!restored) {
Core.info(`Cache not found for key '${cacheKey}'`);
const fetchSrcTask = downloadSource({name: "Shards", repo: RepoShards, ref});
await IO.mv(await fetchSrcTask, path);
await crystalPromise;
await rebuildShards({path});
}
if (restored !== cacheKey) {
Core.info(`Saving cache: '${cacheKey}'`);
try {
await Cache.saveCache([path], cacheKey);
} catch (error) {
Core.warning(error.message);
}
}
Core.info("Setting up environment for Shards");
Core.addPath(Path.join(path, "bin"));
return !!restored;
}
async function rebuildShards({path}) {
Core.info("Building Shards");
await subprocess(["make", "clean"], {cwd: path});
await subprocess(["make"], {cwd: path});
Core.startGroup("Finished building Shards");
Core.endGroup();
}
const RepoCrystal = {owner: "crystal-lang", repo: "crystal"};
const RepoShards = {owner: "crystal-lang", repo: "shards"};
const CircleApiBase = "https://circleci.com/api/v1.1/project/github/crystal-lang/crystal";
async function findRelease({name, repo, tag}) {
if (!(/^\d+\.\d+\.\d\w*$/.test(tag))) {
tag = await getLatestTag({repo, prefix: tag});
}
Core.info(`Getting ${name} release (${tag})`);
const releasesResp = await github.rest.repos.getReleaseByTag({...repo, tag});
const release = releasesResp.data;
Core.info(`Found ${name} release ${release["html_url"]}`);
return release;
}
async function getLatestTag({repo, prefix}) {
Core.info(`Looking for ${repo.owner}/${repo.owner} release (${prefix || "latest"})`);
const pages = github.repos.listReleases.endpoint.merge({
...repo, "per_page": 50,
});
const tags = [];
let assurance = 25;
for await (const item of getItemsFromPages(pages)) {
const tag = item["tag_name"];
if (!prefix || tag === prefix || tag.startsWith(prefix + ".")) {
tags.push(tag);
}
if (tags.length) {
if (--assurance <= 0) {
break;
}
}
}
if (tags.length === 0) {
const error = `The repository "${repo.owner}/${repo.repo}" has no releases matching "${prefix}.*"`;
throw error;
}
tags.sort(cmpTags);
tags.reverse();
Core.debug(`Considered tags ${tags.join("|")}`);
return tags[0];
}
async function findLatestCommit({name, repo, branch = "master"}) {
Core.info(`Looking for latest ${name} commit`);
const commitsResp = await github.rest.repos.getCommit({
...repo, "ref": branch,
});
const commit = commitsResp.data;
Core.info(`Found ${name} commit ${commit["html_url"]}`);
return commit["sha"];
}
async function downloadCrystalRelease(filePattern, version = null) {
const release = await findRelease({name: "Crystal", repo: RepoCrystal, tag: version});
Core.setOutput("crystal", release["tag_name"]);
const asset = release["assets"].find((a) => filePattern.test(a["name"]));
Core.info(`Downloading Crystal build from ${asset["url"]}`);
const resp = await github.request({
url: asset["url"],
headers: {"accept": "application/octet-stream"},
request: {fetch: fetchWithManualRedirect},
});
const url = resp.headers["location"];
const downloadedPath = await ToolCache.downloadTool(url);
Core.info("Extracting Crystal build");
const dl = (asset["name"].endsWith(".zip") ? ToolCache.extractZip : ToolCache.extractTar);
const extractedPath = await dl(downloadedPath);
return onlySubdir(extractedPath);
}
async function findRef({name, repo, version}) {
const v = version.replace(/^v/, "");
if (version === Nightly) {
return findLatestCommit({name, repo});
} else if (version === Latest) {
const release = await findRelease({name, repo});
return release["tag_name"];
} else if (NumericVersion.test(v) && !(/^\d+\.\d+\.\d\w*$/.test(v))) {
return getLatestTag({repo, prefix: version});
}
return version;
}
async function downloadSource({name, repo, ref}) {
Core.info(`Downloading ${name} source for ${ref}`);
const resp = await github.rest.repos.downloadZipballArchive({
...repo, ref,
request: {fetch: fetchWithManualRedirect},
});
const url = resp.headers["location"];
const downloadedPath = await ToolCache.downloadTool(url);
Core.info(`Extracting ${name} source`);
return onlySubdir(await ToolCache.extractZip(downloadedPath));
}
async function downloadCrystalNightly(filePattern, branch) {
Core.info(`Looking for latest Crystal build of branch '${branch}'`);
let build;
for (let offset = 0; ;) {
const req = `/tree/${branch}?filter=successful&shallow=true&limit=100&offset=${offset}`;
const resp = await fetch(CircleApiBase + req);
const builds = await resp.json();
build = builds.find((b) => b["workflows"]["job_name"] === "dist_artifacts");
if (build) {
break;
}
offset += builds.length;
if (offset >= 1000 || builds.length === 0) {
throw `Could not find a matching build for branch '${branch}'`;
}
}
Core.info(`Found Crystal build ${build["build_url"]}`);
Core.setOutput("crystal", build["vcs_revision"]);
const req = `/${build["build_num"]}/artifacts`;
const resp = await fetch(CircleApiBase + req);
const artifacts = await resp.json();
const artifact = artifacts.find((a) => filePattern.test(a["path"]));
if (artifact === undefined) {
throw `Could not find build artifacts for build ${build["build_num"]}`;
}
Core.info(`Downloading Crystal build from ${artifact["url"]}`);
const downloadedPath = await ToolCache.downloadTool(artifact["url"]);
Core.info("Extracting Crystal build");
const extractedPath = await ToolCache.extractTar(downloadedPath);
return onlySubdir(extractedPath);
}
async function installCrystalForWindows({crystal, shards, arch = "x86_64", path}) {
checkVersion(crystal, [Latest, Nightly, NumericVersion, BranchVersion], "1.3");
checkArch(arch, ["x86_64"]);
if (crystal === Nightly) {
await IO.mv(await downloadCrystalNightlyForWindows("master"), path);
} else {
const version = BranchVersion.exec(crystal);
if (version) {
await IO.mv(await downloadCrystalNightlyForWindows(version[1]), path);
} else {
const filePattern = /-windows-x86_64-msvc(-unsupported)?\.zip$/;
await installBinaryRelease({crystal, shards, filePattern, path});
}
}
Core.info("Setting up environment for Crystal");
Core.addPath(path);
if (shards !== Any) {
try {
await FS.unlink(Path.join(path, "shards.exe"));
} catch (e) {}
}
}
async function downloadCrystalNightlyForWindows(branch) {
Core.info(`Looking for latest Crystal build of branch '${branch}'`);
const runsResp = await github.rest.actions.listWorkflowRuns({
...RepoCrystal, "workflow_id": "win.yml", "branch": branch,
"event": "push", "status": "success", "per_page": 1,
});
const [workflowRun] = runsResp.data["workflow_runs"];
const {"head_sha": ref, "id": runId} = workflowRun;
Core.info(`Found Crystal release ${workflowRun["html_url"]}`);
Core.setOutput("crystal", ref);
const artifactsResp = await github.rest.actions.listWorkflowRunArtifacts({
...RepoCrystal, "run_id": runId,
});
const artifact = artifactsResp.data["artifacts"].find((x) => x.name === "crystal");
Core.info("Downloading Crystal build");
const resp = await github.rest.actions.downloadArtifact({
...RepoCrystal, "artifact_id": artifact.id, "archive_format": "zip",
request: {fetch: fetchWithManualRedirect},
});
const url = resp.headers["location"];
const downloadedPath = await ToolCache.downloadTool(url);
Core.info("Extracting Crystal build");
return ToolCache.extractZip(downloadedPath);
}
const github = new Octokit({auth: Core.getInput("token") || null});
function fetchWithManualRedirect(url, options) {
return fetch(url, {...options, redirect: "manual"});
}
async function* getItemsFromPages(pages) {
for await (const page of github.paginate.iterator(pages)) {
for (const item of page.data) {
yield item;
}
}
}
async function onlySubdir(path) {
const subDirs = await FS.readdir(path);
if (subDirs.length === 1) {
path = Path.join(path, subDirs[0]);
}
return path;
}
run();