-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtasks-main.ts
128 lines (114 loc) · 4.88 KB
/
tasks-main.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
/********************************************************************************
* Copyright (C) 2018 Red Hat, Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import {
TasksMain,
MAIN_RPC_CONTEXT,
TasksExt
} from '../../api/plugin-api';
import { RPCProtocol } from '../../api/rpc-protocol';
import { DisposableCollection } from '@theia/core';
import { TaskProviderRegistry, TaskResolverRegistry, TaskProvider, TaskResolver } from '@theia/task/lib/browser/task-contribution';
import { interfaces } from 'inversify';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
import { TaskInfo, TaskExitedEvent } from '@theia/task/lib/common/task-protocol';
import { TaskWatcher } from '@theia/task/lib/common/task-watcher';
import { TaskService } from '@theia/task/lib/browser/task-service';
export class TasksMainImpl implements TasksMain {
private workspaceRootUri: string | undefined = undefined;
private readonly proxy: TasksExt;
private readonly disposables = new Map<number, monaco.IDisposable>();
private readonly taskProviderRegistry: TaskProviderRegistry;
private readonly taskResolverRegistry: TaskResolverRegistry;
private readonly taskWatcher: TaskWatcher;
private readonly taskService: TaskService;
private readonly workspaceService: WorkspaceService;
constructor(rpc: RPCProtocol, container: interfaces.Container, ) {
this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.TASKS_EXT);
this.taskProviderRegistry = container.get(TaskProviderRegistry);
this.taskResolverRegistry = container.get(TaskResolverRegistry);
this.workspaceService = container.get(WorkspaceService);
this.taskWatcher = container.get(TaskWatcher);
this.taskService = container.get(TaskService);
this.workspaceService.roots.then(roots => {
const root = roots[0];
if (root) {
this.workspaceRootUri = root.uri;
}
});
this.taskWatcher.onTaskCreated((event: TaskInfo) => {
if (event.ctx === this.workspaceRootUri) {
this.proxy.$onDidStartTask({
id: event.taskId,
task: event.config
});
}
});
this.taskWatcher.onTaskExit((event: TaskExitedEvent) => {
if (event.ctx === this.workspaceRootUri) {
this.proxy.$onDidEndTask(event.taskId);
}
});
}
$registerTaskProvider(handle: number, type: string): void {
const taskProvider = this.createTaskProvider(handle);
const taskResolver = this.createTaskResolver(handle);
const disposable = new DisposableCollection();
disposable.push(this.taskProviderRegistry.register(type, taskProvider, handle));
disposable.push(this.taskResolverRegistry.register(type, taskResolver));
this.disposables.set(handle, disposable);
}
$unregister(handle: number): void {
const disposable = this.disposables.get(handle);
if (disposable) {
disposable.dispose();
this.disposables.delete(handle);
}
}
async $taskExecutions() {
const runningTasks = await this.taskService.getRunningTasks();
return runningTasks.map(taskInfo => ({
id: taskInfo.taskId,
task: taskInfo.config
}));
}
$terminateTask(id: number): void {
this.taskService.kill(id);
}
protected createTaskProvider(handle: number): TaskProvider {
return {
provideTasks: () =>
this.proxy.$provideTasks(handle).then(v =>
v!.map(taskDto =>
Object.assign(taskDto, {
_source: taskDto.source || 'plugin',
_scope: taskDto.scope
})
)
)
};
}
protected createTaskResolver(handle: number): TaskResolver {
return {
resolveTask: taskConfig =>
this.proxy.$resolveTask(handle, taskConfig).then(v =>
Object.assign(v!, {
_source: v!.source || 'plugin',
_scope: v!.scope
})
)
};
}
}