microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/extensionServer.ts
290lines · modeblame
0502b7a8dlebu10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
710f8655digeff10 years ago | 4 | import * as Q from "q"; |
acf08bc2dlebu10 years ago | 5 | import * as vscode from "vscode"; |
| 6 | | |
7daed3fcArtem Egorov8 years ago | 7 | import {MessagingHelper}from "../common/extensionMessaging"; |
0a68f8dbArtem Egorov8 years ago | 8 | import {OutputChannelLogger} from "./log/OutputChannelLogger"; |
| 9 | import {Packager} from "../common/packager"; | |
710f8655digeff10 years ago | 10 | import {LogCatMonitor} from "./android/logCatMonitor"; |
| 11 | import {FileSystem} from "../common/node/fileSystem"; | |
df4bce40digeff10 years ago | 12 | import {SettingsHelper} from "./settingsHelper"; |
6e4d7a62Joshua Skelton10 years ago | 13 | import {Telemetry} from "../common/telemetry"; |
0a68f8dbArtem Egorov8 years ago | 14 | import {PlatformResolver} from "./platformResolver"; |
| 15 | import {TelemetryHelper} from "../common/telemetryHelper"; | |
| 16 | import {TargetPlatformHelper} from "../common/targetPlatformHelper"; | |
| 17 | import {MobilePlatformDeps} from "./generalMobilePlatform"; | |
51a4641cArtem Egorov7 years ago | 18 | import {IRemoteExtension, OpenFileRequest} from "../common/remoteExtension"; |
7daed3fcArtem Egorov8 years ago | 19 | import * as rpc from "noice-json-rpc"; |
| 20 | import * as WebSocket from "ws"; | |
| 21 | import WebSocketServer = WebSocket.Server; | |
0502b7a8dlebu10 years ago | 22 | |
| 23 | export class ExtensionServer implements vscode.Disposable { | |
7daed3fcArtem Egorov8 years ago | 24 | public api: IRemoteExtension; |
3118589cArtem Egorov8 years ago | 25 | public isDisposed: boolean = false; |
7daed3fcArtem Egorov8 years ago | 26 | private serverInstance: WebSocketServer | null; |
a822ac85dlebu10 years ago | 27 | private reactNativePackager: Packager; |
8411fd8dDaniel Lebu10 years ago | 28 | private pipePath: string; |
5c8365a6Artem Egorov8 years ago | 29 | private logCatMonitor: LogCatMonitor | null = null; |
0a68f8dbArtem Egorov8 years ago | 30 | private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
a822ac85dlebu10 years ago | 31 | |
2e432a9eArtem Egorov8 years ago | 32 | public constructor(projectRootPath: string, reactNativePackager: Packager) { |
7daed3fcArtem Egorov8 years ago | 33 | this.pipePath = MessagingHelper.getPath(projectRootPath); |
a822ac85dlebu10 years ago | 34 | this.reactNativePackager = reactNativePackager; |
| 35 | } | |
0502b7a8dlebu10 years ago | 36 | |
| 37 | /** | |
| 38 | * Starts the server. | |
| 39 | */ | |
| 40 | public setup(): Q.Promise<void> { | |
3118589cArtem Egorov8 years ago | 41 | this.isDisposed = false; |
0502b7a8dlebu10 years ago | 42 | |
1950c5e9Artem Egorov8 years ago | 43 | return Q.Promise((resolve, reject) => { |
| 44 | this._setup(resolve, reject); | |
| 45 | }); | |
0502b7a8dlebu10 years ago | 46 | } |
| 47 | | |
| 48 | /** | |
| 49 | * Stops the server. | |
| 50 | */ | |
| 51 | public dispose(): void { | |
3118589cArtem Egorov8 years ago | 52 | this.isDisposed = true; |
0502b7a8dlebu10 years ago | 53 | if (this.serverInstance) { |
| 54 | this.serverInstance.close(); | |
acf08bc2dlebu10 years ago | 55 | this.serverInstance = null; |
0502b7a8dlebu10 years ago | 56 | } |
710f8655digeff10 years ago | 57 | |
4edcda70Artem Egorov8 years ago | 58 | this.reactNativePackager.statusIndicator.dispose(); |
3118589cArtem Egorov8 years ago | 59 | this.reactNativePackager.stop(true); |
c2bf3c4fdigeff10 years ago | 60 | this.stopMonitoringLogCat(); |
0502b7a8dlebu10 years ago | 61 | } |
| 62 | | |
1950c5e9Artem Egorov8 years ago | 63 | private _setup(resolve: (val: void | Q.IPromise<void>) => void, reject: (reason: any) => void): void { |
| 64 | const errorCallback = this.recoverServer.bind(this, resolve, reject); | |
| 65 | let launchCallback = (done: (val: void | Q.IPromise<void>) => void) => { | |
| 66 | this.logger.debug(`Extension messaging server started at ${this.pipePath}.`); | |
| 67 | | |
| 68 | if (this.serverInstance) { | |
| 69 | this.serverInstance.removeListener("error", errorCallback); | |
| 70 | this.serverInstance.on("error", this.recoverServer.bind(this, null, null)); | |
| 71 | } | |
| 72 | done(void 0); | |
| 73 | }; | |
| 74 | | |
| 75 | this.serverInstance = new WebSocketServer({port: <any>this.pipePath}); | |
| 76 | this.api = new rpc.Server(this.serverInstance).api(); | |
| 77 | this.serverInstance.on("listening", launchCallback.bind(this, resolve)); | |
| 78 | this.serverInstance.on("error", errorCallback); | |
| 79 | | |
| 80 | this.setupApiHandlers(); | |
| 81 | } | |
| 82 | | |
7daed3fcArtem Egorov8 years ago | 83 | private setupApiHandlers(): void { |
| 84 | let methods: any = {}; | |
| 85 | methods.stopMonitoringLogCat = this.stopMonitoringLogCat.bind(this); | |
| 86 | methods.getPackagerPort = this.getPackagerPort.bind(this); | |
| 87 | methods.sendTelemetry = this.sendTelemetry.bind(this); | |
| 88 | methods.openFileAtLocation = this.openFileAtLocation.bind(this); | |
| 89 | methods.showInformationMessage = this.showInformationMessage.bind(this); | |
| 90 | methods.launch = this.launch.bind(this); | |
| 91 | methods.showDevMenu = this.showDevMenu.bind(this); | |
| 92 | methods.reloadApp = this.reloadApp.bind(this); | |
| 93 | | |
| 94 | this.api.Extension.expose(methods); | |
5e651f3edigeff10 years ago | 95 | } |
| 96 | | |
7daed3fcArtem Egorov8 years ago | 97 | private showDevMenu(deviceId?: string) { |
| 98 | this.api.Debugger.emitShowDevMenu(deviceId); | |
514df4f4Patricio Beltran10 years ago | 99 | } |
| 100 | | |
7daed3fcArtem Egorov8 years ago | 101 | private reloadApp(deviceId?: string) { |
| 102 | this.api.Debugger.emitReloadApp(deviceId); | |
27710197Vladimir Kotikov8 years ago | 103 | } |
0502b7a8dlebu10 years ago | 104 | |
| 105 | /** | |
6b6eb911dlebu10 years ago | 106 | * Recovers the server in case the named socket we use already exists, but no other instance of VSCode is active. |
0502b7a8dlebu10 years ago | 107 | */ |
1950c5e9Artem Egorov8 years ago | 108 | private recoverServer(resolve: (value: void) => {} , reject: (reason: any) => {}, error: any): void { |
6b6eb911dlebu10 years ago | 109 | let errorHandler = (e: any) => { |
| 110 | /* The named socket is not used. */ | |
| 111 | if (e.code === "ECONNREFUSED") { | |
8411fd8dDaniel Lebu10 years ago | 112 | new FileSystem().removePathRecursivelyAsync(this.pipePath) |
6b6eb911dlebu10 years ago | 113 | .then(() => { |
1950c5e9Artem Egorov8 years ago | 114 | if (resolve && reject) { |
| 115 | return this._setup(resolve, reject); | |
| 116 | } else { | |
| 117 | return this.setup(); | |
| 118 | } | |
6b6eb911dlebu10 years ago | 119 | }) |
| 120 | .done(); | |
| 121 | } | |
| 122 | }; | |
| 123 | | |
| 124 | /* The named socket already exists. */ | |
| 125 | if (error.code === "EADDRINUSE") { | |
7daed3fcArtem Egorov8 years ago | 126 | let clientSocket = new WebSocket(`ws+unix://${this.pipePath}`); |
6b6eb911dlebu10 years ago | 127 | clientSocket.on("error", errorHandler); |
7daed3fcArtem Egorov8 years ago | 128 | clientSocket.on("open", function() { |
| 129 | clientSocket.close(); | |
6b6eb911dlebu10 years ago | 130 | }); |
0502b7a8dlebu10 years ago | 131 | } |
| 132 | } | |
280c0746Patricio Beltran9 years ago | 133 | |
7daed3fcArtem Egorov8 years ago | 134 | /** |
| 135 | * Message handler for GET_PACKAGER_PORT. | |
| 136 | */ | |
2e432a9eArtem Egorov8 years ago | 137 | private getPackagerPort(program: string): number { |
| 138 | return SettingsHelper.getPackagerPort(program); | |
7daed3fcArtem Egorov8 years ago | 139 | } |
| 140 | | |
| 141 | /** | |
| 142 | * Message handler for OPEN_FILE_AT_LOCATION | |
| 143 | */ | |
51a4641cArtem Egorov7 years ago | 144 | private openFileAtLocation(openFileRequest: OpenFileRequest): Promise<void> { |
| 145 | const { filename, lineNumber } = openFileRequest; | |
7daed3fcArtem Egorov8 years ago | 146 | return new Promise((resolve) => { |
| 147 | vscode.workspace.openTextDocument(vscode.Uri.file(filename)) | |
| 148 | .then((document: vscode.TextDocument) => { | |
| 149 | vscode.window.showTextDocument(document) | |
| 150 | .then((editor: vscode.TextEditor) => { | |
| 151 | let range = editor.document.lineAt(lineNumber - 1).range; | |
| 152 | editor.selection = new vscode.Selection(range.start, range.end); | |
| 153 | editor.revealRange(range, vscode.TextEditorRevealType.InCenter); | |
| 154 | resolve(); | |
| 155 | }); | |
| 156 | }); | |
| 157 | }); | |
| 158 | } | |
| 159 | | |
| 160 | private stopMonitoringLogCat(): void { | |
| 161 | if (this.logCatMonitor) { | |
| 162 | this.logCatMonitor.dispose(); | |
| 163 | this.logCatMonitor = null; | |
| 164 | } | |
| 165 | } | |
| 166 | | |
| 167 | /** | |
| 168 | * Sends telemetry | |
| 169 | */ | |
51a4641cArtem Egorov7 years ago | 170 | private sendTelemetry(telemetryRequest: Telemetry.TelemetryRequest): void { |
| 171 | const { extensionId, extensionVersion, appInsightsKey, eventName, properties, measures } = telemetryRequest; | |
7daed3fcArtem Egorov8 years ago | 172 | Telemetry.sendExtensionTelemetry(extensionId, extensionVersion, appInsightsKey, eventName, properties, measures); |
| 173 | } | |
| 174 | | |
280c0746Patricio Beltran9 years ago | 175 | /** |
| 176 | * Message handler for SHOW_INFORMATION_MESSAGE | |
| 177 | */ | |
7daed3fcArtem Egorov8 years ago | 178 | private showInformationMessage(message: string): void { |
| 179 | vscode.window.showInformationMessage(message); | |
280c0746Patricio Beltran9 years ago | 180 | } |
0a68f8dbArtem Egorov8 years ago | 181 | |
7daed3fcArtem Egorov8 years ago | 182 | private launch(request: any): Promise<any> { |
0a68f8dbArtem Egorov8 years ago | 183 | let mobilePlatformOptions = requestSetup(request.arguments); |
| 184 | | |
| 185 | // We add the parameter if it's defined (adapter crashes otherwise) | |
| 186 | if (!isNullOrUndefined(request.arguments.logCatArguments)) { | |
| 187 | mobilePlatformOptions.logCatArguments = [parseLogCatArguments(request.arguments.logCatArguments)]; | |
| 188 | } | |
| 189 | | |
| 190 | if (!isNullOrUndefined(request.arguments.variant)) { | |
| 191 | mobilePlatformOptions.variant = request.arguments.variant; | |
| 192 | } | |
| 193 | | |
| 194 | if (!isNullOrUndefined(request.arguments.scheme)) { | |
| 195 | mobilePlatformOptions.scheme = request.arguments.scheme; | |
| 196 | } | |
| 197 | | |
3e313f6fArtem Egorov7 years ago | 198 | if (!isNullOrUndefined(request.arguments.productName)) { |
| 199 | mobilePlatformOptions.productName = request.arguments.productName; | |
| 200 | } | |
| 201 | | |
2e432a9eArtem Egorov8 years ago | 202 | mobilePlatformOptions.packagerPort = SettingsHelper.getPackagerPort(request.arguments.program); |
0a68f8dbArtem Egorov8 years ago | 203 | const platformDeps: MobilePlatformDeps = { |
| 204 | packager: this.reactNativePackager, | |
| 205 | }; | |
| 206 | const mobilePlatform = new PlatformResolver() | |
| 207 | .resolveMobilePlatform(request.arguments.platform, mobilePlatformOptions, platformDeps); | |
7daed3fcArtem Egorov8 years ago | 208 | return new Promise((resolve, reject) => { |
031832ffArtem Egorov8 years ago | 209 | const extProps = { |
| 210 | platform: { | |
| 211 | value: request.arguments.platform, | |
| 212 | isPii: false, | |
| 213 | }, | |
| 214 | }; | |
| 215 | | |
| 216 | TelemetryHelper.generate("launch", extProps, (generator) => { | |
7daed3fcArtem Egorov8 years ago | 217 | generator.step("checkPlatformCompatibility"); |
| 218 | TargetPlatformHelper.checkTargetPlatformSupport(mobilePlatformOptions.platform); | |
4787ec09Artem Egorov8 years ago | 219 | return mobilePlatform.beforeStartPackager() |
| 220 | .then(() => { | |
| 221 | generator.step("startPackager"); | |
| 222 | return mobilePlatform.startPackager(); | |
| 223 | }) | |
7daed3fcArtem Egorov8 years ago | 224 | .then(() => { |
| 225 | // We've seen that if we don't prewarm the bundle cache, the app fails on the first attempt to connect to the debugger logic | |
| 226 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue | |
| 227 | generator.step("prewarmBundleCache"); | |
| 228 | this.logger.info("Prewarming bundle cache. This may take a while ..."); | |
| 229 | return mobilePlatform.prewarmBundleCache(); | |
| 230 | }) | |
| 231 | .then(() => { | |
88908964Artem Egorov8 years ago | 232 | generator.step("mobilePlatform.runApp").add("target", mobilePlatformOptions.target, false); |
7daed3fcArtem Egorov8 years ago | 233 | this.logger.info("Building and running application."); |
| 234 | return mobilePlatform.runApp(); | |
| 235 | }) | |
| 236 | .then(() => { | |
| 237 | generator.step("mobilePlatform.enableJSDebuggingMode"); | |
e03193abArtem Egorov8 years ago | 238 | this.logger.info("Enable JS Debugging"); |
7daed3fcArtem Egorov8 years ago | 239 | return mobilePlatform.enableJSDebuggingMode(); |
| 240 | }) | |
| 241 | .then(() => { | |
| 242 | resolve(); | |
| 243 | }) | |
| 244 | .catch(error => { | |
748105d9Artem Egorov8 years ago | 245 | this.logger.error(error); |
7daed3fcArtem Egorov8 years ago | 246 | reject(error); |
| 247 | }); | |
| 248 | }); | |
0a68f8dbArtem Egorov8 years ago | 249 | }); |
| 250 | } | |
| 251 | } | |
| 252 | | |
| 253 | /** | |
| 254 | * Parses log cat arguments to a string | |
| 255 | */ | |
| 256 | function parseLogCatArguments(userProvidedLogCatArguments: any): string { | |
| 257 | return Array.isArray(userProvidedLogCatArguments) | |
| 258 | ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments | |
| 259 | : userProvidedLogCatArguments; // If not, we leave it as-is | |
| 260 | } | |
| 261 | | |
| 262 | function isNullOrUndefined(value: any): boolean { | |
| 263 | return typeof value === "undefined" || value === null; | |
| 264 | } | |
| 265 | | |
| 266 | function requestSetup(args: any): any { | |
2e432a9eArtem Egorov8 years ago | 267 | const workspaceFolder: vscode.WorkspaceFolder = <vscode.WorkspaceFolder>vscode.workspace.getWorkspaceFolder(vscode.Uri.file(args.program)); |
0a68f8dbArtem Egorov8 years ago | 268 | const projectRootPath = getProjectRoot(args); |
| 269 | let mobilePlatformOptions: any = { | |
a41f5c68Artem Egorov8 years ago | 270 | workspaceRoot: workspaceFolder.uri.fsPath, |
0a68f8dbArtem Egorov8 years ago | 271 | projectRoot: projectRootPath, |
| 272 | platform: args.platform, | |
1174ee3dArtem Egorov8 years ago | 273 | env: args.env, |
| 274 | envFile: args.envFile, | |
23d47878Artem Egorov8 years ago | 275 | target: args.target || "simulator", |
0a68f8dbArtem Egorov8 years ago | 276 | }; |
| 277 | | |
| 278 | if (!args.runArguments) { | |
4529aa97Artem Egorov8 years ago | 279 | let runArgs = SettingsHelper.getRunArgs(args.platform, args.target || "simulator", workspaceFolder.uri); |
0a68f8dbArtem Egorov8 years ago | 280 | mobilePlatformOptions.runArguments = runArgs; |
a18a84baArtem Egorov8 years ago | 281 | } else { |
| 282 | mobilePlatformOptions.runArguments = args.runArguments; | |
0a68f8dbArtem Egorov8 years ago | 283 | } |
| 284 | | |
| 285 | return mobilePlatformOptions; | |
| 286 | } | |
| 287 | | |
| 288 | function getProjectRoot(args: any): string { | |
4edcda70Artem Egorov8 years ago | 289 | return SettingsHelper.getReactNativeProjectRoot(args.program); |
0502b7a8dlebu10 years ago | 290 | } |