microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.14

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/androidPlatform.ts

252lines · modeblame

52f3873ddigeff10 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";
df8c800dArtem Egorov8 years ago5import * as semver from "semver";
52f3873ddigeff10 years ago6
5c8365a6Artem Egorov8 years ago7import {GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform";
0db0be15Artem Egorov8 years ago8import {IAndroidRunOptions} from "../launchArgs";
7daed3fcArtem Egorov8 years ago9import {AdbHelper, AndroidAPILevel, IDevice} from "./adb";
0a68f8dbArtem Egorov8 years ago10import {Package} from "../../common/node/package";
11import {PromiseUtil} from "../../common/node/promise";
5c8365a6Artem Egorov8 years ago12import {PackageNameResolver} from "./packageNameResolver";
0a68f8dbArtem Egorov8 years ago13import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
14import {TelemetryHelper} from "../../common/telemetryHelper";
8022afdfVladimir Kotikov8 years ago15import {CommandExecutor} from "../../common/commandExecutor";
0a68f8dbArtem Egorov8 years ago16import {LogCatMonitor} from "./logCatMonitor";
df8c800dArtem Egorov8 years ago17import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper";
5c8365a6Artem Egorov8 years ago18
52f3873ddigeff10 years ago19/**
20* Android specific platform implementation for debugging RN applications.
21*/
299b0557Patricio Beltran10 years ago22export class AndroidPlatform extends GeneralMobilePlatform {
52f3873ddigeff10 years ago23private static MULTIPLE_DEVICES_ERROR = "error: more than one device/emulator";
24
0a68f8dbArtem Egorov8 years ago25// We should add the common Android build/run errors we find to this list
ef902673Vladimir Kotikov9 years ago26private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure[] = [{
27pattern: "Failed to install on any devices",
28message: "Could not install the app on any available device. Make sure you have a correctly"
29+ " configured device or emulator running. See https://facebook.github.io/react-native/docs/android-setup.html",
30}, {
31pattern: "com.android.ddmlib.ShellCommandUnresponsiveException",
32message: "An Android shell command timed-out. Please retry the operation.",
33}, {
34pattern: "Android project not found",
35message: "Android project not found.",
36
37}, {
38pattern: "error: more than one device/emulator",
39message: AndroidPlatform.MULTIPLE_DEVICES_ERROR,
40}, {
41pattern: /^Error: Activity class \{.*\} does not exist\.$/m,
42message: "Failed to launch the specified activity. Try running application manually and "
43+ "start debugging using 'Attach to packager' launch configuration.",
44}];
52f3873ddigeff10 years ago45
46private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"];
47
48private debugTarget: IDevice;
49private devices: IDevice[];
50private packageName: string;
0a68f8dbArtem Egorov8 years ago51private logCatMonitor: LogCatMonitor | null = null;
43e1a996Ruslan Bikkinin7 years ago52private adbHelper: AdbHelper;
52f3873ddigeff10 years ago53
54private needsToLaunchApps: boolean = false;
43e1a996Ruslan Bikkinin7 years ago55
56public showDevMenu(deviceId?: string): Q.Promise<void> {
57return this.adbHelper.showDevMenu(deviceId);
7daed3fcArtem Egorov8 years ago58}
43e1a996Ruslan Bikkinin7 years ago59
60public reloadApp(deviceId?: string): Q.Promise<void> {
61return this.adbHelper.reloadApp(deviceId);
7daed3fcArtem Egorov8 years ago62}
52f3873ddigeff10 years ago63
299b0557Patricio Beltran10 years ago64// We set remoteExtension = null so that if there is an instance of androidPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests.
7daed3fcArtem Egorov8 years ago65constructor(protected runOptions: IAndroidRunOptions, platformDeps: MobilePlatformDeps = {}) {
0a68f8dbArtem Egorov8 years ago66super(runOptions, platformDeps);
1ca47c7cArtem Egorov8 years ago67
68if (this.runOptions.target === AndroidPlatform.simulatorString ||
69this.runOptions.target === AndroidPlatform.deviceString) {
70
71const message = `Target ${this.runOptions.target} is not supported for Android ` +
72"platform. If you want to use particular device or simulator for launching " +
73"Android app, please specify device id (as in 'adb devices' output) instead.";
74
0a68f8dbArtem Egorov8 years ago75this.logger.warning(message);
1ca47c7cArtem Egorov8 years ago76delete this.runOptions.target;
77}
43e1a996Ruslan Bikkinin7 years ago78
79this.adbHelper = new AdbHelper(this.runOptions.projectRoot, this.logger);
80}
81
82// TODO: remove this method when sinon will be updated to upper version. Now it is used for tests only.
83public setAdbHelper(adbHelper: AdbHelper) {
84this.adbHelper = adbHelper;
52f3873ddigeff10 years ago85}
86
87public runApp(shouldLaunchInAllDevices: boolean = false): Q.Promise<void> {
031832ffArtem Egorov8 years ago88const extProps = {
89platform: {
90value: "android",
91isPii: false,
92},
93};
94
95return TelemetryHelper.generate("AndroidPlatform.runApp", extProps, () => {
1174ee3dArtem Egorov8 years ago96const env = this.getEnvArgument();
df8c800dArtem Egorov8 years ago97
98return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
99.then(version => {
6786e358Artem Egorov8 years ago100if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, AndroidPlatform.NO_PACKAGER_VERSION)) {
3e313f6fArtem Egorov7 years ago101this.runArguments.push("--no-packager");
52f3873ddigeff10 years ago102}
df8c800dArtem Egorov8 years ago103
3e313f6fArtem Egorov7 years ago104const runAndroidSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-android", this.runArguments, {env});
df8c800dArtem Egorov8 years ago105const output = new OutputVerifier(
106() =>
107Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS),
108() =>
77a9922aRuslan Bikkinin8 years ago109Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS),
110"android").process(runAndroidSpawn);
df8c800dArtem Egorov8 years ago111
112return output
113.finally(() => {
114return this.initializeTargetDevicesAndPackageName();
115}).then(() => [this.debugTarget], reason => {
116if (reason.message === AndroidPlatform.MULTIPLE_DEVICES_ERROR && this.devices.length > 1 && this.debugTarget) {
117/* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */
118this.needsToLaunchApps = true;
119return shouldLaunchInAllDevices
43e1a996Ruslan Bikkinin7 years ago120? this.adbHelper.getOnlineDevices()
df8c800dArtem Egorov8 years ago121: Q([this.debugTarget]);
122} else {
123return Q.reject<IDevice[]>(reason);
124}
125}).then(devices => {
126return new PromiseUtil().forEach(devices, device => {
127return this.launchAppWithADBReverseAndLogCat(device);
128});
129});
52f3873ddigeff10 years ago130});
131});
132}
133
134public enableJSDebuggingMode(): Q.Promise<void> {
43e1a996Ruslan Bikkinin7 years ago135return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, true, this.debugTarget.id);
b57ea017Artem Egorov8 years ago136}
137
138public disableJSDebuggingMode(): Q.Promise<void> {
43e1a996Ruslan Bikkinin7 years ago139return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, false, this.debugTarget.id);
52f3873ddigeff10 years ago140}
141
299b0557Patricio Beltran10 years ago142public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago143return this.packager.prewarmBundleCache("android");
299b0557Patricio Beltran10 years ago144}
145
3e313f6fArtem Egorov7 years ago146protected getRunArguments(): string[] {
8022afdfVladimir Kotikov8 years ago147let runArguments: string[] = [];
148
149if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
150runArguments = this.runOptions.runArguments;
151} else {
152if (this.runOptions.variant) {
153runArguments.push("--variant", this.runOptions.variant);
154}
155if (this.runOptions.target) {
156runArguments.push("--deviceId", this.runOptions.target);
157}
158}
159
160return runArguments;
161}
162
52f3873ddigeff10 years ago163private initializeTargetDevicesAndPackageName(): Q.Promise<void> {
43e1a996Ruslan Bikkinin7 years ago164return this.adbHelper.getConnectedDevices().then(devices => {
52f3873ddigeff10 years ago165this.devices = devices;
166this.debugTarget = this.getTargetEmulator(devices);
167return this.getPackageName().then(packageName => {
168this.packageName = packageName;
169});
170});
171}
172
173private launchAppWithADBReverseAndLogCat(device: IDevice): Q.Promise<void> {
174return Q({})
175.then(() => {
176return this.configureADBReverseWhenApplicable(device);
177}).then(() => {
178return this.needsToLaunchApps
43e1a996Ruslan Bikkinin7 years ago179? this.adbHelper.launchApp(this.runOptions.projectRoot, this.packageName, device.id)
52f3873ddigeff10 years ago180: Q<void>(void 0);
181}).then(() => {
0a68f8dbArtem Egorov8 years ago182return this.startMonitoringLogCat(device, this.runOptions.logCatArguments);
52f3873ddigeff10 years ago183});
184}
185
186private configureADBReverseWhenApplicable(device: IDevice): Q.Promise<void> {
b57ea017Artem Egorov8 years ago187return Q({}) // For other emulators and devices we try to enable adb reverse
43e1a996Ruslan Bikkinin7 years ago188.then(() => this.adbHelper.apiVersion(device.id))
b57ea017Artem Egorov8 years ago189.then(apiVersion => {
190if (apiVersion >= AndroidAPILevel.LOLLIPOP) { // If we support adb reverse
43e1a996Ruslan Bikkinin7 years ago191return this.adbHelper.reverseAdb(device.id, Number(this.runOptions.packagerPort));
b57ea017Artem Egorov8 years ago192} else {
0a68f8dbArtem Egorov8 years ago193this.logger.warning(`Device ${device.id} supports only API Level ${apiVersion}. `
b57ea017Artem Egorov8 years ago194+ `Level ${AndroidAPILevel.LOLLIPOP} is needed to support port forwarding via adb reverse. `
195+ "For debugging to work you'll need <Shake or press menu button> for the dev menu, "
196+ "go into <Dev Settings> and configure <Debug Server host & port for Device> to be "
197+ "an IP address of your computer that the Device can reach. More info at: "
198+ "https://facebook.github.io/react-native/docs/debugging.html#debugging-react-native-apps");
199return void 0;
200}
201});
52f3873ddigeff10 years ago202}
203
204private getPackageName(): Q.Promise<string> {
8022afdfVladimir Kotikov8 years ago205return new Package(this.runOptions.projectRoot).name().then(appName =>
52f3873ddigeff10 years ago206new PackageNameResolver(appName).resolvePackageName(this.runOptions.projectRoot));
207}
208
209/**
210* Returns the target emulator, using the following logic:
211* * If an emulator is specified and it is connected, use that one.
212* * Otherwise, use the first one in the list.
213*/
214private getTargetEmulator(devices: IDevice[]): IDevice {
215let activeFilterFunction = (device: IDevice) => {
216return device.isOnline;
217};
218
219let targetFilterFunction = (device: IDevice) => {
220return device.id === this.runOptions.target && activeFilterFunction(device);
221};
222
223if (this.runOptions && this.runOptions.target && devices) {
224/* check if the specified target is active */
225const targetDevice = devices.find(targetFilterFunction);
226if (targetDevice) {
227return targetDevice;
228}
229}
230
231/* return the first active device in the list */
232let activeDevices = devices && devices.filter(activeFilterFunction);
233return activeDevices && activeDevices[0];
234}
235
0a68f8dbArtem Egorov8 years ago236private startMonitoringLogCat(device: IDevice, logCatArguments: string): void {
237this.stopMonitoringLogCat(); // Stop previous logcat monitor if it's running
238
239// this.logCatMonitor can be mutated, so we store it locally too
43e1a996Ruslan Bikkinin7 years ago240this.logCatMonitor = new LogCatMonitor(device.id, logCatArguments, this.adbHelper);
0a68f8dbArtem Egorov8 years ago241this.logCatMonitor.start() // The LogCat will continue running forever, so we don't wait for it
242.catch(error => this.logger.warning("Error while monitoring LogCat", error)) // The LogCatMonitor failing won't stop the debugging experience
243.done();
244}
245
246private stopMonitoringLogCat(): void {
247if (this.logCatMonitor) {
248this.logCatMonitor.dispose();
249this.logCatMonitor = null;
250}
52f3873ddigeff10 years ago251}
ef902673Vladimir Kotikov9 years ago252}