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