-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathinstaller.js
506 lines (506 loc) · 18.1 KB
/
installer.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
500
501
502
503
504
505
506
"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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
let tempDirectory = process.env["RUNNER_TEMP"] || "";
const core = __importStar(require("@actions/core"));
const exec = __importStar(require("@actions/exec"));
const tc = __importStar(require("@actions/tool-cache"));
const io = __importStar(require("@actions/io"));
const util = __importStar(require("util"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const restm = __importStar(require("typed-rest-client/RestClient"));
const semver = __importStar(require("semver"));
const linux_os_info_1 = __importDefault(require("linux-os-info"));
const IS_WINDOWS = process.platform === "win32";
const IS_MAC = process.platform === "darwin";
if (!tempDirectory) {
let baseLocation;
if (IS_WINDOWS) {
// On windows use the USERPROFILE env variable
baseLocation = process.env["USERPROFILE"] || "C:\\";
}
else {
if (IS_MAC) {
baseLocation = "/Users";
}
else {
baseLocation = "/home";
}
}
tempDirectory = path.join(baseLocation, "actions", "temp");
}
function getR(version) {
return __awaiter(this, void 0, void 0, function* () {
const selected = yield determineVersion(version);
if (selected) {
version = selected;
}
let rtoolsVersion = core.getInput("rtools-version") || (version.charAt(0) == "3" ? "35" : "40");
let toolPath = tc.find("R", version);
if (toolPath) {
core.debug(`Tool found in cache ${toolPath}`);
}
else {
try {
yield acquireR(version, rtoolsVersion);
}
catch (error) {
core.debug(error);
throw `Failed to get R ${version}: ${error}`;
}
}
setREnvironmentVariables();
setupRLibrary();
});
}
exports.getR = getR;
function acquireR(version, rtoolsVersion) {
return __awaiter(this, void 0, void 0, function* () {
if (!core.getInput("install-r")) {
return;
}
try {
if (IS_WINDOWS) {
yield Promise.all([
acquireRWindows(version),
acquireRtools(rtoolsVersion),
acquireQpdfWindows()
]);
}
else if (IS_MAC) {
yield Promise.all([
acquireFortranMacOS(),
acquireUtilsMacOS(),
acquireRMacOS(version)
]);
if (core.getInput("remove-openmp-macos")) {
yield removeOpenmpFlags();
}
}
else {
yield acquireRUbuntu(version);
}
}
catch (error) {
core.debug(error);
throw `Failed to get R ${version}: ${error}`;
}
});
}
function acquireFortranMacOS() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield exec.exec("brew", ["cask", "install", "gfortran"]);
}
catch (error) {
core.debug(error);
throw `Failed to install gfortan: ${error}`;
}
});
}
function acquireUtilsMacOS() {
return __awaiter(this, void 0, void 0, function* () {
// qpdf is needed by `--as-cran`
try {
yield exec.exec("brew", ["install", "qpdf", "pkgconfig", "checkbashisms"]);
}
catch (error) {
core.debug(error);
throw `Failed to install qpdf: ${error}`;
}
});
}
function removeOpenmpFlags() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield exec.exec("sed", [
"-i",
".bak",
"-e",
"s/-fopenmp//g",
"/Library/Frameworks/R.framework/Resources/etc/Makeconf"
]);
}
catch (error) {
core.debug(error);
throw `Failed to remove OpenMP flags: ${error}`;
}
});
}
function acquireRUbuntu(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 = getFileNameUbuntu(version);
let downloadUrl = getDownloadUrlUbuntu(fileName);
let downloadPath = null;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
yield io.mv(downloadPath, path.join(tempDirectory, fileName));
}
catch (error) {
core.debug(error);
throw `Failed to download version ${version}: ${error}`;
}
//
// Install
//
let extPath = tempDirectory;
if (!extPath) {
throw new Error("Temp directory not set");
}
try {
// Important backports needed for CRAN packages, including libgit2
yield exec.exec("sudo DEBIAN_FRONTEND=noninteractive add-apt-repository -y ppa:cran/travis");
yield exec.exec("sudo DEBIAN_FRONTEND=noninteractive apt-get update -qq");
// install gdbi-core and also qpdf, which is used by `--as-cran`
yield exec.exec("sudo DEBIAN_FRONTEND=noninteractive apt-get install gdebi-core qpdf devscripts");
yield exec.exec("sudo gdebi", [
"--non-interactive",
path.join(tempDirectory, fileName)
]);
}
catch (error) {
core.debug(error);
throw `Failed to install R: ${error}`;
}
//
// Add symlinks to the installed R to the path
//
//
try {
yield exec.exec("sudo ln", [
"-s",
path.join("/opt", "R", version, "bin", "R"),
"/usr/local/bin/R"
]);
yield exec.exec("sudo ln", [
"-s",
path.join("/opt", "R", version, "bin", "Rscript"),
"/usr/local/bin/Rscript"
]);
}
catch (error) {
core.debug(error);
throw `Failed to setup symlinks to R: ${error}`;
}
return "/usr/local/bin";
});
}
function acquireRMacOS(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 = getFileNameMacOS(version);
let downloadUrl = getDownloadUrlMacOS(version);
let downloadPath = null;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
yield io.mv(downloadPath, path.join(tempDirectory, fileName));
}
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");
}
try {
yield exec.exec("sudo", [
"installer",
"-pkg",
path.join(tempDirectory, fileName),
"-target",
"/"
]);
}
catch (error) {
core.debug(error);
throw `Failed to install R: ${error}`;
}
return "/";
});
}
function acquireRWindows(version) {
return __awaiter(this, void 0, void 0, function* () {
let fileName = getFileNameWindows(version);
let downloadUrl = yield getDownloadUrlWindows(version);
let downloadPath = null;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
yield io.mv(downloadPath, path.join(tempDirectory, fileName));
}
catch (error) {
core.debug(error);
throw `Failed to download version ${version}: ${error}`;
}
//
// Install
//
let extPath = tempDirectory;
if (!extPath) {
throw new Error("Temp directory not set");
}
try {
yield exec.exec(path.join(tempDirectory, fileName), [
"/VERYSILENT",
"/SUPPRESSMSGBOXES",
"/DIR=C:\\R"
]);
}
catch (error) {
core.debug(error);
throw `Failed to install R: ${error}`;
}
core.addPath(`C:\\R\\bin`);
return "";
});
}
function acquireRtools(version) {
return __awaiter(this, void 0, void 0, function* () {
const rtools4 = version.charAt(0) == "4";
let fileName = util.format(rtools4 ? "rtools%s-x86_64.exe" : "Rtools%s.exe", version);
// If Rtools is already installed just return, as there is a message box
// which hangs the build otherwise.
if ((!rtools4 && fs.existsSync("C:\\Rtools")) ||
(rtools4 && fs.existsSync("C:\\rtools40"))) {
core.debug("Skipping Rtools installation as a suitable Rtools is already installed");
}
else {
let downloadUrl = util.format("http://cloud.r-project.org/bin/windows/Rtools/%s", fileName);
console.log(`Downloading ${downloadUrl}...`);
let downloadPath = null;
try {
downloadPath = yield tc.downloadTool(downloadUrl);
yield io.mv(downloadPath, path.join(tempDirectory, fileName));
}
catch (error) {
core.debug(error);
throw `Failed to download version ${version}: ${error}`;
}
try {
yield exec.exec(path.join(tempDirectory, fileName), [
"/VERYSILENT",
"/SUPPRESSMSGBOXES"
]);
}
catch (error) {
core.debug(error);
throw `Failed to install Rtools: ${error}`;
}
}
if (rtools4) {
core.addPath(`C:\\rtools40\\usr\\bin`);
core.addPath(`C:\\rtools40\\mingw64\\bin`);
}
else {
core.addPath(`C:\\Rtools\\bin`);
core.addPath(`C:\\Rtools\\mingw_64\\bin`);
}
});
}
function acquireQpdfWindows() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield exec.exec("choco", ["install", "qpdf", "--no-progress"]);
}
catch (error) {
core.debug(error);
throw `Failed to install qpdf: ${error}`;
}
});
}
function setupRLibrary() {
return __awaiter(this, void 0, void 0, function* () {
let profilePath;
if (IS_WINDOWS) {
profilePath = path.join(process.env["USERPROFILE"] || "C:\\", "Documents", ".Rprofile");
}
else {
profilePath = path.join(process.env["HOME"] || "/Users", ".Rprofile");
}
core.debug("R profile is at " + profilePath);
let rspm = process.env["RSPM"] ? `'${process.env["RSPM"]}'` : "NULL";
let cran = `'${process.env["CRAN"] || "https://cloud.r-project.org"}'`;
let user_agent = core.getInput("http-user-agent") == "default" ||
core.getInput("http-user-agent") == ""
? 'sprintf("R/%s R (%s) on GitHub Actions", getRversion(), paste(getRversion(), R.version$platform, R.version$arch, R.version$os))'
: `"${core.getInput("http-user-agent")}"`;
yield fs.promises.writeFile(profilePath, `options(\
repos = c(\
RSPM = ${rspm},\
CRAN = ${cran}\
),\
crayon.enabled = ${core.getInput("crayon.enabled")},\
Ncpus = ${core.getInput("Ncpus")},\
HTTPUserAgent = ${user_agent}\
)\n`);
// Make R_LIBS_USER
io.mkdirP(process.env["R_LIBS_USER"] || path.join(tempDirectory, "Library"));
});
}
function getFileNameMacOS(version) {
const filename = util.format("R-%s.pkg", version);
return filename;
}
function getDownloadUrlMacOS(version) {
if (version == "devel") {
return "https://mac.r-project.org/high-sierra/R-devel/R-devel.pkg";
}
const filename = getFileNameMacOS(version);
if (semver.eq(version, "3.2.5")) {
// 3.2.5 is 'special', it is actually 3.2.4-revised...
return "https://cloud.r-project.org/bin/macosx/old/R-3.2.4-revised.pkg";
}
if (semver.lt(version, "3.4.0")) {
// older versions are in /old
return util.format("https://cloud.r-project.org/bin/macosx/old/%s", filename);
}
return util.format("https://cloud.r-project.org/bin/macosx/%s", filename);
}
function getFileNameUbuntu(version) {
const filename = util.format("r-%s_1_amd64.deb", version);
return filename;
}
function getDownloadUrlUbuntu(filename) {
try {
const info = linux_os_info_1.default({ mode: "sync" });
const versionStr = info.version_id.replace(/[.]/g, "");
return util.format("https://cdn.rstudio.com/r/ubuntu-%s/pkgs/%s", versionStr, filename);
}
catch (error) {
throw `Failed to get OS info: ${error}`;
}
}
function getFileNameWindows(version) {
const filename = util.format("R-%s-win.exe", version);
return filename;
}
function getDownloadUrlWindows(version) {
return __awaiter(this, void 0, void 0, function* () {
if (version == "devel") {
return "https://cloud.r-project.org/bin/windows/base/R-devel-win.exe";
}
const filename = getFileNameWindows(version);
const releaseVersion = yield getReleaseVersion("win");
if (version == releaseVersion) {
return util.format("https://cloud.r-project.org/bin/windows/base/%s", filename);
}
return util.format("https://cloud.r-project.org/bin/windows/base/old/%s/%s", version, filename);
});
}
function setREnvironmentVariables() {
core.exportVariable("R_LIBS_USER", path.join(tempDirectory, "Library"));
core.exportVariable("TZ", "UTC");
if (!process.env["NOT_CRAN"])
core.exportVariable("NOT_CRAN", "true");
}
function determineVersion(version) {
return __awaiter(this, void 0, void 0, function* () {
// There is no linux endpoint, so we just use the tarball one for linux.
version = version.toLowerCase();
if (version == "latest" || version == "release") {
let platform = IS_MAC ? "macos" : IS_WINDOWS ? "win" : "tarball";
return yield getReleaseVersion(platform);
}
if (version == "oldrel") {
return yield getOldrelVersion();
}
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);
});
}
// 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");
}
if (versionPart[2] == null) {
//append patch version if not available
return version.concat(".0");
}
return version;
}
function getReleaseVersion(platform) {
return __awaiter(this, void 0, void 0, function* () {
let rest = new restm.RestClient("setup-r");
let tags = (yield rest.get(util.format("https://rversions.r-pkg.org/r-release-%s", platform))).result || [];
return tags[0].version;
});
}
function getOldrelVersion() {
return __awaiter(this, void 0, void 0, function* () {
let rest = new restm.RestClient("setup-r");
let tags = (yield rest.get("https://rversions.r-pkg.org/r-oldrel")).result ||
[];
return tags[0].version;
});
}
function getAvailableVersions() {
return __awaiter(this, void 0, void 0, function* () {
let rest = new restm.RestClient("setup-r");
let tags = (yield rest.get("https://rversions.r-pkg.org/r-versions"))
.result || [];
return tags.map(tag => tag.version);
});
}
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));
});
}
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];
});
}