extHostCLIServer.ts 6.4 KB
Newer Older
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import { createRandomIPCHandle } from 'vs/base/parts/ipc/node/ipc.net';
7 8
import * as http from 'http';
import * as fs from 'fs';
9
import { IExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
10
import { IWindowOpenable, IOpenWindowOptions } from 'vs/platform/windows/common/windows';
11 12
import { URI } from 'vs/base/common/uri';
import { hasWorkspaceFileExtension } from 'vs/platform/workspaces/common/workspaces';
13
import { ILogService } from 'vs/platform/log/common/log';
14

M
Martin Aeschlimann 已提交
15 16 17
export interface OpenCommandPipeArgs {
	type: 'open';
	fileURIs?: string[];
18
	folderURIs?: string[];
M
Martin Aeschlimann 已提交
19 20 21
	forceNewWindow?: boolean;
	diffMode?: boolean;
	addMode?: boolean;
22
	gotoLineMode?: boolean;
M
Martin Aeschlimann 已提交
23 24 25
	forceReuseWindow?: boolean;
	waitMarkerFilePath?: string;
}
26

27 28 29 30 31
export interface OpenExternalCommandPipeArgs {
	type: 'openExternal';
	uris: string[];
}

M
Martin Aeschlimann 已提交
32 33 34 35
export interface StatusPipeArgs {
	type: 'status';
}

36 37 38
export interface RunCommandPipeArgs {
	type: 'command';
	command: string;
M
Martin Aeschlimann 已提交
39
	args: any[];
40 41
}

42 43 44 45 46 47 48 49 50
export interface ExtensionManagementPipeArgs {
	type: 'extensionManagement';
	list?: { showVersions?: boolean, category?: string; };
	install?: string[];
	uninstall?: string[];
	force?: boolean;
}

export type PipeCommand = OpenCommandPipeArgs | StatusPipeArgs | RunCommandPipeArgs | OpenExternalCommandPipeArgs | ExtensionManagementPipeArgs;
51

A
Alex Dima 已提交
52 53 54
export interface ICommandsExecuter {
	executeCommand<T>(id: string, ...args: any[]): Promise<T>;
}
55

A
Alex Dima 已提交
56 57
export class CLIServerBase {
	private readonly _server: http.Server;
58

A
Alex Dima 已提交
59 60 61 62 63
	constructor(
		private readonly _commands: ICommandsExecuter,
		private readonly logService: ILogService,
		private readonly _ipcHandlePath: string,
	) {
64 65
		this._server = http.createServer((req, res) => this.onRequest(req, res));
		this.setup().catch(err => {
66
			logService.error(err);
67 68 69 70 71 72 73 74 75 76 77
			return '';
		});
	}

	public get ipcHandlePath() {
		return this._ipcHandlePath;
	}

	private async setup(): Promise<string> {
		try {
			this._server.listen(this.ipcHandlePath);
78
			this._server.on('error', err => this.logService.error(err));
79
		} catch (err) {
80
			this.logService.error('Could not start open from terminal server.');
81 82
		}

M
Matt Bierner 已提交
83
		return this._ipcHandlePath;
84 85 86 87 88 89 90
	}

	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', () => {
91
			const data: PipeCommand | any = JSON.parse(chunks.join(''));
92 93 94 95
			switch (data.type) {
				case 'open':
					this.open(data, res);
					break;
96 97 98
				case 'openExternal':
					this.openExternal(data, res);
					break;
M
Martin Aeschlimann 已提交
99 100 101
				case 'status':
					this.getStatus(data, res);
					break;
102 103
				case 'command':
					this.runCommand(data, res)
104
						.catch(this.logService.error);
105
					break;
106 107 108 109
				case 'extensionManagement':
					this.manageExtensions(data, res)
						.catch(this.logService.error);
					break;
110 111
				default:
					res.writeHead(404);
J
Julien Brianceau 已提交
112
					res.write(`Unknown message type: ${data.type}`, err => {
113
						if (err) {
114
							this.logService.error(err);
115 116 117 118 119 120 121 122
						}
					});
					res.end();
					break;
			}
		});
	}

M
Martin Aeschlimann 已提交
123
	private open(data: OpenCommandPipeArgs, res: http.ServerResponse) {
124
		let { fileURIs, folderURIs, forceNewWindow, diffMode, addMode, forceReuseWindow, gotoLineMode, waitMarkerFilePath } = data;
125
		const urisToOpen: IWindowOpenable[] = [];
M
Martin Aeschlimann 已提交
126 127 128 129 130 131 132
		if (Array.isArray(folderURIs)) {
			for (const s of folderURIs) {
				try {
					urisToOpen.push({ folderUri: URI.parse(s) });
				} catch (e) {
					// ignore
				}
133
			}
M
Martin Aeschlimann 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
		}
		if (Array.isArray(fileURIs)) {
			for (const s of fileURIs) {
				try {
					if (hasWorkspaceFileExtension(s)) {
						urisToOpen.push({ workspaceUri: URI.parse(s) });
					} else {
						urisToOpen.push({ fileUri: URI.parse(s) });
					}
				} catch (e) {
					// ignore
				}
			}
		}
		if (urisToOpen.length) {
M
Martin Aeschlimann 已提交
149
			const waitMarkerFileURI = waitMarkerFilePath ? URI.file(waitMarkerFilePath) : undefined;
150
			const preferNewWindow = !forceReuseWindow && !waitMarkerFileURI && !addMode;
151
			const windowOpenArgs: IOpenWindowOptions = { forceNewWindow, diffMode, addMode, gotoLineMode, forceReuseWindow, preferNewWindow, waitMarkerFileURI };
M
Martin Aeschlimann 已提交
152
			this._commands.executeCommand('_files.windowOpen', urisToOpen, windowOpenArgs);
153 154 155 156 157
		}
		res.writeHead(200);
		res.end();
	}

158
	private async openExternal(data: OpenExternalCommandPipeArgs, res: http.ServerResponse) {
159
		for (const uri of data.uris) {
160
			await this._commands.executeCommand('_cli.openExternal', URI.parse(uri), { allowTunneling: true });
161 162 163 164 165
		}
		res.writeHead(200);
		res.end();
	}

166 167 168 169 170 171 172 173 174 175 176 177 178
	private async manageExtensions(data: ExtensionManagementPipeArgs, res: http.ServerResponse) {
		console.log('server: manageExtensions');
		try {
			const output = await this._commands.executeCommand('_cli.manageExtensions', data, { allowTunneling: true });
			res.writeHead(200);
			res.write(output);
		} catch (e) {
			res.writeHead(500);
			res.write(toString());
		}
		res.end();
	}

M
Martin Aeschlimann 已提交
179 180 181 182 183 184 185 186 187 188
	private async getStatus(data: StatusPipeArgs, res: http.ServerResponse) {
		try {
			const status = await this._commands.executeCommand('_issues.getSystemStatus');
			res.writeHead(200);
			res.write(status);
			res.end();
		} catch (err) {
			res.writeHead(500);
			res.write(String(err), err => {
				if (err) {
189
					this.logService.error(err);
M
Martin Aeschlimann 已提交
190 191 192 193 194 195
				}
			});
			res.end();
		}
	}

196 197 198 199 200 201 202
	private async runCommand(data: RunCommandPipeArgs, res: http.ServerResponse) {
		try {
			const { command, args } = data;
			const result = await this._commands.executeCommand(command, ...args);
			res.writeHead(200);
			res.write(JSON.stringify(result), err => {
				if (err) {
203
					this.logService.error(err);
204 205 206 207 208 209 210
				}
			});
			res.end();
		} catch (err) {
			res.writeHead(500);
			res.write(String(err), err => {
				if (err) {
211
					this.logService.error(err);
212 213 214 215 216 217
				}
			});
			res.end();
		}
	}

218 219 220 221 222 223 224
	dispose(): void {
		this._server.close();

		if (this._ipcHandlePath && process.platform !== 'win32' && fs.existsSync(this._ipcHandlePath)) {
			fs.unlinkSync(this._ipcHandlePath);
		}
	}
225
}
A
Alex Dima 已提交
226 227 228 229 230 231 232 233 234

export class CLIServer extends CLIServerBase {
	constructor(
		@IExtHostCommands commands: IExtHostCommands,
		@ILogService logService: ILogService
	) {
		super(commands, logService, createRandomIPCHandle());
	}
}