microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/android/androidPlatform.ts
93lines · modecode
| 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"; |
| 5 | import {IAppPlatform} from "../platformResolver"; |
| 6 | import {IRunOptions} from "../../common/launchArgs"; |
| 7 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 8 | import {Package} from "../../common/node/package"; |
| 9 | import {PackageNameResolver} from "../../common/android/packageNameResolver"; |
| 10 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 11 | import {DeviceHelper, IDevice} from "../../common/android/deviceHelper"; |
| 12 | |
| 13 | /** |
| 14 | * Android specific platform implementation for debugging RN applications. |
| 15 | */ |
| 16 | export class AndroidPlatform implements IAppPlatform { |
| 17 | // We should add the common Android build/run erros we find to this list |
| 18 | private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure = { |
| 19 | "Failed to install on any devices": "Could not install the app on any available device. Make sure you have a correctly" |
| 20 | + " configured device or emulator running. See https://facebook.github.io/react-native/docs/android-setup.html", |
| 21 | "com.android.ddmlib.ShellCommandUnresponsiveException": "An Android shell command timed-out. Please retry the operation.", |
| 22 | "Android project not found": "Android project not found." }; |
| 23 | |
| 24 | private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"]; |
| 25 | |
| 26 | private debugTarget: string; |
| 27 | private packageName: string; |
| 28 | private deviceHelper: DeviceHelper; |
| 29 | |
| 30 | constructor() { |
| 31 | this.deviceHelper = new DeviceHelper(); |
| 32 | } |
| 33 | |
| 34 | public runApp(runOptions: IRunOptions): Q.Promise<void> { |
| 35 | let pkg = new Package(runOptions.projectRoot); |
| 36 | let cexec = new CommandExecutor(runOptions.projectRoot); |
| 37 | |
| 38 | const runAndroidSpawn = cexec.spawnChildReactCommandProcess("run-android"); |
| 39 | const output = new OutputVerifier( |
| 40 | () => |
| 41 | Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS), |
| 42 | () => |
| 43 | Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS)).process(runAndroidSpawn); |
| 44 | |
| 45 | return output |
| 46 | .then(() => pkg.name()) |
| 47 | .then(appName => new PackageNameResolver(appName).resolvePackageName(runOptions.projectRoot)) |
| 48 | .then(packageName => { |
| 49 | this.packageName = packageName; |
| 50 | return this.deviceHelper.getConnectedDevices() |
| 51 | .then((devices: IDevice[]) => { |
| 52 | if (devices.length > 1) { |
| 53 | /* more than one device or emulator */ |
| 54 | this.debugTarget = this.getTargetEmulator(runOptions, devices); |
| 55 | if (this.debugTarget) { |
| 56 | /* Launching is needed only if we have more than one device active */ |
| 57 | return this.deviceHelper.launchApp(runOptions.projectRoot, packageName, this.debugTarget); |
| 58 | } |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | public enableJSDebuggingMode(runOptions: IRunOptions): Q.Promise<void> { |
| 65 | return this.deviceHelper.reloadAppInDebugMode(runOptions.projectRoot, this.packageName, this.debugTarget); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns the target emulator, using the following logic: |
| 70 | * * If an emulator is specified and it is connected, use that one. |
| 71 | * * Otherwise, use the first one in the list. |
| 72 | */ |
| 73 | private getTargetEmulator(runOptions: IRunOptions, devices: IDevice[]): string { |
| 74 | let activeFilterFunction = (device: IDevice) => { |
| 75 | return device.isOnline; |
| 76 | }; |
| 77 | |
| 78 | let targetFilterFunction = (device: IDevice) => { |
| 79 | return device.id === runOptions.target && activeFilterFunction(device); |
| 80 | }; |
| 81 | |
| 82 | if (runOptions && runOptions.target && devices) { |
| 83 | /* check if the specified target is active */ |
| 84 | if (devices.some(targetFilterFunction)) { |
| 85 | return runOptions.target; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* return the first active device in the list */ |
| 90 | let activeDevices = devices && devices.filter(activeFilterFunction); |
| 91 | return activeDevices && activeDevices[0] && activeDevices[0].id; |
| 92 | } |
| 93 | } |