-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtask-service.ts
497 lines (440 loc) · 20.9 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
/********************************************************************************
* 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 { inject, injectable, named, postConstruct } from 'inversify';
import { EditorManager } from '@theia/editor/lib/browser';
import { ILogger } from '@theia/core/lib/common';
import { ApplicationShell, FrontendApplication, WidgetManager } from '@theia/core/lib/browser';
import { TaskResolverRegistry, TaskProviderRegistry } from './task-contribution';
import { TERMINAL_WIDGET_FACTORY_ID, TerminalWidgetFactoryOptions } from '@theia/terminal/lib/browser/terminal-widget-impl';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
import { TerminalWidget } from '@theia/terminal/lib/browser/base/terminal-widget';
import { MessageService } from '@theia/core/lib/common/message-service';
import { ProblemManager } from '@theia/markers/lib/browser/problem/problem-manager';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { VariableResolverService } from '@theia/variable-resolver/lib/browser';
import {
ProblemMatcher,
ProblemMatchData,
ProblemMatcherContribution,
TaskServer,
TaskExitedEvent,
TaskInfo,
TaskConfiguration,
TaskCustomization,
TaskOutputProcessedEvent,
RunTaskOption
} from '../common';
import { TaskWatcher } from '../common/task-watcher';
import { TaskConfigurationClient, TaskConfigurations } from './task-configurations';
import { ProvidedTaskConfigurations } from './provided-task-configurations';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { ProblemMatcherRegistry } from './task-problem-matcher-registry';
import { Range } from 'vscode-languageserver-types';
import URI from '@theia/core/lib/common/uri';
@injectable()
export class TaskService implements TaskConfigurationClient {
/**
* Reflects whether a valid task configuration file was found
* in the current workspace, and is being watched for changes.
*/
protected configurationFileFound: boolean = false;
/**
* The last executed task.
*/
protected lastTask: { source: string, taskLabel: string } | undefined = undefined;
protected cachedRecentTasks: TaskConfiguration[] = [];
@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;
/**
* @deprecated To be removed in 0.5.0
*/
@inject(TaskProviderRegistry)
protected readonly taskProviderRegistry: TaskProviderRegistry;
@postConstruct()
protected init(): void {
this.workspaceService.onWorkspaceChanged(async roots => {
this.configurationFileFound = (await Promise.all(roots.map(r => this.taskConfigurations.watchConfigurationFile(r.uri)))).some(result => !!result);
const rootUris = roots.map(r => new URI(r.uri));
const taskConfigFileUris = this.taskConfigurations.configFileUris.map(strUri => new URI(strUri));
for (const taskConfigUri of taskConfigFileUris) {
if (!rootUris.some(rootUri => !!rootUri.relative(taskConfigUri))) {
this.taskConfigurations.unwatchConfigurationFile(taskConfigUri.toString());
this.taskConfigurations.removeTasks(taskConfigUri.toString());
}
}
});
// notify user that task has started
this.taskWatcher.onTaskCreated((event: TaskInfo) => {
if (this.isEventForThisClient(event.ctx)) {
const task = event.config;
let taskIdentifier = event.taskId.toString();
if (task) {
taskIdentifier = !!this.taskDefinitionRegistry.getDefinition(task) ? `${task._source}: ${task.label}` : `${task.type}: ${task.label}`;
}
this.messageService.info(`Task ${taskIdentifier} has been started`);
}
});
this.taskWatcher.onOutputProcessed((event: TaskOutputProcessedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
if (event.problems) {
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) {
const uri = new URI(problem.resource.path).withScheme(problem.resource.scheme);
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, []));
}
});
}
});
// notify user that task has finished
this.taskWatcher.onTaskExit((event: TaskExitedEvent) => {
if (!this.isEventForThisClient(event.ctx)) {
return;
}
const taskConfiguration = event.config;
let taskIdentifier = event.taskId.toString();
if (taskConfiguration) {
taskIdentifier = !!this.taskDefinitionRegistry.getDefinition(taskConfiguration)
? `${taskConfiguration._source}: ${taskConfiguration.label}`
: `${taskConfiguration.type}: ${taskConfiguration.label}`;
}
if (event.code !== undefined) {
const message = `Task ${taskIdentifier} has exited with code ${event.code}.`;
if (event.code === 0) {
this.messageService.info(message);
} else {
this.messageService.error(message);
}
} 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.');
}
});
}
/** Returns an array of the task configurations configured in tasks.json and provided by the extensions. */
async getTasks(): Promise<TaskConfiguration[]> {
const configuredTasks = this.getConfiguredTasks();
const providedTasks = await this.getProvidedTasks();
return [...configuredTasks, ...providedTasks];
}
/** Returns an array of the task configurations which are configured in tasks.json files */
getConfiguredTasks(): TaskConfiguration[] {
return this.taskConfigurations.getTasks();
}
/** Returns an array of the task configurations which are provided by the extensions. */
getProvidedTasks(): Promise<TaskConfiguration[]> {
return this.providedTaskConfigurations.getTasks();
}
addRecentTasks(tasks: TaskConfiguration | TaskConfiguration[]): void {
if (Array.isArray(tasks)) {
tasks.forEach(task => this.addRecentTasks(task));
} else {
const ind = this.cachedRecentTasks.findIndex(recent => TaskConfiguration.equals(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 = [];
}
/**
* Returns a task configuration provided by an extension by task source and label.
* If there are no task configuration, returns undefined.
*/
async getProvidedTask(source: string, label: string): Promise<TaskConfiguration | undefined> {
return this.providedTaskConfigurations.getTask(source, label);
}
/** 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.taskServer.getRegisteredTaskTypes();
}
/**
* Get the last executed task.
*
* @returns the last executed task or `undefined`.
*/
getLastTask(): { source: string, taskLabel: string } | undefined {
return this.lastTask;
}
/**
* Runs a task, by task configuration label.
* Note, it looks for a task configured in tasks.json only.
*/
async runConfiguredTask(source: string, taskLabel: string): Promise<void> {
const task = this.taskConfigurations.getTask(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
}
this.run(source, taskLabel);
}
/**
* Run the last executed task.
*/
async runLastTask(): Promise<void> {
if (!this.lastTask) {
return;
}
const { source, taskLabel } = this.lastTask;
return this.run(source, taskLabel);
}
/**
* Runs a task, by the source and label of the task configuration.
* It looks for configured and provided tasks.
*/
async run(source: string, taskLabel: string): Promise<void> {
let task = await this.getProvidedTask(source, taskLabel);
const matchers: (string | ProblemMatcherContribution)[] = [];
if (!task) { // if a provided task cannot be found, search from tasks.json
task = this.taskConfigurations.getTask(source, taskLabel);
if (!task) {
this.logger.error(`Can't get task launch configuration for label: ${taskLabel}`);
return;
} else if (task.problemMatcher) {
if (Array.isArray(task.problemMatcher)) {
matchers.push(...task.problemMatcher);
} else {
matchers.push(task.problemMatcher);
}
}
} else { // if a provided task is found, check if it is customized in tasks.json
const taskType = task.taskType || task.type;
const customizations = this.taskConfigurations.getTaskCustomizations(taskType);
const matcherContributions = this.getProblemMatchers(task, customizations);
matchers.push(...matcherContributions);
}
await this.problemMatcherRegistry.onReady();
const resolvedMatchers: ProblemMatcher[] = [];
// resolve matchers before passing them to the server
for (const matcher of matchers) {
let resolvedMatcher: ProblemMatcher | undefined;
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') };
const resolvedPrefix = await this.variableResolverService.resolve(resolvedMatcher.filePrefix, options);
Object.assign(resolvedMatcher, { filePrefix: resolvedPrefix });
}
resolvedMatchers.push(resolvedMatcher);
}
}
this.runTask(task, {
customization: { type: task.taskType || task.type, problemMatcher: resolvedMatchers }
});
}
async runTask(task: TaskConfiguration, option?: RunTaskOption): Promise<void> {
const source = task._source;
const taskLabel = task.label;
const resolver = this.taskResolverRegistry.getResolver(task.type);
let resolvedTask: TaskConfiguration;
try {
resolvedTask = resolver ? await resolver.resolveTask(task) : task;
this.addRecentTasks(task);
} catch (error) {
this.logger.error(`Error resolving task '${taskLabel}': ${error}`);
this.messageService.error(`Error resolving task '${taskLabel}': ${error}`);
return;
}
await this.removeProblemMarks(option);
let taskInfo: TaskInfo;
try {
taskInfo = await this.taskServer.run(resolvedTask, this.getContext(), option);
this.lastTask = { source, taskLabel };
} catch (error) {
const errorStr = `Error launching task '${taskLabel}': ${error.message}`;
this.logger.error(errorStr);
this.messageService.error(errorStr);
return;
}
this.logger.debug(`Task created. Task id: ${taskInfo.taskId}`);
// open terminal widget if the task is based on a terminal process (type: shell)
if (taskInfo.terminalId !== undefined) {
this.attach(taskInfo.terminalId, taskInfo.taskId);
}
}
private getProblemMatchers(taskConfiguration: TaskConfiguration, customizations: TaskCustomization[]): (string | ProblemMatcherContribution)[] {
const hasCustomization = customizations.length > 0;
const problemMatchers: (string | ProblemMatcherContribution)[] = [];
if (hasCustomization) {
const taskDefinition = this.taskDefinitionRegistry.getDefinition(taskConfiguration);
if (taskDefinition) {
const cus = customizations.filter(customization =>
taskDefinition.properties.required.every(rp => customization[rp] === taskConfiguration[rp])
)[0]; // Only support having one customization per task
if (cus && cus.problemMatcher) {
if (Array.isArray(cus.problemMatcher)) {
problemMatchers.push(...cus.problemMatcher);
} else {
problemMatchers.push(cus.problemMatcher);
}
}
}
}
return problemMatchers;
}
private async removeProblemMarks(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, []));
}
}
}
}
/**
* Run selected text in the last active terminal.
*/
async runSelectedText(): Promise<void> {
if (!this.editorManager.currentEditor) { return; }
const startLine = this.editorManager.currentEditor.editor.selection.start.line;
const startCharacter = this.editorManager.currentEditor.editor.selection.start.character;
const endLine = this.editorManager.currentEditor.editor.selection.end.line;
const endCharacter = this.editorManager.currentEditor.editor.selection.end.character;
let selectedRange: Range = Range.create(startLine, startCharacter, endLine, endCharacter);
// if no text is selected, default to selecting entire line
if (startLine === endLine && startCharacter === endCharacter) {
selectedRange = Range.create(startLine, 0, endLine + 1, 0);
}
const selectedText: string = this.editorManager.currentEditor.editor.document.getText(selectedRange).trimRight() + '\n';
let terminal = this.terminalService.currentTerminal;
if (!terminal) {
terminal = <TerminalWidget>await this.terminalService.newTerminal(<TerminalWidgetFactoryOptions>{ created: new Date().toString() });
await terminal.start();
this.terminalService.activateTerminal(terminal);
}
terminal.sendText(selectedText);
}
async attach(terminalId: number, taskId: number): Promise<void> {
// Get the list of all available running tasks.
const runningTasks: TaskInfo[] = await this.getRunningTasks();
// Get the corresponding task information based on task id if available.
const taskInfo: TaskInfo | undefined = runningTasks.find((t: TaskInfo) => t.taskId === taskId);
// Create terminal widget to display an execution output of a task that was launched as a command inside a shell.
const widget = <TerminalWidget>await this.widgetManager.getOrCreateWidget(
TERMINAL_WIDGET_FACTORY_ID,
<TerminalWidgetFactoryOptions>{
created: new Date().toString(),
id: 'terminal-' + terminalId,
title: taskInfo
? `Task: ${taskInfo.config.label}`
: `Task: #${taskId}`,
destroyTermOnClose: true
}
);
this.shell.addWidget(widget, { area: 'bottom' });
this.shell.activateWidget(widget.id);
widget.start(terminalId);
}
async configure(task: TaskConfiguration): Promise<void> {
await this.taskConfigurations.configure(task);
}
protected isEventForThisClient(context: string | undefined): boolean {
if (context === this.getContext()) {
return true;
}
return false;
}
taskConfigurationChanged(event: string[]) {
// do nothing for now
}
protected getContext(): string | undefined {
return this.workspaceService.workspace && this.workspaceService.workspace.uri;
}
/** Kill task for a given id if task is found */
async kill(id: number): Promise<void> {
try {
await this.taskServer.kill(id);
} catch (error) {
this.logger.error(`Error killing task '${id}': ${error}`);
this.messageService.error(`Error killing task '${id}': ${error}`);
return;
}
this.logger.debug(`Task killed. Task id: ${id}`);
}
}