microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.10.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/androidPlatform.ts

248lines · 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";
d7d405aeYuri Skorokhodov7 years ago18import * as nls from "vscode-nls";
19import { InternalErrorCode } from "../../common/error/internalErrorCode";
20import { ErrorHelper } from "../../common/error/errorHelper";
21const localize = nls.loadMessageBundle();
5c8365a6Artem Egorov8 years ago22
52f3873ddigeff10 years ago23/**
24* Android specific platform implementation for debugging RN applications.
25*/
299b0557Patricio Beltran10 years ago26export class AndroidPlatform extends GeneralMobilePlatform {
52f3873ddigeff10 years ago27
0a68f8dbArtem Egorov8 years ago28// We should add the common Android build/run errors we find to this list
ef902673Vladimir Kotikov9 years ago29private static RUN_ANDROID_FAILURE_PATTERNS: PatternToFailure[] = [{
30pattern: "Failed to install on any devices",
d7d405aeYuri Skorokhodov7 years ago31errorCode: InternalErrorCode.AndroidCouldNotInstallTheAppOnAnyAvailibleDevice,
ef902673Vladimir Kotikov9 years ago32}, {
33pattern: "com.android.ddmlib.ShellCommandUnresponsiveException",
d7d405aeYuri Skorokhodov7 years ago34errorCode: InternalErrorCode.AndroidShellCommandTimedOut,
ef902673Vladimir Kotikov9 years ago35}, {
36pattern: "Android project not found",
d7d405aeYuri Skorokhodov7 years ago37errorCode: InternalErrorCode.AndroidProjectNotFound,
ef902673Vladimir Kotikov9 years ago38
39}, {
40pattern: "error: more than one device/emulator",
d7d405aeYuri Skorokhodov7 years ago41errorCode: InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator,
ef902673Vladimir Kotikov9 years ago42}, {
43pattern: /^Error: Activity class \{.*\} does not exist\.$/m,
d7d405aeYuri Skorokhodov7 years ago44errorCode: InternalErrorCode.AndroidFailedToLaunchTheSpecifiedActivity,
ef902673Vladimir Kotikov9 years ago45}];
52f3873ddigeff10 years ago46
47private static RUN_ANDROID_SUCCESS_PATTERNS: string[] = ["BUILD SUCCESSFUL", "Starting the app", "Starting: Intent"];
48
49private debugTarget: IDevice;
50private devices: IDevice[];
51private packageName: string;
0a68f8dbArtem Egorov8 years ago52private logCatMonitor: LogCatMonitor | null = null;
db6fd42aRuslan Bikkinin7 years ago53private adbHelper: AdbHelper;
52f3873ddigeff10 years ago54
55private needsToLaunchApps: boolean = false;
db6fd42aRuslan Bikkinin7 years ago56
57public showDevMenu(deviceId?: string): Q.Promise<void> {
58return this.adbHelper.showDevMenu(deviceId);
7daed3fcArtem Egorov8 years ago59}
db6fd42aRuslan Bikkinin7 years ago60
61public reloadApp(deviceId?: string): Q.Promise<void> {
62return this.adbHelper.reloadApp(deviceId);
7daed3fcArtem Egorov8 years ago63}
52f3873ddigeff10 years ago64
299b0557Patricio Beltran10 years ago65// 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 ago66constructor(protected runOptions: IAndroidRunOptions, platformDeps: MobilePlatformDeps = {}) {
0a68f8dbArtem Egorov8 years ago67super(runOptions, platformDeps);
db6fd42aRuslan Bikkinin7 years ago68this.adbHelper = new AdbHelper(this.runOptions.projectRoot, this.logger);
69}
43e1a996Ruslan Bikkinin7 years ago70
db6fd42aRuslan Bikkinin7 years ago71// TODO: remove this method when sinon will be updated to upper version. Now it is used for tests only.
72public setAdbHelper(adbHelper: AdbHelper) {
73this.adbHelper = adbHelper;
52f3873ddigeff10 years ago74}
75
76public runApp(shouldLaunchInAllDevices: boolean = false): Q.Promise<void> {
031832ffArtem Egorov8 years ago77const extProps = {
78platform: {
79value: "android",
80isPii: false,
81},
82};
83
84return TelemetryHelper.generate("AndroidPlatform.runApp", extProps, () => {
1174ee3dArtem Egorov8 years ago85const env = this.getEnvArgument();
df8c800dArtem Egorov8 years ago86
87return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot)
88.then(version => {
6786e358Artem Egorov8 years ago89if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, AndroidPlatform.NO_PACKAGER_VERSION)) {
db6fd42aRuslan Bikkinin7 years ago90this.runArguments.push("--no-packager");
52f3873ddigeff10 years ago91}
df8c800dArtem Egorov8 years ago92
db6fd42aRuslan Bikkinin7 years ago93const runAndroidSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-android", this.runArguments, {env});
df8c800dArtem Egorov8 years ago94const output = new OutputVerifier(
95() =>
96Q(AndroidPlatform.RUN_ANDROID_SUCCESS_PATTERNS),
97() =>
77a9922aRuslan Bikkinin8 years ago98Q(AndroidPlatform.RUN_ANDROID_FAILURE_PATTERNS),
99"android").process(runAndroidSpawn);
df8c800dArtem Egorov8 years ago100
101return output
102.finally(() => {
103return this.initializeTargetDevicesAndPackageName();
104}).then(() => [this.debugTarget], reason => {
d7d405aeYuri Skorokhodov7 years ago105if (reason.message === ErrorHelper.getInternalError(InternalErrorCode.AndroidMoreThanOneDeviceOrEmulator).message && this.devices.length > 1 && this.debugTarget) {
df8c800dArtem Egorov8 years ago106/* If it failed due to multiple devices, we'll apply this workaround to make it work anyways */
107this.needsToLaunchApps = true;
108return shouldLaunchInAllDevices
db6fd42aRuslan Bikkinin7 years ago109? this.adbHelper.getOnlineDevices()
df8c800dArtem Egorov8 years ago110: Q([this.debugTarget]);
111} else {
112return Q.reject<IDevice[]>(reason);
113}
114}).then(devices => {
115return new PromiseUtil().forEach(devices, device => {
116return this.launchAppWithADBReverseAndLogCat(device);
117});
118});
52f3873ddigeff10 years ago119});
120});
121}
122
123public enableJSDebuggingMode(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago124return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, true, this.debugTarget.id);
b57ea017Artem Egorov8 years ago125}
126
127public disableJSDebuggingMode(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago128return this.adbHelper.switchDebugMode(this.runOptions.projectRoot, this.packageName, false, this.debugTarget.id);
52f3873ddigeff10 years ago129}
130
299b0557Patricio Beltran10 years ago131public prewarmBundleCache(): Q.Promise<void> {
0a68f8dbArtem Egorov8 years ago132return this.packager.prewarmBundleCache("android");
299b0557Patricio Beltran10 years ago133}
134
cbc7ac5bArtem Egorov7 years ago135public getRunArguments(): string[] {
8022afdfVladimir Kotikov8 years ago136let runArguments: string[] = [];
137
138if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
139runArguments = this.runOptions.runArguments;
140} else {
141if (this.runOptions.variant) {
142runArguments.push("--variant", this.runOptions.variant);
143}
144if (this.runOptions.target) {
cbc7ac5bArtem Egorov7 years ago145if (this.runOptions.target === AndroidPlatform.simulatorString ||
146this.runOptions.target === AndroidPlatform.deviceString) {
147
d7d405aeYuri Skorokhodov7 years ago148const message = localize("TargetIsNotSupportedForAndroid",
149"Target {0} is not supported for Android platform. \n If you want to use particular device or simulator for launching Android app,\n please specify device id (as in 'adb devices' output) instead.",
150this.runOptions.target);
cbc7ac5bArtem Egorov7 years ago151this.logger.warning(message);
152} else {
153runArguments.push("--deviceId", this.runOptions.target);
154}
8022afdfVladimir Kotikov8 years ago155}
156}
157
158return runArguments;
159}
160
52f3873ddigeff10 years ago161private initializeTargetDevicesAndPackageName(): Q.Promise<void> {
db6fd42aRuslan Bikkinin7 years ago162return this.adbHelper.getConnectedDevices().then(devices => {
52f3873ddigeff10 years ago163this.devices = devices;
164this.debugTarget = this.getTargetEmulator(devices);
165return this.getPackageName().then(packageName => {
166this.packageName = packageName;
167});
168});
169}
170
171private launchAppWithADBReverseAndLogCat(device: IDevice): Q.Promise<void> {
172return Q({})
173.then(() => {
174return this.configureADBReverseWhenApplicable(device);
175}).then(() => {
176return this.needsToLaunchApps
db6fd42aRuslan Bikkinin7 years ago177? this.adbHelper.launchApp(this.runOptions.projectRoot, this.packageName, device.id)
52f3873ddigeff10 years ago178: Q<void>(void 0);
179}).then(() => {
0a68f8dbArtem Egorov8 years ago180return this.startMonitoringLogCat(device, this.runOptions.logCatArguments);
52f3873ddigeff10 years ago181});
182}
183
184private configureADBReverseWhenApplicable(device: IDevice): Q.Promise<void> {
b57ea017Artem Egorov8 years ago185return Q({}) // For other emulators and devices we try to enable adb reverse
db6fd42aRuslan Bikkinin7 years ago186.then(() => this.adbHelper.apiVersion(device.id))
b57ea017Artem Egorov8 years ago187.then(apiVersion => {
188if (apiVersion >= AndroidAPILevel.LOLLIPOP) { // If we support adb reverse
db6fd42aRuslan Bikkinin7 years ago189return this.adbHelper.reverseAdb(device.id, Number(this.runOptions.packagerPort));
b57ea017Artem Egorov8 years ago190} else {
d7d405aeYuri Skorokhodov7 years ago191const message = localize("DeviceSupportsOnlyAPILevel",
192"Device {0} supports only API Level {1}. \n Level {2} is needed to support port forwarding via adb reverse. \n For debugging to work you'll need <Shake or press menu button> for the dev menu, \n go into <Dev Settings> and configure <Debug Server host & port for Device> to be \n an IP address of your computer that the Device can reach. More info at: \n https://facebook.github.io/react-native/docs/debugging.html#debugging-react-native-apps",
193device.id, apiVersion, AndroidAPILevel.LOLLIPOP);
194this.logger.warning(message);
b57ea017Artem Egorov8 years ago195return void 0;
196}
197});
52f3873ddigeff10 years ago198}
199
200private getPackageName(): Q.Promise<string> {
8022afdfVladimir Kotikov8 years ago201return new Package(this.runOptions.projectRoot).name().then(appName =>
52f3873ddigeff10 years ago202new PackageNameResolver(appName).resolvePackageName(this.runOptions.projectRoot));
203}
204
205/**
206* Returns the target emulator, using the following logic:
207* * If an emulator is specified and it is connected, use that one.
208* * Otherwise, use the first one in the list.
209*/
210private getTargetEmulator(devices: IDevice[]): IDevice {
211let activeFilterFunction = (device: IDevice) => {
212return device.isOnline;
213};
214
215let targetFilterFunction = (device: IDevice) => {
216return device.id === this.runOptions.target && activeFilterFunction(device);
217};
218
219if (this.runOptions && this.runOptions.target && devices) {
220/* check if the specified target is active */
221const targetDevice = devices.find(targetFilterFunction);
222if (targetDevice) {
223return targetDevice;
224}
225}
226
227/* return the first active device in the list */
228let activeDevices = devices && devices.filter(activeFilterFunction);
229return activeDevices && activeDevices[0];
230}
231
0a68f8dbArtem Egorov8 years ago232private startMonitoringLogCat(device: IDevice, logCatArguments: string): void {
233this.stopMonitoringLogCat(); // Stop previous logcat monitor if it's running
234
235// this.logCatMonitor can be mutated, so we store it locally too
db6fd42aRuslan Bikkinin7 years ago236this.logCatMonitor = new LogCatMonitor(device.id, logCatArguments, this.adbHelper);
0a68f8dbArtem Egorov8 years ago237this.logCatMonitor.start() // The LogCat will continue running forever, so we don't wait for it
d7d405aeYuri Skorokhodov7 years ago238.catch(error => this.logger.warning(localize("ErrorWhileMonitoringLogCat", "Error while monitoring LogCat"), error)) // The LogCatMonitor failing won't stop the debugging experience
0a68f8dbArtem Egorov8 years ago239.done();
240}
241
242private stopMonitoringLogCat(): void {
243if (this.logCatMonitor) {
244this.logCatMonitor.dispose();
245this.logCatMonitor = null;
246}
52f3873ddigeff10 years ago247}
ef902673Vladimir Kotikov9 years ago248}