-
Notifications
You must be signed in to change notification settings - Fork 59
/
tool.ts
556 lines (476 loc) · 18.5 KB
/
tool.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
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import * as httpm from 'typed-rest-client/HttpClient';
import * as ifm from 'typed-rest-client/Interfaces';
import * as path from 'path';
import * as os from 'os';
import * as process from 'process';
import * as fs from 'fs';
import * as semver from 'semver';
import * as tl from 'azure-pipelines-task-lib/task';
import * as trm from 'azure-pipelines-task-lib/toolrunner';
const cmp = require('semver-compare');
const uuidV4 = require('uuid/v4');
declare let rest;
let pkg = require(path.join(__dirname, 'package.json'));
let userAgent = 'vsts-task-installer/' + pkg.version;
let requestOptions = {
// ignoreSslError: true,
proxy: tl.getHttpProxyConfiguration(),
cert: tl.getHttpCertConfiguration(),
allowRedirects: true,
allowRetries: true,
maxRetries: 2
} as ifm.IRequestOptions;
tl.setResourcePath(path.join(__dirname, 'lib.json'));
export function debug(message: string): void {
tl.debug(message);
}
export function prependPath(toolPath: string) {
tl.assertAgent('2.115.0');
if (!toolPath) {
throw new Error('Parameter toolPath must not be null or empty');
}
else if (!tl.exist(toolPath) || !tl.stats(toolPath).isDirectory()) {
throw new Error('Directory does not exist: ' + toolPath);
}
// todo: add a test for path
console.log(tl.loc('TOOL_LIB_PrependPath', toolPath));
let newPath: string = toolPath + path.delimiter + process.env['PATH'];
tl.debug('new Path: ' + newPath);
process.env['PATH'] = newPath;
// instruct the agent to set this path on future tasks
console.log('##vso[task.prependpath]' + toolPath);
}
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
//-----------------------------
// Version Functions
//-----------------------------
/**
* Checks if a version spec is an explicit version (e.g. 1.0.1 or v1.0.1)
* As opposed to a version spec like 1.x
*
* @param versionSpec
*/
export function isExplicitVersion(versionSpec: string) {
let c = semver.clean(versionSpec);
tl.debug('isExplicit: ' + c);
let valid = semver.valid(c) != null;
tl.debug('explicit? ' + valid);
return valid;
}
/**
* Returns cleaned (removed leading/trailing whitespace, remove '=v' prefix)
* and parsed version, or null if version is invalid.
*/
export function cleanVersion(version: string) {
tl.debug('cleaning: ' + version);
return semver.clean(version);
}
/**
* evaluates a list of versions and returns the latest version matching the version spec
*
* @param versions an array of versions to evaluate
* @param versionSpec a version spec (e.g. 1.x)
*/
export function evaluateVersions(versions: string[], versionSpec: string): string {
let version: string;
tl.debug('evaluating ' + versions.length + ' versions');
versions = versions.sort(cmp);
for (let i = versions.length - 1; i >= 0; i--) {
let potential: string = versions[i];
let satisfied: boolean = semver.satisfies(potential, versionSpec);
if (satisfied) {
version = potential;
break;
}
}
if (version) {
tl.debug('matched: ' + version);
}
else {
tl.debug('match not found');
}
return version;
}
//-----------------------------
// Local Tool Cache Functions
//-----------------------------
/**
* finds the path to a tool in the local installed tool cache
*
* @param toolName name of the tool
* @param versionSpec version of the tool
* @param arch optional arch. defaults to arch of computer
*/
export function findLocalTool(toolName: string, versionSpec: string, arch?: string): string {
if (!toolName) {
throw new Error('toolName parameter is required');
}
if (!versionSpec) {
throw new Error('versionSpec parameter is required');
}
arch = arch || os.arch();
// attempt to resolve an explicit version
if (!isExplicitVersion(versionSpec)) {
let localVersions: string[] = findLocalToolVersions(toolName, arch);
let match = evaluateVersions(localVersions, versionSpec);
versionSpec = match;
}
// check for the explicit version in the cache
let toolPath: string;
if (versionSpec) {
versionSpec = semver.clean(versionSpec);
let cacheRoot = _getCacheRoot();
let cachePath = path.join(cacheRoot, toolName, versionSpec, arch);
tl.debug('checking cache: ' + cachePath);
if (tl.exist(cachePath) && tl.exist(`${cachePath}.complete`)) {
console.log(tl.loc('TOOL_LIB_FoundInCache', toolName, versionSpec, arch));
toolPath = cachePath;
}
else {
tl.debug('not found');
}
}
return toolPath;
}
/**
* Retrieves the versions of a tool that is intalled in the local tool cache
*
* @param toolName name of the tool
* @param arch optional arch. defaults to arch of computer
*/
export function findLocalToolVersions(toolName: string, arch?: string) {
let versions: string[] = [];
arch = arch || os.arch();
let toolPath = path.join(_getCacheRoot(), toolName);
if (tl.exist(toolPath)) {
let children: string[] = tl.ls('', [toolPath]);
children.forEach((child: string) => {
if (isExplicitVersion(child)) {
let fullPath = path.join(toolPath, child, arch);
if (tl.exist(fullPath) && tl.exist(`${fullPath}.complete`)) {
versions.push(child);
}
}
});
}
return versions;
}
//---------------------
// Download Functions
//---------------------
//
// TODO: keep extension intact
//
/**
* Download a tool from an url and stream it into a file
*
* @param url url of tool to download
* @param fileName optional fileName. Should typically not use (will be a guid for reliability). Can pass fileName with an absolute path.
* @param handlers optional handlers array. Auth handlers to pass to the HttpClient for the tool download.
*/
export async function downloadTool(url: string, fileName?: string, handlers?: ifm.IRequestHandler[]): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
try {
handlers = handlers || null;
let http: httpm.HttpClient = new httpm.HttpClient(userAgent, handlers, requestOptions);
tl.debug(fileName);
fileName = fileName || uuidV4();
// check if it's an absolute path already
var destPath: string;
if(path.isAbsolute(fileName))
{
destPath = fileName;
}
else
{
destPath = path.join(_getAgentTemp(), fileName);
}
// make sure that the folder exists
tl.mkdirP(path.dirname(destPath));
console.log(tl.loc('TOOL_LIB_Downloading', url));
tl.debug('destination ' + destPath);
if (fs.existsSync(destPath)) {
throw new Error("Destination file path already exists");
}
tl.debug('downloading');
let response: httpm.HttpClientResponse = await http.get(url);
if (response.message.statusCode != 200) {
let err: Error = new Error('Unexpected HTTP response: ' + response.message.statusCode);
err['httpStatusCode'] = response.message.statusCode;
tl.debug(`Failed to download "${fileName}" from "${url}". Code(${response.message.statusCode}) Message(${response.message.statusMessage})`);
throw err;
}
tl.debug('creating stream');
let file: NodeJS.WritableStream = fs.createWriteStream(destPath);
file.on('open', async (fd) => {
try {
let stream = response.message.pipe(file);
stream.on('close', () => {
tl.debug('download complete');
resolve(destPath);
});
}
catch (err) {
reject(err);
}
});
file.on('error', (err) => {
file.end();
reject(err);
})
}
catch (error) {
reject(error);
}
});
}
//---------------------
// Install Functions
//---------------------
function _createToolPath(tool: string, version: string, arch?: string): string {
// todo: add test for clean
let folderPath = path.join(_getCacheRoot(), tool, semver.clean(version), arch);
tl.debug('destination ' + folderPath);
let markerPath: string = `${folderPath}.complete`;
tl.rmRF(folderPath);
tl.rmRF(markerPath);
tl.mkdirP(folderPath);
return folderPath;
}
function _completeToolPath(tool: string, version: string, arch?: string): void {
let folderPath = path.join(_getCacheRoot(), tool, semver.clean(version), arch);
let markerPath: string = `${folderPath}.complete`;
tl.writeFile(markerPath, '');
tl.debug('finished caching tool');
}
/**
* Caches a directory and installs it into the tool cacheDir
*
* @param sourceDir the directory to cache into tools
* @param tool tool name
* @param version version of the tool. semver format
* @param arch architecture of the tool. Optional. Defaults to machine architecture
*/
export async function cacheDir(sourceDir: string,
tool: string,
version: string,
arch?: string): Promise<string> {
version = semver.clean(version);
arch = arch || os.arch();
console.log(tl.loc('TOOL_LIB_CachingTool', tool, version, arch));
tl.debug('source dir: ' + sourceDir);
if (!tl.stats(sourceDir).isDirectory()) {
throw new Error('sourceDir is not a directory');
}
// create the tool dir
let destPath: string = _createToolPath(tool, version, arch);
// copy each child item. do not move. move can fail on Windows
// due to anti-virus software having an open handle on a file.
for (let itemName of fs.readdirSync(sourceDir)) {
let s = path.join(sourceDir, itemName);
tl.cp(s, destPath + '/', '-r');
}
// write .complete
_completeToolPath(tool, version, arch);
return destPath;
}
/**
* Caches a downloaded file (GUID) and installs it
* into the tool cache with a given targetName
*
* @param sourceFile the file to cache into tools. Typically a result of downloadTool which is a guid.
* @param targetFile the name of the file name in the tools directory
* @param tool tool name
* @param version version of the tool. semver format
* @param arch architecture of the tool. Optional. Defaults to machine architecture
*/
export async function cacheFile(sourceFile: string,
targetFile: string,
tool: string,
version: string,
arch?: string): Promise<string> {
version = semver.clean(version);
arch = arch || os.arch();
console.log(tl.loc('TOOL_LIB_CachingTool', tool, version, arch));
tl.debug('source file:' + sourceFile);
if (!tl.stats(sourceFile).isFile()) {
throw new Error('sourceFile is not a file');
}
// create the tool dir
let destFolder: string = _createToolPath(tool, version, arch);
// copy instead of move. move can fail on Windows due to
// anti-virus software having an open handle on a file.
let destPath: string = path.join(destFolder, targetFile);
tl.debug('destination file' + destPath);
tl.cp(sourceFile, destPath);
// write .complete
_completeToolPath(tool, version, arch);
return destFolder;
}
//---------------------
// Extract Functions
//---------------------
/**
* Extract a .7z file
*
* @param file path to the .7z file
* @param dest destination directory. Optional.
* @param _7zPath path to 7zr.exe. Optional, for long path support. Most .7z archives do not have this
* problem. If your .7z archive contains very long paths, you can pass the path to 7zr.exe which will
* gracefully handle long paths. By default 7zdec.exe is used because it is a very small program and is
* bundled with the tool lib. However it does not support long paths. 7zr.exe is the reduced command line
* interface, it is smaller than the full command line interface, and it does support long paths. At the
* time of this writing, it is freely available from the LZMA SDK that is available on the 7zip website.
* Be sure to check the current license agreement. If 7zr.exe is bundled with your task, then the path
* to 7zr.exe can be pass to this function.
* @returns path to the destination directory
*/
export async function extract7z(file: string, dest?: string, _7zPath?: string): Promise<string> {
if (process.platform != 'win32') {
throw new Error('extract7z() not supported on current OS');
}
if (!file) {
throw new Error("parameter 'file' is required");
}
console.log(tl.loc('TOOL_LIB_ExtractingArchive'));
dest = _createExtractFolder(dest);
let originalCwd = process.cwd();
try {
process.chdir(dest);
if (_7zPath) {
// extract
let _7z: trm.ToolRunner = tl.tool(_7zPath)
.arg('x') // eXtract files with full paths
.arg('-bb1') // -bb[0-3] : set output log level
.arg('-bd') // disable progress indicator
.arg('-sccUTF-8') // set charset for for console input/output
.arg(file);
await _7z.exec();
}
else {
// extract
let escapedScript = path.join(__dirname, 'Invoke-7zdec.ps1').replace(/'/g, "''").replace(/"|\n|\r/g, ''); // double-up single quotes, remove double quotes and newlines
let escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, '');
let escapedTarget = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '');
let command: string = `& '${escapedScript}' -Source '${escapedFile}' -Target '${escapedTarget}'`
let powershellPath = tl.which('powershell', true);
let powershell: trm.ToolRunner = tl.tool(powershellPath)
.line('-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command')
.arg(command);
powershell.on('stdout', (buffer: Buffer) => {
process.stdout.write(buffer);
});
powershell.on('stderr', (buffer: Buffer) => {
process.stderr.write(buffer);
});
await powershell.exec(<trm.IExecOptions>{ silent: true });
}
}
finally {
process.chdir(originalCwd);
}
return dest;
}
/**
* installs a tool from a tar by extracting the tar and installing it into the tool cache
*
* @param file file path of the tar
* @param tool name of tool in the tool cache
* @param version version of the tool
* @param arch arch of the tool. optional. defaults to the arch of the machine
* @param options IExtractOptions
* @param destination destination directory. optional.
*/
export async function extractTar(file: string, destination?: string): Promise<string> {
// mkdir -p node/4.7.0/x64
// tar xzC ./node/4.7.0/x64 -f node-v4.7.0-darwin-x64.tar.gz --strip-components 1
console.log(tl.loc('TOOL_LIB_ExtractingArchive'));
let dest = _createExtractFolder(destination);
let tr: trm.ToolRunner = tl.tool('tar');
tr.arg(['xC', dest, '-f', file]);
await tr.exec();
return dest;
}
export async function extractZip(file: string, destination?: string): Promise<string> {
if (!file) {
throw new Error("parameter 'file' is required");
}
console.log(tl.loc('TOOL_LIB_ExtractingArchive'));
let dest = _createExtractFolder(destination);
if (process.platform == 'win32') {
// build the powershell command
let escapedFile = file.replace(/'/g, "''").replace(/"|\n|\r/g, ''); // double-up single quotes, remove double quotes and newlines
let escapedDest = dest.replace(/'/g, "''").replace(/"|\n|\r/g, '');
let command: string = `$ErrorActionPreference = 'Stop' ; try { Add-Type -AssemblyName System.IO.Compression.FileSystem } catch { } ; [System.IO.Compression.ZipFile]::ExtractToDirectory('${escapedFile}', '${escapedDest}')`;
// change the console output code page to UTF-8.
// TODO: FIX WHICH: let chcpPath = tl.which('chcp.com', true);
let chcpPath = path.join(process.env.windir, "system32", "chcp.com");
await tl.exec(chcpPath, '65001');
// run powershell
let powershell: trm.ToolRunner = tl.tool('powershell')
.line('-NoLogo -Sta -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command')
.arg(command);
await powershell.exec();
}
else {
let unzip: trm.ToolRunner = tl.tool('unzip')
.arg(file);
await unzip.exec(<trm.IExecOptions>{ cwd: dest });
}
return dest;
}
function _createExtractFolder(dest?: string): string {
if (!dest) {
// create a temp dir
dest = path.join(_getAgentTemp(), uuidV4());
}
tl.mkdirP(dest);
return dest;
}
//---------------------
// Query Functions
//---------------------
// default input will be >= LTS version. drop label different than value.
// v4 (LTS) would have a value of 4.x
// option to always download? (not cache), TTL?
/**
* Scrape a web page for versions by regex
*
* @param url url to scrape
* @param regex regex to use for version matches
* @param handlers optional handlers array. Auth handlers to pass to the HttpClient for the tool download.
*/
export async function scrape(url: string, regex: RegExp, handlers?: ifm.IRequestHandler[]): Promise<string[]> {
handlers = handlers || null;
let http: httpm.HttpClient = new httpm.HttpClient(userAgent, handlers, requestOptions);
let output: string = await (await http.get(url)).readBody();
let matches = output.match(regex);
let seen: any = {};
let versions: string[] = [];
for (let i = 0; i < matches.length; i++) {
let ver: string = semver.clean(matches[i]);
if (!seen.hasOwnProperty(ver)) {
seen[ver] = true;
versions.push(ver);
}
}
return versions;
}
function _getCacheRoot(): string {
tl.assertAgent('2.115.0');
let cacheRoot = tl.getVariable('Agent.ToolsDirectory');
if (!cacheRoot) {
throw new Error('Agent.ToolsDirectory is not set');
}
return cacheRoot;
}
function _getAgentTemp(): string {
tl.assertAgent('2.115.0');
let tempDirectory = tl.getVariable('Agent.TempDirectory');
if (!tempDirectory) {
throw new Error('Agent.TempDirectory is not set');
}
return tempDirectory;
}