microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/extensionServer.ts
258lines · 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"; | |
7daed3fcArtem Egorov8 years ago | 18 | import {IRemoteExtension} from "../common/remoteExtension"; |
| 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 | let deferred = Q.defer<void>(); |
| 43 | | |
| 44 | let launchCallback = (error: any) => { | |
0a68f8dbArtem Egorov8 years ago | 45 | this.logger.debug(`Extension messaging server started at ${this.pipePath}.`); |
7daed3fcArtem Egorov8 years ago | 46 | deferred.resolve(void 0); |
0502b7a8dlebu10 years ago | 47 | }; |
| 48 | | |
7daed3fcArtem Egorov8 years ago | 49 | this.serverInstance = new WebSocketServer({port: <any>this.pipePath}); |
| 50 | this.api = new rpc.Server(this.serverInstance).api(); | |
3118589cArtem Egorov8 years ago | 51 | this.serverInstance.on("listening", launchCallback.bind(this)); |
6b6eb911dlebu10 years ago | 52 | this.serverInstance.on("error", this.recoverServer.bind(this)); |
7daed3fcArtem Egorov8 years ago | 53 | |
| 54 | this.setupApiHandlers(); | |
0502b7a8dlebu10 years ago | 55 | |
| 56 | return deferred.promise; | |
| 57 | } | |
| 58 | | |
| 59 | /** | |
| 60 | * Stops the server. | |
| 61 | */ | |
| 62 | public dispose(): void { | |
3118589cArtem Egorov8 years ago | 63 | this.isDisposed = true; |
0502b7a8dlebu10 years ago | 64 | if (this.serverInstance) { |
| 65 | this.serverInstance.close(); | |
acf08bc2dlebu10 years ago | 66 | this.serverInstance = null; |
0502b7a8dlebu10 years ago | 67 | } |
710f8655digeff10 years ago | 68 | |
4edcda70Artem Egorov8 years ago | 69 | this.reactNativePackager.statusIndicator.dispose(); |
3118589cArtem Egorov8 years ago | 70 | this.reactNativePackager.stop(true); |
c2bf3c4fdigeff10 years ago | 71 | this.stopMonitoringLogCat(); |
0502b7a8dlebu10 years ago | 72 | } |
| 73 | | |
7daed3fcArtem Egorov8 years ago | 74 | private setupApiHandlers(): void { |
| 75 | let methods: any = {}; | |
| 76 | methods.stopMonitoringLogCat = this.stopMonitoringLogCat.bind(this); | |
| 77 | methods.getPackagerPort = this.getPackagerPort.bind(this); | |
| 78 | methods.sendTelemetry = this.sendTelemetry.bind(this); | |
| 79 | methods.openFileAtLocation = this.openFileAtLocation.bind(this); | |
| 80 | methods.showInformationMessage = this.showInformationMessage.bind(this); | |
| 81 | methods.launch = this.launch.bind(this); | |
| 82 | methods.showDevMenu = this.showDevMenu.bind(this); | |
| 83 | methods.reloadApp = this.reloadApp.bind(this); | |
| 84 | | |
| 85 | this.api.Extension.expose(methods); | |
5e651f3edigeff10 years ago | 86 | } |
| 87 | | |
7daed3fcArtem Egorov8 years ago | 88 | private showDevMenu(deviceId?: string) { |
| 89 | this.api.Debugger.emitShowDevMenu(deviceId); | |
514df4f4Patricio Beltran10 years ago | 90 | } |
| 91 | | |
7daed3fcArtem Egorov8 years ago | 92 | private reloadApp(deviceId?: string) { |
| 93 | this.api.Debugger.emitReloadApp(deviceId); | |
27710197Vladimir Kotikov8 years ago | 94 | } |
0502b7a8dlebu10 years ago | 95 | |
| 96 | /** | |
6b6eb911dlebu10 years ago | 97 | * Recovers the server in case the named socket we use already exists, but no other instance of VSCode is active. |
0502b7a8dlebu10 years ago | 98 | */ |
6b6eb911dlebu10 years ago | 99 | private recoverServer(error: any): void { |
| 100 | let errorHandler = (e: any) => { | |
| 101 | /* The named socket is not used. */ | |
| 102 | if (e.code === "ECONNREFUSED") { | |
8411fd8dDaniel Lebu10 years ago | 103 | new FileSystem().removePathRecursivelyAsync(this.pipePath) |
6b6eb911dlebu10 years ago | 104 | .then(() => { |
7daed3fcArtem Egorov8 years ago | 105 | return this.setup(); |
6b6eb911dlebu10 years ago | 106 | }) |
| 107 | .done(); | |
| 108 | } | |
| 109 | }; | |
| 110 | | |
| 111 | /* The named socket already exists. */ | |
| 112 | if (error.code === "EADDRINUSE") { | |
7daed3fcArtem Egorov8 years ago | 113 | let clientSocket = new WebSocket(`ws+unix://${this.pipePath}`); |
6b6eb911dlebu10 years ago | 114 | clientSocket.on("error", errorHandler); |
7daed3fcArtem Egorov8 years ago | 115 | clientSocket.on("open", function() { |
| 116 | clientSocket.close(); | |
6b6eb911dlebu10 years ago | 117 | }); |
0502b7a8dlebu10 years ago | 118 | } |
| 119 | } | |
280c0746Patricio Beltran9 years ago | 120 | |
7daed3fcArtem Egorov8 years ago | 121 | /** |
| 122 | * Message handler for GET_PACKAGER_PORT. | |
| 123 | */ | |
2e432a9eArtem Egorov8 years ago | 124 | private getPackagerPort(program: string): number { |
| 125 | return SettingsHelper.getPackagerPort(program); | |
7daed3fcArtem Egorov8 years ago | 126 | } |
| 127 | | |
| 128 | /** | |
| 129 | * Message handler for OPEN_FILE_AT_LOCATION | |
| 130 | */ | |
| 131 | private openFileAtLocation(filename: string, lineNumber: number): Promise<void> { | |
| 132 | return new Promise((resolve) => { | |
| 133 | vscode.workspace.openTextDocument(vscode.Uri.file(filename)) | |
| 134 | .then((document: vscode.TextDocument) => { | |
| 135 | vscode.window.showTextDocument(document) | |
| 136 | .then((editor: vscode.TextEditor) => { | |
| 137 | let range = editor.document.lineAt(lineNumber - 1).range; | |
| 138 | editor.selection = new vscode.Selection(range.start, range.end); | |
| 139 | editor.revealRange(range, vscode.TextEditorRevealType.InCenter); | |
| 140 | resolve(); | |
| 141 | }); | |
| 142 | }); | |
| 143 | }); | |
| 144 | } | |
| 145 | | |
| 146 | private stopMonitoringLogCat(): void { | |
| 147 | if (this.logCatMonitor) { | |
| 148 | this.logCatMonitor.dispose(); | |
| 149 | this.logCatMonitor = null; | |
| 150 | } | |
| 151 | } | |
| 152 | | |
| 153 | /** | |
| 154 | * Sends telemetry | |
| 155 | */ | |
| 156 | private sendTelemetry(extensionId: string, extensionVersion: string, appInsightsKey: string, eventName: string, properties: {[key: string]: string}, measures: {[key: string]: number}): void { | |
| 157 | Telemetry.sendExtensionTelemetry(extensionId, extensionVersion, appInsightsKey, eventName, properties, measures); | |
| 158 | } | |
| 159 | | |
280c0746Patricio Beltran9 years ago | 160 | /** |
| 161 | * Message handler for SHOW_INFORMATION_MESSAGE | |
| 162 | */ | |
7daed3fcArtem Egorov8 years ago | 163 | private showInformationMessage(message: string): void { |
| 164 | vscode.window.showInformationMessage(message); | |
280c0746Patricio Beltran9 years ago | 165 | } |
0a68f8dbArtem Egorov8 years ago | 166 | |
7daed3fcArtem Egorov8 years ago | 167 | private launch(request: any): Promise<any> { |
0a68f8dbArtem Egorov8 years ago | 168 | let mobilePlatformOptions = requestSetup(request.arguments); |
| 169 | | |
| 170 | // We add the parameter if it's defined (adapter crashes otherwise) | |
| 171 | if (!isNullOrUndefined(request.arguments.logCatArguments)) { | |
| 172 | mobilePlatformOptions.logCatArguments = [parseLogCatArguments(request.arguments.logCatArguments)]; | |
| 173 | } | |
| 174 | | |
| 175 | if (!isNullOrUndefined(request.arguments.variant)) { | |
| 176 | mobilePlatformOptions.variant = request.arguments.variant; | |
| 177 | } | |
| 178 | | |
| 179 | if (!isNullOrUndefined(request.arguments.scheme)) { | |
| 180 | mobilePlatformOptions.scheme = request.arguments.scheme; | |
| 181 | } | |
| 182 | | |
2e432a9eArtem Egorov8 years ago | 183 | mobilePlatformOptions.packagerPort = SettingsHelper.getPackagerPort(request.arguments.program); |
0a68f8dbArtem Egorov8 years ago | 184 | const platformDeps: MobilePlatformDeps = { |
| 185 | packager: this.reactNativePackager, | |
| 186 | }; | |
| 187 | const mobilePlatform = new PlatformResolver() | |
| 188 | .resolveMobilePlatform(request.arguments.platform, mobilePlatformOptions, platformDeps); | |
7daed3fcArtem Egorov8 years ago | 189 | return new Promise((resolve, reject) => { |
| 190 | TelemetryHelper.generate("launch", (generator) => { | |
| 191 | generator.step("checkPlatformCompatibility"); | |
| 192 | TargetPlatformHelper.checkTargetPlatformSupport(mobilePlatformOptions.platform); | |
| 193 | generator.step("startPackager"); | |
| 194 | return mobilePlatform.startPackager() | |
| 195 | .then(() => { | |
| 196 | // 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 | |
| 197 | // and the user needs to Reload JS manually. We prewarm it to prevent that issue | |
| 198 | generator.step("prewarmBundleCache"); | |
| 199 | this.logger.info("Prewarming bundle cache. This may take a while ..."); | |
| 200 | return mobilePlatform.prewarmBundleCache(); | |
| 201 | }) | |
| 202 | .then(() => { | |
| 203 | generator.step("mobilePlatform.runApp"); | |
| 204 | this.logger.info("Building and running application."); | |
| 205 | return mobilePlatform.runApp(); | |
| 206 | }) | |
| 207 | .then(() => { | |
| 208 | generator.step("mobilePlatform.enableJSDebuggingMode"); | |
| 209 | return mobilePlatform.enableJSDebuggingMode(); | |
| 210 | }) | |
| 211 | .then(() => { | |
| 212 | resolve(); | |
| 213 | }) | |
| 214 | .catch(error => { | |
748105d9Artem Egorov8 years ago | 215 | this.logger.error(error); |
7daed3fcArtem Egorov8 years ago | 216 | reject(error); |
| 217 | }); | |
| 218 | }); | |
0a68f8dbArtem Egorov8 years ago | 219 | }); |
| 220 | } | |
| 221 | } | |
| 222 | | |
| 223 | /** | |
| 224 | * Parses log cat arguments to a string | |
| 225 | */ | |
| 226 | function parseLogCatArguments(userProvidedLogCatArguments: any): string { | |
| 227 | return Array.isArray(userProvidedLogCatArguments) | |
| 228 | ? userProvidedLogCatArguments.join(" ") // If it's an array, we join the arguments | |
| 229 | : userProvidedLogCatArguments; // If not, we leave it as-is | |
| 230 | } | |
| 231 | | |
| 232 | function isNullOrUndefined(value: any): boolean { | |
| 233 | return typeof value === "undefined" || value === null; | |
| 234 | } | |
| 235 | | |
| 236 | function requestSetup(args: any): any { | |
2e432a9eArtem Egorov8 years ago | 237 | const workspaceFolder: vscode.WorkspaceFolder = <vscode.WorkspaceFolder>vscode.workspace.getWorkspaceFolder(vscode.Uri.file(args.program)); |
0a68f8dbArtem Egorov8 years ago | 238 | const projectRootPath = getProjectRoot(args); |
| 239 | let mobilePlatformOptions: any = { | |
a41f5c68Artem Egorov8 years ago | 240 | workspaceRoot: workspaceFolder.uri.fsPath, |
0a68f8dbArtem Egorov8 years ago | 241 | projectRoot: projectRootPath, |
| 242 | platform: args.platform, | |
23d47878Artem Egorov8 years ago | 243 | target: args.target || "simulator", |
0a68f8dbArtem Egorov8 years ago | 244 | }; |
| 245 | | |
| 246 | if (!args.runArguments) { | |
4529aa97Artem Egorov8 years ago | 247 | let runArgs = SettingsHelper.getRunArgs(args.platform, args.target || "simulator", workspaceFolder.uri); |
0a68f8dbArtem Egorov8 years ago | 248 | mobilePlatformOptions.runArguments = runArgs; |
a18a84baArtem Egorov8 years ago | 249 | } else { |
| 250 | mobilePlatformOptions.runArguments = args.runArguments; | |
0a68f8dbArtem Egorov8 years ago | 251 | } |
| 252 | | |
| 253 | return mobilePlatformOptions; | |
| 254 | } | |
| 255 | | |
| 256 | function getProjectRoot(args: any): string { | |
4edcda70Artem Egorov8 years ago | 257 | return SettingsHelper.getReactNativeProjectRoot(args.program); |
0502b7a8dlebu10 years ago | 258 | } |