forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenShiftTerminal.ts
More file actions
623 lines (568 loc) · 22.4 KB
/
openShiftTerminal.ts
File metadata and controls
623 lines (568 loc) · 22.4 KB
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import { randomUUID } from 'crypto';
import * as fs from 'fs/promises';
import type * as pty from 'node-pty';
import { platform } from 'os';
import {
CancellationToken,
ColorTheme, commands, Disposable,
Webview,
WebviewView,
WebviewViewProvider,
WebviewViewResolveContext, window,
workspace
} from 'vscode';
import { SerializeAddon } from 'xterm-addon-serialize';
import { Terminal } from 'xterm-headless';
import { CommandText } from '../../base/command';
import { CliChannel } from '../../cli';
import { ToolsConfig } from '../../tools';
import { getVscodeModule } from '../../util/credentialManager';
import { loadWebviewHtml } from '../common-ext/utils';
// HACK: we cannot include node-pty ourselves,
// since the library can only be run under one version of node
// (whichever one it was compiled for).
// If we want to install the extension across different VS Code versions,
// we need to use the `node-pty` included in those versions
const ptyInstance: typeof pty | undefined = getVscodeModule('node-pty');
if (!ptyInstance) {
throw new Error('Unable to access node-pty from VS Code');
}
interface Message {
kind: string;
data: any;
}
/**
* An API to interact with a running terminal instance
*/
export interface OpenShiftTerminalApi {
/**
* Open the OpenShift terminal and switch to the tab for the given program
*/
focusTerminal: () => void;
/**
* Input text into the terminal.
*
* @param text the text to input into the terminal
*/
sendText: (text: string) => void;
/**
* Close the terminal by sending `\u0003` (`^C`).
*/
kill: () => void;
/**
* Close the terminal. If the extension is not running on Windows, the process will be terminated using SIGABRT.
*/
forceKill: () => void;
}
/**
* Represents a command running in the OpenShift Terminal view.
*/
class OpenShiftTerminal {
private _pty;
private _file: string;
private _args: string | string[];
private _options;
private _name: string;
private _sendTerminalData: (data: string) => void;
private _sendExitMessage: () => void;
private _onSpawnListener: () => void;
private _onExitListener: () => void;
private _onTextListener: (text: string) => void;
private _uuid: string;
private _headlessTerm: Terminal;
private _termSerializer: SerializeAddon;
private _disposables: { dispose(): void }[];
private _ptyExited = false;
private _terminalRendering = false;
private _buffer: Buffer;
/**
* Creates a new OpenShiftTerminal
*
* @param uuid a unique identifier for the terminal
* @param sendMessage the function used to send a message to the UI terminal
* @param file the path to the program to execute
* @param args the arguments to pass to the program
* @param options the options for spawning the pty (name, current working directory, environment)
* @param closeOnExit true if the terminal should close when the program exits, or false if it should stay open until the user presses an additional key
* @param callbacks functions to execute in response to events in the terminal:
* - onSpawn(): called when the pty is created
* - onExit(): called when the pty exits (whether normally or though SIGABRT)
* - onText(): called when text is printed to the terminal, whether by the program or by user input
*/
constructor(
uuid: string,
sendMessage: (message: Message) => Promise<void>,
file: string,
args: string | string[],
options,
callbacks?: {
onSpawn?: () => void;
onExit?: () => void;
onText?: (text: string) => void;
},
) {
this._uuid = uuid;
this._sendTerminalData = (data) => {
void sendMessage({ kind: 'termOutput', data: { uuid, output: data } });
};
this._sendExitMessage = () => {
void sendMessage({ kind: 'termExit', data: { uuid } });
};
this._onSpawnListener = callbacks?.onSpawn || (() => undefined);
this._onExitListener = callbacks?.onExit || (() => undefined);
this._onTextListener = callbacks?.onText || ((_text: string) => undefined);
this._file = file;
this._args = args;
this._options = options;
this._name = options.name;
this._disposables = [];
this._headlessTerm = new Terminal({ allowProposedApi: true });
this._termSerializer = new SerializeAddon();
this._headlessTerm.loadAddon(this._termSerializer);
this._buffer = Buffer.from('');
this._disposables.push(this._headlessTerm);
this._disposables.push(this._termSerializer);
}
startPty() {
if (platform() === 'win32') {
const escapedArgs = Array.isArray(this._args)
? this._args.join(' ')
: this._args;
this._options.useConpty = false;
this._pty = (/\s/).test(this._file) ? ptyInstance.spawn(
`${this._file}`,
[
`${escapedArgs}`,
],
this._options,
) : ptyInstance.spawn(
'cmd.EXE',
[
'/c',
`${this._file} ${escapedArgs}`,
],
this._options,
);
} else {
this._pty = ptyInstance.spawn(this._file, this._args, this._options);
}
this._disposables.push(
this._pty.onData((data) => {
if (this._terminalRendering) {
this._sendTerminalData(data);
this._headlessTerm.write(data);
this._onTextListener(data);
} else {
this._buffer = Buffer.concat([this._buffer, Buffer.from(data)]);
this._onTextListener(data);
}
}),
);
this._disposables.push(
this._pty.onExit((_e) => {
this.onExit();
}),
);
this._onSpawnListener();
}
private onExit() {
this._onExitListener();
const msg = '\r\n\r\nPress any key to close this terminal\r\n';
this._sendTerminalData(msg);
this._headlessTerm.write(msg);
this._ptyExited = true;
}
/**
* Returns the name of this terminal.
*
* @returns the name of this terminal
*/
public get name() {
return this._name;
}
/**
* Returns the unique identifier of this terminal.
*
* @returns the unique identifier of this terminal
*/
public get uuid() {
return this._uuid;
}
/**
* Returns true if the pty that's running the program associated with this terminal has been started and has not exited, and false otherwise.
*
* @return true if the pty that's running the program associated with this terminal has been started and has not exited, and false otherwise
*/
public get isPtyLive() {
return this._pty && !this._ptyExited;
}
/**
* Returns the string data of the terminal, serialized
*/
public serialized(): Promise<string> {
return new Promise<string>((resolve) => {
this._headlessTerm.write(this._buffer, () => {
resolve(this._termSerializer.serialize());
});
this._buffer = Buffer.from('');
});
}
/**
* Resizes the terminal to the given size.
*
* @param cols the new number of columns for the terminal
* @param rows the new number of rows for the terminal
*/
public resize(cols: number, rows: number): void {
if (this.isPtyLive) {
this._pty.resize(cols, rows);
}
this._headlessTerm.resize(cols, rows);
}
/**
* Input text into the terminal.
*
* @param data the text to input into the terminal
*/
public write(data: string) {
if (this.isPtyLive) {
this._pty.write(data);
} else if (this._ptyExited) {
this._sendExitMessage();
}
}
/**
* Dispose of all resources associated with this terminal.
*/
public dispose(): void {
if (this.isPtyLive) {
const termKilled = new Promise<void>((resolve) => {
this._disposables.push(
this._pty.onExit((_e) => {
resolve();
}),
);
});
this._pty.kill();
void Promise.race([
termKilled,
new Promise<void>((_, reject) => {
// force kill the terminal if it takes more than a minute to shut down
setTimeout(reject, 60_000);
}),
])
.then(() => {
for (const disposable of this._disposables) {
disposable.dispose();
}
})
.catch((_error) => {
// force kill handles disposing of the disposables
this.forceKill();
});
} else {
for (const disposable of this._disposables) {
disposable.dispose();
}
}
}
/**
* Send SIGABRT to the program if it's running and the operating system is not on Windows,
* then dispose of all resources associated with this terminal.
*/
public forceKill(): void {
if (this.isPtyLive) {
if (platform() !== 'win32') {
this._pty.kill('SIGABRT');
// pty won't send the exit message, so we have to perform the exit code ourselves
this.onExit();
}
// can't do anything better on windows, so just wait
}
this.dispose();
}
/**
* Start the program if it hasn't been started yet, and start sending data to the terminal UI.
*/
public startRendering(): void {
this._terminalRendering = true;
if (!this._pty) {
this.startPty();
}
}
/**
* Stop sending data to the terminal UI.
*
* Terminal output will be buffered in a headless terminal until rendering is started again.
*/
public stopRendering(): void {
this._terminalRendering = false;
}
/**
* Close this terminal tab, force killing the process if it's still running.
*/
public closeTab(): void {
this.forceKill();
this._sendExitMessage();
}
public clear(): void {
this._headlessTerm.clear();
}
}
/**
* Represents the OpenShift Terminal view.
*/
export class OpenShiftTerminalManager implements WebviewViewProvider {
private static INSTANCE = new OpenShiftTerminalManager();
private webview: Webview;
private webviewView: WebviewView;
private readonly openShiftTerminals: Map<string, OpenShiftTerminal> = new Map();
private webviewResolved: Promise<void>;
private markWebviewResolved: () => void;
constructor() {
// create a promise that is resolved when `markWebviewResolved` is called
this.webviewResolved = new Promise((resolve) => {
this.markWebviewResolved = resolve;
});
}
public static getInstance() {
return OpenShiftTerminalManager.INSTANCE;
}
/**
* Resolves the HTML content for the webview of the given webview view.
*
* To be called by VS Code only
*
* @param webviewView the webview view containing the webview to resolve the content for
* @param context ignored
* @param token the cancellation token
*/
async resolveWebviewView(
webviewView: WebviewView,
_context: WebviewViewResolveContext<unknown>,
token: CancellationToken,
): Promise<void> {
this.webviewView = webviewView;
this.webview = webviewView.webview;
this.webviewView.show();
this.webview.options = {
enableScripts: true,
};
const newHtml: string = await loadWebviewHtml('openshiftTerminalViewer', this.webviewView);
if (!token.isCancellationRequested) {
this.webview.html = newHtml;
}
const disposables: Disposable[] = [];
// handle messages from the webview:
// - `termInit(uuid): string`: one of the following happened
// - a terminal was created in the webview and is ready to receive output; start the pty
// - an existing webview terminal which had been unfocused was refocused.
// Since the terminal output gets cleared between tab switches,
// rehydrate the terminal with the old output
// and start forwarding pty data to the terminal again
// - `termSuspend(uuid): void`: the given webview terminal was unfocused; stop sending pty data to the terminal
// - `input(uuid): void`: then given webview terminal received user input; pass this on to the pty
// - `resize(uuid, row, col): void`: the given webview terminal was resize; resize the headless terminal, and resize the pty if it's still alive
// - `closeTerminal(uuid): void`: the given webview terminal was closed; kill the process if needed and dispose of all the resources for the terminal in node
// - `termMuxUp(): void`: the webview has rendered for the first time and is ready to respond to `createTerminal` messages
this.webview.onDidReceiveMessage(
(event) => {
const message = event as Message;
const terminal = this.openShiftTerminals.get(message?.data?.uuid);
if (terminal) {
if (message.kind === 'termInit') {
void terminal
.serialized()
.then((serializedData) => {
void this.sendMessage({
kind: 'termInit',
data: {
uuid: terminal.uuid,
serializedOutput: serializedData,
},
});
})
.then(() => {
terminal.startRendering();
});
} else if (message.kind === 'termSuspend') {
terminal.stopRendering();
} else if (message.kind === 'termClear') {
terminal.clear();
} else if (message.kind === 'input') {
terminal.write(message.data.data);
} else if (message.kind === 'resize') {
terminal.resize(message.data.cols, message.data.rows);
} else if (message.kind === 'closeTerminal') {
terminal.dispose();
this.openShiftTerminals.delete(message?.data?.uuid);
}
} else if (message.kind === 'termMuxUp') {
// mark the webview as resolved, to signal to `createTerminal`
// that it can issue requests to get a terminal
this.markWebviewResolved();
}
},
undefined,
disposables,
);
webviewView.onDidDispose(() => {
disposables.forEach((disposable) => {
disposable.dispose();
});
});
// Synchronize the color theme, font family, and font size of VS Code with the webview
workspace.onDidChangeConfiguration((e) => {
// adapt to the font family and size changes
// see note in ./app/index.tsx for a detailed explanation on why
// we listen to 'editor' config changes
// instead of 'terminal.integrated' config changes
if (e.affectsConfiguration('terminal.integrated')) {
void this.sendMessage({
kind: 'setTheme',
data: {
kind: window.activeColorTheme.kind,
fontFamily: workspace
.getConfiguration('terminal.integrated')
.get('fontFamily'),
fontSize: Number.parseInt(
workspace.getConfiguration('terminal.integrated').get('fontSize'),
10,
),
},
});
}
}, disposables);
void this.webviewResolved.then(() => {
void this.sendMessage({
kind: 'setTheme',
data: {
kind: window.activeColorTheme.kind,
fontFamily: workspace.getConfiguration('terminal.integrated').get('fontFamily'),
fontSize: Number.parseInt(
workspace.getConfiguration('terminal.integrated').get('fontSize'),
10,
),
},
});
disposables.push(
window.onDidChangeActiveColorTheme((colorTheme: ColorTheme) => {
void this.sendMessage({
kind: 'setTheme',
data: {
kind: colorTheme.kind,
fontFamily: workspace
.getConfiguration('terminal.integrated')
.get('fontFamily'),
fontSize: Number.parseInt(
workspace.getConfiguration('terminal.integrated').get('fontSize'),
10,
),
},
});
}),
);
});
}
public async executeInTerminal(command: CommandText, cwd: string = process.cwd(), name = 'OpenShift', addEnv = {} as {[key : string]: string} ): Promise<void> {
const merged = Object.fromEntries([...Object.entries(addEnv), ...Object.entries(CliChannel.createTelemetryEnv()), ...Object.entries(process.env)]);
await OpenShiftTerminalManager.getInstance().createTerminal(command, name, cwd, merged);
}
/**
* Run a command in the OpenShift Terminal view and return an api to interact with the running command.
*
* The command will be run in a new 'tab' of the terminal.
*
* @param commandText the command to run in the terminal
* @param name the display name of the terminal session
* @param cwd the current working directory to use when running the command
* @param env the environment to use when running the command
* @param exitOnClose true if the terminal should close when the program exits, or false if it should stay open until the user presses an additional key
* @param callbacks functions to execute in response to events in the terminal:
* - onSpawn(): called when the pty is created
* - onExit(): called when the pty exits (whether normally or though SIGABRT)
* - onText(): called when text is printed to the terminal, whether by the program or by user input
* @returns an api to interact with the running command
*/
public async createTerminal(
commandText: CommandText,
name: string,
cwd = process.cwd(),
env = process.env,
callbacks?: {
onSpawn?: () => void;
onExit?: () => void;
onText?: (text: string) => void;
},
): Promise<OpenShiftTerminalApi> {
// focus the OpenShift terminal view in order to force the webview to be created
// (if it hasn't already)
await commands.executeCommand('openShiftTerminalView.focus');
// wait until the webview is ready to receive requests to create terminals
await this.webviewResolved;
const tool = commandText.command;
let toolLocation: string | undefined;
try {
toolLocation = await ToolsConfig.detect(tool);
} catch (_e) {
// do nothing
}
if (!toolLocation) {
try {
await fs.access(tool);
toolLocation = tool;
} catch (__e) {
// do nothing
}
}
if (!toolLocation) {
const msg = `OpenShift Toolkit internal error: could not find ${tool}`;
void window.showErrorMessage(msg);
throw new Error(msg);
}
// try to clean up an existing exited terminal in place of this one
for (const existingTerm of this.openShiftTerminals.values()) {
if (!existingTerm.isPtyLive && existingTerm.name === name) {
existingTerm.closeTab();
break;
}
}
const newTermUUID = randomUUID();
// create the object that manages the headless terminal and the pty.
// the process is run as a child process under node.
// the webview is synchronized to the pty and headless terminal using message passing
this.openShiftTerminals.set(
newTermUUID,
new OpenShiftTerminal(
newTermUUID,
(message: Message) => {
return this.sendMessage(message);
},
toolLocation,
commandText.args,
{
cwd,
env,
name,
},
callbacks,
),
);
// issue request to create terminal in the webview
await this.sendMessage({ kind: 'createTerminal', data: { uuid: newTermUUID, name } });
return {
sendText: (text: string) => this.openShiftTerminals.get(newTermUUID).write(text),
focusTerminal: () =>
void this.sendMessage({ kind: 'switchToTerminal', data: { uuid: newTermUUID } }),
kill: () => this.openShiftTerminals.get(newTermUUID).write('\u0003'),
forceKill: () => this.openShiftTerminals.get(newTermUUID).forceKill(),
};
}
private async sendMessage(msg: Message): Promise<void> {
await this.webview.postMessage(msg);
}
}