microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/androidPlatform.ts
252lines · modeblame
52f3873ddigeff10 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"; | |
df8c800dArtem Egorov8 years ago | 5 | import * as semver from "semver"; |
52f3873ddigeff10 years ago | 6 | |
5c8365a6Artem Egorov8 years ago | 7 | import {GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
0db0be15Artem Egorov8 years ago | 8 | import {IAndroidRunOptions} from "../launchArgs"; |
7daed3fcArtem Egorov8 years ago | 9 | import {AdbHelper, AndroidAPILevel, IDevice} from "./adb"; |
0a68f8dbArtem Egorov8 years ago | 10 | import {Package} from "../../common/node/package"; |
| 11 | import {PromiseUtil} from "../../common/node/promise"; | |
5c8365a6Artem Egorov8 years ago | 12 | import {PackageNameResolver} from "./packageNameResolver"; |
0a68f8dbArtem Egorov8 years ago | 13 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 14 | import {TelemetryHelper} from "../../common/telemetryHelper"; | |
8022afdfVladimir Kotikov8 years ago | 15 | import {CommandExecutor} from "../../common/commandExecutor"; |
0a68f8dbArtem Egorov8 years ago | 16 | import {LogCatMonitor} from "./logCatMonitor"; |
df8c800dArtem Egorov8 years ago | 17 | import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper"; |
5c8365a6Artem Egorov8 years ago | 18 | |
52f3873ddigeff10 years ago | 19 | /** |
| 20 | * Android specific platform implementation for debugging RN applications. | |
| 21 | */ | |
299b0557Patricio Beltran10 years ago | 22 | export class AndroidPlatform extends GeneralMobilePlatform { |
52f3873ddigeff10 years ago | 23 | private static MULTIPLE_DEVICES_ERROR = "error: more than one device/emulator"; |
| 24 | | |
0a68f8dbArtem Egorov8 years ago | 25 | // We should add the common Android build/run errors we find to this list |
ef902673Vladimir Kotikov9 years ago | 26 | private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 27 | pattern: "Failed to install on any devices", | |
| 28 | message: "Could not install the app on any available device. Make sure you have a correctly" | |
| 29 | + " configured device or emulator running. See https://facebook.github.io/react-native/docs/android-setup.html", | |
| 30 | }, { | |
| 31 | pattern: "com.android.ddmlib.ShellCommandUnresponsiveException", | |
| 32 | message: "An Android shell command timed-out. Please retry the operation.", | |
| 33 | }, { | |
| 34 | pattern: "Android project not found", | |
| 35 | message: "Android project not found.", | |
| 36 | | |
| 37 | }, { | |
| 38 | pattern: "error: more than one device/emulator", | |
| 39 | message: AndroidPlatform.MULTIPLE_DEVICES_ERROR, | |
| 40 | }, { | |
| 41 | pattern: /^Error: Activity class \{.*\} does not exist\.$/m, | |
| 42 | message: "Failed to launch the specified activity. Try running application manually and " | |
| 43 | + "start debugging using 'Attach to packager' launch configuration.", | |
| 44 | }]; | |
52f3873ddigeff10 years ago | 45 | |
| 46 | private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"]; | |
| 47 | | |
| 48 | private debugTarget: IDevice; | |
| 49 | private devices: IDevice[]; | |
| 50 | private packageName: string; | |
0a68f8dbArtem Egorov8 years ago | 51 | private logCatMonitor: LogCatMonitor | null = null; |
43e1a996Ruslan Bikkinin7 years ago | 52 | private adbHelper: AdbHelper; |
52f3873ddigeff10 years ago | 53 | |
| 54 | private needsToLaunchApps: boolean = false; | |
43e1a996Ruslan Bikkinin7 years ago | 55 | |
| 56 | public showDevMenu(deviceId?: string): Q.Promise<void> { | |
| 57 | return this.adbHelper.showDevMenu(deviceId); | |
7daed3fcArtem Egorov8 years ago | 58 | } |
43e1a996Ruslan Bikkinin7 years ago | 59 | |
| 60 | public reloadApp(deviceId?: string): Q.Promise<void> { | |
| 61 | return this.adbHelper.reloadApp(deviceId); | |
7daed3fcArtem Egorov8 years ago | 62 | } |
52f3873ddigeff10 years ago | 63 | |
299b0557Patricio Beltran10 years ago | 64 | // We set remoteExtension = null so that if there is an instance of androidPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests. |
7daed3fcArtem Egorov8 years ago | 65 | constructor(protected runOptions: IAndroidRunOptions, platformDeps: MobilePlatformDeps = {}) { |
0a68f8dbArtem Egorov8 years ago | 66 | super(runOptions, platformDeps); |
1ca47c7cArtem Egorov8 years ago | 67 | |
| 68 | if (this.runOptions.target === AndroidPlatform.simulatorString || | |
| 69 | this.runOptions.target === AndroidPlatform.deviceString) { | |
| 70 | | |
| 71 | const message = `Target ${this.runOptions.target} is not supported for Android ` + | |
| 72 | "platform. If you want to use particular device or simulator for launching " + | |
| 73 | "Android app, please specify device id (as in 'adb devices' output) instead."; | |
| 74 | | |
0a68f8dbArtem Egorov8 years ago | 75 | this.logger.warning(message); |
1ca47c7cArtem Egorov8 years ago | 76 | delete this.runOptions.target; |
| 77 | } | |
43e1a996Ruslan Bikkinin7 years ago | 78 | |
| 79 | this.adbHelper = new AdbHelper(this.runOptions.projectRoot, this.logger); | |
| 80 | } | |
| 81 | | |
| 82 | // TODO: remove this method when sinon will be updated to upper version. Now it is used for tests only. | |
| 83 | public setAdbHelper(adbHelper: AdbHelper) { | |
| 84 | this.adbHelper = adbHelper; | |
52f3873ddigeff10 years ago | 85 | } |
| 86 | | |
| 87 | public runApp(shouldLaunchInAllDevices: boolean = false): Q.Promise<void> { | |
031832ffArtem Egorov8 years ago | 88 | const extProps = { |
| 89 | platform: { | |
| 90 | value: "android", | |
| 91 | isPii: false, | |
| 92 | }, | |
| 93 | }; | |
| 94 | | |
| 95 | return TelemetryHelper.generate("AndroidPlatform.runApp", extProps, () => { | |
1174ee3dArtem Egorov8 years ago | 96 | const env = this.getEnvArgument(); |
df8c800dArtem Egorov8 years ago | 97 | |
| 98 | return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot) | |
| 99 | .then(version => { | |
6786e358Artem Egorov8 years ago | 100 | if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, AndroidPlatform.NO_PACKAGER_VERSION)) { |
3e313f6fArtem Egorov7 years ago | 101 | this.runArguments.push("--no-packager"); |
52f3873ddigeff10 years ago | 102 | } |
df8c800dArtem Egorov8 years ago | 103 | |
3e313f6fArtem Egorov7 years ago | 104 | const runAndroidSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-android", this.runArguments, {env}); |
df8c800dArtem Egorov8 years ago | 105 | const output = new OutputVerifier( |
| 106 | () => | |
| 107 | Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS), | |
| 108 | () => | |
77a9922aRuslan Bikkinin8 years ago | 109 | Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS), |
| 110 | "android").process(runAndroidSpawn); | |
df8c800dArtem Egorov8 years ago | 111 | |
| 112 | return output | |
| 113 | .finally(() => { | |
| 114 | return this.initializeTargetDevicesAndPackageName(); | |
| 115 | }).then(() => [this.debugTarget], reason => { | |
| 116 | if (reason.message === AndroidPlatform.MULTIPLE_DEVICES_ERROR && this.devices.length > 1 && this.debugTarget) { | |
| 117 | /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */ | |
| 118 | this.needsToLaunchApps = true; | |
| 119 | return shouldLaunchInAllDevices | |
43e1a996Ruslan Bikkinin7 years ago | 120 | ? this.adbHelper.getOnlineDevices() |
df8c800dArtem Egorov8 years ago | 121 | : Q([this.debugTarget]); |
| 122 | } else { | |
| 123 | return Q.reject<IDevice[]>(reason); | |
| 124 | } | |
| 125 | }).then(devices => { | |
| 126 | return new PromiseUtil().forEach(devices, device => { | |
| 127 | return this.launchAppWithADBReverseAndLogCat(device); | |
| 128 | }); | |
| 129 | }); | |
52f3873ddigeff10 years ago | 130 | }); |
| 131 | }); | |
| 132 | } | |
| 133 | | |
| 134 | public enableJSDebuggingMode(): Q.Promise<void> { | |
43e1a996Ruslan Bikkinin7 years ago | 135 | return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, true, this.debugTarget.id); |
b57ea017Artem Egorov8 years ago | 136 | } |
| 137 | | |
| 138 | public disableJSDebuggingMode(): Q.Promise<void> { | |
43e1a996Ruslan Bikkinin7 years ago | 139 | return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, false, this.debugTarget.id); |
52f3873ddigeff10 years ago | 140 | } |
| 141 | | |
299b0557Patricio Beltran10 years ago | 142 | public prewarmBundleCache(): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 143 | return this.packager.prewarmBundleCache("android"); |
299b0557Patricio Beltran10 years ago | 144 | } |
| 145 | | |
3e313f6fArtem Egorov7 years ago | 146 | protected getRunArguments(): string[] { |
8022afdfVladimir Kotikov8 years ago | 147 | let runArguments: string[] = []; |
| 148 | | |
| 149 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
| 150 | runArguments = this.runOptions.runArguments; | |
| 151 | } else { | |
| 152 | if (this.runOptions.variant) { | |
| 153 | runArguments.push("--variant", this.runOptions.variant); | |
| 154 | } | |
| 155 | if (this.runOptions.target) { | |
| 156 | runArguments.push("--deviceId", this.runOptions.target); | |
| 157 | } | |
| 158 | } | |
| 159 | | |
| 160 | return runArguments; | |
| 161 | } | |
| 162 | | |
52f3873ddigeff10 years ago | 163 | private initializeTargetDevicesAndPackageName(): Q.Promise<void> { |
43e1a996Ruslan Bikkinin7 years ago | 164 | return this.adbHelper.getConnectedDevices().then(devices => { |
52f3873ddigeff10 years ago | 165 | this.devices = devices; |
| 166 | this.debugTarget = this.getTargetEmulator(devices); | |
| 167 | return this.getPackageName().then(packageName => { | |
| 168 | this.packageName = packageName; | |
| 169 | }); | |
| 170 | }); | |
| 171 | } | |
| 172 | | |
| 173 | private launchAppWithADBReverseAndLogCat(device: IDevice): Q.Promise<void> { | |
| 174 | return Q({}) | |
| 175 | .then(() => { | |
| 176 | return this.configureADBReverseWhenApplicable(device); | |
| 177 | }).then(() => { | |
| 178 | return this.needsToLaunchApps | |
43e1a996Ruslan Bikkinin7 years ago | 179 | ? this.adbHelper.launchApp(this.runOptions.projectRoot, this.packageName, device.id) |
52f3873ddigeff10 years ago | 180 | : Q<void>(void 0); |
| 181 | }).then(() => { | |
0a68f8dbArtem Egorov8 years ago | 182 | return this.startMonitoringLogCat(device, this.runOptions.logCatArguments); |
52f3873ddigeff10 years ago | 183 | }); |
| 184 | } | |
| 185 | | |
| 186 | private configureADBReverseWhenApplicable(device: IDevice): Q.Promise<void> { | |
b57ea017Artem Egorov8 years ago | 187 | return Q({}) // For other emulators and devices we try to enable adb reverse |
43e1a996Ruslan Bikkinin7 years ago | 188 | .then(() => this.adbHelper.apiVersion(device.id)) |
b57ea017Artem Egorov8 years ago | 189 | .then(apiVersion => { |
| 190 | if (apiVersion >= AndroidAPILevel.LOLLIPOP) { // If we support adb reverse | |
43e1a996Ruslan Bikkinin7 years ago | 191 | return this.adbHelper.reverseAdb(device.id, Number(this.runOptions.packagerPort)); |
b57ea017Artem Egorov8 years ago | 192 | } else { |
0a68f8dbArtem Egorov8 years ago | 193 | this.logger.warning(`Device ${device.id} supports only API Level ${apiVersion}. ` |
b57ea017Artem Egorov8 years ago | 194 | + `Level ${AndroidAPILevel.LOLLIPOP} is needed to support port forwarding via adb reverse. ` |
| 195 | + "For debugging to work you'll need <Shake or press menu button> for the dev menu, " | |
| 196 | + "go into <Dev Settings> and configure <Debug Server host & port for Device> to be " | |
| 197 | + "an IP address of your computer that the Device can reach. More info at: " | |
| 198 | + "https://facebook.github.io/react-native/docs/debugging.html#debugging-react-native-apps"); | |
| 199 | return void 0; | |
| 200 | } | |
| 201 | }); | |
52f3873ddigeff10 years ago | 202 | } |
| 203 | | |
| 204 | private getPackageName(): Q.Promise<string> { | |
8022afdfVladimir Kotikov8 years ago | 205 | return new Package(this.runOptions.projectRoot).name().then(appName => |
52f3873ddigeff10 years ago | 206 | new PackageNameResolver(appName).resolvePackageName(this.runOptions.projectRoot)); |
| 207 | } | |
| 208 | | |
| 209 | /** | |
| 210 | * Returns the target emulator, using the following logic: | |
| 211 | * * If an emulator is specified and it is connected, use that one. | |
| 212 | * * Otherwise, use the first one in the list. | |
| 213 | */ | |
| 214 | private getTargetEmulator(devices: IDevice[]): IDevice { | |
| 215 | let activeFilterFunction = (device: IDevice) => { | |
| 216 | return device.isOnline; | |
| 217 | }; | |
| 218 | | |
| 219 | let targetFilterFunction = (device: IDevice) => { | |
| 220 | return device.id === this.runOptions.target && activeFilterFunction(device); | |
| 221 | }; | |
| 222 | | |
| 223 | if (this.runOptions && this.runOptions.target && devices) { | |
| 224 | /* check if the specified target is active */ | |
| 225 | const targetDevice = devices.find(targetFilterFunction); | |
| 226 | if (targetDevice) { | |
| 227 | return targetDevice; | |
| 228 | } | |
| 229 | } | |
| 230 | | |
| 231 | /* return the first active device in the list */ | |
| 232 | let activeDevices = devices && devices.filter(activeFilterFunction); | |
| 233 | return activeDevices && activeDevices[0]; | |
| 234 | } | |
| 235 | | |
0a68f8dbArtem Egorov8 years ago | 236 | private startMonitoringLogCat(device: IDevice, logCatArguments: string): void { |
| 237 | this.stopMonitoringLogCat(); // Stop previous logcat monitor if it's running | |
| 238 | | |
| 239 | // this.logCatMonitor can be mutated, so we store it locally too | |
43e1a996Ruslan Bikkinin7 years ago | 240 | this.logCatMonitor = new LogCatMonitor(device.id, logCatArguments, this.adbHelper); |
0a68f8dbArtem Egorov8 years ago | 241 | this.logCatMonitor.start() // The LogCat will continue running forever, so we don't wait for it |
| 242 | .catch(error => this.logger.warning("Error while monitoring LogCat", error)) // The LogCatMonitor failing won't stop the debugging experience | |
| 243 | .done(); | |
| 244 | } | |
| 245 | | |
| 246 | private stopMonitoringLogCat(): void { | |
| 247 | if (this.logCatMonitor) { | |
| 248 | this.logCatMonitor.dispose(); | |
| 249 | this.logCatMonitor = null; | |
| 250 | } | |
52f3873ddigeff10 years ago | 251 | } |
ef902673Vladimir Kotikov9 years ago | 252 | } |