forked from microsoft/azure-pipelines-task-lib
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmake-util.js
176 lines (148 loc) · 4.2 KB
/
make-util.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
var admZip = require('adm-zip');
var child_process = require('child_process');
var fs = require('fs');
var path = require('path');
var process = require('process');
var shell = require('shelljs');
var syncRequest = require('sync-request');
// global paths
var downloadPath = path.join(__dirname, '_download');
//------------------------------------------------------------------------------
// shell functions
//------------------------------------------------------------------------------
var shellAssert = function () {
var errMsg = shell.error();
if (errMsg) {
throw new Error(errMsg);
}
}
var cd = function (dir) {
shell.cd(dir);
shellAssert();
}
exports.cd = cd;
var cp = function (options, source, dest) {
if (dest) {
shell.cp(options, source, dest);
}
else {
shell.cp(options, source);
}
shellAssert();
}
exports.cp = cp;
var mkdir = function (options, target) {
if (target) {
shell.mkdir(options, target);
}
else {
shell.mkdir(options);
}
shellAssert();
}
exports.mkdir = mkdir;
var rm = function (options, target) {
if (target) {
shell.rm(options, target);
}
else {
shell.rm(options);
}
shellAssert();
}
exports.rm = rm;
var test = function (options, p) {
var result = shell.test(options, p);
shellAssert();
return result;
}
exports.test = test;
//------------------------------------------------------------------------------
var run = function (cl, stdio) {
console.log();
console.log('> ' + cl);
var options = {
stdio: (stdio || 'inherit')
};
var rc = 0;
var output;
try {
output = child_process.execSync(cl, options);
}
catch (err) {
process.exit(1);
}
return (output || '').toString().trim();
}
exports.run = run;
var ensureTool = function (name, versionArgs, validate) {
console.log(name + ' tool:');
var toolPath = which(name);
if (!toolPath) {
throw new Error(name + ' not found. might need to run npm install');
}
if (versionArgs) {
var result = exec(name + ' ' + versionArgs);
if (typeof validate == 'string') {
if (result.output.trim() != validate) {
throw new Error('expected version: ' + validate);
}
}
else {
validate(result.output.trim());
}
}
console.log(toolPath + '');
}
exports.ensureTool = ensureTool;
var downloadFile = function (url) {
// validate parameters
if (!url) {
throw new Error('Parameter "url" must be set.');
}
// skip if already downloaded
var scrubbedUrl = url.replace(/[/\:?]/g, '_');
var targetPath = path.join(downloadPath, 'file', scrubbedUrl);
var marker = targetPath + '.completed';
if (!test('-f', marker)) {
console.log('Downloading file: ' + url);
// delete any previous partial attempt
if (test('-f', targetPath)) {
rm('-f', targetPath);
}
// download the file
mkdir('-p', path.join(downloadPath, 'file'));
var result = syncRequest('GET', url);
fs.writeFileSync(targetPath, result.getBody());
// write the completed marker
fs.writeFileSync(marker, '');
}
return targetPath;
}
exports.downloadFile = downloadFile;
var downloadArchive = function (url) {
if (!url) {
throw new Error('Parameter "url" must be set.');
}
// skip if already downloaded and extracted
var scrubbedUrl = url.replace(/[\/\\:?]/g, '_');
var targetPath = path.join(downloadPath, 'archive', scrubbedUrl);
var marker = targetPath + '.completed';
if (!test('-f', marker)) {
// download the archive
var archivePath = downloadFile(url);
console.log('Extracting archive: ' + url);
// delete any previously attempted extraction directory
if (test('-d', targetPath)) {
rm('-rf', targetPath);
}
// extract
mkdir('-p', targetPath);
var zip = new admZip(archivePath);
zip.extractAllTo(targetPath);
// write the completed marker
fs.writeFileSync(marker, '');
}
return targetPath;
}
exports.downloadArchive = downloadArchive;