-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtask-service.ts
1152 lines (1045 loc) · 54.5 KB
/
task-service.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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************************
* Copyright (C) 2017 Ericsson 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
********************************************************************************/
import { ApplicationShell, FrontendApplication, WidgetManager, WidgetOpenMode } from '@theia/core/lib/browser';
import { open, OpenerService } from '@theia/core/lib/browser/opener-service';
import { CommandService, ILogger } from '@theia/core/lib/common';
import { MessageService } from '@theia/core/lib/common/message-service';
import { Deferred } from '@theia/core/lib/common/promise-util';
import { QuickPickItem, QuickPickService } from '@theia/core/lib/common/quick-pick-service';
import { LabelProvider } from '@theia/core/lib/browser/label-provider';
import URI from '@theia/core/lib/common/uri';
import { EditorManager } from '@theia/editor/lib/browser';
import { ProblemManager } from '@theia/markers/lib/browser/problem/problem-manager';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
import { TerminalWidget } from '@theia/terminal/lib/browser/base/terminal-widget';
import { TerminalWidgetFactoryOptions } from '@theia/terminal/lib/browser/terminal-widget-impl';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { inject, injectable, named, postConstruct } from 'inversify';
import { DiagnosticSeverity, Range } from 'vscode-languageserver-types';
import {
ApplyToKind,
BackgroundTaskEndedEvent,
DependsOrder,
NamedProblemMatcher,
ProblemMatchData,
ProblemMatcher,
RevealKind,
RunTaskOption,
TaskConfiguration,
TaskConfigurationScope,
TaskCustomization,
TaskDefinition,
TaskExitedEvent,
TaskIdentifier,
TaskInfo,
TaskOutputPresentation,
TaskOutputProcessedEvent,
TaskServer
} from '../common';
import { TaskWatcher } from '../common/task-watcher';
import { ProvidedTaskConfigurations } from './provided-task-configurations';
import { TaskConfigurationClient, TaskConfigurations } from './task-configurations';
import { TaskResolverRegistry } from './task-contribution';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { TaskNameResolver } from './task-name-resolver';
import { TaskSourceResolver } from './task-source-resolver';
import { ProblemMatcherRegistry } from './task-problem-matcher-registry';
import { TaskSchemaUpdater } from './task-schema-updater';
import { TaskConfigurationManager } from './task-configuration-manager';
import { PROBLEMS_WIDGET_ID, ProblemWidget } from '@theia/markers/lib/browser/problem/problem-widget';
import { TaskNode } from './task-node';
import { MonacoWorkspace } from '@theia/monaco/lib/browser/monaco-workspace';
import { TaskTerminalWidgetManager } from './task-terminal-widget-manager';
export interface QuickPickProblemMatcherItem {
problemMatchers: NamedProblemMatcher[] | undefined;
learnMore?: boolean;
}
interface TaskGraphNode {
taskConfiguration: TaskConfiguration;
node: TaskNode;
}
export enum TaskEndedTypes {
TaskExited,
BackgroundTaskEnded
}
export interface TaskEndedInfo {
taskEndedType: TaskEndedTypes,
value: number | boolean | undefined
}
@injectable()
export class TaskService implements TaskConfigurationClient {
/**
* The last executed task.
*/
protected lastTask: { source: string, taskLabel: string, scope: TaskConfigurationScope } | undefined = undefined;
protected cachedRecentTasks: TaskConfiguration[] = [];
protected runningTasks = new Map<number, {
exitCode: Deferred<number | undefined>,
terminateSignal: Deferred<string | undefined>,
isBackgroundTaskEnded: Deferred<boolean | undefined>
}>();
@inject(FrontendApplication)
protected readonly app: FrontendApplication;
@inject(ApplicationShell)
protected readonly shell: ApplicationShell;
@inject(TaskServer)
protected readonly taskServer: TaskServer;
@inject(ILogger) @named('task')
protected readonly logger: ILogger;
@inject(WidgetManager)
protected readonly widgetManager: WidgetManager;
@inject(TaskWatcher)
protected readonly taskWatcher: TaskWatcher;
@inject(MessageService)
protected readonly messageService: MessageService;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@inject(TaskConfigurations)
protected readonly taskConfigurations: TaskConfigurations;
@inject(ProvidedTaskConfigurations)
protected readonly providedTaskConfigurations: ProvidedTaskConfigurations;
@inject(VariableResolverService)
protected readonly variableResolverService: VariableResolverService;
@inject(TaskResolverRegistry)
protected readonly taskResolverRegistry: TaskResolverRegistry;
@inject(TerminalService)
protected readonly terminalService: TerminalService;
@inject(EditorManager)
protected readonly editorManager: EditorManager;
@inject(ProblemManager)
protected readonly problemManager: ProblemManager;
@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
@inject(ProblemMatcherRegistry)
protected readonly problemMatcherRegistry: ProblemMatcherRegistry;
@inject(QuickPickService)
protected readonly quickPick: QuickPickService;
@inject(OpenerService)
protected readonly openerService: OpenerService;
@inject(TaskNameResolver)
protected readonly taskNameResolver: TaskNameResolver;
@inject(TaskSourceResolver)
protected readonly taskSourceResolver: TaskSourceResolver;
@inject(TaskSchemaUpdater)
protected readonly taskSchemaUpdater: TaskSchemaUpdater;
@inject(TaskConfigurationManager)
protected readonly taskConfigurationManager: TaskConfigurationManager;
@inject(CommandService)
protected readonly commands: CommandService;
@inject(LabelProvider)
protected readonly labelProvider: LabelProvider;
@inject(MonacoWorkspace)
protected monacoWorkspace: MonacoWorkspace;
@inject(TaskTerminalWidgetManager)
protected readonly taskTerminalWidgetManager: TaskTerminalWidgetManager;
@postConstruct()
protected init(): void {
this.getRunningTasks().then(tasks =>
tasks.forEach(task => {
if (!this.runningTasks.has(task.taskId)) {
this.runningTasks.set(task.taskId, {
exitCode: new Deferred<number | undefined>(), terminateSignal: new Deferred<string | undefined>(),
isBackgroundTaskEnded: new Deferred<boolean | undefined>()
});
}
}));
// notify user that task has started
this.taskWatcher.onTaskCreated((event: TaskInfo) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
this.runningTasks.set(event.taskId, {
exitCode: new Deferred<number | undefined>(),
terminateSignal: new Deferred<string | undefined>(),
isBackgroundTaskEnded: new Deferred<boolean | undefined>()
});
});
this.taskWatcher.onOutputProcessed(async (event: TaskOutputProcessedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
if (event.problems) {
const runningTasksInfo: TaskInfo[] = await this.getRunningTasks();
// check if the task is active
const matchedRunningTaskInfo = runningTasksInfo.find(taskInfo => {
const taskConfig = taskInfo.config;
return this.taskDefinitionRegistry.compareTasks(taskConfig, event.config);
});
const isTaskActiveAndOutputSilent = matchedRunningTaskInfo &&
matchedRunningTaskInfo.config.presentation && matchedRunningTaskInfo.config.presentation.reveal === RevealKind.Silent;
event.problems.forEach(problem => {
const existingMarkers = this.problemManager.findMarkers({ owner: problem.description.owner });
const uris = new Set<string>();
existingMarkers.forEach(marker => uris.add(marker.uri));
if (ProblemMatchData.is(problem) && problem.resource) {
// When task.presentation.reveal === RevealKind.Silent, put focus on the terminal only if it is an error
if (isTaskActiveAndOutputSilent && problem.marker.severity === DiagnosticSeverity.Error) {
const terminalId = matchedRunningTaskInfo!.terminalId;
if (terminalId) {
const terminal = this.terminalService.getByTerminalId(terminalId);
if (terminal) {
const focus = !!matchedRunningTaskInfo!.config.presentation!.focus;
if (focus) { // assign focus to the terminal if presentation.focus is true
this.terminalService.open(terminal, { mode: 'activate' });
} else { // show the terminal but not assign focus
this.terminalService.open(terminal, { mode: 'reveal' });
}
}
}
}
const uri = new URI(problem.resource.path).withScheme(problem.resource.scheme);
const document = this.monacoWorkspace.getTextDocument(uri.toString());
if (problem.description.applyTo === ApplyToKind.openDocuments && !!document ||
problem.description.applyTo === ApplyToKind.closedDocuments && !document ||
problem.description.applyTo === ApplyToKind.allDocuments
) {
if (uris.has(uri.toString())) {
const newData = [
...existingMarkers
.filter(marker => marker.uri === uri.toString())
.map(markerData => markerData.data),
problem.marker
];
this.problemManager.setMarkers(uri, problem.description.owner, newData);
} else {
this.problemManager.setMarkers(uri, problem.description.owner, [problem.marker]);
}
}
} else { // should have received an event for finding the "background task begins" pattern
uris.forEach(uriString => this.problemManager.setMarkers(new URI(uriString), problem.description.owner, []));
}
});
}
});
this.taskWatcher.onBackgroundTaskEnded((event: BackgroundTaskEndedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
if (!this.runningTasks.has(event.taskId)) {
this.runningTasks.set(event.taskId, {
exitCode: new Deferred<number | undefined>(),
terminateSignal: new Deferred<string | undefined>(),
isBackgroundTaskEnded: new Deferred<boolean | undefined>()
});
}
this.runningTasks.get(event.taskId)!.isBackgroundTaskEnded.resolve(true);
});
// notify user that task has finished
this.taskWatcher.onTaskExit((event: TaskExitedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
if (!this.runningTasks.has(event.taskId)) {
this.runningTasks.set(event.taskId, {
exitCode: new Deferred<number | undefined>(),
terminateSignal: new Deferred<string | undefined>(),
isBackgroundTaskEnded: new Deferred<boolean | undefined>()
});
}
this.runningTasks.get(event.taskId)!.exitCode.resolve(event.code);
this.runningTasks.get(event.taskId)!.terminateSignal.resolve(event.signal);
setTimeout(() => this.runningTasks.delete(event.taskId), 60 * 1000);
const taskConfig = event.config;
const taskIdentifier = taskConfig ? this.getTaskIdentifier(taskConfig) : event.taskId.toString();
if (event.code !== undefined) {
if (event.code !== 0) {
const eventTaskConfig = event.config;
if (eventTaskConfig && eventTaskConfig.presentation && eventTaskConfig.presentation.reveal === RevealKind.Silent && event.terminalId) {
const terminal = this.terminalService.getByTerminalId(event.terminalId);
const focus = !!eventTaskConfig.presentation.focus;
if (terminal) {
if (focus) { // assign focus to the terminal if presentation.focus is true
this.terminalService.open(terminal, { mode: 'activate' });
} else { // show the terminal but not assign focus
this.terminalService.open(terminal, { mode: 'reveal' });
}
}
}
this.messageService.error(`Task '${taskIdentifier}' has exited with code ${event.code}.`);
}
} else if (event.signal !== undefined) {
this.messageService.info(`Task '${taskIdentifier}' was terminated by signal ${event.signal}.`);
} else {
console.error('Invalid TaskExitedEvent received, neither code nor signal is set.');
}
});
}
protected getTaskIdentifier(taskConfig: TaskConfiguration): string {
const taskName = this.taskNameResolver.resolve(taskConfig);
const sourceStrUri = this.taskSourceResolver.resolve(taskConfig);
return `${taskName} (${this.labelProvider.getName(new URI(sourceStrUri))})`;
}
/**
* Client should call this method to indicate that a new user-level action related to tasks has been started,
* like invoking "Run Task..."
* This method returns a token that can be used with various methods in this service.
* As long as a client uses the same token, task providers will only asked once to contribute
* tasks and the set of tasks will be cached. Each time the a new token is used, the cache of
* contributed tasks is cleared.
* @returns a token to be used for task-related actions
*/
startUserAction(): number {
return this.providedTaskConfigurations.startUserAction();
}
/**
* Returns an array of the task configurations configured in tasks.json and provided by the extensions.
* @param token The cache token for the user interaction in progress
*/
async getTasks(token: number): Promise<TaskConfiguration[]> {
const configuredTasks = await this.getConfiguredTasks(token);
const providedTasks = await this.getProvidedTasks(token);
const notCustomizedProvidedTasks = providedTasks.filter(provided =>
!configuredTasks.some(configured => this.taskDefinitionRegistry.compareTasks(configured, provided))
);
return [...configuredTasks, ...notCustomizedProvidedTasks];
}
/**
* Returns an array of the valid task configurations which are configured in tasks.json files
* @param token The cache token for the user interaction in progress
*
*/
async getConfiguredTasks(token: number): Promise<TaskConfiguration[]> {
const invalidTaskConfig = this.taskConfigurations.getInvalidTaskConfigurations()[0];
if (invalidTaskConfig) {
const widget = <ProblemWidget>await this.widgetManager.getOrCreateWidget(PROBLEMS_WIDGET_ID);
const isProblemsWidgetVisible = widget && widget.isVisible;
const currentEditorUri = this.editorManager.currentEditor && this.editorManager.currentEditor.editor.getResourceUri();
let isInvalidTaskConfigFileOpen = false;
if (currentEditorUri) {
const folderUri = this.workspaceService.getWorkspaceRootUri(currentEditorUri);
if (folderUri && folderUri.toString() === invalidTaskConfig._scope) {
isInvalidTaskConfigFileOpen = true;
}
}
const warningMessage = 'Invalid task configurations are found. Open tasks.json and find details in the Problems view.';
if (!isProblemsWidgetVisible || !isInvalidTaskConfigFileOpen) {
this.messageService.warn(warningMessage, 'Open').then(actionOpen => {
if (actionOpen) {
if (invalidTaskConfig && invalidTaskConfig._scope) {
this.taskConfigurationManager.openConfiguration(invalidTaskConfig._scope);
}
if (!isProblemsWidgetVisible) {
this.commands.executeCommand('problemsView:toggle');
}
}
});
} else {
this.messageService.warn(warningMessage);
}
}
const validTaskConfigs = await this.taskConfigurations.getTasks(token);
return validTaskConfigs;
}
/**
* Returns an array of the task configurations which are provided by the extensions.
* @param token The cache token for the user interaction in progress
*/
getProvidedTasks(token: number): Promise<TaskConfiguration[]> {
return this.providedTaskConfigurations.getTasks(token);
}
addRecentTasks(tasks: TaskConfiguration | TaskConfiguration[]): void {
if (Array.isArray(tasks)) {
tasks.forEach(task => this.addRecentTasks(task));
} else {
const ind = this.cachedRecentTasks.findIndex(recent => this.taskDefinitionRegistry.compareTasks(recent, tasks));
if (ind >= 0) {
this.cachedRecentTasks.splice(ind, 1);
}
this.cachedRecentTasks.unshift(tasks);
}
}
get recentTasks(): TaskConfiguration[] {
return this.cachedRecentTasks;
}
set recentTasks(recent: TaskConfiguration[]) {
this.cachedRecentTasks = recent;
}
/**
* Clears the list of recently used tasks.
*/
clearRecentTasks(): void {
this.cachedRecentTasks = [];
}
/**
* Open user ser
*/
openUserTasks(): Promise<void> {
return this.taskConfigurations.openUserTasks();
}
/**
* Returns a task configuration provided by an extension by task source, scope and label.
* If there are no task configuration, returns undefined.
* @param token The cache token for the user interaction in progress
* @param source The source for configured tasks
* @param label The label of the task to find
* @param scope The task scope to look in
*/
async getProvidedTask(token: number, source: string, label: string, scope: TaskConfigurationScope): Promise<TaskConfiguration | undefined> {
return this.providedTaskConfigurations.getTask(token, source, label, scope);
}
/** Returns an array of running tasks 'TaskInfo' objects */
getRunningTasks(): Promise<TaskInfo[]> {
return this.taskServer.getTasks(this.getContext());
}
/** Returns an array of task types that are registered, including the default types */
getRegisteredTaskTypes(): Promise<string[]> {
return this.taskSchemaUpdater.getRegisteredTaskTypes();
}
/**
* Get the last executed task.
*
* @returns the last executed task or `undefined`.
*/
getLastTask(): { source: string, taskLabel: string, scope: TaskConfigurationScope } | undefined {
return this.lastTask;
}
/**
* Runs a task, by task configuration label.
* Note, it looks for a task configured in tasks.json only.
* @param token The cache token for the user interaction in progress
* @param scope The scope where to look for tasks
* @param taskLabel the label to look for
*/
async runConfiguredTask(token: number, scope: TaskConfigurationScope, taskLabel: string): Promise<void> {
const task = this.taskConfigurations.getTask(scope, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}
this.run(token, task._source, taskLabel, scope);
}
/**
* Run the last executed task.
* @param token The cache token for the user interaction in progress
*/
async runLastTask(token: number): Promise<TaskInfo | undefined> {
if (!this.lastTask) {
return;
}
const { source, taskLabel, scope } = this.lastTask;
return this.run(token, source, taskLabel, scope);
}
/**
* Runs a task, by the source and label of the task configuration.
* It looks for configured and detected tasks.
* @param token The cache token for the user interaction in progress
* @param source The source for configured tasks
* @param taskLabel The label to look for
* @param scope The scope where to look for tasks
*/
async run(token: number, source: string, taskLabel: string, scope: TaskConfigurationScope): Promise<TaskInfo | undefined> {
let task: TaskConfiguration | undefined;
task = this.taskConfigurations.getTask(scope, taskLabel);
if (!task) { // if a configured task cannot be found, search from detected tasks
task = await this.getProvidedTask(token, source, taskLabel, scope);
if (!task) { // find from the customized detected tasks
task = await this.taskConfigurations.getCustomizedTask(token, scope, taskLabel);
}
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}
}
const customizationObject = await this.getTaskCustomization(task);
if (!customizationObject.problemMatcher) {
// ask the user what s/he wants to use to parse the task output
const items = this.getCustomizeProblemMatcherItems();
const selected = await this.quickPick.show(items, {
placeholder: 'Select for which kind of errors and warnings to scan the task output'
});
if (selected) {
if (selected.problemMatchers) {
let matcherNames: string[] = [];
if (selected.problemMatchers && selected.problemMatchers.length === 0) { // never parse output for this task
matcherNames = [];
} else if (selected.problemMatchers && selected.problemMatchers.length > 0) { // continue with user-selected parser
matcherNames = selected.problemMatchers.map(matcher => matcher.name);
}
customizationObject.problemMatcher = matcherNames;
// write the selected matcher (or the decision of "never parse") into the `tasks.json`
this.updateTaskConfiguration(token, task, { problemMatcher: matcherNames });
} else if (selected.learnMore) { // user wants to learn more about parsing task output
open(this.openerService, new URI('https://code.visualstudio.com/docs/editor/tasks#_processing-task-output-with-problem-matchers'));
}
// else, continue the task with no parser
} else { // do not start the task in case that the user did not select any item from the list
return;
}
}
const resolvedMatchers = await this.resolveProblemMatchers(task, customizationObject);
const runTaskOption: RunTaskOption = {
customization: { ...customizationObject, ...{ problemMatcher: resolvedMatchers } }
};
if (task.dependsOn) {
return this.runCompoundTask(token, task, runTaskOption);
} else {
return this.runTask(task, runTaskOption).catch(error => {
console.error('Error at launching task', error);
return undefined;
});
}
}
/**
* Runs a compound task
* @param token The cache token for the user interaction in progress
* @param task The task to be executed
* @param option options for executing the task
*/
async runCompoundTask(token: number, task: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
const tasks = await this.getWorkspaceTasks(token, task._scope);
try {
const rootNode = new TaskNode(task, [], []);
this.detectDirectedAcyclicGraph(task, rootNode, tasks);
} catch (error) {
console.error(`Error at launching task '${task.label}'`, error);
this.messageService.error(error.message);
return undefined;
}
return this.runTasksGraph(task, tasks, option).catch(error => {
console.error(`Error at launching task '${task.label}'`, error);
return undefined;
});
}
/**
* A recursive function that runs a task and all its sub tasks that it depends on.
* A task can be executed only when all of its dependencies have been executed, or when it doesn’t have any dependencies at all.
*/
async runTasksGraph(task: TaskConfiguration, tasks: TaskConfiguration[], option?: RunTaskOption): Promise<TaskInfo | undefined> {
if (task && task.dependsOn) {
// In case it is an array of task dependencies
if (Array.isArray(task.dependsOn) && task.dependsOn.length > 0) {
const dependentTasks: { 'task': TaskConfiguration; 'taskCustomization': TaskCustomization; 'resolvedMatchers': ProblemMatcher[] | undefined }[] = [];
for (let i = 0; i < task.dependsOn.length; i++) {
// It may be a string (a task label) or a JSON object which represents a TaskIdentifier (e.g. {"type":"npm", "script":"script1"})
const taskIdentifier = task.dependsOn[i];
const dependentTask = this.getDependentTask(taskIdentifier, tasks);
const taskCustomization = await this.getTaskCustomization(dependentTask);
const resolvedMatchers = await this.resolveProblemMatchers(dependentTask, taskCustomization);
dependentTasks.push({ 'task': dependentTask, 'taskCustomization': taskCustomization, 'resolvedMatchers': resolvedMatchers });
// In case the 'dependsOrder' is 'sequence'
if (task.dependsOrder && task.dependsOrder === DependsOrder.Sequence) {
await this.runTasksGraph(dependentTask, tasks, {
customization: { ...taskCustomization, ...{ problemMatcher: resolvedMatchers } }
});
}
}
// In case the 'dependsOrder' is 'parallel'
if (((!task.dependsOrder) || (task.dependsOrder && task.dependsOrder === DependsOrder.Parallel))) {
const promises = dependentTasks.map(item =>
this.runTasksGraph(item.task, tasks, {
customization: { ...item.taskCustomization, ...{ problemMatcher: item.resolvedMatchers } }
})
);
await Promise.all(promises);
}
} else if (!Array.isArray(task.dependsOn)) {
// In case it is a string (a task label) or a JSON object which represents a TaskIdentifier (e.g. {"type":"npm", "script":"script1"})
const taskIdentifier = task.dependsOn;
const dependentTask = this.getDependentTask(taskIdentifier, tasks);
const taskCustomization = await this.getTaskCustomization(dependentTask);
const resolvedMatchers = await this.resolveProblemMatchers(dependentTask, taskCustomization);
await this.runTasksGraph(dependentTask, tasks, {
customization: { ...taskCustomization, ...{ problemMatcher: resolvedMatchers } }
});
}
}
const taskInfo = await this.runTask(task, option);
if (taskInfo) {
const getExitCodePromise: Promise<TaskEndedInfo> = this.getExitCode(taskInfo.taskId).then(result => ({ taskEndedType: TaskEndedTypes.TaskExited, value: result }));
const isBackgroundTaskEndedPromise: Promise<TaskEndedInfo> = this.isBackgroundTaskEnded(taskInfo.taskId).then(result =>
({ taskEndedType: TaskEndedTypes.BackgroundTaskEnded, value: result }));
// After start running the task, we wait for the task process to exit and if it is a background task, we also wait for a feedback
// that a background task is active, as soon as one of the promises fulfills, we can continue and analyze the results.
const taskEndedInfo: TaskEndedInfo = await Promise.race([getExitCodePromise, isBackgroundTaskEndedPromise]);
if ((taskEndedInfo.taskEndedType === TaskEndedTypes.TaskExited && taskEndedInfo.value !== 0) ||
(taskEndedInfo.taskEndedType === TaskEndedTypes.BackgroundTaskEnded && !taskEndedInfo.value)) {
throw new Error('The task: ' + task.label + ' terminated with exit code ' + taskEndedInfo.value + '.');
}
}
return taskInfo;
}
/**
* Creates a graph of dependencies tasks from the root task and verify there is no DAG (Directed Acyclic Graph).
* In case of detection of a circular dependency, an error is thrown with a message which describes the detected circular reference.
*/
detectDirectedAcyclicGraph(task: TaskConfiguration, taskNode: TaskNode, tasks: TaskConfiguration[]): void {
if (task && task.dependsOn) {
// In case the 'dependsOn' is an array
if (Array.isArray(task.dependsOn) && task.dependsOn.length > 0) {
for (let i = 0; i < task.dependsOn.length; i++) {
const childNode = this.createChildTaskNode(task, taskNode, task.dependsOn[i], tasks);
this.detectDirectedAcyclicGraph(childNode.taskConfiguration, childNode.node, tasks);
}
} else if (!Array.isArray(task.dependsOn)) {
const childNode = this.createChildTaskNode(task, taskNode, task.dependsOn, tasks);
this.detectDirectedAcyclicGraph(childNode.taskConfiguration, childNode.node, tasks);
}
}
}
// 'childTaskIdentifier' may be a string (a task label) or a JSON object which represents a TaskIdentifier (e.g. {"type":"npm", "script":"script1"})
createChildTaskNode(task: TaskConfiguration, taskNode: TaskNode, childTaskIdentifier: string | TaskIdentifier, tasks: TaskConfiguration[]): TaskGraphNode {
const childTaskConfiguration = this.getDependentTask(childTaskIdentifier, tasks);
// If current task and child task are identical or if
// one of the child tasks is identical to one of the current task ancestors, then raise an error
if (this.taskDefinitionRegistry.compareTasks(task, childTaskConfiguration) ||
taskNode.parentsID.filter(t => this.taskDefinitionRegistry.compareTasks(childTaskConfiguration, t)).length > 0) {
const fromNode = task.label;
const toNode = childTaskConfiguration.label;
throw new Error('Circular reference detected: ' + fromNode + ' --> ' + toNode);
}
const childNode = new TaskNode(childTaskConfiguration, [], Object.assign([], taskNode.parentsID));
childNode.addParentDependency(taskNode.taskId);
taskNode.addChildDependency(childNode);
return { 'taskConfiguration': childTaskConfiguration, 'node': childNode };
}
/**
* Gets task configuration by task label or by a JSON object which represents a task identifier
*
* @param taskIdentifier The task label (string) or a JSON object which represents a TaskIdentifier (e.g. {"type":"npm", "script":"script1"})
* @param tasks an array of the task configurations
* @returns the correct TaskConfiguration object which matches the taskIdentifier
*/
getDependentTask(taskIdentifier: string | TaskIdentifier, tasks: TaskConfiguration[]): TaskConfiguration {
const notEnoughDataError = 'The information provided in the "dependsOn" is not enough for matching the correct task !';
let currentTaskChildConfiguration: TaskConfiguration;
if (typeof (taskIdentifier) !== 'string') {
// TaskIdentifier object does not support tasks of type 'shell' (The same behavior as in VS Code).
// So if we want the 'dependsOn' property to include tasks of type 'shell',
// then we must mention their labels (in the 'dependsOn' property) and not to create a task identifier object for them.
const taskDefinition = this.taskDefinitionRegistry.getDefinition(taskIdentifier);
if (taskDefinition) {
currentTaskChildConfiguration = this.getTaskByTaskIdentifierAndTaskDefinition(taskDefinition, taskIdentifier, tasks);
if (!currentTaskChildConfiguration.type) {
this.messageService.error(notEnoughDataError);
throw new Error(notEnoughDataError);
}
return currentTaskChildConfiguration;
} else {
this.messageService.error(notEnoughDataError);
throw new Error(notEnoughDataError);
}
} else {
currentTaskChildConfiguration = tasks.filter(t => taskIdentifier === this.taskNameResolver.resolve(t))[0];
return currentTaskChildConfiguration;
}
}
/**
* Gets the matched task from an array of task configurations by TaskDefinition and TaskIdentifier.
* In case that more than one task configuration matches, we returns the first one.
*
* @param taskDefinition The task definition for the task configuration.
* @param taskIdentifier The task label (string) or a JSON object which represents a TaskIdentifier (e.g. {"type":"npm", "script":"script1"})
* @param tasks An array of task configurations.
* @returns The correct TaskConfiguration object which matches the taskDefinition and taskIdentifier.
*/
getTaskByTaskIdentifierAndTaskDefinition(taskDefinition: TaskDefinition | undefined, taskIdentifier: TaskIdentifier, tasks: TaskConfiguration[]): TaskConfiguration {
const identifierProperties: string[] = [];
let relevantTasks = tasks.filter(t =>
taskDefinition && t.hasOwnProperty('taskType') &&
taskDefinition['taskType'] === t['taskType'] &&
t.hasOwnProperty('source') &&
taskDefinition['source'] === t['source']);
Object.keys(taskIdentifier).forEach(key => {
identifierProperties.push(key);
});
identifierProperties.forEach(key => {
if (key === 'type' || key === 'taskType') {
relevantTasks = relevantTasks.filter(t => (t.hasOwnProperty('type') || t.hasOwnProperty('taskType')) &&
((taskIdentifier[key] === t['type']) || (taskIdentifier[key] === t['taskType'])));
} else {
relevantTasks = relevantTasks.filter(t => t.hasOwnProperty(key) && taskIdentifier[key] === t[key]);
}
});
if (relevantTasks.length > 0) {
return relevantTasks[0];
} else {
// return empty TaskConfiguration
return { 'label': '', '_scope': '', 'type': '' };
}
}
async runTask(task: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
const runningTasksInfo: TaskInfo[] = await this.getRunningTasks();
// check if the task is active
const matchedRunningTaskInfo = runningTasksInfo.find(taskInfo => {
const taskConfig = taskInfo.config;
return this.taskDefinitionRegistry.compareTasks(taskConfig, task);
});
if (matchedRunningTaskInfo) { // the task is active
const taskName = this.taskNameResolver.resolve(task);
const terminalId = matchedRunningTaskInfo.terminalId;
if (terminalId) {
const terminal = this.terminalService.getByTerminalId(terminalId);
if (terminal) {
if (TaskOutputPresentation.shouldSetFocusToTerminal(task)) { // assign focus to the terminal if presentation.focus is true
this.terminalService.open(terminal, { mode: 'activate' });
} else if (TaskOutputPresentation.shouldAlwaysRevealTerminal(task)) { // show the terminal but not assign focus
this.terminalService.open(terminal, { mode: 'reveal' });
}
}
}
const selectedAction = await this.messageService.info(`The task '${taskName}' is already active`, 'Terminate Task', 'Restart Task');
if (selectedAction === 'Terminate Task') {
await this.terminateTask(matchedRunningTaskInfo);
} else if (selectedAction === 'Restart Task') {
return this.restartTask(matchedRunningTaskInfo, option);
}
} else { // run task as the task is not active
return this.doRunTask(task, option);
}
}
/**
* Terminates a task that is actively running.
* @param activeTaskInfo the TaskInfo of the task that is actively running
*/
async terminateTask(activeTaskInfo: TaskInfo): Promise<void> {
const taskId = activeTaskInfo.taskId;
return this.kill(taskId);
}
/**
* Terminates a task that is actively running, and restarts it.
* @param activeTaskInfo the TaskInfo of the task that is actively running
*/
async restartTask(activeTaskInfo: TaskInfo, option?: RunTaskOption): Promise<TaskInfo | undefined> {
await this.terminateTask(activeTaskInfo);
return this.doRunTask(activeTaskInfo.config, option);
}
protected async doRunTask(task: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
if (option && option.customization) {
const taskDefinition = this.taskDefinitionRegistry.getDefinition(task);
if (taskDefinition) { // use the customization object to override the task config
Object.keys(option.customization).forEach(customizedProperty => {
// properties used to define the task cannot be customized
if (customizedProperty !== 'type' && !taskDefinition.properties.all.some(pDefinition => pDefinition === customizedProperty)) {
task[customizedProperty] = option.customization![customizedProperty];
}
});
}
}
const resolvedTask = await this.getResolvedTask(task);
if (resolvedTask) {
// remove problem markers from the same source before running the task
await this.removeProblemMarkers(option);
return this.runResolvedTask(resolvedTask, option);
}
}
/**
* Runs the first task with the given label.
*
* @param token The cache token for the user interaction in progress
* @param taskLabel The label of the task to be executed
*/
async runTaskByLabel(token: number, taskLabel: string): Promise<TaskInfo | undefined> {
const tasks: TaskConfiguration[] = await this.getTasks(token);
for (const task of tasks) {
if (task.label === taskLabel) {
return this.runTask(task);
}
}
return;
}
/**
* Runs a task identified by the given identifier, but only if found in the give workspace folder
*
* @param token The cache token for the user interaction in progress
* @param workspaceFolderUri The folder to restrict the search to
* @param taskIdentifier The identifier to look for
*/
async runWorkspaceTask(token: number, workspaceFolderUri: string | undefined, taskIdentifier: string | TaskIdentifier): Promise<TaskInfo | undefined> {
const tasks = await this.getWorkspaceTasks(token, workspaceFolderUri);
const task = this.getDependentTask(taskIdentifier, tasks);
if (!task) {
return undefined;
}
const taskCustomization = await this.getTaskCustomization(task);
const resolvedMatchers = await this.resolveProblemMatchers(task, taskCustomization);
try {
const rootNode = new TaskNode(task, [], []);
this.detectDirectedAcyclicGraph(task, rootNode, tasks);
} catch (error) {
this.logger.error(error.message);
this.messageService.error(error.message);
return undefined;
}
return this.runTasksGraph(task, tasks, {
customization: { ...taskCustomization, ...{ problemMatcher: resolvedMatchers } }
}).catch(error => {
console.log(error.message);
return undefined;
});
}
/**
* Updates the task configuration in the `tasks.json`.
* The task config, together with updates, will be written into the `tasks.json` if it is not found in the file.
*
* @param token The cache token for the user interaction in progress
* @param task task that the updates will be applied to
* @param update the updates to be applied
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async updateTaskConfiguration(token: number, task: TaskConfiguration, update: { [name: string]: any }): Promise<void> {
if (update.problemMatcher) {
if (Array.isArray(update.problemMatcher)) {
update.problemMatcher.forEach((name, index) => {
if (!name.startsWith('$')) {
update.problemMatcher[index] = `$${update.problemMatcher[index]}`;
}
});
} else if (!update.problemMatcher.startsWith('$')) {
update.problemMatcher = `$${update.problemMatcher}`;
}
}
this.taskConfigurations.updateTaskConfig(token, task, update);
}
protected async getWorkspaceTasks(token: number, restrictToFolder: TaskConfigurationScope | undefined): Promise<TaskConfiguration[]> {
const tasks = await this.getTasks(token);
// if we pass undefined, return everything, otherwise only tasks with the same uri or workspace/global scope tasks
return tasks.filter(t => typeof t._scope !== 'string' || t._scope === restrictToFolder);
}
protected async resolveProblemMatchers(task: TaskConfiguration, customizationObject: TaskCustomization): Promise<ProblemMatcher[] | undefined> {
const notResolvedMatchers = customizationObject.problemMatcher ?
(Array.isArray(customizationObject.problemMatcher) ? customizationObject.problemMatcher : [customizationObject.problemMatcher]) : undefined;
let resolvedMatchers: ProblemMatcher[] | undefined = [];
if (notResolvedMatchers) {
// resolve matchers before passing them to the server
for (const matcher of notResolvedMatchers) {
let resolvedMatcher: ProblemMatcher | undefined;
await this.problemMatcherRegistry.onReady();
if (typeof matcher === 'string') {
resolvedMatcher = this.problemMatcherRegistry.get(matcher);
} else {
resolvedMatcher = await this.problemMatcherRegistry.getProblemMatcherFromContribution(matcher);
}
if (resolvedMatcher) {
const scope = task._scope || task._source;
if (resolvedMatcher.filePrefix && scope) {
const options = {
context: new URI(scope).withScheme('file'),
configurationSection: 'tasks'
};
const resolvedPrefix = await this.variableResolverService.resolve(resolvedMatcher.filePrefix, options);
Object.assign(resolvedMatcher, { filePrefix: resolvedPrefix });
}
resolvedMatchers.push(resolvedMatcher);
}
}
} else {
resolvedMatchers = undefined;
}
return resolvedMatchers;
}
protected async getTaskCustomization(task: TaskConfiguration): Promise<TaskCustomization> {
const customizationObject: TaskCustomization = { type: '', _scope: task._scope };
const customizationFound = this.taskConfigurations.getCustomizationForTask(task);
if (customizationFound) {
Object.assign(customizationObject, customizationFound);
} else {
Object.assign(customizationObject, {
type: task.type,
problemMatcher: task.problemMatcher
});
}
return customizationObject;
}
protected async removeProblemMarkers(option?: RunTaskOption): Promise<void> {
if (option && option.customization) {
const matchersFromOption = option.customization.problemMatcher || [];
for (const matcher of matchersFromOption) {
if (matcher && matcher.owner) {
const existingMarkers = this.problemManager.findMarkers({ owner: matcher.owner });
const uris = new Set<string>();
existingMarkers.forEach(marker => uris.add(marker.uri));
uris.forEach(uriString => this.problemManager.setMarkers(new URI(uriString), matcher.owner, []));
}
}
}
}
protected async getResolvedTask(task: TaskConfiguration): Promise<TaskConfiguration | undefined> {
let resolver = undefined;
let resolvedTask: TaskConfiguration;
try {
resolver = await this.taskResolverRegistry.getResolver(task.type);
resolvedTask = resolver ? await resolver.resolveTask(task) : task;
} catch (error) {
const errMessage = `Error resolving task '${task.label}': ${error}`;
this.logger.error(errMessage);
resolvedTask = task;
}
this.addRecentTasks(task);
return resolvedTask;
}
/**
* Runs the resolved task and opens terminal widget if the task is based on a terminal process
* @param resolvedTask the resolved task
* @param option options to run the resolved task
*/
protected async runResolvedTask(resolvedTask: TaskConfiguration, option?: RunTaskOption): Promise<TaskInfo | undefined> {
const source = resolvedTask._source;
const taskLabel = resolvedTask.label;
try {
const taskInfo = await this.taskServer.run(resolvedTask, this.getContext(), option);
this.lastTask = { source, taskLabel, scope: resolvedTask._scope };
this.logger.debug(`Task created. Task id: ${taskInfo.taskId}`);
/**
* open terminal widget if the task is based on a terminal process (type: 'shell' or 'process')
*
* @todo Use a different mechanism to determine if the task should be attached?
* Reason: Maybe a new task type wants to also be displayed in a terminal.
*/
if (typeof taskInfo.terminalId === 'number') {
this.attach(taskInfo.terminalId, taskInfo.taskId);
}
return taskInfo;
} catch (error) {