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