@theia/task 1.34.2 → 1.34.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (169) hide show
  1. package/LICENSE +641 -641
  2. package/README.md +193 -193
  3. package/lib/browser/index.d.ts +6 -6
  4. package/lib/browser/index.js +33 -33
  5. package/lib/browser/process/process-task-contribution.d.ts +6 -6
  6. package/lib/browser/process/process-task-contribution.js +43 -43
  7. package/lib/browser/process/process-task-frontend-module.d.ts +2 -2
  8. package/lib/browser/process/process-task-frontend-module.js +27 -27
  9. package/lib/browser/process/process-task-resolver.d.ts +17 -17
  10. package/lib/browser/process/process-task-resolver.js +93 -93
  11. package/lib/browser/provided-task-configurations.d.ts +32 -32
  12. package/lib/browser/provided-task-configurations.js +171 -171
  13. package/lib/browser/provided-task-configurations.spec.d.ts +1 -1
  14. package/lib/browser/provided-task-configurations.spec.js +43 -43
  15. package/lib/browser/quick-open-task.d.ts +129 -129
  16. package/lib/browser/quick-open-task.js +636 -636
  17. package/lib/browser/task-configuration-manager.d.ts +64 -64
  18. package/lib/browser/task-configuration-manager.js +261 -261
  19. package/lib/browser/task-configuration-model.d.ts +32 -32
  20. package/lib/browser/task-configuration-model.js +79 -79
  21. package/lib/browser/task-configurations.d.ts +127 -127
  22. package/lib/browser/task-configurations.js +479 -479
  23. package/lib/browser/task-contribution.d.ts +173 -173
  24. package/lib/browser/task-contribution.js +199 -199
  25. package/lib/browser/task-definition-registry.d.ts +38 -38
  26. package/lib/browser/task-definition-registry.js +131 -131
  27. package/lib/browser/task-definition-registry.spec.d.ts +1 -1
  28. package/lib/browser/task-definition-registry.spec.js +170 -170
  29. package/lib/browser/task-frontend-contribution.d.ts +56 -56
  30. package/lib/browser/task-frontend-contribution.js +353 -353
  31. package/lib/browser/task-frontend-module.d.ts +5 -5
  32. package/lib/browser/task-frontend-module.js +79 -79
  33. package/lib/browser/task-name-resolver.d.ts +13 -13
  34. package/lib/browser/task-name-resolver.js +65 -65
  35. package/lib/browser/task-node.d.ts +9 -9
  36. package/lib/browser/task-node.js +17 -17
  37. package/lib/browser/task-preferences.d.ts +4 -4
  38. package/lib/browser/task-preferences.js +40 -40
  39. package/lib/browser/task-problem-matcher-registry.d.ts +40 -40
  40. package/lib/browser/task-problem-matcher-registry.js +310 -310
  41. package/lib/browser/task-problem-pattern-registry.d.ts +23 -23
  42. package/lib/browser/task-problem-pattern-registry.js +208 -208
  43. package/lib/browser/task-schema-updater.d.ts +58 -58
  44. package/lib/browser/task-schema-updater.js +672 -672
  45. package/lib/browser/task-service.d.ts +276 -276
  46. package/lib/browser/task-service.js +1119 -1119
  47. package/lib/browser/task-source-resolver.d.ts +9 -9
  48. package/lib/browser/task-source-resolver.js +51 -51
  49. package/lib/browser/task-templates.d.ts +13 -13
  50. package/lib/browser/task-templates.js +161 -161
  51. package/lib/browser/task-terminal-widget-manager.d.ts +44 -44
  52. package/lib/browser/task-terminal-widget-manager.js +224 -224
  53. package/lib/browser/tasks-monaco-contribution.d.ts +1 -1
  54. package/lib/browser/tasks-monaco-contribution.js +27 -27
  55. package/lib/common/index.d.ts +3 -3
  56. package/lib/common/index.js +30 -30
  57. package/lib/common/problem-matcher-protocol.d.ts +124 -124
  58. package/lib/common/problem-matcher-protocol.js +132 -132
  59. package/lib/common/process/task-protocol.d.ts +68 -68
  60. package/lib/common/process/task-protocol.js +33 -33
  61. package/lib/common/task-common-module.d.ts +7 -7
  62. package/lib/common/task-common-module.js +33 -33
  63. package/lib/common/task-protocol.d.ts +190 -190
  64. package/lib/common/task-protocol.js +125 -125
  65. package/lib/common/task-watcher.d.ts +17 -17
  66. package/lib/common/task-watcher.js +86 -86
  67. package/lib/node/custom/custom-task-runner-backend-module.d.ts +2 -2
  68. package/lib/node/custom/custom-task-runner-backend-module.js +36 -36
  69. package/lib/node/custom/custom-task-runner-contribution.d.ts +6 -6
  70. package/lib/node/custom/custom-task-runner-contribution.js +42 -42
  71. package/lib/node/custom/custom-task-runner.d.ts +15 -15
  72. package/lib/node/custom/custom-task-runner.js +69 -69
  73. package/lib/node/custom/custom-task.d.ts +20 -20
  74. package/lib/node/custom/custom-task.js +78 -78
  75. package/lib/node/index.d.ts +3 -3
  76. package/lib/node/index.js +30 -30
  77. package/lib/node/process/process-task-runner-backend-module.d.ts +2 -2
  78. package/lib/node/process/process-task-runner-backend-module.js +36 -36
  79. package/lib/node/process/process-task-runner-contribution.d.ts +6 -6
  80. package/lib/node/process/process-task-runner-contribution.js +43 -43
  81. package/lib/node/process/process-task-runner.d.ts +60 -60
  82. package/lib/node/process/process-task-runner.js +352 -352
  83. package/lib/node/process/process-task.d.ts +26 -26
  84. package/lib/node/process/process-task.js +139 -139
  85. package/lib/node/process/process-task.spec.d.ts +1 -1
  86. package/lib/node/process/process-task.spec.js +29 -29
  87. package/lib/node/task-abstract-line-matcher.d.ts +49 -49
  88. package/lib/node/task-abstract-line-matcher.js +278 -278
  89. package/lib/node/task-backend-application-contribution.d.ts +8 -8
  90. package/lib/node/task-backend-application-contribution.js +48 -48
  91. package/lib/node/task-backend-module.d.ts +3 -3
  92. package/lib/node/task-backend-module.js +50 -50
  93. package/lib/node/task-line-matchers.d.ts +27 -27
  94. package/lib/node/task-line-matchers.js +121 -121
  95. package/lib/node/task-manager.d.ts +55 -55
  96. package/lib/node/task-manager.js +135 -135
  97. package/lib/node/task-problem-collector.d.ts +10 -10
  98. package/lib/node/task-problem-collector.js +57 -57
  99. package/lib/node/task-problem-collector.spec.d.ts +1 -1
  100. package/lib/node/task-problem-collector.spec.js +310 -310
  101. package/lib/node/task-runner-protocol.d.ts +16 -16
  102. package/lib/node/task-runner-protocol.js +19 -19
  103. package/lib/node/task-runner.d.ts +50 -50
  104. package/lib/node/task-runner.js +97 -97
  105. package/lib/node/task-server.d.ts +39 -39
  106. package/lib/node/task-server.js +237 -237
  107. package/lib/node/task-server.slow-spec.d.ts +1 -1
  108. package/lib/node/task-server.slow-spec.js +395 -395
  109. package/lib/node/task.d.ts +50 -50
  110. package/lib/node/task.js +77 -77
  111. package/lib/node/test/task-test-container.d.ts +2 -2
  112. package/lib/node/test/task-test-container.js +58 -58
  113. package/package.json +13 -13
  114. package/src/browser/index.ts +22 -22
  115. package/src/browser/process/process-task-contribution.ts +31 -31
  116. package/src/browser/process/process-task-frontend-module.ts +27 -27
  117. package/src/browser/process/process-task-resolver.ts +89 -89
  118. package/src/browser/provided-task-configurations.spec.ts +46 -46
  119. package/src/browser/provided-task-configurations.ts +170 -170
  120. package/src/browser/quick-open-task.ts +701 -701
  121. package/src/browser/style/index.css +19 -19
  122. package/src/browser/task-configuration-manager.ts +256 -256
  123. package/src/browser/task-configuration-model.ts +101 -101
  124. package/src/browser/task-configurations.ts +507 -507
  125. package/src/browser/task-contribution.ts +254 -254
  126. package/src/browser/task-definition-registry.spec.ts +203 -203
  127. package/src/browser/task-definition-registry.ts +128 -128
  128. package/src/browser/task-frontend-contribution.ts +396 -396
  129. package/src/browser/task-frontend-module.ts +86 -86
  130. package/src/browser/task-name-resolver.ts +55 -55
  131. package/src/browser/task-node.ts +37 -37
  132. package/src/browser/task-preferences.ts +40 -40
  133. package/src/browser/task-problem-matcher-registry.ts +308 -308
  134. package/src/browser/task-problem-pattern-registry.ts +195 -195
  135. package/src/browser/task-schema-updater.ts +703 -703
  136. package/src/browser/task-service.ts +1163 -1163
  137. package/src/browser/task-source-resolver.ts +36 -36
  138. package/src/browser/task-templates.ts +168 -168
  139. package/src/browser/task-terminal-widget-manager.ts +221 -221
  140. package/src/browser/tasks-monaco-contribution.ts +27 -27
  141. package/src/common/index.ts +19 -19
  142. package/src/common/problem-matcher-protocol.ts +235 -235
  143. package/src/common/process/task-protocol.ts +97 -97
  144. package/src/common/task-common-module.ts +34 -34
  145. package/src/common/task-protocol.ts +310 -310
  146. package/src/common/task-watcher.ts +78 -78
  147. package/src/node/custom/custom-task-runner-backend-module.ts +37 -37
  148. package/src/node/custom/custom-task-runner-contribution.ts +30 -30
  149. package/src/node/custom/custom-task-runner.ts +60 -60
  150. package/src/node/custom/custom-task.ts +73 -73
  151. package/src/node/index.ts +19 -19
  152. package/src/node/process/process-task-runner-backend-module.ts +37 -37
  153. package/src/node/process/process-task-runner-contribution.ts +31 -31
  154. package/src/node/process/process-task-runner.ts +371 -371
  155. package/src/node/process/process-task.spec.ts +30 -30
  156. package/src/node/process/process-task.ts +147 -147
  157. package/src/node/task-abstract-line-matcher.ts +317 -317
  158. package/src/node/task-backend-application-contribution.ts +36 -36
  159. package/src/node/task-backend-module.ts +57 -57
  160. package/src/node/task-line-matchers.ts +127 -127
  161. package/src/node/task-manager.ts +129 -129
  162. package/src/node/task-problem-collector.spec.ts +338 -338
  163. package/src/node/task-problem-collector.ts +62 -62
  164. package/src/node/task-runner-protocol.ts +33 -33
  165. package/src/node/task-runner.ts +96 -96
  166. package/src/node/task-server.slow-spec.ts +444 -444
  167. package/src/node/task-server.ts +263 -263
  168. package/src/node/task.ts +103 -103
  169. package/src/node/test/task-test-container.ts +63 -63
@@ -1,507 +1,507 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2017-2018 Ericsson and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
18
- import {
19
- TaskConfiguration,
20
- TaskCustomization,
21
- TaskDefinition,
22
- TaskOutputPresentation,
23
- TaskConfigurationScope,
24
- TaskScope
25
- } from '../common';
26
- import { TaskDefinitionRegistry } from './task-definition-registry';
27
- import { ProvidedTaskConfigurations } from './provided-task-configurations';
28
- import { TaskConfigurationManager, TasksChange } from './task-configuration-manager';
29
- import { TaskSchemaUpdater } from './task-schema-updater';
30
- import { TaskSourceResolver } from './task-source-resolver';
31
- import { Disposable, DisposableCollection } from '@theia/core/lib/common';
32
- import { FileChangeType } from '@theia/filesystem/lib/common/filesystem-watcher-protocol';
33
- import { WorkspaceService } from '@theia/workspace/lib/browser';
34
-
35
- export interface TaskConfigurationClient {
36
- /**
37
- * The task configuration file has changed, so a client might want to refresh its configurations
38
- * @returns an array of strings, each one being a task label
39
- */
40
- taskConfigurationChanged: (event: string[]) => void;
41
- }
42
-
43
- /**
44
- * Watches a tasks.json configuration file and provides a parsed version of the contained task configurations
45
- */
46
- @injectable()
47
- export class TaskConfigurations implements Disposable {
48
- protected readonly toDispose = new DisposableCollection();
49
- /**
50
- * Map of source (path of root folder that the task configs come from) and task config map.
51
- * For the inner map (i.e., task config map), the key is task label and value TaskConfiguration
52
- */
53
- protected tasksMap = new Map<string, Map<string, TaskConfiguration>>();
54
- /**
55
- * Map of source (path of root folder that the task configs come from) and task customizations map.
56
- */
57
- protected taskCustomizationMap = new Map<string, TaskCustomization[]>();
58
-
59
- protected client: TaskConfigurationClient | undefined = undefined;
60
-
61
- /**
62
- * Map of source (path of root folder that the task configs come from) and raw task configurations / customizations.
63
- * This map is used to store the data from `tasks.json` files in workspace.
64
- */
65
- private rawTaskConfigurations = new Map<string, TaskCustomization[]>();
66
-
67
- @inject(WorkspaceService)
68
- protected readonly workspaceService: WorkspaceService;
69
-
70
- @inject(TaskDefinitionRegistry)
71
- protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
72
-
73
- @inject(ProvidedTaskConfigurations)
74
- protected readonly providedTaskConfigurations: ProvidedTaskConfigurations;
75
-
76
- @inject(TaskConfigurationManager)
77
- protected readonly taskConfigurationManager: TaskConfigurationManager;
78
-
79
- @inject(TaskSchemaUpdater)
80
- protected readonly taskSchemaUpdater: TaskSchemaUpdater;
81
-
82
- @inject(TaskSourceResolver)
83
- protected readonly taskSourceResolver: TaskSourceResolver;
84
-
85
- constructor() {
86
- this.toDispose.push(Disposable.create(() => {
87
- this.tasksMap.clear();
88
- this.taskCustomizationMap.clear();
89
- this.rawTaskConfigurations.clear();
90
- this.client = undefined;
91
- }));
92
- }
93
-
94
- @postConstruct()
95
- protected init(): void {
96
- this.toDispose.push(
97
- this.taskConfigurationManager.onDidChangeTaskConfig(async change => {
98
- try {
99
- await this.onDidTaskFileChange([change]);
100
- if (this.client) {
101
- this.client.taskConfigurationChanged(this.getTaskLabels());
102
- }
103
- } catch (err) {
104
- console.error(err);
105
- }
106
- })
107
- );
108
- this.reorganizeTasks();
109
- this.toDispose.push(this.taskSchemaUpdater.onDidChangeTaskSchema(() => this.reorganizeTasks()));
110
- }
111
-
112
- setClient(client: TaskConfigurationClient): void {
113
- this.client = client;
114
- }
115
-
116
- dispose(): void {
117
- this.toDispose.dispose();
118
- }
119
-
120
- /** returns the list of known task labels */
121
- getTaskLabels(): string[] {
122
- return Array.from(this.tasksMap.values()).reduce((acc, labelConfigMap) => acc.concat(Array.from(labelConfigMap.keys())), [] as string[]);
123
- }
124
-
125
- /**
126
- * returns a collection of known tasks, which includes:
127
- * - all the configured tasks in `tasks.json`, and
128
- * - the customized detected tasks.
129
- *
130
- * The invalid task configs are not returned.
131
- */
132
- async getTasks(token: number): Promise<TaskConfiguration[]> {
133
- const configuredTasks = Array.from(this.tasksMap.values()).reduce((acc, labelConfigMap) => acc.concat(Array.from(labelConfigMap.values())), [] as TaskConfiguration[]);
134
- const detectedTasksAsConfigured: TaskConfiguration[] = [];
135
- for (const [rootFolder, customizations] of Array.from(this.taskCustomizationMap.entries())) {
136
- for (const customization of customizations) {
137
- // TODO: getTasksToCustomize() will ask all task providers to contribute tasks. Doing this in a loop is bad.
138
- const detected = await this.providedTaskConfigurations.getTaskToCustomize(token, customization, rootFolder);
139
- if (detected) {
140
- // there might be a provided task that has a different scope from the task we're inspecting
141
- detectedTasksAsConfigured.push({ ...detected, ...customization });
142
- }
143
- }
144
- }
145
- return [...configuredTasks, ...detectedTasksAsConfigured];
146
- }
147
-
148
- getRawTaskConfigurations(scope?: TaskConfigurationScope): (TaskCustomization | TaskConfiguration)[] {
149
- if (scope === undefined) {
150
- const tasks: (TaskCustomization | TaskConfiguration)[] = [];
151
- for (const configs of this.rawTaskConfigurations.values()) {
152
- tasks.push(...configs);
153
- }
154
- return tasks;
155
- }
156
-
157
- const scopeKey = this.getKeyFromScope(scope);
158
- if (this.rawTaskConfigurations.has(scopeKey)) {
159
- return Array.from(this.rawTaskConfigurations.get(scopeKey)!.values());
160
- }
161
- return [];
162
- }
163
-
164
- /**
165
- * returns a collection of invalid task configs as per the task schema defined in Theia.
166
- */
167
- getInvalidTaskConfigurations(): (TaskCustomization | TaskConfiguration)[] {
168
- const invalidTaskConfigs: (TaskCustomization | TaskConfiguration)[] = [];
169
- for (const taskConfigs of this.rawTaskConfigurations.values()) {
170
- for (const taskConfig of taskConfigs) {
171
- const isValid = this.isTaskConfigValid(taskConfig);
172
- if (!isValid) {
173
- invalidTaskConfigs.push(taskConfig);
174
- }
175
- }
176
- }
177
- return invalidTaskConfigs;
178
- }
179
-
180
- /** returns the task configuration for a given label or undefined if none */
181
- getTask(scope: TaskConfigurationScope, taskLabel: string): TaskConfiguration | undefined {
182
- const labelConfigMap = this.tasksMap.get(this.getKeyFromScope(scope));
183
- if (labelConfigMap) {
184
- return labelConfigMap.get(taskLabel);
185
- }
186
- }
187
-
188
- /** returns the customized task for a given label or undefined if none */
189
- async getCustomizedTask(token: number, scope: TaskConfigurationScope, taskLabel: string): Promise<TaskConfiguration | undefined> {
190
- const customizations = this.taskCustomizationMap.get(this.getKeyFromScope(scope));
191
- if (customizations) {
192
- const customization = customizations.find(cus => cus.label === taskLabel);
193
- if (customization) {
194
- const detected = await this.providedTaskConfigurations.getTaskToCustomize(token, customization, scope);
195
- if (detected) {
196
- return {
197
- ...detected,
198
- ...customization,
199
- type: detected.type
200
- };
201
- }
202
- }
203
- }
204
- }
205
-
206
- /** removes tasks configured in the given task config file */
207
- private removeTasks(scope: TaskConfigurationScope): void {
208
- const source = this.getKeyFromScope(scope);
209
- this.tasksMap.delete(source);
210
- this.taskCustomizationMap.delete(source);
211
- }
212
-
213
- /**
214
- * Removes task customization objects found in the given task config file from the memory.
215
- * Please note: this function does not modify the task config file.
216
- */
217
- private removeTaskCustomizations(scope: TaskConfigurationScope): void {
218
- const source = this.getKeyFromScope(scope);
219
- this.taskCustomizationMap.delete(source);
220
- }
221
-
222
- /**
223
- * Returns the task customizations by type from a given root folder in the workspace.
224
- * @param type the type of task customizations
225
- * @param rootFolder the root folder to find task customizations from. If `undefined`, this function returns an empty array.
226
- */
227
- private getTaskCustomizations(type: string, scope: TaskConfigurationScope): TaskCustomization[] {
228
- const customizationInRootFolder = this.taskCustomizationMap.get(this.getKeyFromScope(scope));
229
- if (customizationInRootFolder) {
230
- return customizationInRootFolder.filter(c => c.type === type);
231
- } else {
232
- return [];
233
- }
234
- }
235
-
236
- /**
237
- * Returns the customization object in `tasks.json` for the given task. Please note, this function
238
- * returns `undefined` if the given task is not a detected task, because configured tasks don't need
239
- * customization objects - users can modify its config directly in `tasks.json`.
240
- * @param taskConfig The task config, which could either be a configured task or a detected task.
241
- */
242
- getCustomizationForTask(taskConfig: TaskConfiguration): TaskCustomization | undefined {
243
- if (!this.isDetectedTask(taskConfig)) {
244
- return undefined;
245
- }
246
-
247
- const customizationByType = this.getTaskCustomizations(taskConfig.type, taskConfig._scope) || [];
248
- const hasCustomization = customizationByType.length > 0;
249
- if (hasCustomization) {
250
- const taskDefinition = this.taskDefinitionRegistry.getDefinition(taskConfig);
251
- if (taskDefinition) {
252
- const required = taskDefinition.properties.required || [];
253
- // Only support having one customization per task.
254
- return customizationByType.find(customization => required.every(property => customization[property] === taskConfig[property]));
255
- }
256
- }
257
- return undefined;
258
- }
259
-
260
- /**
261
- * Called when a change, to a config file we watch, is detected.
262
- */
263
- protected async onDidTaskFileChange(fileChanges: TasksChange[]): Promise<void> {
264
- for (const change of fileChanges) {
265
- if (change.type === FileChangeType.DELETED) {
266
- this.removeTasks(change.scope);
267
- } else {
268
- // re-parse the config file
269
- await this.refreshTasks(change.scope);
270
- }
271
- }
272
- }
273
-
274
- /**
275
- * Read the task configs from the task configuration manager, and updates the list of available tasks.
276
- */
277
- protected async refreshTasks(scope: TaskConfigurationScope): Promise<void> {
278
- await this.readTasks(scope);
279
-
280
- this.removeTasks(scope);
281
- this.removeTaskCustomizations(scope);
282
-
283
- this.reorganizeTasks();
284
- }
285
-
286
- /** parses a config file and extracts the tasks launch configurations */
287
- protected async readTasks(scope: TaskConfigurationScope): Promise<(TaskCustomization | TaskConfiguration)[] | undefined> {
288
- const rawConfigArray = this.taskConfigurationManager.getTasks(scope);
289
- const key = this.getKeyFromScope(scope);
290
- if (this.rawTaskConfigurations.has(key)) {
291
- this.rawTaskConfigurations.delete(key);
292
- }
293
- this.rawTaskConfigurations.set(key, rawConfigArray);
294
- return rawConfigArray;
295
- }
296
-
297
- async openUserTasks(): Promise<void> {
298
- await this.taskConfigurationManager.openConfiguration(TaskScope.Global);
299
- }
300
-
301
- /** Adds given task to a config file and opens the file to provide ability to edit task configuration. */
302
- async configure(token: number, task: TaskConfiguration): Promise<void> {
303
- const scope = task._scope;
304
- if (scope === TaskScope.Global) {
305
- return this.openUserTasks();
306
- }
307
-
308
- const workspace = this.workspaceService.workspace;
309
- if (!workspace) {
310
- return;
311
- }
312
-
313
- const configuredAndCustomizedTasks = await this.getTasks(token);
314
- if (!configuredAndCustomizedTasks.some(t => this.taskDefinitionRegistry.compareTasks(t, task))) {
315
- await this.saveTask(scope, task);
316
- }
317
-
318
- try {
319
- await this.taskConfigurationManager.openConfiguration(scope);
320
- } catch (e) {
321
- console.error(`Error occurred while opening 'tasks.json' in ${this.taskSourceResolver.resolve(task)}.`, e);
322
- }
323
- }
324
-
325
- private getTaskCustomizationTemplate(task: TaskConfiguration): TaskCustomization | undefined {
326
- const definition = this.getTaskDefinition(task);
327
- if (!definition) {
328
- console.error('Detected / Contributed tasks should have a task definition.');
329
- return;
330
- }
331
- const customization: TaskCustomization = { type: task.type, runOptions: task.runOptions };
332
- definition.properties.all.forEach(p => {
333
- if (task[p] !== undefined) {
334
- customization[p] = task[p];
335
- }
336
- });
337
- if ('problemMatcher' in task) {
338
- const problemMatcher: string[] = [];
339
- if (Array.isArray(task.problemMatcher)) {
340
- problemMatcher.push(...task.problemMatcher.map(t => {
341
- if (typeof t === 'string') {
342
- return t;
343
- } else {
344
- return t.name!;
345
- }
346
- }));
347
- } else if (typeof task.problemMatcher === 'string') {
348
- problemMatcher.push(task.problemMatcher);
349
- } else if (task.problemMatcher) {
350
- problemMatcher.push(task.problemMatcher.name!);
351
- }
352
- customization.problemMatcher = problemMatcher.map(name => name.startsWith('$') ? name : `$${name}`);
353
- }
354
- if (task.group) {
355
- customization.group = task.group;
356
- }
357
-
358
- customization.label = task.label;
359
-
360
- return { ...customization };
361
- }
362
-
363
- /** Writes the task to a config file. Creates a config file if this one does not exist */
364
- saveTask(scope: TaskConfigurationScope, task: TaskConfiguration): Promise<boolean> {
365
- const { _source, $ident, ...preparedTask } = task;
366
- const customizedTaskTemplate = this.getTaskCustomizationTemplate(task) || preparedTask;
367
- return this.taskConfigurationManager.addTaskConfiguration(scope, customizedTaskTemplate);
368
- }
369
-
370
- /**
371
- * This function is called after a change in TaskDefinitionRegistry happens.
372
- * It checks all tasks that have been loaded, and re-organized them in `tasksMap` and `taskCustomizationMap`.
373
- */
374
- protected reorganizeTasks(): void {
375
- const newTaskMap = new Map<string, Map<string, TaskConfiguration>>();
376
- const newTaskCustomizationMap = new Map<string, TaskCustomization[]>();
377
- const addCustomization = (rootFolder: string, customization: TaskCustomization) => {
378
- if (newTaskCustomizationMap.has(rootFolder)) {
379
- newTaskCustomizationMap.get(rootFolder)!.push(customization);
380
- } else {
381
- newTaskCustomizationMap.set(rootFolder, [customization]);
382
- }
383
- };
384
- const addConfiguredTask = (rootFolder: string, label: string, configuredTask: TaskCustomization) => {
385
- if (newTaskMap.has(rootFolder)) {
386
- newTaskMap.get(rootFolder)!.set(label, configuredTask as TaskConfiguration);
387
- } else {
388
- const newConfigMap = new Map();
389
- newConfigMap.set(label, configuredTask);
390
- newTaskMap.set(rootFolder, newConfigMap);
391
- }
392
- };
393
-
394
- for (const [scopeKey, taskConfigs] of this.rawTaskConfigurations.entries()) {
395
- for (const taskConfig of taskConfigs) {
396
- const scope = this.getScopeFromKey(scopeKey);
397
- const isValid = this.isTaskConfigValid(taskConfig);
398
- if (!isValid) {
399
- continue;
400
- }
401
- const transformedTask = this.getTransformedRawTask(taskConfig, scope);
402
- if (this.isDetectedTask(transformedTask)) {
403
- addCustomization(scopeKey, transformedTask);
404
- } else {
405
- addConfiguredTask(scopeKey, transformedTask['label'] as string, transformedTask);
406
- }
407
- }
408
- }
409
-
410
- this.taskCustomizationMap = newTaskCustomizationMap;
411
- this.tasksMap = newTaskMap;
412
- }
413
-
414
- private getTransformedRawTask(rawTask: TaskCustomization | TaskConfiguration, scope: TaskConfigurationScope): TaskCustomization | TaskConfiguration {
415
- let taskConfig: TaskCustomization | TaskConfiguration;
416
- if (this.isDetectedTask(rawTask)) {
417
- const def = this.getTaskDefinition(rawTask);
418
- taskConfig = {
419
- ...rawTask,
420
- _source: def!.source,
421
- _scope: scope
422
- };
423
- } else {
424
- taskConfig = {
425
- ...rawTask,
426
- _source: scope,
427
- _scope: scope
428
- };
429
- }
430
- return {
431
- ...taskConfig,
432
- presentation: TaskOutputPresentation.fromJson(rawTask)
433
- };
434
- }
435
-
436
- /**
437
- * Returns `true` if the given task configuration is valid as per the task schema defined in Theia
438
- * or contributed by Theia extensions and plugins, `false` otherwise.
439
- */
440
- private isTaskConfigValid(task: TaskCustomization): boolean {
441
- return this.taskSchemaUpdater.validate({ tasks: [task] });
442
- }
443
-
444
- /**
445
- * Updates the task config in the `tasks.json`.
446
- * The task config, together with updates, will be written into the `tasks.json` if it is not found in the file.
447
- *
448
- * @param task task that the updates will be applied to
449
- * @param update the updates to be applied
450
- */
451
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
452
- async updateTaskConfig(token: number, task: TaskConfiguration, update: { [name: string]: any }): Promise<void> {
453
- const scope = task._scope;
454
- const configuredAndCustomizedTasks = await this.getTasks(token);
455
- if (configuredAndCustomizedTasks.some(t => this.taskDefinitionRegistry.compareTasks(t, task))) { // task is already in `tasks.json`
456
- const jsonTasks = this.taskConfigurationManager.getTasks(scope);
457
- if (jsonTasks) {
458
- const ind = jsonTasks.findIndex((t: TaskCustomization | TaskConfiguration) => {
459
- if (t.type !== (task.type)) {
460
- return false;
461
- }
462
- const def = this.taskDefinitionRegistry.getDefinition(t);
463
- if (def) {
464
- return def.properties.all.every(p => t[p] === task[p]);
465
- }
466
- return t.label === task.label;
467
- });
468
- jsonTasks[ind] = {
469
- ...jsonTasks[ind],
470
- ...update
471
- };
472
- }
473
- this.taskConfigurationManager.setTaskConfigurations(scope, jsonTasks);
474
- } else { // task is not in `tasks.json`
475
- Object.keys(update).forEach(taskProperty => {
476
- task[taskProperty] = update[taskProperty];
477
- });
478
- this.saveTask(scope, task);
479
- }
480
- }
481
-
482
- private getKeyFromScope(scope: TaskConfigurationScope): string {
483
- // Converting the enums to string will not yield a valid URI, so the keys will be distinct from any URI.
484
- return scope.toString();
485
- }
486
-
487
- private getScopeFromKey(key: string): TaskConfigurationScope {
488
- if (TaskScope.Global.toString() === key) {
489
- return TaskScope.Global;
490
- } else if (TaskScope.Workspace.toString() === key) {
491
- return TaskScope.Workspace;
492
- } else {
493
- return key;
494
- }
495
- }
496
-
497
- /** checks if the config is a detected / contributed task */
498
- private isDetectedTask(task: TaskConfiguration | TaskCustomization): boolean {
499
- const taskDefinition = this.getTaskDefinition(task);
500
- // it is considered as a customization if the task definition registry finds a def for the task configuration
501
- return !!taskDefinition;
502
- }
503
-
504
- private getTaskDefinition(task: TaskCustomization): TaskDefinition | undefined {
505
- return this.taskDefinitionRegistry.getDefinition(task);
506
- }
507
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2017-2018 Ericsson and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
18
+ import {
19
+ TaskConfiguration,
20
+ TaskCustomization,
21
+ TaskDefinition,
22
+ TaskOutputPresentation,
23
+ TaskConfigurationScope,
24
+ TaskScope
25
+ } from '../common';
26
+ import { TaskDefinitionRegistry } from './task-definition-registry';
27
+ import { ProvidedTaskConfigurations } from './provided-task-configurations';
28
+ import { TaskConfigurationManager, TasksChange } from './task-configuration-manager';
29
+ import { TaskSchemaUpdater } from './task-schema-updater';
30
+ import { TaskSourceResolver } from './task-source-resolver';
31
+ import { Disposable, DisposableCollection } from '@theia/core/lib/common';
32
+ import { FileChangeType } from '@theia/filesystem/lib/common/filesystem-watcher-protocol';
33
+ import { WorkspaceService } from '@theia/workspace/lib/browser';
34
+
35
+ export interface TaskConfigurationClient {
36
+ /**
37
+ * The task configuration file has changed, so a client might want to refresh its configurations
38
+ * @returns an array of strings, each one being a task label
39
+ */
40
+ taskConfigurationChanged: (event: string[]) => void;
41
+ }
42
+
43
+ /**
44
+ * Watches a tasks.json configuration file and provides a parsed version of the contained task configurations
45
+ */
46
+ @injectable()
47
+ export class TaskConfigurations implements Disposable {
48
+ protected readonly toDispose = new DisposableCollection();
49
+ /**
50
+ * Map of source (path of root folder that the task configs come from) and task config map.
51
+ * For the inner map (i.e., task config map), the key is task label and value TaskConfiguration
52
+ */
53
+ protected tasksMap = new Map<string, Map<string, TaskConfiguration>>();
54
+ /**
55
+ * Map of source (path of root folder that the task configs come from) and task customizations map.
56
+ */
57
+ protected taskCustomizationMap = new Map<string, TaskCustomization[]>();
58
+
59
+ protected client: TaskConfigurationClient | undefined = undefined;
60
+
61
+ /**
62
+ * Map of source (path of root folder that the task configs come from) and raw task configurations / customizations.
63
+ * This map is used to store the data from `tasks.json` files in workspace.
64
+ */
65
+ private rawTaskConfigurations = new Map<string, TaskCustomization[]>();
66
+
67
+ @inject(WorkspaceService)
68
+ protected readonly workspaceService: WorkspaceService;
69
+
70
+ @inject(TaskDefinitionRegistry)
71
+ protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
72
+
73
+ @inject(ProvidedTaskConfigurations)
74
+ protected readonly providedTaskConfigurations: ProvidedTaskConfigurations;
75
+
76
+ @inject(TaskConfigurationManager)
77
+ protected readonly taskConfigurationManager: TaskConfigurationManager;
78
+
79
+ @inject(TaskSchemaUpdater)
80
+ protected readonly taskSchemaUpdater: TaskSchemaUpdater;
81
+
82
+ @inject(TaskSourceResolver)
83
+ protected readonly taskSourceResolver: TaskSourceResolver;
84
+
85
+ constructor() {
86
+ this.toDispose.push(Disposable.create(() => {
87
+ this.tasksMap.clear();
88
+ this.taskCustomizationMap.clear();
89
+ this.rawTaskConfigurations.clear();
90
+ this.client = undefined;
91
+ }));
92
+ }
93
+
94
+ @postConstruct()
95
+ protected init(): void {
96
+ this.toDispose.push(
97
+ this.taskConfigurationManager.onDidChangeTaskConfig(async change => {
98
+ try {
99
+ await this.onDidTaskFileChange([change]);
100
+ if (this.client) {
101
+ this.client.taskConfigurationChanged(this.getTaskLabels());
102
+ }
103
+ } catch (err) {
104
+ console.error(err);
105
+ }
106
+ })
107
+ );
108
+ this.reorganizeTasks();
109
+ this.toDispose.push(this.taskSchemaUpdater.onDidChangeTaskSchema(() => this.reorganizeTasks()));
110
+ }
111
+
112
+ setClient(client: TaskConfigurationClient): void {
113
+ this.client = client;
114
+ }
115
+
116
+ dispose(): void {
117
+ this.toDispose.dispose();
118
+ }
119
+
120
+ /** returns the list of known task labels */
121
+ getTaskLabels(): string[] {
122
+ return Array.from(this.tasksMap.values()).reduce((acc, labelConfigMap) => acc.concat(Array.from(labelConfigMap.keys())), [] as string[]);
123
+ }
124
+
125
+ /**
126
+ * returns a collection of known tasks, which includes:
127
+ * - all the configured tasks in `tasks.json`, and
128
+ * - the customized detected tasks.
129
+ *
130
+ * The invalid task configs are not returned.
131
+ */
132
+ async getTasks(token: number): Promise<TaskConfiguration[]> {
133
+ const configuredTasks = Array.from(this.tasksMap.values()).reduce((acc, labelConfigMap) => acc.concat(Array.from(labelConfigMap.values())), [] as TaskConfiguration[]);
134
+ const detectedTasksAsConfigured: TaskConfiguration[] = [];
135
+ for (const [rootFolder, customizations] of Array.from(this.taskCustomizationMap.entries())) {
136
+ for (const customization of customizations) {
137
+ // TODO: getTasksToCustomize() will ask all task providers to contribute tasks. Doing this in a loop is bad.
138
+ const detected = await this.providedTaskConfigurations.getTaskToCustomize(token, customization, rootFolder);
139
+ if (detected) {
140
+ // there might be a provided task that has a different scope from the task we're inspecting
141
+ detectedTasksAsConfigured.push({ ...detected, ...customization });
142
+ }
143
+ }
144
+ }
145
+ return [...configuredTasks, ...detectedTasksAsConfigured];
146
+ }
147
+
148
+ getRawTaskConfigurations(scope?: TaskConfigurationScope): (TaskCustomization | TaskConfiguration)[] {
149
+ if (scope === undefined) {
150
+ const tasks: (TaskCustomization | TaskConfiguration)[] = [];
151
+ for (const configs of this.rawTaskConfigurations.values()) {
152
+ tasks.push(...configs);
153
+ }
154
+ return tasks;
155
+ }
156
+
157
+ const scopeKey = this.getKeyFromScope(scope);
158
+ if (this.rawTaskConfigurations.has(scopeKey)) {
159
+ return Array.from(this.rawTaskConfigurations.get(scopeKey)!.values());
160
+ }
161
+ return [];
162
+ }
163
+
164
+ /**
165
+ * returns a collection of invalid task configs as per the task schema defined in Theia.
166
+ */
167
+ getInvalidTaskConfigurations(): (TaskCustomization | TaskConfiguration)[] {
168
+ const invalidTaskConfigs: (TaskCustomization | TaskConfiguration)[] = [];
169
+ for (const taskConfigs of this.rawTaskConfigurations.values()) {
170
+ for (const taskConfig of taskConfigs) {
171
+ const isValid = this.isTaskConfigValid(taskConfig);
172
+ if (!isValid) {
173
+ invalidTaskConfigs.push(taskConfig);
174
+ }
175
+ }
176
+ }
177
+ return invalidTaskConfigs;
178
+ }
179
+
180
+ /** returns the task configuration for a given label or undefined if none */
181
+ getTask(scope: TaskConfigurationScope, taskLabel: string): TaskConfiguration | undefined {
182
+ const labelConfigMap = this.tasksMap.get(this.getKeyFromScope(scope));
183
+ if (labelConfigMap) {
184
+ return labelConfigMap.get(taskLabel);
185
+ }
186
+ }
187
+
188
+ /** returns the customized task for a given label or undefined if none */
189
+ async getCustomizedTask(token: number, scope: TaskConfigurationScope, taskLabel: string): Promise<TaskConfiguration | undefined> {
190
+ const customizations = this.taskCustomizationMap.get(this.getKeyFromScope(scope));
191
+ if (customizations) {
192
+ const customization = customizations.find(cus => cus.label === taskLabel);
193
+ if (customization) {
194
+ const detected = await this.providedTaskConfigurations.getTaskToCustomize(token, customization, scope);
195
+ if (detected) {
196
+ return {
197
+ ...detected,
198
+ ...customization,
199
+ type: detected.type
200
+ };
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ /** removes tasks configured in the given task config file */
207
+ private removeTasks(scope: TaskConfigurationScope): void {
208
+ const source = this.getKeyFromScope(scope);
209
+ this.tasksMap.delete(source);
210
+ this.taskCustomizationMap.delete(source);
211
+ }
212
+
213
+ /**
214
+ * Removes task customization objects found in the given task config file from the memory.
215
+ * Please note: this function does not modify the task config file.
216
+ */
217
+ private removeTaskCustomizations(scope: TaskConfigurationScope): void {
218
+ const source = this.getKeyFromScope(scope);
219
+ this.taskCustomizationMap.delete(source);
220
+ }
221
+
222
+ /**
223
+ * Returns the task customizations by type from a given root folder in the workspace.
224
+ * @param type the type of task customizations
225
+ * @param rootFolder the root folder to find task customizations from. If `undefined`, this function returns an empty array.
226
+ */
227
+ private getTaskCustomizations(type: string, scope: TaskConfigurationScope): TaskCustomization[] {
228
+ const customizationInRootFolder = this.taskCustomizationMap.get(this.getKeyFromScope(scope));
229
+ if (customizationInRootFolder) {
230
+ return customizationInRootFolder.filter(c => c.type === type);
231
+ } else {
232
+ return [];
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Returns the customization object in `tasks.json` for the given task. Please note, this function
238
+ * returns `undefined` if the given task is not a detected task, because configured tasks don't need
239
+ * customization objects - users can modify its config directly in `tasks.json`.
240
+ * @param taskConfig The task config, which could either be a configured task or a detected task.
241
+ */
242
+ getCustomizationForTask(taskConfig: TaskConfiguration): TaskCustomization | undefined {
243
+ if (!this.isDetectedTask(taskConfig)) {
244
+ return undefined;
245
+ }
246
+
247
+ const customizationByType = this.getTaskCustomizations(taskConfig.type, taskConfig._scope) || [];
248
+ const hasCustomization = customizationByType.length > 0;
249
+ if (hasCustomization) {
250
+ const taskDefinition = this.taskDefinitionRegistry.getDefinition(taskConfig);
251
+ if (taskDefinition) {
252
+ const required = taskDefinition.properties.required || [];
253
+ // Only support having one customization per task.
254
+ return customizationByType.find(customization => required.every(property => customization[property] === taskConfig[property]));
255
+ }
256
+ }
257
+ return undefined;
258
+ }
259
+
260
+ /**
261
+ * Called when a change, to a config file we watch, is detected.
262
+ */
263
+ protected async onDidTaskFileChange(fileChanges: TasksChange[]): Promise<void> {
264
+ for (const change of fileChanges) {
265
+ if (change.type === FileChangeType.DELETED) {
266
+ this.removeTasks(change.scope);
267
+ } else {
268
+ // re-parse the config file
269
+ await this.refreshTasks(change.scope);
270
+ }
271
+ }
272
+ }
273
+
274
+ /**
275
+ * Read the task configs from the task configuration manager, and updates the list of available tasks.
276
+ */
277
+ protected async refreshTasks(scope: TaskConfigurationScope): Promise<void> {
278
+ await this.readTasks(scope);
279
+
280
+ this.removeTasks(scope);
281
+ this.removeTaskCustomizations(scope);
282
+
283
+ this.reorganizeTasks();
284
+ }
285
+
286
+ /** parses a config file and extracts the tasks launch configurations */
287
+ protected async readTasks(scope: TaskConfigurationScope): Promise<(TaskCustomization | TaskConfiguration)[] | undefined> {
288
+ const rawConfigArray = this.taskConfigurationManager.getTasks(scope);
289
+ const key = this.getKeyFromScope(scope);
290
+ if (this.rawTaskConfigurations.has(key)) {
291
+ this.rawTaskConfigurations.delete(key);
292
+ }
293
+ this.rawTaskConfigurations.set(key, rawConfigArray);
294
+ return rawConfigArray;
295
+ }
296
+
297
+ async openUserTasks(): Promise<void> {
298
+ await this.taskConfigurationManager.openConfiguration(TaskScope.Global);
299
+ }
300
+
301
+ /** Adds given task to a config file and opens the file to provide ability to edit task configuration. */
302
+ async configure(token: number, task: TaskConfiguration): Promise<void> {
303
+ const scope = task._scope;
304
+ if (scope === TaskScope.Global) {
305
+ return this.openUserTasks();
306
+ }
307
+
308
+ const workspace = this.workspaceService.workspace;
309
+ if (!workspace) {
310
+ return;
311
+ }
312
+
313
+ const configuredAndCustomizedTasks = await this.getTasks(token);
314
+ if (!configuredAndCustomizedTasks.some(t => this.taskDefinitionRegistry.compareTasks(t, task))) {
315
+ await this.saveTask(scope, task);
316
+ }
317
+
318
+ try {
319
+ await this.taskConfigurationManager.openConfiguration(scope);
320
+ } catch (e) {
321
+ console.error(`Error occurred while opening 'tasks.json' in ${this.taskSourceResolver.resolve(task)}.`, e);
322
+ }
323
+ }
324
+
325
+ private getTaskCustomizationTemplate(task: TaskConfiguration): TaskCustomization | undefined {
326
+ const definition = this.getTaskDefinition(task);
327
+ if (!definition) {
328
+ console.error('Detected / Contributed tasks should have a task definition.');
329
+ return;
330
+ }
331
+ const customization: TaskCustomization = { type: task.type, runOptions: task.runOptions };
332
+ definition.properties.all.forEach(p => {
333
+ if (task[p] !== undefined) {
334
+ customization[p] = task[p];
335
+ }
336
+ });
337
+ if ('problemMatcher' in task) {
338
+ const problemMatcher: string[] = [];
339
+ if (Array.isArray(task.problemMatcher)) {
340
+ problemMatcher.push(...task.problemMatcher.map(t => {
341
+ if (typeof t === 'string') {
342
+ return t;
343
+ } else {
344
+ return t.name!;
345
+ }
346
+ }));
347
+ } else if (typeof task.problemMatcher === 'string') {
348
+ problemMatcher.push(task.problemMatcher);
349
+ } else if (task.problemMatcher) {
350
+ problemMatcher.push(task.problemMatcher.name!);
351
+ }
352
+ customization.problemMatcher = problemMatcher.map(name => name.startsWith('$') ? name : `$${name}`);
353
+ }
354
+ if (task.group) {
355
+ customization.group = task.group;
356
+ }
357
+
358
+ customization.label = task.label;
359
+
360
+ return { ...customization };
361
+ }
362
+
363
+ /** Writes the task to a config file. Creates a config file if this one does not exist */
364
+ saveTask(scope: TaskConfigurationScope, task: TaskConfiguration): Promise<boolean> {
365
+ const { _source, $ident, ...preparedTask } = task;
366
+ const customizedTaskTemplate = this.getTaskCustomizationTemplate(task) || preparedTask;
367
+ return this.taskConfigurationManager.addTaskConfiguration(scope, customizedTaskTemplate);
368
+ }
369
+
370
+ /**
371
+ * This function is called after a change in TaskDefinitionRegistry happens.
372
+ * It checks all tasks that have been loaded, and re-organized them in `tasksMap` and `taskCustomizationMap`.
373
+ */
374
+ protected reorganizeTasks(): void {
375
+ const newTaskMap = new Map<string, Map<string, TaskConfiguration>>();
376
+ const newTaskCustomizationMap = new Map<string, TaskCustomization[]>();
377
+ const addCustomization = (rootFolder: string, customization: TaskCustomization) => {
378
+ if (newTaskCustomizationMap.has(rootFolder)) {
379
+ newTaskCustomizationMap.get(rootFolder)!.push(customization);
380
+ } else {
381
+ newTaskCustomizationMap.set(rootFolder, [customization]);
382
+ }
383
+ };
384
+ const addConfiguredTask = (rootFolder: string, label: string, configuredTask: TaskCustomization) => {
385
+ if (newTaskMap.has(rootFolder)) {
386
+ newTaskMap.get(rootFolder)!.set(label, configuredTask as TaskConfiguration);
387
+ } else {
388
+ const newConfigMap = new Map();
389
+ newConfigMap.set(label, configuredTask);
390
+ newTaskMap.set(rootFolder, newConfigMap);
391
+ }
392
+ };
393
+
394
+ for (const [scopeKey, taskConfigs] of this.rawTaskConfigurations.entries()) {
395
+ for (const taskConfig of taskConfigs) {
396
+ const scope = this.getScopeFromKey(scopeKey);
397
+ const isValid = this.isTaskConfigValid(taskConfig);
398
+ if (!isValid) {
399
+ continue;
400
+ }
401
+ const transformedTask = this.getTransformedRawTask(taskConfig, scope);
402
+ if (this.isDetectedTask(transformedTask)) {
403
+ addCustomization(scopeKey, transformedTask);
404
+ } else {
405
+ addConfiguredTask(scopeKey, transformedTask['label'] as string, transformedTask);
406
+ }
407
+ }
408
+ }
409
+
410
+ this.taskCustomizationMap = newTaskCustomizationMap;
411
+ this.tasksMap = newTaskMap;
412
+ }
413
+
414
+ private getTransformedRawTask(rawTask: TaskCustomization | TaskConfiguration, scope: TaskConfigurationScope): TaskCustomization | TaskConfiguration {
415
+ let taskConfig: TaskCustomization | TaskConfiguration;
416
+ if (this.isDetectedTask(rawTask)) {
417
+ const def = this.getTaskDefinition(rawTask);
418
+ taskConfig = {
419
+ ...rawTask,
420
+ _source: def!.source,
421
+ _scope: scope
422
+ };
423
+ } else {
424
+ taskConfig = {
425
+ ...rawTask,
426
+ _source: scope,
427
+ _scope: scope
428
+ };
429
+ }
430
+ return {
431
+ ...taskConfig,
432
+ presentation: TaskOutputPresentation.fromJson(rawTask)
433
+ };
434
+ }
435
+
436
+ /**
437
+ * Returns `true` if the given task configuration is valid as per the task schema defined in Theia
438
+ * or contributed by Theia extensions and plugins, `false` otherwise.
439
+ */
440
+ private isTaskConfigValid(task: TaskCustomization): boolean {
441
+ return this.taskSchemaUpdater.validate({ tasks: [task] });
442
+ }
443
+
444
+ /**
445
+ * Updates the task config in the `tasks.json`.
446
+ * The task config, together with updates, will be written into the `tasks.json` if it is not found in the file.
447
+ *
448
+ * @param task task that the updates will be applied to
449
+ * @param update the updates to be applied
450
+ */
451
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
452
+ async updateTaskConfig(token: number, task: TaskConfiguration, update: { [name: string]: any }): Promise<void> {
453
+ const scope = task._scope;
454
+ const configuredAndCustomizedTasks = await this.getTasks(token);
455
+ if (configuredAndCustomizedTasks.some(t => this.taskDefinitionRegistry.compareTasks(t, task))) { // task is already in `tasks.json`
456
+ const jsonTasks = this.taskConfigurationManager.getTasks(scope);
457
+ if (jsonTasks) {
458
+ const ind = jsonTasks.findIndex((t: TaskCustomization | TaskConfiguration) => {
459
+ if (t.type !== (task.type)) {
460
+ return false;
461
+ }
462
+ const def = this.taskDefinitionRegistry.getDefinition(t);
463
+ if (def) {
464
+ return def.properties.all.every(p => t[p] === task[p]);
465
+ }
466
+ return t.label === task.label;
467
+ });
468
+ jsonTasks[ind] = {
469
+ ...jsonTasks[ind],
470
+ ...update
471
+ };
472
+ }
473
+ this.taskConfigurationManager.setTaskConfigurations(scope, jsonTasks);
474
+ } else { // task is not in `tasks.json`
475
+ Object.keys(update).forEach(taskProperty => {
476
+ task[taskProperty] = update[taskProperty];
477
+ });
478
+ this.saveTask(scope, task);
479
+ }
480
+ }
481
+
482
+ private getKeyFromScope(scope: TaskConfigurationScope): string {
483
+ // Converting the enums to string will not yield a valid URI, so the keys will be distinct from any URI.
484
+ return scope.toString();
485
+ }
486
+
487
+ private getScopeFromKey(key: string): TaskConfigurationScope {
488
+ if (TaskScope.Global.toString() === key) {
489
+ return TaskScope.Global;
490
+ } else if (TaskScope.Workspace.toString() === key) {
491
+ return TaskScope.Workspace;
492
+ } else {
493
+ return key;
494
+ }
495
+ }
496
+
497
+ /** checks if the config is a detected / contributed task */
498
+ private isDetectedTask(task: TaskConfiguration | TaskCustomization): boolean {
499
+ const taskDefinition = this.getTaskDefinition(task);
500
+ // it is considered as a customization if the task definition registry finds a def for the task configuration
501
+ return !!taskDefinition;
502
+ }
503
+
504
+ private getTaskDefinition(task: TaskCustomization): TaskDefinition | undefined {
505
+ return this.taskDefinitionRegistry.getDefinition(task);
506
+ }
507
+ }