-
Notifications
You must be signed in to change notification settings - Fork 29.7k
/
taskDefinitionRegistry.ts
182 lines (163 loc) · 5.78 KB
/
taskDefinitionRegistry.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vs/nls';
import { IJSONSchema, IJSONSchemaMap } from 'vs/base/common/jsonSchema';
import { IStringDictionary } from 'vs/base/common/collections';
import * as Types from 'vs/base/common/types';
import * as Objects from 'vs/base/common/objects';
import { ExtensionsRegistry, ExtensionMessageCollector } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import * as Tasks from 'vs/workbench/contrib/tasks/common/tasks';
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
import { Emitter, Event } from 'vs/base/common/event';
const taskDefinitionSchema: IJSONSchema = {
type: 'object',
additionalProperties: false,
properties: {
type: {
type: 'string',
description: nls.localize('TaskDefinition.description', 'The actual task type. Please note that types starting with a \'$\' are reserved for internal usage.')
},
required: {
type: 'array',
items: {
type: 'string'
}
},
properties: {
type: 'object',
description: nls.localize('TaskDefinition.properties', 'Additional properties of the task type'),
additionalProperties: {
$ref: 'http://json-schema.org/draft-07/schema#'
}
},
when: {
type: 'string',
markdownDescription: nls.localize('TaskDefinition.when', 'Condition which must be true to enable this type of task. Consider using `shellExecutionSupported`, `processExecutionSupported`, and `customExecutionSupported` as appropriate for this task definition.'),
default: ''
}
}
};
namespace Configuration {
export interface TaskDefinition {
type?: string;
required?: string[];
properties?: IJSONSchemaMap;
when?: string;
}
export function from(value: TaskDefinition, extensionId: ExtensionIdentifier, messageCollector: ExtensionMessageCollector): Tasks.TaskDefinition | undefined {
if (!value) {
return undefined;
}
let taskType = Types.isString(value.type) ? value.type : undefined;
if (!taskType || taskType.length === 0) {
messageCollector.error(nls.localize('TaskTypeConfiguration.noType', 'The task type configuration is missing the required \'taskType\' property'));
return undefined;
}
let required: string[] = [];
if (Array.isArray(value.required)) {
for (let element of value.required) {
if (Types.isString(element)) {
required.push(element);
}
}
}
return {
extensionId: extensionId.value,
taskType, required: required,
properties: value.properties ? Objects.deepClone(value.properties) : {},
when: value.when ? ContextKeyExpr.deserialize(value.when) : undefined
};
}
}
const taskDefinitionsExtPoint = ExtensionsRegistry.registerExtensionPoint<Configuration.TaskDefinition[]>({
extensionPoint: 'taskDefinitions',
jsonSchema: {
description: nls.localize('TaskDefinitionExtPoint', 'Contributes task kinds'),
type: 'array',
items: taskDefinitionSchema
}
});
export interface ITaskDefinitionRegistry {
onReady(): Promise<void>;
get(key: string): Tasks.TaskDefinition;
all(): Tasks.TaskDefinition[];
getJsonSchema(): IJSONSchema;
onDefinitionsChanged: Event<void>;
}
class TaskDefinitionRegistryImpl implements ITaskDefinitionRegistry {
private taskTypes: IStringDictionary<Tasks.TaskDefinition>;
private readyPromise: Promise<void>;
private _schema: IJSONSchema | undefined;
private _onDefinitionsChanged: Emitter<void> = new Emitter();
public onDefinitionsChanged: Event<void> = this._onDefinitionsChanged.event;
constructor() {
this.taskTypes = Object.create(null);
this.readyPromise = new Promise<void>((resolve, reject) => {
taskDefinitionsExtPoint.setHandler((extensions, delta) => {
try {
for (let extension of delta.removed) {
let taskTypes = extension.value;
for (let taskType of taskTypes) {
if (this.taskTypes && taskType.type && this.taskTypes[taskType.type]) {
delete this.taskTypes[taskType.type];
}
}
}
for (let extension of delta.added) {
let taskTypes = extension.value;
for (let taskType of taskTypes) {
let type = Configuration.from(taskType, extension.description.identifier, extension.collector);
if (type) {
this.taskTypes[type.taskType] = type;
}
}
}
if ((delta.removed.length > 0) || (delta.added.length > 0)) {
this._onDefinitionsChanged.fire();
}
} catch (error) {
}
resolve(undefined);
});
});
}
public onReady(): Promise<void> {
return this.readyPromise;
}
public get(key: string): Tasks.TaskDefinition {
return this.taskTypes[key];
}
public all(): Tasks.TaskDefinition[] {
return Object.keys(this.taskTypes).map(key => this.taskTypes[key]);
}
public getJsonSchema(): IJSONSchema {
if (this._schema === undefined) {
let schemas: IJSONSchema[] = [];
for (let definition of this.all()) {
let schema: IJSONSchema = {
type: 'object',
additionalProperties: false
};
if (definition.required.length > 0) {
schema.required = definition.required.slice(0);
}
if (definition.properties !== undefined) {
schema.properties = Objects.deepClone(definition.properties);
} else {
schema.properties = Object.create(null);
}
schema.properties!.type = {
type: 'string',
enum: [definition.taskType]
};
schemas.push(schema);
}
this._schema = { oneOf: schemas };
}
return this._schema;
}
}
export const TaskDefinitionRegistry: ITaskDefinitionRegistry = new TaskDefinitionRegistryImpl();