/
Common.ts
520 lines (439 loc) · 19.3 KB
/
Common.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
import * as os from "os";
import * as path from "path";
import * as stream from "stream";
import * as fs from "fs";
import * as tl from "vsts-task-lib/task";
import * as trl from "vsts-task-lib/toolrunner";
import * as fse from "fs-extra";
import ToolRunner = trl.ToolRunner;
import * as uuidv5 from "uuidv5";
function writeBuildTempFile(taskName: string, data: any): string {
let tempFile: string;
do {
// Let's add a random suffix
let randomSuffix = Math.random().toFixed(6);
tempFile = path.join(os.tmpdir(), `${taskName}-${randomSuffix}.tmp`);
} while (tl.exist(tempFile));
tl.debug(`Generating Build temp file: ${tempFile}`);
tl.writeFile(tempFile, data);
return tempFile;
}
function deleteBuildTempFile(tempFile: string) {
if (tempFile && tl.exist(tempFile)) {
tl.debug(`Deleting temp file: ${tempFile}`);
fs.unlinkSync(tempFile);
}
}
/**
* Set manifest related arguments for "tfx extension" command, such as
* the --root or the --manifest-globs switches.
* @param {ToolRunner} tfx
* @returns {() => void} Cleaner function that the caller should use to cleanup temporary files created to be used as arguments
*/
export function validateAndSetTfxManifestArguments(tfx: ToolRunner): (() => void) {
const rootFolder = tl.getInput("rootFolder", false);
tfx.argIf(rootFolder, ["--root", rootFolder]);
const globsManifest = tl.getInput("patternManifest", false);
tfx.argIf(globsManifest, ["--manifest-globs", globsManifest]);
// Overrides manifest file
const publisher = tl.getInput("publisherId", false);
const localizationRoot = tl.getInput("localizationRoot", false);
tfx.argIf(localizationRoot, ["--loc-root", localizationRoot]);
let extensionId = tl.getInput("extensionId", false);
const extensionTag = tl.getInput("extensionTag", false);
if (extensionId && extensionTag) {
extensionId += extensionTag;
tl.debug(`Overriding extension id to: ${extensionId}`);
}
// for backwards compat check both "method" and "fileType"
switch (tl.getInput("method", false) || tl.getInput("fileType", false)) {
// for backwards compat trigger on both "manifest" and "id"
case "manifest":
case "id":
default:
tfx.argIf(publisher, ["--publisher", publisher]);
tfx.argIf(extensionId, ["--extension-id", extensionId]);
break;
case "vsix":
let vsixFilePattern = tl.getPathInput("vsixFile", true);
let matchingVsixFile: string[];
if (vsixFilePattern.indexOf("*") >= 0 || vsixFilePattern.indexOf("?") >= 0) {
tl.debug("Pattern found in vsixFile parameter.");
matchingVsixFile = tl.findMatch(process.cwd(), vsixFilePattern);
}
else {
tl.debug("No pattern found in vsixFile parameter.");
matchingVsixFile = [vsixFilePattern];
}
if (!matchingVsixFile || matchingVsixFile.length === 0) {
tl.setResult(tl.TaskResult.Failed, `Found no vsix files matching: ${vsixFilePattern}.`);
throw "failed";
}
if (matchingVsixFile.length !== 1) {
tl.setResult(tl.TaskResult.Failed, `Found multiple vsix files matching: ${vsixFilePattern}.`);
throw "failed";
}
tfx.arg(["--vsix", matchingVsixFile[0]]);
break;
}
let jsonOverrides: any;
const extensionName = tl.getInput("extensionName", false);
if (extensionName) {
tl.debug(`Overriding extension name to: ${extensionName}`);
jsonOverrides = (jsonOverrides || {});
jsonOverrides.name = extensionName;
}
const extensionVisibility = tl.getInput("extensionVisibility", false);
if (extensionVisibility && extensionVisibility !== "default") {
tl.debug(`Overriding extension visibility to: ${extensionVisibility}`);
jsonOverrides = (jsonOverrides || {});
const isPublic = extensionVisibility.indexOf("public") >= 0;
const isPreview = extensionVisibility.indexOf("preview") >= 0;
jsonOverrides.public = isPublic;
if (isPreview) {
jsonOverrides.galleryFlags = jsonOverrides.galleryFlags || [];
jsonOverrides.galleryFlags.push("Preview");
}
}
const extensionPricing = tl.getInput("extensionPricing", false);
if (extensionPricing && extensionPricing !== "default") {
tl.debug(`Overriding extension pricing to: ${extensionPricing}`);
jsonOverrides = (jsonOverrides || {});
const isPaid = extensionPricing.indexOf("paid") >= 0;
if (isPaid) {
jsonOverrides.galleryFlags = jsonOverrides.galleryFlags || [];
jsonOverrides.galleryFlags.push("Paid");
}
}
let extensionVersion = getExtensionVersion();
if (extensionVersion) {
tl.debug(`Overriding extension version to: ${extensionVersion}`);
jsonOverrides = (jsonOverrides || {});
jsonOverrides.version = extensionVersion;
}
const noWaitValidation = tl.getBoolInput("noWaitValidation", false);
if (noWaitValidation) {
tl.debug(`Not waiting for validation.`);
tfx.arg("--no-wait-validation");
}
const bypassLocalValidation = tl.getBoolInput("bypassLocalValidation", false);
if (bypassLocalValidation) {
tl.debug(`Bypassing local validation.`);
tfx.arg("--bypass-validation");
}
let overrideFilePath: string;
if (jsonOverrides) {
// Generate a temp file
overrideFilePath = writeBuildTempFile("PackageTask", JSON.stringify(jsonOverrides));
tl.debug(`Generated a JSON temp file to override manifest values Path: ${overrideFilePath}`);
tfx.arg(["--overrides-file", overrideFilePath]);
}
const args = tl.getInput("arguments", false);
if (args) {
tl.debug(`Adding additional arguments: ${args}.`);
tfx.line(args);
}
return () => deleteBuildTempFile(overrideFilePath);
}
/**
* Run a tfx command by ensuring that "tfx" exists, installing it on the fly if needed.
* @param {(tfx:ToolRunner)=>void} cmd
*/
export async function runTfx(cmd: (tfx: ToolRunner) => void) {
let tfx: ToolRunner;
let tfxPath: string;
const tryRunCmd = async (tfx: ToolRunner) => {
try {
// Set working folder
const cwd = tl.getInput("cwd", false);
if (cwd) {
tl.cd(cwd);
}
cmd(tfx);
return true;
}
catch (err) {
tl.setResult(tl.TaskResult.Failed, `Error running task: ${err}`);
return false;
}
};
const checkTfxGlobalVar = tl.getVariable("vstsDevTools.buildTasks.checkGlobalTfx");
if (checkTfxGlobalVar && checkTfxGlobalVar.toLowerCase() !== "false") {
console.log("Checking tfx globally");
tfxPath = tl.which("tfx");
if (tfxPath) {
console.log(`Found tfx globally ${tfxPath}`);
tfx = new trl.ToolRunner(tfxPath);
await tryRunCmd(tfx);
return;
}
}
// Check the local tfx to see if it is installed in the workfolder/_tools folder
let agentToolsPath = path.join(tl.getVariable("Agent.Workfolder"), "/_tools/");
let tfxLocalPathBin = path.join(agentToolsPath, "/node_modules/.bin/tfx");
let tfxLocalPath = path.join(agentToolsPath, "/tfx");
if (tl.osType() === "Windows_NT") {
tfxLocalPathBin += ".cmd";
tfxLocalPath += ".cmd";
}
console.log(`Checking tfx under: ${tfxLocalPath}`);
tfxPath = tl.which(tfxLocalPath) || tl.which(tfxLocalPathBin);
if (tfxPath) {
console.log(`Found tfx under: ${tfxPath}`);
tfx = new trl.ToolRunner(tfxPath);
await tryRunCmd(tfx);
return;
}
console.log(`Could not find tfx command. Preparing to install it under: ${agentToolsPath}`);
tl.mkdirP(path.join(agentToolsPath, "/node_modules/"));
const npm = new trl.ToolRunner(tl.which("npm", true));
npm.arg(["install", "tfx-cli@^0.6", "--prefix", agentToolsPath]);
try {
await npm.exec();
tfx = new trl.ToolRunner(tl.which(tfxLocalPath) || tl.which(tfxLocalPathBin, true));
await tryRunCmd(tfx);
} catch (err) {
tl.setResult(tl.TaskResult.Failed, `Error installing tfx: ${err}`);
}
}
/**
* Reads the extension version from the 'extensionVersion' variable, extracting
* just the part that is compatible with the versioning scheme used in the Marketplace.
*
* @returns string
*/
export function getExtensionVersion(): string {
const extensionVersion = tl.getInput("extensionVersion", false);
if (extensionVersion) {
const extractedVersions = extensionVersion.match(/[0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?/);
if (extractedVersions && extractedVersions.length === 1) {
return extractedVersions[0];
}
else {
throw new Error(`Supplied ExtensionVersion must contain a string matching '##.##.##(.##)'.`);
}
}
return "";
}
/**
* Get the Marketplace endpoint details to be used while publishing or installing an extension.
*
* @param {string="connectedServiceName"} inputFieldName
* @returns string
*/
export function getMarketplaceEndpointDetails(inputFieldName: string): any {
const marketplaceEndpoint = tl.getInput(inputFieldName, true);
const hostUrl = tl.getEndpointUrl(marketplaceEndpoint, false);
const auth = tl.getEndpointAuthorization(marketplaceEndpoint, false);
const password = auth.parameters["password"];
const username = auth.parameters["username"];
const apitoken = auth.parameters["apitoken"];
return {
"url": hostUrl,
"username": username,
"password": password,
"apitoken": apitoken
};
}
/**
* Sets the marketplace endpoint details (url, credentials) for the toolrunner.
*
* @param {ToolRunner} tfx
* @returns string
*/
export function setTfxMarketplaceArguments(tfx: ToolRunner, setServiceUrl: boolean = true) {
const connectTo = tl.getInput("connectTo", false) || "VsTeam";
let galleryEndpoint;
if (connectTo === "VsTeam") {
galleryEndpoint = getMarketplaceEndpointDetails("connectedServiceName");
tfx.argIf(setServiceUrl, ["--service-url", galleryEndpoint.url]);
tfx.arg(["--auth-type", "pat"]);
tfx.arg(["--token", galleryEndpoint.password]);
} else {
galleryEndpoint = getMarketplaceEndpointDetails("connectedServiceNameTFS");
tfx.argIf(setServiceUrl, ["--service-url", galleryEndpoint.url]);
if (galleryEndpoint.username) {
tfx.arg(["--auth-type", "basic"]);
tfx.arg(["--username", galleryEndpoint.username]);
tfx.arg(["--password", galleryEndpoint.password]);
}
else {
tfx.arg(["--auth-type", "pat"]);
tfx.arg(["--token", galleryEndpoint.apitoken]);
}
}
}
/**
* A writable stream intended to be used with Tfx when using JSON output.
* This class overcomes the problem of having tfx warnings being displayed
* in stdout as regular messages, even when using --json switch in tfx.
*
*/
export class TfxJsonOutputStream extends stream.Writable {
jsonString: string = "";
messages: string[] = [];
constructor(public out: (message: string) => void) {
super();
}
_write(chunk: any, enc: string, cb: Function) {
const chunkStr: string = chunk.toString();
if (chunkStr.startsWith("[command]"))
{
this.taskOutput(chunkStr, this.out);
}
else if (!this.jsonString && (chunkStr.toString()[0] !== "{" && chunkStr.toString()[0] !== "[")) {
this.messages.push(chunkStr);
this.taskOutput(chunkStr, this.out);
}
else {
this.jsonString += chunkStr;
this.taskOutput(chunkStr, tl.debug);
}
cb();
}
private taskOutput(messages: string, lineWriter: (m: string) => void) {
if (!messages) { return; }
// Split messages to be sure that we are invoking the write lineWriter for each lineWriter
// Otherwise we could get messages in console with the wrong prefix used by vsts-task-lib
messages.split("\n").forEach(lineWriter);
}
}
function getTaskPathContributions(manifest: any): string[] {
// Check for task contributions
if (!manifest.contributions) {
return [];
}
return manifest.contributions
.filter((c: any) => c.type === "ms.vss-distributed-task.task" && c.properties && c.properties["name"])
.map((c: any) => c.properties["name"]);
}
function updateTaskId(manifest: any, publisherId: string, extensionId: string): Object {
tl.debug(`Task manifest ${manifest.name} id before: ${manifest.id}`);
let extensionNs = uuidv5("url", "https://marketplace.visualstudio.com/vsts", true);
manifest.id = uuidv5(extensionNs, `${publisherId}.${extensionId}.${manifest.name}`, false);
tl.debug(`Task manifest ${manifest.name} id after: ${manifest.id}`);
return manifest;
}
function updateTaskVersion(manifest: any, extensionVersionString: string, extensionVersionType: string): Object {
const versionParts = extensionVersionString.split(".");
if (versionParts.length > 3) {
tl.warning("Detected a version that consists of more than 3 parts. Build tasks support only 3 parts, ignoring the rest.");
}
const extensionversion = { major: +versionParts[0], minor: +versionParts[1], patch: +versionParts[2] };
if (!manifest.version && extensionVersionType !== "major") {
tl.warning("Detected no version in task manifest. Forcing major.");
manifest.version = extensionversion;
} else {
tl.debug(`Task manifest ${manifest.name} version before: ${JSON.stringify(manifest.version)}`);
switch (extensionVersionType) {
default:
case "major": manifest.version.Major = `${extensionversion.major}`;
case "minor": manifest.version.Minor = `${extensionversion.minor}`;
case "patch": manifest.version.Patch = `${extensionversion.patch}`;
}
}
tl.debug(`Task manifest ${manifest.name} version after: ${JSON.stringify(manifest.version)}`);
return manifest;
}
/**
*
* Check whether the version update should be propagated to tasks included
* in the extension.
*
*/
export async function updateManifests(manifestPaths: string[]): Promise<void> {
const updateTasksVersion = tl.getBoolInput("updateTasksVersion", false);
const updateTasksId = tl.getBoolInput("updateTasksId", false);
if (updateTasksVersion || updateTasksId) {
if (!(manifestPaths && manifestPaths.length)) {
manifestPaths = getExtensionManifestPaths();
}
tl.debug(`Found manifests: ${manifestPaths.join(", ")}`);
await Promise.all(manifestPaths.map(async (extensionPath) => {
const manifest: any = await getManifest(extensionPath);
const taskManifestPaths: string[] = getTaskManifestPaths(extensionPath, manifest);
if (taskManifestPaths && taskManifestPaths.length) {
await Promise.all(taskManifestPaths.map(async (taskPath) => {
tl.debug(`Patching: ${taskPath}.`);
let taskManifest = await getManifest(taskPath);
if (updateTasksId) {
tl.debug(`Updating Id...`);
const publisherId = tl.getInput("publisherId", false) || manifest.publisher;
const extensionTag = tl.getInput("extensionTag", false) || "";
const extensionId = `${(tl.getInput("extensionId", false) || manifest.id)}${extensionTag}`;
taskManifest = updateTaskId(taskManifest, publisherId, extensionId);
}
if (updateTasksVersion) {
tl.debug(`Updating version...`);
const extensionVersion = tl.getInput("extensionVersion", false) || manifest.version;
if (!extensionVersion) {
throw new Error(
"Extension Version was not supplied nor does the extension manifest define one.");
}
const extensionVersionType = tl.getInput("updateTasksVersionType", false) || "major";
taskManifest = updateTaskVersion(taskManifest, extensionVersion, extensionVersionType);
}
await writeTaskmanifest(taskManifest, taskPath);
tl.debug(`Updated: ${taskPath}.`);
}));
}
}));
}
}
function getExtensionManifestPaths(): string[] {
let rootFolder: string;
// Search for extension manifests given the rootFolder and patternManifest inputs
rootFolder = tl.getInput("rootFolder", false) || tl.getInput("System.DefaultWorkingDirectory");
const manifestsPattern = tl.getInput("patternManifest", false) || "vss-extension.json";
tl.debug(`Searching for extension manifests: ${manifestsPattern}`);
return tl.findMatch(rootFolder, manifestsPattern);
}
function getManifest(path: string): Promise<Object> {
return fse.readFile(path, "utf8").then((data: string) => {
try {
data = data.replace(/^\uFEFF/,
() => {
tl.warning(`Removing Unicode BOM from manifest file: ${path}.`);
return "";
});
return JSON.parse(data);
} catch (jsonError) {
throw new Error(`Error parsing task manifest: ${path} - ${jsonError}`);
}
});
}
function getTaskManifestPaths(manifestPath: string, manifest: object): string[] {
const tasks = getTaskPathContributions(manifest);
const rootFolder = path.dirname(manifestPath);
return tasks.reduce((result: string[], task: string) => {
tl.debug(`Found task: ${task}`);
const taskRoot: string = path.join(rootFolder, task);
const rootManifest: string = path.join(taskRoot, "task.json");
let localizationRoot = tl.getInput("localizationRoot", false);
if (localizationRoot) {
localizationRoot = path.resolve(localizationRoot);
}
if (tl.exist(rootManifest)) {
tl.debug(`Found single-task manifest: ${rootManifest}`);
let rootManifests: string[] = [rootManifest];
const rootLocManifest: string = path.join(localizationRoot || taskRoot, "task.loc.json");
if (tl.exist(rootLocManifest)) {
tl.debug(`Found localized single-task manifest: ${rootLocManifest}`);
rootManifests.push(rootLocManifest);
}
return (result).concat(rootManifests);
} else {
const versionManifests = tl.findMatch(taskRoot, "*/task.json");
const locVersionManifests = tl.findMatch(localizationRoot || taskRoot, "*/task.loc.json");
tl.debug(`Found multi-task manifests: ${versionManifests.join(", ")}`);
tl.debug(`Found multi-task localized manifests: ${locVersionManifests.join(", ")}`);
return (result).concat(versionManifests).concat(locVersionManifests);
}
}, []);
}
export function writeTaskmanifest(manifest: object, path: string): Promise<void> {
return fse.writeJSON(path, manifest);
}
export function checkUpdateTasksManifests(manifestFile?: string): Promise<void> {
return updateManifests(manifestFile ? [manifestFile] : []);
}