-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
task-terminal-widget-manager.ts
210 lines (188 loc) · 9.99 KB
/
task-terminal-widget-manager.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
/********************************************************************************
* Copyright (C) 2020 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, postConstruct } from 'inversify';
import { ApplicationShell, WidgetOpenerOptions } from '@theia/core/lib/browser';
import { TerminalWidget } from '@theia/terminal/lib/browser/base/terminal-widget';
import { TerminalWidgetFactoryOptions } from '@theia/terminal/lib/browser/terminal-widget-impl';
import { TerminalService } from '@theia/terminal/lib/browser/base/terminal-service';
import { PanelKind, TaskConfiguration, TaskWatcher, TaskExitedEvent, TaskServer, TaskOutputPresentation, TaskInfo } from '../common';
import { ProcessTaskInfo } from '../common/process/task-protocol';
import { TaskDefinitionRegistry } from './task-definition-registry';
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
export interface TaskTerminalWidget extends TerminalWidget {
readonly kind: 'task';
dedicated?: boolean;
taskId?: number;
taskConfig?: TaskConfiguration;
busy?: boolean;
}
export namespace TaskTerminalWidget {
export function is(widget: TerminalWidget): widget is TaskTerminalWidget {
return widget.kind === 'task';
}
}
export interface TaskTerminalWidgetOpenerOptions extends WidgetOpenerOptions {
taskId: number;
taskInfo?: TaskInfo;
}
export namespace TaskTerminalWidgetOpenerOptions {
export function isDedicatedTerminal(options: TaskTerminalWidgetOpenerOptions): boolean {
const taskConfig = options.taskInfo ? options.taskInfo.config : undefined;
return !!taskConfig && !!taskConfig.presentation && taskConfig.presentation.panel === PanelKind.Dedicated;
}
export function isNewTerminal(options: TaskTerminalWidgetOpenerOptions): boolean {
const taskConfig = options.taskInfo ? options.taskInfo.config : undefined;
return !!taskConfig && !!taskConfig.presentation && taskConfig.presentation.panel === PanelKind.New;
}
export function isSharedTerminal(options: TaskTerminalWidgetOpenerOptions): boolean {
const taskConfig = options.taskInfo ? options.taskInfo.config : undefined;
return !!taskConfig && (taskConfig.presentation === undefined || taskConfig.presentation.panel === undefined || taskConfig.presentation.panel === PanelKind.Shared);
}
export function echoExecutedCommand(options: TaskTerminalWidgetOpenerOptions): boolean {
const taskConfig = options.taskInfo ? options.taskInfo.config : undefined;
return !!taskConfig && (taskConfig.presentation === undefined || taskConfig.presentation.echo === undefined || taskConfig.presentation.echo);
}
}
@injectable()
export class TaskTerminalWidgetManager {
@inject(ApplicationShell)
protected readonly shell: ApplicationShell;
@inject(TaskDefinitionRegistry)
protected readonly taskDefinitionRegistry: TaskDefinitionRegistry;
@inject(TerminalService)
protected readonly terminalService: TerminalService;
@inject(TaskWatcher)
protected readonly taskWatcher: TaskWatcher;
@inject(TaskServer)
protected readonly taskServer: TaskServer;
@inject(WorkspaceService)
protected readonly workspaceService: WorkspaceService;
@postConstruct()
protected init(): void {
this.taskWatcher.onTaskExit((event: TaskExitedEvent) => {
const finishedTaskId = event.taskId;
// find the terminal where the task ran, and mark it as "idle"
for (const terminal of this.getTaskTerminalWidgets()) {
if (terminal.taskId === finishedTaskId) {
const showReuseMessage = !!event.config && TaskOutputPresentation.shouldShowReuseMessage(event.config);
this.notifyTaskFinished(terminal, showReuseMessage);
break;
}
}
});
this.terminalService.onDidCreateTerminal(async (widget: TerminalWidget) => {
const terminal = TaskTerminalWidget.is(widget) && widget;
if (terminal) {
const didConnectListener = terminal.onDidOpen(async () => {
const context = this.workspaceService.workspace && this.workspaceService.workspace.uri;
const tasksInfo = await this.taskServer.getTasks(context);
const taskInfo = tasksInfo.find(info => info.terminalId === widget.terminalId);
if (taskInfo) {
const taskConfig = taskInfo.config;
terminal.dedicated = !!taskConfig.presentation && !!taskConfig.presentation.panel && taskConfig.presentation.panel === PanelKind.Dedicated;
terminal.taskId = taskInfo.taskId;
terminal.taskConfig = taskConfig;
terminal.busy = true;
} else {
this.notifyTaskFinished(terminal, true);
}
});
const didConnectFailureListener = terminal.onDidOpenFailure(async () => {
this.notifyTaskFinished(terminal, true);
});
terminal.onDidDispose(() => {
didConnectListener.dispose();
didConnectFailureListener.dispose();
});
}
});
}
async open(factoryOptions: TerminalWidgetFactoryOptions, openerOptions: TaskTerminalWidgetOpenerOptions): Promise<TerminalWidget> {
const dedicated = TaskTerminalWidgetOpenerOptions.isDedicatedTerminal(openerOptions);
if (dedicated && (!openerOptions.taskInfo || !openerOptions.taskInfo.config)) {
throw new Error('"taskConfig" must be included as part of the "option.taskInfo" if "isDedicated" is true');
}
const { isNew, widget } = await this.getWidgetToRunTask(factoryOptions, openerOptions);
if (isNew) {
this.shell.addWidget(widget, { area: openerOptions.widgetOptions ? openerOptions.widgetOptions.area : 'bottom' });
widget.resetTerminal();
} else {
if (factoryOptions.title) {
widget.setTitle(factoryOptions.title);
}
const taskConfig = openerOptions.taskInfo ? openerOptions.taskInfo.config : undefined;
if (taskConfig && TaskOutputPresentation.shouldClearTerminalBeforeRun(taskConfig)) {
widget.clearOutput();
}
}
this.terminalService.open(widget, openerOptions);
const taskInfo = openerOptions.taskInfo;
if (TaskTerminalWidgetOpenerOptions.echoExecutedCommand(openerOptions) &&
taskInfo && ProcessTaskInfo.is(taskInfo) && taskInfo.command && taskInfo.command.length > 0
) {
widget.writeLine(`\x1b[1m> Executing task: ${taskInfo.command} <\x1b[0m\n`);
}
return widget;
}
protected async getWidgetToRunTask(
factoryOptions: TerminalWidgetFactoryOptions, openerOptions: TaskTerminalWidgetOpenerOptions
): Promise<{ isNew: boolean, widget: TerminalWidget }> {
let reusableTerminalWidget: TerminalWidget | undefined;
if (TaskTerminalWidgetOpenerOptions.isDedicatedTerminal(openerOptions)) {
for (const widget of this.getTaskTerminalWidgets()) {
// to run a task whose `taskPresentation === 'dedicated'`, the terminal to be reused must be
// 1) dedicated, 2) idle, 3) the one that ran the same task
if (widget.dedicated &&
!widget.busy &&
widget.taskConfig && openerOptions.taskInfo &&
this.taskDefinitionRegistry.compareTasks(openerOptions.taskInfo.taskConfig, widget.taskConfig)) {
reusableTerminalWidget = widget;
break;
}
}
} else if (TaskTerminalWidgetOpenerOptions.isSharedTerminal(openerOptions)) {
const availableWidgets: TerminalWidget[] = [];
for (const widget of this.getTaskTerminalWidgets()) {
// to run a task whose `taskPresentation === 'shared'`, the terminal to be used must be
// 1) not dedicated, and 2) idle
if (!widget.dedicated && !widget.busy) {
availableWidgets.push(widget);
}
}
const lastUsedWidget = availableWidgets.find(w => {
const lastUsedTerminal = this.terminalService.lastUsedTerminal;
return lastUsedTerminal && lastUsedTerminal.id === w.id;
});
reusableTerminalWidget = lastUsedWidget || availableWidgets[0];
}
// we are unable to find a terminal widget to run the task, or `taskPresentation === 'new'`
if (!reusableTerminalWidget) {
const widget = await this.terminalService.newTerminal({ ...factoryOptions, kind: 'task' });
return { isNew: true, widget };
}
return { isNew: false, widget: reusableTerminalWidget };
}
private getTaskTerminalWidgets(): TaskTerminalWidget[] {
return this.terminalService.all.filter(TaskTerminalWidget.is);
}
private notifyTaskFinished(terminal: TaskTerminalWidget, showReuseMessage: boolean): void {
terminal.busy = false;
terminal.scrollToBottom();
if (showReuseMessage) {
terminal.writeLine('\x1b[1m\n\rTerminal will be reused by tasks. \x1b[0m\n');
}
}
}