mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 10:08:49 +01:00
cli server for internal terminal
This commit is contained in:
@@ -14,6 +14,10 @@ import { ILogService } from 'vs/platform/log/common/log';
|
||||
import { EXT_HOST_CREATION_DELAY } from 'vs/workbench/parts/terminal/common/terminal';
|
||||
import { TerminalProcess } from 'vs/workbench/parts/terminal/node/terminalProcess';
|
||||
import { timeout } from 'vs/base/common/async';
|
||||
import { generateRandomPipeName } from 'vs/base/parts/ipc/node/ipc.net';
|
||||
import * as http from 'http';
|
||||
import * as fs from 'fs';
|
||||
import { ExtHostCommands } from 'vs/workbench/api/node/extHostCommands';
|
||||
|
||||
const RENDERER_NO_PROCESS_ID = -1;
|
||||
|
||||
@@ -242,6 +246,7 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
|
||||
private _terminalProcesses: { [id: number]: TerminalProcess } = {};
|
||||
private _terminalRenderers: ExtHostTerminalRenderer[] = [];
|
||||
private _getTerminalPromises: { [id: number]: Promise<ExtHostTerminal> } = {};
|
||||
private _cliServer: CLIServer | undefined;
|
||||
|
||||
public get activeTerminal(): ExtHostTerminal { return this._activeTerminal; }
|
||||
public get terminals(): ExtHostTerminal[] { return this._terminals; }
|
||||
@@ -256,7 +261,8 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
|
||||
constructor(
|
||||
mainContext: IMainContext,
|
||||
private _extHostConfiguration: ExtHostConfiguration,
|
||||
private _logService: ILogService
|
||||
private _logService: ILogService,
|
||||
private _commands: ExtHostCommands
|
||||
) {
|
||||
this._proxy = mainContext.getProxy(MainContext.MainThreadTerminalService);
|
||||
}
|
||||
@@ -412,6 +418,11 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
|
||||
// config and adding keys that are needed to create the process
|
||||
terminalEnvironment.addTerminalEnvironmentKeys(env, platform.locale, terminalConfig.get('setLocaleVariables'));
|
||||
|
||||
if (!this._cliServer) {
|
||||
this._cliServer = new CLIServer(this._commands);
|
||||
}
|
||||
env['VSCODE_IPC_HOOK_CLI'] = this._cliServer.ipcHandlePath;
|
||||
|
||||
// Fork the process and listen for messages
|
||||
this._logService.debug(`Terminal process launching on ext host`, shellLaunchConfig, initialCwd, cols, rows, env);
|
||||
this._terminalProcesses[id] = new TerminalProcess(shellLaunchConfig, initialCwd, cols, rows, env, terminalConfig.get('windowsEnableConpty'));
|
||||
@@ -421,6 +432,7 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
|
||||
this._terminalProcesses[id].onProcessExit((exitCode) => this._onProcessExit(id, exitCode));
|
||||
}
|
||||
|
||||
|
||||
public $acceptProcessInput(id: number, data: string): void {
|
||||
this._terminalProcesses[id].input(data);
|
||||
}
|
||||
@@ -449,6 +461,12 @@ export class ExtHostTerminalService implements ExtHostTerminalServiceShape {
|
||||
|
||||
// Send exit event to main side
|
||||
this._proxy.$sendProcessExit(id, exitCode);
|
||||
|
||||
if (this._cliServer && !Object.keys(this._terminalProcesses).length) {
|
||||
this._cliServer.dispose();
|
||||
this._cliServer = undefined;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private _getTerminalByIdEventually(id: number, retries: number = 5): Promise<ExtHostTerminal> {
|
||||
@@ -520,3 +538,70 @@ class ApiRequest {
|
||||
this._callback.apply(proxy, [id].concat(this._args));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class CLIServer {
|
||||
|
||||
private _server: http.Server;
|
||||
private _ipcHandlePath: string | undefined;
|
||||
|
||||
constructor(private _commands: ExtHostCommands) {
|
||||
this._server = http.createServer((req, res) => this.onRequest(req, res));
|
||||
this.setup().catch(err => {
|
||||
console.error(err);
|
||||
return '';
|
||||
});
|
||||
}
|
||||
|
||||
public get ipcHandlePath() {
|
||||
return this._ipcHandlePath;
|
||||
}
|
||||
|
||||
private async setup(): Promise<string> {
|
||||
this._ipcHandlePath = generateRandomPipeName();
|
||||
|
||||
try {
|
||||
this._server.listen(this.ipcHandlePath);
|
||||
this._server.on('error', err => console.error(err));
|
||||
} catch (err) {
|
||||
console.error('Could not start open from terminal server.');
|
||||
}
|
||||
|
||||
return this.ipcHandlePath;
|
||||
}
|
||||
private toURIs(strs: string[]): URI[] {
|
||||
const result: URI[] = [];
|
||||
if (Array.isArray(strs)) {
|
||||
for (const s of strs) {
|
||||
try {
|
||||
result.push(URI.parse(s));
|
||||
} catch (e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private onRequest(req: http.IncomingMessage, res: http.ServerResponse): void {
|
||||
const chunks: string[] = [];
|
||||
req.setEncoding('utf8');
|
||||
req.on('data', (d: string) => chunks.push(d));
|
||||
req.on('end', () => {
|
||||
const { fileURIs, folderURIs, forceNewWindow } = JSON.parse(chunks.join(''));
|
||||
if (folderURIs && folderURIs.length || fileURIs && fileURIs.length) {
|
||||
this._commands.executeCommand('_files.windowOpen', { folderURIs: this.toURIs(folderURIs), fileURIs: this.toURIs(fileURIs), forceNewWindow });
|
||||
}
|
||||
res.writeHead(200);
|
||||
res.end();
|
||||
});
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this._server.close();
|
||||
|
||||
if (this._ipcHandlePath && process.platform !== 'win32' && fs.existsSync(this._ipcHandlePath)) {
|
||||
fs.unlinkSync(this._ipcHandlePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user