-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
base-terminal-server.ts
267 lines (232 loc) · 10.5 KB
/
base-terminal-server.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
/********************************************************************************
* 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 } from 'inversify';
import { ILogger, DisposableCollection, isWindows } from '@theia/core/lib/common';
import {
IBaseTerminalServer,
IBaseTerminalServerOptions,
IBaseTerminalClient,
TerminalProcessInfo,
EnvironmentVariableCollection,
MergedEnvironmentVariableCollection,
SerializableEnvironmentVariableCollection,
EnvironmentVariableMutator,
ExtensionOwnedEnvironmentVariableMutator,
EnvironmentVariableMutatorType,
EnvironmentVariableCollectionWithPersistence,
SerializableExtensionEnvironmentVariableCollection
} from '../common/base-terminal-protocol';
import { TerminalProcess, ProcessManager } from '@theia/process/lib/node';
import { ShellProcess } from './shell-process';
@injectable()
export abstract class BaseTerminalServer implements IBaseTerminalServer {
protected client: IBaseTerminalClient | undefined = undefined;
protected terminalToDispose = new Map<number, DisposableCollection>();
readonly collections: Map<string, EnvironmentVariableCollectionWithPersistence> = new Map();
mergedCollection: MergedEnvironmentVariableCollection;
constructor(
@inject(ProcessManager) protected readonly processManager: ProcessManager,
@inject(ILogger) @named('terminal') protected readonly logger: ILogger
) {
processManager.onDelete(id => {
const toDispose = this.terminalToDispose.get(id);
if (toDispose !== undefined) {
toDispose.dispose();
this.terminalToDispose.delete(id);
}
});
this.mergedCollection = this.resolveMergedCollection();
}
abstract create(options: IBaseTerminalServerOptions): Promise<number>;
async attach(id: number): Promise<number> {
const term = this.processManager.get(id);
if (term && term instanceof TerminalProcess) {
return term.id;
} else {
this.logger.warn(`Couldn't attach - can't find terminal with id: ${id} `);
return -1;
}
}
async getProcessId(id: number): Promise<number> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
}
return terminal.pid;
}
async getProcessInfo(id: number): Promise<TerminalProcessInfo> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
}
return {
executable: terminal.executable,
arguments: terminal.arguments,
};
}
async getCwdURI(id: number): Promise<string> {
const terminal = this.processManager.get(id);
if (!(terminal instanceof TerminalProcess)) {
throw new Error(`terminal "${id}" does not exist`);
}
return terminal.getCwdURI();
}
async close(id: number): Promise<void> {
const term = this.processManager.get(id);
if (term instanceof TerminalProcess) {
term.kill();
}
}
async getDefaultShell(): Promise<string> {
return ShellProcess.getShellExecutablePath();
}
dispose(): void {
// noop
}
async resize(id: number, cols: number, rows: number): Promise<void> {
const term = this.processManager.get(id);
if (term && term instanceof TerminalProcess) {
term.resize(cols, rows);
} else {
console.warn("Couldn't resize terminal " + id + ", because it doesn't exist.");
}
}
/* Set the client to receive notifications on. */
setClient(client: IBaseTerminalClient | undefined): void {
this.client = client;
if (!this.client) {
return;
}
this.client.updateTerminalEnvVariables();
}
protected postCreate(term: TerminalProcess): void {
const toDispose = new DisposableCollection();
toDispose.push(term.onError(error => {
this.logger.error(`Terminal pid: ${term.pid} error: ${error}, closing it.`);
if (this.client !== undefined) {
this.client.onTerminalError({
'terminalId': term.id,
'error': new Error(`Failed to execute terminal process (${error.code})`),
});
}
}));
toDispose.push(term.onExit(event => {
if (this.client !== undefined) {
this.client.onTerminalExitChanged({
'terminalId': term.id,
'code': event.code,
'signal': event.signal
});
}
}));
this.terminalToDispose.set(term.id, toDispose);
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// some code copied and modified from https://github.com/microsoft/vscode/blob/1.49.0/src/vs/workbench/contrib/terminal/common/environmentVariableService.ts
setCollection(extensionIdentifier: string, persistent: boolean, collection: SerializableEnvironmentVariableCollection): void {
const translatedCollection = { persistent, map: new Map<string, EnvironmentVariableMutator>(collection) };
this.collections.set(extensionIdentifier, translatedCollection);
this.updateCollections();
}
deleteCollection(extensionIdentifier: string): void {
this.collections.delete(extensionIdentifier);
this.updateCollections();
}
private updateCollections(): void {
this.persistCollections();
this.mergedCollection = this.resolveMergedCollection();
}
protected persistCollections(): void {
const collectionsJson: SerializableExtensionEnvironmentVariableCollection[] = [];
this.collections.forEach((collection, extensionIdentifier) => {
if (collection.persistent) {
collectionsJson.push({
extensionIdentifier,
collection: [...this.collections.get(extensionIdentifier)!.map.entries()]
});
}
});
if (this.client) {
const stringifiedJson = JSON.stringify(collectionsJson);
this.client.storeTerminalEnvVariables(stringifiedJson);
}
}
private resolveMergedCollection(): MergedEnvironmentVariableCollection {
return new MergedEnvironmentVariableCollectionImpl(this.collections);
}
}
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// some code copied and modified from https://github.com/microsoft/vscode/blob/1.49.0/src/vs/workbench/contrib/terminal/common/environmentVariableCollection.ts
export class MergedEnvironmentVariableCollectionImpl implements MergedEnvironmentVariableCollection {
readonly map: Map<string, ExtensionOwnedEnvironmentVariableMutator[]> = new Map();
constructor(collections: Map<string, EnvironmentVariableCollection>) {
collections.forEach((collection, extensionIdentifier) => {
const it = collection.map.entries();
let next = it.next();
while (!next.done) {
const variable = next.value[0];
let entry = this.map.get(variable);
if (!entry) {
entry = [];
this.map.set(variable, entry);
}
// If the first item in the entry is replace ignore any other entries as they would
// just get replaced by this one.
if (entry.length > 0 && entry[0].type === EnvironmentVariableMutatorType.Replace) {
next = it.next();
continue;
}
// Mutators get applied in the reverse order than they are created
const mutator = next.value[1];
entry.unshift({
extensionIdentifier,
value: mutator.value,
type: mutator.type
});
next = it.next();
}
});
}
applyToProcessEnvironment(env: { [key: string]: string | null }): void {
let lowerToActualVariableNames: { [lowerKey: string]: string | undefined } | undefined;
if (isWindows) {
lowerToActualVariableNames = {};
Object.keys(env).forEach(e => lowerToActualVariableNames![e.toLowerCase()] = e);
}
this.map.forEach((mutators, variable) => {
const actualVariable = isWindows ? lowerToActualVariableNames![variable.toLowerCase()] || variable : variable;
mutators.forEach(mutator => {
switch (mutator.type) {
case EnvironmentVariableMutatorType.Append:
env[actualVariable] = (env[actualVariable] || '') + mutator.value;
break;
case EnvironmentVariableMutatorType.Prepend:
env[actualVariable] = mutator.value + (env[actualVariable] || '');
break;
case EnvironmentVariableMutatorType.Replace:
env[actualVariable] = mutator.value;
break;
}
});
});
}
}