microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/android/androidPlatform.ts
121lines · modeblame
e639e7a4Daniel10 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 | | |
| 4 | import * as Q from "q"; | |
710f8655digeff10 years ago | 5 | |
f8d32439dlebu10 years ago | 6 | import {IAppPlatform} from "../platformResolver"; |
725cf712digeff10 years ago | 7 | import {RemoteExtension} from "../../common/remoteExtension"; |
710f8655digeff10 years ago | 8 | import {IRunOptions} from "../../common/launchArgs"; |
c2bf3c4fdigeff10 years ago | 9 | import {Log} from "../../common/log/log"; |
8953be57dlebu10 years ago | 10 | import {PackageNameResolver} from "../../common/android/packageNameResolver"; |
f8b7022ddigeff10 years ago | 11 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
aab2095edigeff10 years ago | 12 | import {IDeviceHelper, DeviceHelper, IDevice} from "../../common/android/deviceHelper"; |
710f8655digeff10 years ago | 13 | import {Package} from "../../common/node/package"; |
aab2095edigeff10 years ago | 14 | import {FileSystem} from "../../common/node/fileSystem"; |
| 15 | import {IReactNative, ReactNative} from "../../common/reactNative"; | |
e639e7a4Daniel10 years ago | 16 | |
| 17 | /** | |
| 18 | * Android specific platform implementation for debugging RN applications. | |
| 19 | */ | |
f8d32439dlebu10 years ago | 20 | export class AndroidPlatform implements IAppPlatform { |
725cf712digeff10 years ago | 21 | private remoteExtension: RemoteExtension; |
710f8655digeff10 years ago | 22 | |
a1348eeddigeff10 years ago | 23 | private static MULTIPLE_DEVICES_ERROR = "error: more than one device/emulator"; |
| 24 | | |
7cc67271digeff10 years ago | 25 | // We should add the common Android build/run erros we find to this list |
| 26 | private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure = { | |
| 27 | "Failed to install on any devices": "Could not install the app on any available device. Make sure you have a correctly" | |
| 28 | + " configured device or emulator running. See https://facebook.github.io/react-native/docs/android-setup.html", | |
9ce5a776digeff10 years ago | 29 | "com.android.ddmlib.ShellCommandUnresponsiveException": "An Android shell command timed-out. Please retry the operation.", |
a1348eeddigeff10 years ago | 30 | "Android project not found": "Android project not found.", |
cdf34447digeff10 years ago | 31 | "error: more than one device/emulator": AndroidPlatform.MULTIPLE_DEVICES_ERROR, |
| 32 | }; | |
7cc67271digeff10 years ago | 33 | |
| 34 | private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"]; | |
bc7a32ceJimmy Thomson10 years ago | 35 | |
74e87372dlebu10 years ago | 36 | private debugTarget: string; |
a1348eeddigeff10 years ago | 37 | private devices: IDevice[]; |
9f34c1bddigeff10 years ago | 38 | private packageName: string; |
aab2095edigeff10 years ago | 39 | private deviceHelper: IDeviceHelper; |
| 40 | private reactNative: IReactNative; | |
| 41 | private fileSystem: FileSystem; | |
fcb6a5dadlebu10 years ago | 42 | |
4f4e082ddigeff10 years ago | 43 | constructor(private runOptions: IRunOptions, { |
c7f1165cdigeff10 years ago | 44 | remoteExtension = RemoteExtension.atProjectRootPath(runOptions.projectRoot), |
aab2095edigeff10 years ago | 45 | deviceHelper = <IDeviceHelper>new DeviceHelper(), |
| 46 | reactNative = <IReactNative>new ReactNative(), | |
| 47 | fileSystem = new FileSystem(), | |
| 48 | } = {}) { | |
725cf712digeff10 years ago | 49 | this.remoteExtension = remoteExtension; |
aab2095edigeff10 years ago | 50 | this.deviceHelper = deviceHelper; |
| 51 | this.reactNative = reactNative; | |
| 52 | this.fileSystem = fileSystem; | |
fcb6a5dadlebu10 years ago | 53 | } |
e639e7a4Daniel10 years ago | 54 | |
7be1388cdigeff10 years ago | 55 | public runApp(): Q.Promise<void> { |
| 56 | const runAndroidSpawn = this.reactNative.runAndroid(this.runOptions.projectRoot); | |
ae883d21digeff10 years ago | 57 | const output = new OutputVerifier( |
7cc67271digeff10 years ago | 58 | () => |
| 59 | Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS), | |
| 60 | () => | |
| 61 | Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS)).process(runAndroidSpawn); | |
8953be57dlebu10 years ago | 62 | |
c7819f8cdigeff10 years ago | 63 | return output |
| 64 | .finally(() => { | |
| 65 | return this.deviceHelper.getConnectedDevices().then(devices => { | |
| 66 | this.devices = devices; | |
7be1388cdigeff10 years ago | 67 | this.debugTarget = this.getTargetEmulator(devices); |
| 68 | return this.getPackageName(this.runOptions.projectRoot).then(packageName => | |
c7819f8cdigeff10 years ago | 69 | this.packageName = packageName); |
| 70 | }); | |
| 71 | }).catch(reason => { | |
a1348eeddigeff10 years ago | 72 | if (reason.message === AndroidPlatform.MULTIPLE_DEVICES_ERROR && this.devices.length > 1 && this.debugTarget) { |
| 73 | /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */ | |
7be1388cdigeff10 years ago | 74 | return this.deviceHelper.launchApp(this.runOptions.projectRoot, this.packageName, this.debugTarget); |
a1348eeddigeff10 years ago | 75 | } else { |
| 76 | return Q.reject<void>(reason); | |
| 77 | } | |
710f8655digeff10 years ago | 78 | }).then(() => |
7be1388cdigeff10 years ago | 79 | this.startMonitoringLogCat(this.runOptions.logCatArguments).catch(error => // The LogCatMonitor failing won't stop the debugging experience |
710f8655digeff10 years ago | 80 | Log.logWarning("Couldn't start LogCat monitor", error))); |
e639e7a4Daniel10 years ago | 81 | } |
2f567aafdlebu10 years ago | 82 | |
7be1388cdigeff10 years ago | 83 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 84 | return this.deviceHelper.reloadAppInDebugMode(this.runOptions.projectRoot, this.packageName, this.debugTarget); | |
a1348eeddigeff10 years ago | 85 | } |
| 86 | | |
| 87 | private getPackageName(projectRoot: string): Q.Promise<string> { | |
aab2095edigeff10 years ago | 88 | return new Package(projectRoot, { fileSystem: this.fileSystem }).name().then(appName => |
a1348eeddigeff10 years ago | 89 | new PackageNameResolver(appName).resolvePackageName(projectRoot)); |
74e87372dlebu10 years ago | 90 | } |
| 91 | | |
2f567aafdlebu10 years ago | 92 | /** |
| 93 | * Returns the target emulator, using the following logic: | |
| 94 | * * If an emulator is specified and it is connected, use that one. | |
| 95 | * * Otherwise, use the first one in the list. | |
| 96 | */ | |
7be1388cdigeff10 years ago | 97 | private getTargetEmulator(devices: IDevice[]): string { |
2f567aafdlebu10 years ago | 98 | let activeFilterFunction = (device: IDevice) => { |
74e87372dlebu10 years ago | 99 | return device.isOnline; |
2f567aafdlebu10 years ago | 100 | }; |
| 101 | | |
| 102 | let targetFilterFunction = (device: IDevice) => { | |
7be1388cdigeff10 years ago | 103 | return device.id === this.runOptions.target && activeFilterFunction(device); |
2f567aafdlebu10 years ago | 104 | }; |
| 105 | | |
7be1388cdigeff10 years ago | 106 | if (this.runOptions && this.runOptions.target && devices) { |
2f567aafdlebu10 years ago | 107 | /* check if the specified target is active */ |
74e87372dlebu10 years ago | 108 | if (devices.some(targetFilterFunction)) { |
7be1388cdigeff10 years ago | 109 | return this.runOptions.target; |
2f567aafdlebu10 years ago | 110 | } |
| 111 | } | |
| 112 | | |
| 113 | /* return the first active device in the list */ | |
| 114 | let activeDevices = devices && devices.filter(activeFilterFunction); | |
| 115 | return activeDevices && activeDevices[0] && activeDevices[0].id; | |
| 116 | } | |
710f8655digeff10 years ago | 117 | |
a9d77c8bdigeff10 years ago | 118 | private startMonitoringLogCat(logCatArguments: string): Q.Promise<void> { |
725cf712digeff10 years ago | 119 | return this.remoteExtension.startMonitoringLogcat(this.debugTarget, logCatArguments); |
710f8655digeff10 years ago | 120 | } |
e639e7a4Daniel10 years ago | 121 | } |