microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e4ac653e109fbed056449d4ec6ef8b37b6dce8c0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/android/androidPlatform.ts

121lines · modeblame

e639e7a4Daniel10 years ago1// 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";
710f8655digeff10 years ago5
f8d32439dlebu10 years ago6import {IAppPlatform} from "../platformResolver";
725cf712digeff10 years ago7import {RemoteExtension} from "../../common/remoteExtension";
710f8655digeff10 years ago8import {IRunOptions} from "../../common/launchArgs";
c2bf3c4fdigeff10 years ago9import {Log} from "../../common/log/log";
8953be57dlebu10 years ago10import {PackageNameResolver} from "../../common/android/packageNameResolver";
f8b7022ddigeff10 years ago11import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
aab2095edigeff10 years ago12import {IDeviceHelper, DeviceHelper, IDevice} from "../../common/android/deviceHelper";
710f8655digeff10 years ago13import {Package} from "../../common/node/package";
aab2095edigeff10 years ago14import {FileSystem} from "../../common/node/fileSystem";
15import {IReactNative, ReactNative} from "../../common/reactNative";
e639e7a4Daniel10 years ago16
17/**
18* Android specific platform implementation for debugging RN applications.
19*/
f8d32439dlebu10 years ago20export class AndroidPlatform implements IAppPlatform {
725cf712digeff10 years ago21private remoteExtension: RemoteExtension;
710f8655digeff10 years ago22
a1348eeddigeff10 years ago23private static MULTIPLE_DEVICES_ERROR = "error: more than one device/emulator";
24
7cc67271digeff10 years ago25// We should add the common Android build/run erros we find to this list
26private 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",
9ce5a776digeff10 years ago29"com.android.ddmlib.ShellCommandUnresponsiveException": "An Android shell command timed-out. Please retry the operation.",
a1348eeddigeff10 years ago30"Android project not found": "Android project not found.",
cdf34447digeff10 years ago31"error: more than one device/emulator": AndroidPlatform.MULTIPLE_DEVICES_ERROR,
32};
7cc67271digeff10 years ago33
34private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"];
bc7a32ceJimmy Thomson10 years ago35
74e87372dlebu10 years ago36private debugTarget: string;
a1348eeddigeff10 years ago37private devices: IDevice[];
9f34c1bddigeff10 years ago38private packageName: string;
aab2095edigeff10 years ago39private deviceHelper: IDeviceHelper;
40private reactNative: IReactNative;
41private fileSystem: FileSystem;
fcb6a5dadlebu10 years ago42
4f4e082ddigeff10 years ago43constructor(private runOptions: IRunOptions, {
c7f1165cdigeff10 years ago44remoteExtension = RemoteExtension.atProjectRootPath(runOptions.projectRoot),
aab2095edigeff10 years ago45deviceHelper = <IDeviceHelper>new DeviceHelper(),
46reactNative = <IReactNative>new ReactNative(),
47fileSystem = new FileSystem(),
48} = {}) {
725cf712digeff10 years ago49this.remoteExtension = remoteExtension;
aab2095edigeff10 years ago50this.deviceHelper = deviceHelper;
51this.reactNative = reactNative;
52this.fileSystem = fileSystem;
fcb6a5dadlebu10 years ago53}
e639e7a4Daniel10 years ago54
7be1388cdigeff10 years ago55public runApp(): Q.Promise<void> {
56const runAndroidSpawn = this.reactNative.runAndroid(this.runOptions.projectRoot);
ae883d21digeff10 years ago57const output = new OutputVerifier(
7cc67271digeff10 years ago58() =>
59Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS),
60() =>
61Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS)).process(runAndroidSpawn);
8953be57dlebu10 years ago62
c7819f8cdigeff10 years ago63return output
64.finally(() => {
65return this.deviceHelper.getConnectedDevices().then(devices => {
66this.devices = devices;
7be1388cdigeff10 years ago67this.debugTarget = this.getTargetEmulator(devices);
68return this.getPackageName(this.runOptions.projectRoot).then(packageName =>
c7819f8cdigeff10 years ago69this.packageName = packageName);
70});
71}).catch(reason => {
a1348eeddigeff10 years ago72if (reason.message === AndroidPlatform.MULTIPLE_DEVICES_ERROR && this.devices.length > 1 && this.debugTarget) {
73/* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */
7be1388cdigeff10 years ago74return this.deviceHelper.launchApp(this.runOptions.projectRoot, this.packageName, this.debugTarget);
a1348eeddigeff10 years ago75} else {
76return Q.reject<void>(reason);
77}
710f8655digeff10 years ago78}).then(() =>
7be1388cdigeff10 years ago79this.startMonitoringLogCat(this.runOptions.logCatArguments).catch(error => // The LogCatMonitor failing won't stop the debugging experience
710f8655digeff10 years ago80Log.logWarning("Couldn't start LogCat monitor", error)));
e639e7a4Daniel10 years ago81}
2f567aafdlebu10 years ago82
7be1388cdigeff10 years ago83public enableJSDebuggingMode(): Q.Promise<void> {
84return this.deviceHelper.reloadAppInDebugMode(this.runOptions.projectRoot, this.packageName, this.debugTarget);
a1348eeddigeff10 years ago85}
86
87private getPackageName(projectRoot: string): Q.Promise<string> {
aab2095edigeff10 years ago88return new Package(projectRoot, { fileSystem: this.fileSystem }).name().then(appName =>
a1348eeddigeff10 years ago89new PackageNameResolver(appName).resolvePackageName(projectRoot));
74e87372dlebu10 years ago90}
91
2f567aafdlebu10 years ago92/**
93* Returns the target emulator, using the following logic:
94* * If an emulator is specified and it is connected, use that one.
95* * Otherwise, use the first one in the list.
96*/
7be1388cdigeff10 years ago97private getTargetEmulator(devices: IDevice[]): string {
2f567aafdlebu10 years ago98let activeFilterFunction = (device: IDevice) => {
74e87372dlebu10 years ago99return device.isOnline;
2f567aafdlebu10 years ago100};
101
102let targetFilterFunction = (device: IDevice) => {
7be1388cdigeff10 years ago103return device.id === this.runOptions.target && activeFilterFunction(device);
2f567aafdlebu10 years ago104};
105
7be1388cdigeff10 years ago106if (this.runOptions && this.runOptions.target && devices) {
2f567aafdlebu10 years ago107/* check if the specified target is active */
74e87372dlebu10 years ago108if (devices.some(targetFilterFunction)) {
7be1388cdigeff10 years ago109return this.runOptions.target;
2f567aafdlebu10 years ago110}
111}
112
113/* return the first active device in the list */
114let activeDevices = devices && devices.filter(activeFilterFunction);
115return activeDevices && activeDevices[0] && activeDevices[0].id;
116}
710f8655digeff10 years ago117
a9d77c8bdigeff10 years ago118private startMonitoringLogCat(logCatArguments: string): Q.Promise<void> {
725cf712digeff10 years ago119return this.remoteExtension.startMonitoringLogcat(this.debugTarget, logCatArguments);
710f8655digeff10 years ago120}
e639e7a4Daniel10 years ago121}