microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
aab2095e4c0a69d9abdccbe48f848f209856ba4d

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/android/androidPlatform.ts

119lines · 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
4import * as Q from "q";
5
6import {IAppPlatform} from "../platformResolver";
7import {ExtensionMessageSender, ExtensionMessage} from "../../common/extensionMessaging";
8import {IRunOptions} from "../../common/launchArgs";
9import {Log} from "../../common/log/log";
10import {PackageNameResolver} from "../../common/android/packageNameResolver";
11import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
12import {IDeviceHelper, DeviceHelper, IDevice} from "../../common/android/deviceHelper";
13import {Package} from "../../common/node/package";
14import {FileSystem} from "../../common/node/fileSystem";
15import {IReactNative, ReactNative} from "../../common/reactNative";
16
17/**
18 * Android specific platform implementation for debugging RN applications.
19 */
20export class AndroidPlatform implements IAppPlatform {
21 private extensionMessageSender: ExtensionMessageSender;
22
23 private static MULTIPLE_DEVICES_ERROR = "error: more than one device/emulator";
24
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",
29 "com.android.ddmlib.ShellCommandUnresponsiveException": "An Android shell command timed-out. Please retry the operation.",
30 "Android project not found": "Android project not found.",
31 "error: more than one device/emulator": AndroidPlatform.MULTIPLE_DEVICES_ERROR };
32
33 private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"];
34
35 private debugTarget: string;
36 private devices: IDevice[];
37 private packageName: string;
38 private deviceHelper: IDeviceHelper;
39 private reactNative: IReactNative;
40 private fileSystem: FileSystem;
41
42 constructor({ extensionMessageSender = new ExtensionMessageSender(),
43 deviceHelper = <IDeviceHelper>new DeviceHelper(),
44 reactNative = <IReactNative>new ReactNative(),
45 fileSystem = new FileSystem(),
46 } = {}) {
47 this.extensionMessageSender = extensionMessageSender;
48 this.deviceHelper = deviceHelper;
49 this.reactNative = reactNative;
50 this.fileSystem = fileSystem;
51 }
52
53 public runApp(runOptions: IRunOptions): Q.Promise<void> {
54 const runAndroidSpawn = this.reactNative.runAndroid(runOptions.projectRoot);
55 const output = new OutputVerifier(
56 () =>
57 Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS),
58 () =>
59 Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS)).process(runAndroidSpawn);
60
61 return output
62 .finally(() => {
63 return this.deviceHelper.getConnectedDevices().then(devices => {
64 this.devices = devices;
65 this.debugTarget = this.getTargetEmulator(runOptions, devices);
66 return this.getPackageName(runOptions.projectRoot).then(packageName =>
67 this.packageName = packageName);
68 });
69 }).catch(reason => {
70 if (reason.message === AndroidPlatform.MULTIPLE_DEVICES_ERROR && this.devices.length > 1 && this.debugTarget) {
71 /* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */
72 return this.deviceHelper.launchApp(runOptions.projectRoot, this.packageName, this.debugTarget);
73 } else {
74 return Q.reject<void>(reason);
75 }
76 }).then(() =>
77 this.startMonitoringLogCat(runOptions.logCatArguments).catch(error => // The LogCatMonitor failing won't stop the debugging experience
78 Log.logWarning("Couldn't start LogCat monitor", error)));
79 }
80
81 public enableJSDebuggingMode(runOptions: IRunOptions): Q.Promise<void> {
82 return this.deviceHelper.reloadAppInDebugMode(runOptions.projectRoot, this.packageName, this.debugTarget);
83 }
84
85 private getPackageName(projectRoot: string): Q.Promise<string> {
86 return new Package(projectRoot, { fileSystem: this.fileSystem }).name().then(appName =>
87 new PackageNameResolver(appName).resolvePackageName(projectRoot));
88 }
89
90 /**
91 * Returns the target emulator, using the following logic:
92 * * If an emulator is specified and it is connected, use that one.
93 * * Otherwise, use the first one in the list.
94 */
95 private getTargetEmulator(runOptions: IRunOptions, devices: IDevice[]): string {
96 let activeFilterFunction = (device: IDevice) => {
97 return device.isOnline;
98 };
99
100 let targetFilterFunction = (device: IDevice) => {
101 return device.id === runOptions.target && activeFilterFunction(device);
102 };
103
104 if (runOptions && runOptions.target && devices) {
105 /* check if the specified target is active */
106 if (devices.some(targetFilterFunction)) {
107 return runOptions.target;
108 }
109 }
110
111 /* return the first active device in the list */
112 let activeDevices = devices && devices.filter(activeFilterFunction);
113 return activeDevices && activeDevices[0] && activeDevices[0].id;
114 }
115
116 private startMonitoringLogCat(logCatArguments: string): Q.Promise<void> {
117 return this.extensionMessageSender.sendMessage(ExtensionMessage.START_MONITORING_LOGCAT, [this.debugTarget, logCatArguments]);
118 }
119}