-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
task-schema-updater.ts
689 lines (650 loc) · 25.7 KB
/
task-schema-updater.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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/********************************************************************************
* Copyright (C) 2019 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
// This file is inspired by VSCode and partially copied from https://github.com/Microsoft/vscode/blob/1.33.1/src/vs/workbench/contrib/tasks/common/problemMatcher.ts
// 'problemMatcher.ts' copyright:
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as Ajv from 'ajv';
import debounce = require('p-debounce');
import { injectable, inject, postConstruct } from 'inversify';
import { JsonSchemaStore } from '@theia/core/lib/browser/json-schema-store';
import { InMemoryResources, deepClone, Emitter } from '@theia/core/lib/common';
import { IJSONSchema } from '@theia/core/lib/common/json-schema';
import { inputsSchema } from '@theia/variable-resolver/lib/browser/variable-input-schema';
import URI from '@theia/core/lib/common/uri';
import { ProblemMatcherRegistry } from './task-problem-matcher-registry';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { TaskServer } from '../common';
import { USER_TASKS_URI } from './task-configurations';
export const taskSchemaId = 'vscode://schemas/tasks';
@injectable()
export class TaskSchemaUpdater {
@inject(JsonSchemaStore)
protected readonly jsonSchemaStore: JsonSchemaStore;
@inject(InMemoryResources)
protected readonly inmemoryResources: InMemoryResources;
@inject(ProblemMatcherRegistry)
protected readonly problemMatcherRegistry: ProblemMatcherRegistry;
@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
@inject(TaskServer)
protected readonly taskServer: TaskServer;
protected readonly onDidChangeTaskSchemaEmitter = new Emitter<void>();
readonly onDidChangeTaskSchema = this.onDidChangeTaskSchemaEmitter.event;
@postConstruct()
protected init(): void {
const taskSchemaUri = new URI(taskSchemaId);
this.jsonSchemaStore.onDidChangeSchema(uri => {
if (uri.toString() === taskSchemaUri.toString()) {
this.onDidChangeTaskSchemaEmitter.fire(undefined);
}
});
this.updateProblemMatcherNames();
this.updateSupportedTaskTypes();
// update problem matcher names in the task schema every time a problem matcher is added or disposed
this.problemMatcherRegistry.onDidChangeProblemMatcher(() => this.updateProblemMatcherNames());
// update supported task types in the task schema every time a task definition is registered or removed
this.taskDefinitionRegistry.onDidRegisterTaskDefinition(() => this.updateSupportedTaskTypes());
this.taskDefinitionRegistry.onDidUnregisterTaskDefinition(() => this.updateSupportedTaskTypes());
}
readonly update = debounce(() => this.doUpdate(), 0);
protected doUpdate(): void {
const taskSchemaUri = new URI(taskSchemaId);
taskConfigurationSchema.anyOf = [processTaskConfigurationSchema, ...customizedDetectedTasks, ...customSchemas];
const schema = this.getTaskSchema();
this.doValidate = new Ajv().compile(schema);
const schemaContent = JSON.stringify(schema);
try {
this.inmemoryResources.update(taskSchemaUri, schemaContent);
} catch (e) {
this.inmemoryResources.add(taskSchemaUri, schemaContent);
this.jsonSchemaStore.registerSchema({
fileMatch: ['tasks.json', USER_TASKS_URI.toString()],
url: taskSchemaUri.toString()
});
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
validate(data: any): boolean {
return !!this.doValidate && !!this.doValidate(data);
}
protected doValidate: Ajv.ValidateFunction | undefined;
/**
* Adds given task schema to `taskConfigurationSchema` as `oneOf` subschema.
* Replaces existed subschema by given schema if the corresponding `$id` properties are equal.
*
* Note: please provide `$id` property for subschema to have ability remove/replace it.
* @param schema subschema for adding to `taskConfigurationSchema`
*/
addSubschema(schema: IJSONSchema): void {
const schemaId = schema.$id;
if (schemaId) {
this.doRemoveSubschema(schemaId);
}
customSchemas.push(schema);
this.update();
}
/**
* Removes task subschema from `taskConfigurationSchema`.
*
* @param arg `$id` property of subschema
*/
removeSubschema(arg: string): void {
const isRemoved = this.doRemoveSubschema(arg);
if (isRemoved) {
this.update();
}
}
/**
* Removes task subschema from `customSchemas`, use `update()` to apply the changes for `taskConfigurationSchema`.
*
* @param arg `$id` property of subschema
* @returns `true` if subschema was removed, `false` otherwise
*/
protected doRemoveSubschema(arg: string): boolean {
const index = customSchemas.findIndex(existed => !!existed.$id && existed.$id === arg);
if (index > -1) {
customSchemas.splice(index, 1);
return true;
}
return false;
}
/** Returns an array of task types that are registered, including the default types */
async getRegisteredTaskTypes(): Promise<string[]> {
const serverSupportedTypes = await this.taskServer.getRegisteredTaskTypes();
const browserSupportedTypes = this.taskDefinitionRegistry.getAll().map(def => def.taskType);
const allTypes = new Set([...serverSupportedTypes, ...browserSupportedTypes]);
return Array.from(allTypes.values()).sort();
}
private updateSchemasForRegisteredTasks(): void {
customizedDetectedTasks.length = 0;
const definitions = this.taskDefinitionRegistry.getAll();
definitions.forEach(def => {
const customizedDetectedTask = {
type: 'object',
required: ['type'],
properties: {}
} as IJSONSchema;
const taskType = {
...defaultTaskType,
enum: [def.taskType],
default: def.taskType,
description: 'The task type to customize'
};
customizedDetectedTask.properties!.type = taskType;
def.properties.all.forEach(taskProp => {
if (!!def.properties.required.find(requiredProp => requiredProp === taskProp)) { // property is mandatory
customizedDetectedTask.required!.push(taskProp);
}
customizedDetectedTask.properties![taskProp] = { ...def.properties.schema.properties![taskProp] };
});
customizedDetectedTask.properties!.label = taskLabel;
customizedDetectedTask.properties!.problemMatcher = problemMatcher;
customizedDetectedTask.properties!.presentation = presentation;
customizedDetectedTask.properties!.options = commandOptionsSchema;
customizedDetectedTask.properties!.group = group;
customizedDetectedTask.additionalProperties = true;
customizedDetectedTasks.push(customizedDetectedTask);
});
}
/** Returns the task's JSON schema */
protected getTaskSchema(): IJSONSchema {
return {
type: 'object',
properties: {
version: {
type: 'string'
},
tasks: {
type: 'array',
items: {
...deepClone(taskConfigurationSchema)
}
},
inputs: inputsSchema.definitions!.inputs
},
additionalProperties: false
};
}
/** Gets the most up-to-date names of problem matchers from the registry and update the task schema */
private updateProblemMatcherNames(): void {
const matcherNames = this.problemMatcherRegistry.getAll().map(m => m.name.startsWith('$') ? m.name : `$${m.name}`);
problemMatcherNames.length = 0;
problemMatcherNames.push(...matcherNames);
this.update();
}
private async updateSupportedTaskTypes(): Promise<void> {
this.updateSchemasForRegisteredTasks();
this.update();
}
}
const commandSchema: IJSONSchema = {
type: 'string',
description: 'The actual command or script to execute'
};
const commandArgSchema: IJSONSchema = {
type: 'array',
description: 'A list of strings, each one being one argument to pass to the command',
items: {
type: 'string'
}
};
const commandOptionsSchema: IJSONSchema = {
type: 'object',
description: 'The command options used when the command is executed',
properties: {
cwd: {
type: 'string',
description: 'The directory in which the command will be executed',
default: '${workspaceFolder}'
},
env: {
type: 'object',
description: 'The environment of the executed program or shell. If omitted the parent process\' environment is used'
},
shell: {
type: 'object',
description: 'Configuration of the shell when task type is `shell`',
properties: {
executable: {
type: 'string',
description: 'The shell to use'
},
args: {
type: 'array',
description: `The arguments to be passed to the shell executable to run in command mode
(e.g ['-c'] for bash or ['/S', '/C'] for cmd.exe)`,
items: {
type: 'string'
}
}
}
}
}
};
const problemMatcherNames: string[] = [];
const defaultTaskTypes = ['shell', 'process'];
const supportedTaskTypes = [...defaultTaskTypes];
const taskLabel = {
type: 'string',
description: 'A unique string that identifies the task that is also used as task\'s user interface label'
};
const defaultTaskType = {
type: 'string',
enum: supportedTaskTypes,
default: defaultTaskTypes[0],
description: 'Determines what type of process will be used to execute the task. Only shell types will have output shown on the user interface'
};
const commandAndArgs = {
command: commandSchema,
args: commandArgSchema,
options: commandOptionsSchema
};
const group = {
oneOf: [
{
type: 'string'
},
{
type: 'object',
properties: {
kind: {
type: 'string',
default: 'none',
description: 'The task\'s execution group.'
},
isDefault: {
type: 'boolean',
default: false,
description: 'Defines if this task is the default task in the group.'
}
}
}
],
enum: [
{ kind: 'build', isDefault: true },
{ kind: 'test', isDefault: true },
'build',
'test',
'none'
],
enumDescriptions: [
'Marks the task as the default build task.',
'Marks the task as the default test task.',
'Marks the task as a build task accessible through the \'Run Build Task\' command.',
'Marks the task as a test task accessible through the \'Run Test Task\' command.',
'Assigns the task to no group'
],
// eslint-disable-next-line max-len
description: 'Defines to which execution group this task belongs to. It supports "build" to add it to the build group and "test" to add it to the test group.'
};
const problemPattern: IJSONSchema = {
default: {
regexp: '^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$',
file: 1,
location: 2,
message: 3
},
type: 'object',
properties: {
regexp: {
type: 'string',
description: 'The regular expression to find an error, warning or info in the output.'
},
kind: {
type: 'string',
description: 'whether the pattern matches a location (file and line) or only a file.'
},
file: {
type: 'integer',
description: 'The match group index of the filename. If omitted 1 is used.'
},
location: {
type: 'integer',
// eslint-disable-next-line max-len
description: 'The match group index of the problem\'s location. Valid location patterns are: (line), (line,column) and (startLine,startColumn,endLine,endColumn). If omitted (line,column) is assumed.'
},
line: {
type: 'integer',
description: 'The match group index of the problem\'s line. Defaults to 2'
},
column: {
type: 'integer',
description: 'The match group index of the problem\'s line character. Defaults to 3'
},
endLine: {
type: 'integer',
description: 'The match group index of the problem\'s end line. Defaults to undefined'
},
endColumn: {
type: 'integer',
description: 'The match group index of the problem\'s end line character. Defaults to undefined'
},
severity: {
type: 'integer',
description: 'The match group index of the problem\'s severity. Defaults to undefined'
},
code: {
type: 'integer',
description: 'The match group index of the problem\'s code. Defaults to undefined'
},
message: {
type: 'integer',
description: 'The match group index of the message. If omitted it defaults to 4 if location is specified. Otherwise it defaults to 5.'
},
loop: {
type: 'boolean',
// eslint-disable-next-line max-len
description: 'In a multi line matcher loop indicated whether this pattern is executed in a loop as long as it matches. Can only specified on a last pattern in a multi line pattern.'
}
}
};
const multiLineProblemPattern: IJSONSchema = {
type: 'array',
items: problemPattern
};
const watchingPattern: IJSONSchema = {
type: 'object',
additionalProperties: false,
properties: {
regexp: {
type: 'string',
description: 'The regular expression to detect the begin or end of a background task.'
},
file: {
type: 'integer',
description: 'The match group index of the filename. Can be omitted.'
},
}
};
const patternType: IJSONSchema = {
anyOf: [
{
type: 'string',
description: 'The name of a contributed or predefined pattern'
},
problemPattern,
multiLineProblemPattern
],
description: 'A problem pattern or the name of a contributed or predefined problem pattern. Can be omitted if base is specified.'
};
const problemMatcherObject: IJSONSchema = {
type: 'object',
properties: {
base: {
type: 'string',
enum: problemMatcherNames,
description: 'The name of a base problem matcher to use.'
},
owner: {
type: 'string',
description: 'The owner of the problem inside Code. Can be omitted if base is specified. Defaults to \'external\' if omitted and base is not specified.'
},
source: {
type: 'string',
description: 'A human-readable string describing the source of this diagnostic, e.g. \'typescript\' or \'super lint\'.'
},
severity: {
type: 'string',
enum: ['error', 'warning', 'info'],
description: 'The default severity for captures problems. Is used if the pattern doesn\'t define a match group for severity.'
},
applyTo: {
type: 'string',
enum: ['allDocuments', 'openDocuments', 'closedDocuments'],
description: 'Controls if a problem reported on a text document is applied only to open, closed or all documents.'
},
pattern: patternType,
fileLocation: {
oneOf: [
{
type: 'string',
enum: ['absolute', 'relative', 'autoDetect']
},
{
type: 'array',
items: {
type: 'string'
}
}
],
description: 'Defines how file names reported in a problem pattern should be interpreted.'
},
background: {
type: 'object',
additionalProperties: false,
description: 'Patterns to track the begin and end of a matcher active on a background task.',
properties: {
activeOnStart: {
type: 'boolean',
description: 'If set to true the background monitor is in active mode when the task starts. This is equals of issuing a line that matches the beginsPattern'
},
beginsPattern: {
oneOf: [
{
type: 'string'
},
watchingPattern
],
description: 'If matched in the output the start of a background task is signaled.'
},
endsPattern: {
oneOf: [
{
type: 'string'
},
watchingPattern
],
description: 'If matched in the output the end of a background task is signaled.'
}
}
},
watching: {
type: 'object',
additionalProperties: false,
deprecationMessage: 'The watching property is deprecated. Use background instead.',
description: 'Patterns to track the begin and end of a watching matcher.',
properties: {
activeOnStart: {
type: 'boolean',
description: 'If set to true the watcher is in active mode when the task starts. This is equals of issuing a line that matches the beginPattern'
},
beginsPattern: {
oneOf: [
{
type: 'string'
},
watchingPattern
],
description: 'If matched in the output the start of a watching task is signaled.'
},
endsPattern: {
oneOf: [
{
type: 'string'
},
watchingPattern
],
description: 'If matched in the output the end of a watching task is signaled.'
}
}
}
}
};
const problemMatcher = {
anyOf: [
{
type: 'string',
description: 'Name of the problem matcher to parse the output of the task',
enum: problemMatcherNames
},
{
type: 'array',
description: 'Name(s) of the problem matcher(s) to parse the output of the task',
items: {
type: 'string',
enum: problemMatcherNames
}
},
problemMatcherObject,
{
type: 'array',
description: 'User defined problem matcher(s) to parse the output of the task',
items: problemMatcherObject
}
]
};
const presentation: IJSONSchema = {
type: 'object',
default: {
echo: true,
reveal: 'always',
focus: false,
panel: 'shared',
showReuseMessage: true,
clear: false
},
description: 'Configures the panel that is used to present the task\'s output and reads its input.',
additionalProperties: true,
properties: {
echo: {
type: 'boolean',
default: true,
description: 'Controls whether the executed command is echoed to the panel. Default is true.'
},
focus: {
type: 'boolean',
default: false,
description: 'Controls whether the panel takes focus. Default is false. If set to true the panel is revealed as well.'
},
reveal: {
type: 'string',
enum: ['always', 'silent', 'never'],
enumDescriptions: [
'Always reveals the terminal when this task is executed.',
'Only reveals the terminal if the task exits with an error or the problem matcher finds an error.',
'Never reveals the terminal when this task is executed.'
],
default: 'always',
description: 'Controls whether the terminal running the task is revealed or not. May be overridden by option \"revealProblems\". Default is \"always\".'
},
panel: {
type: 'string',
enum: ['shared', 'dedicated', 'new'],
enumDescriptions: [
'The terminal is shared and the output of other task runs are added to the same terminal.',
// eslint-disable-next-line max-len
'The terminal is dedicated to a specific task. If that task is executed again, the terminal is reused. However, the output of a different task is presented in a different terminal.',
'Every execution of that task is using a new clean terminal.'
],
default: 'shared',
description: 'Controls if the panel is shared between tasks, dedicated to this task or a new one is created on every run.'
},
showReuseMessage: {
type: 'boolean',
default: true,
description: 'Controls whether to show the "Terminal will be reused by tasks" message.'
},
clear: {
type: 'boolean',
default: false,
description: 'Controls whether the terminal is cleared before this task is run.'
}
}
};
const taskIdentifier: IJSONSchema = {
type: 'object',
additionalProperties: true,
properties: {
type: {
type: 'string',
description: 'The task identifier.'
}
}
};
const processTaskConfigurationSchema: IJSONSchema = {
type: 'object',
required: ['type', 'label', 'command'],
properties: {
label: taskLabel,
type: defaultTaskType,
...commandAndArgs,
isBackground: {
type: 'boolean',
default: false,
description: 'Whether the executed task is kept alive and is running in the background.'
},
dependsOn: {
anyOf: [
{
type: 'string',
description: 'Another task this task depends on.'
},
taskIdentifier,
{
type: 'array',
description: 'The other tasks this task depends on.',
items: {
anyOf: [
{
type: 'string'
},
taskIdentifier
]
}
}
],
description: 'Either a string representing another task or an array of other tasks that this task depends on.'
},
dependsOrder: {
type: 'string',
enum: ['parallel', 'sequence'],
enumDescriptions: [
'Run all dependsOn tasks in parallel.',
'Run all dependsOn tasks in sequence.'
],
default: 'parallel',
description: 'Determines the order of the dependsOn tasks for this task. Note that this property is not recursive.'
},
windows: {
type: 'object',
description: 'Windows specific command configuration that overrides the command, args, and options',
properties: commandAndArgs
},
osx: {
type: 'object',
description: 'MacOS specific command configuration that overrides the command, args, and options',
properties: commandAndArgs
},
linux: {
type: 'object',
description: 'Linux specific command configuration that overrides the default command, args, and options',
properties: commandAndArgs
},
group,
problemMatcher,
presentation
},
additionalProperties: true
};
const customizedDetectedTasks: IJSONSchema[] = [];
const customSchemas: IJSONSchema[] = [];
const taskConfigurationSchema: IJSONSchema = {
$id: taskSchemaId,
anyOf: [processTaskConfigurationSchema, ...customizedDetectedTasks, ...customSchemas]
};