microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/android/androidPlatform.ts
75lines · 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 {DeviceResolver, IDevice} from "../../common/android/deviceResolver"; |
| 11 | |
| 12 | /** |
| 13 | * Android specific platform implementation for debugging RN applications. |
| 14 | */ |
| 15 | export class AndroidPlatform implements IAppPlatform { |
| 16 | |
| 17 | public runApp(runOptions: IRunOptions): Q.Promise<void> { |
| 18 | return new CommandExecutor(runOptions.projectRoot).spawnAndWaitReactCommand("run-android"); |
| 19 | } |
| 20 | |
| 21 | public enableJSDebuggingMode(runOptions: IRunOptions): Q.Promise<void> { |
| 22 | let pkg = new Package(runOptions.projectRoot); |
| 23 | return pkg.name() |
| 24 | .then(appName => new PackageNameResolver(appName).resolvePackageName(runOptions.projectRoot)) |
| 25 | .then(packageName => { |
| 26 | let deviceResolver = new DeviceResolver(); |
| 27 | let cexec = new CommandExecutor(runOptions.projectRoot); |
| 28 | let debugTarget: string = null; |
| 29 | return deviceResolver.getConnectedDevices() |
| 30 | .then((devices: IDevice[]) => { |
| 31 | if (devices.length > 1) { |
| 32 | /* more than one device or emulator */ |
| 33 | debugTarget = this.getTargetEmulator(runOptions, devices); |
| 34 | } |
| 35 | }) |
| 36 | .then(() => { |
| 37 | if (debugTarget) { |
| 38 | /* Launching is needed only if we have more than one device available */ |
| 39 | let launchAppCommand = `adb -s ${debugTarget} shell am start -n com.awesomeproject/.MainActivity`; |
| 40 | return cexec.execute(launchAppCommand); |
| 41 | } |
| 42 | }) |
| 43 | .then(() => { |
| 44 | let enableDebugCommand = `adb ${debugTarget ? "-s " + debugTarget : ""} shell am broadcast -a "${packageName.toLowerCase()}.RELOAD_APP_ACTION" --ez jsproxy true`; |
| 45 | return cexec.execute(enableDebugCommand); |
| 46 | }); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the target emulator, using the following logic: |
| 52 | * * If an emulator is specified and it is connected, use that one. |
| 53 | * * Otherwise, use the first one in the list. |
| 54 | */ |
| 55 | private getTargetEmulator(runOptions: IRunOptions, devices: IDevice[]): string { |
| 56 | let activeFilterFunction = (device: IDevice) => { |
| 57 | return device.status === "device"; |
| 58 | }; |
| 59 | |
| 60 | let targetFilterFunction = (device: IDevice) => { |
| 61 | return device.id === runOptions.target && activeFilterFunction(device); |
| 62 | }; |
| 63 | |
| 64 | if (runOptions.target && devices) { |
| 65 | /* check if the specified target is active */ |
| 66 | if (devices.filter(targetFilterFunction).length > 0) { |
| 67 | return runOptions.target; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* return the first active device in the list */ |
| 72 | let activeDevices = devices && devices.filter(activeFilterFunction); |
| 73 | return activeDevices && activeDevices[0] && activeDevices[0].id; |
| 74 | } |
| 75 | } |