-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-debug-service.ts
203 lines (158 loc) · 8.3 KB
/
android-debug-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { sleep } from "../common/helpers";
import { DebugServiceBase } from "./debug-service-base";
import { LiveSyncPaths } from "../common/constants";
export class AndroidDebugService extends DebugServiceBase implements IPlatformDebugService {
private _packageName: string;
public get platform() {
return "android";
}
constructor(protected device: Mobile.IAndroidDevice,
protected $devicesService: Mobile.IDevicesService,
private $errors: IErrors,
private $logger: ILogger,
private $androidDeviceDiscovery: Mobile.IDeviceDiscovery,
private $androidProcessService: Mobile.IAndroidProcessService,
private $net: INet,
private $projectDataService: IProjectDataService,
private $deviceLogProvider: Mobile.IDeviceLogProvider) {
super(device, $devicesService);
}
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
this._packageName = debugData.applicationIdentifier;
const result = debugOptions.emulator
? await this.debugOnEmulator(debugData, debugOptions)
: await this.debugOnDevice(debugData, debugOptions);
if (!debugOptions.justlaunch) {
const pid = await this.$androidProcessService.getAppProcessId(debugData.deviceIdentifier, debugData.applicationIdentifier);
if (pid) {
this.$deviceLogProvider.setApplicationPidForDevice(debugData.deviceIdentifier, pid);
const device = await this.$devicesService.getDevice(debugData.deviceIdentifier);
await device.openDeviceLogStream();
}
}
return result;
}
public async debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
const appData: Mobile.IApplicationData = {
appId: debugData.applicationIdentifier,
projectName: projectData.projectName
};
const action = (device: Mobile.IAndroidDevice): Promise<void> => this.debugStartCore(appData, debugOptions);
await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
}
public debugStop(): Promise<void> {
return this.removePortForwarding();
}
protected getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
const debugOpts = _.cloneDeep(debugOptions);
debugOpts.useBundledDevTools = debugOpts.useBundledDevTools === undefined ? true : debugOpts.useBundledDevTools;
const chromeDebugUrl = super.getChromeDebugUrl(debugOpts, port);
return chromeDebugUrl;
}
private async debugOnEmulator(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
// Assure we've detected the emulator as device
// For example in case deployOnEmulator had stated new emulator instance
// we need some time to detect it. Let's force detection.
await this.$androidDeviceDiscovery.startLookingForDevices();
return this.debugOnDevice(debugData, debugOptions);
}
private async removePortForwarding(packageName?: string): Promise<void> {
const port = await this.getForwardedLocalDebugPortForPackageName(this.device.deviceInfo.identifier, packageName || this._packageName);
return this.device.adb.executeCommand(["forward", "--remove", `tcp:${port}`]);
}
private async getForwardedLocalDebugPortForPackageName(deviceId: string, packageName: string): Promise<number> {
let port = -1;
const forwardsResult = await this.device.adb.executeCommand(["forward", "--list"]);
const unixSocketName = `${packageName}-inspectorServer`;
//matches 123a188909e6czzc tcp:40001 localabstract:org.nativescript.testUnixSockets-debug
const regexp = new RegExp(`(?:${deviceId} tcp:)([\\d]+)(?= localabstract:${unixSocketName})`, "g");
const match = regexp.exec(forwardsResult);
if (match) {
port = parseInt(match[1]);
} else {
port = await this.$net.getAvailablePortInRange(40000);
await this.unixSocketForward(port, `${unixSocketName}`);
}
return port;
}
private async unixSocketForward(local: number, remote: string): Promise<void> {
return this.device.adb.executeCommand(["forward", `tcp:${local}`, `localabstract:${remote}`]);
}
private async debugOnDevice(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
let packageFile = "";
if (!debugOptions.start && !debugOptions.emulator) {
packageFile = debugData.pathToAppPackage;
this.$logger.out("Using ", packageFile);
}
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
const projectName = this.$projectDataService.getProjectData(debugData.projectDir).projectName;
const appData: Mobile.IApplicationData = {
appId: debugData.applicationIdentifier,
projectName
};
const action = (device: Mobile.IAndroidDevice): Promise<string> => this.debugCore(device, packageFile, appData, debugOptions);
const deviceActionResult = await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
return deviceActionResult[0].result;
}
private async debugCore(device: Mobile.IAndroidDevice, packageFile: string, appData: Mobile.IApplicationData, debugOptions: IDebugOptions): Promise<string> {
await this.printDebugPort(device.deviceInfo.identifier, appData.appId);
if (debugOptions.start) {
return await this.attachDebugger(device.deviceInfo.identifier, appData.appId, debugOptions);
} else if (debugOptions.stop) {
await this.removePortForwarding();
return null;
} else {
await this.debugStartCore(appData, debugOptions);
return await this.attachDebugger(device.deviceInfo.identifier, appData.appId, debugOptions);
}
}
private async printDebugPort(deviceId: string, packageName: string): Promise<void> {
const port = await this.getForwardedLocalDebugPortForPackageName(deviceId, packageName);
this.$logger.info("device: " + deviceId + " debug port: " + port + "\n");
}
private async attachDebugger(deviceId: string, packageName: string, debugOptions: IDebugOptions): Promise<string> {
if (!(await this.isAppRunning(packageName, deviceId))) {
this.$errors.failWithoutHelp(`The application ${packageName} does not appear to be running on ${deviceId} or is not built with debugging enabled.`);
}
const port = await this.getForwardedLocalDebugPortForPackageName(deviceId, packageName);
return this.getChromeDebugUrl(debugOptions, port);
}
private async debugStartCore(appData: Mobile.IApplicationData, debugOptions: IDebugOptions): Promise<void> {
// Arguments passed to executeShellCommand must be in array ([]), but it turned out adb shell "arg with intervals" still works correctly.
// As we need to redirect output of a command on the device, keep using only one argument.
// We could rewrite this with two calls - touch and rm -f , but -f flag is not available on old Android, so rm call will fail when file does not exist.
await this.device.applicationManager.stopApplication(appData);
if (debugOptions.debugBrk) {
await this.device.adb.executeShellCommand([`cat /dev/null > ${LiveSyncPaths.ANDROID_TMP_DIR_NAME}/${appData.appId}-debugbreak`]);
}
await this.device.adb.executeShellCommand([`cat /dev/null > ${LiveSyncPaths.ANDROID_TMP_DIR_NAME}/${appData.appId}-debugger-started`]);
await this.device.applicationManager.startApplication(appData);
await this.waitForDebugger(appData.appId);
}
private async waitForDebugger(packageName: String): Promise<void> {
const debuggerStartedFilePath = `${LiveSyncPaths.ANDROID_TMP_DIR_NAME}/${packageName}-debugger-started`;
const waitText: string = `0 ${debuggerStartedFilePath}`;
let maxWait = 12;
let debuggerStarted: boolean = false;
while (maxWait > 0 && !debuggerStarted) {
const forwardsResult = await this.device.adb.executeShellCommand(["ls", "-s", debuggerStartedFilePath]);
maxWait--;
debuggerStarted = forwardsResult.indexOf(waitText) === -1;
if (!debuggerStarted) {
await sleep(500);
}
}
if (debuggerStarted) {
this.$logger.info("# NativeScript Debugger started #");
} else {
this.$logger.warn("# NativeScript Debugger did not start in time #");
}
}
private async isAppRunning(appIdentifier: string, deviceIdentifier: string): Promise<boolean> {
const debuggableApps = await this.$androidProcessService.getDebuggableApps(deviceIdentifier);
return !!_.find(debuggableApps, a => a.appIdentifier === appIdentifier);
}
}
$injector.register("androidDebugService", AndroidDebugService, false);