-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathandroid-debug-service.ts
242 lines (205 loc) · 9.75 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
///<reference path="../.d.ts"/>
"use strict";
import * as helpers from "../common/helpers";
import * as path from "path";
import * as util from "util";
class AndroidDebugService implements IDebugService {
private static ENV_DEBUG_IN_FILENAME = "envDebug.in";
private static ENV_DEBUG_OUT_FILENAME = "envDebug.out";
private static DEFAULT_NODE_INSPECTOR_URL = "http://127.0.0.1:8080/debug";
private static PACKAGE_EXTERNAL_DIR_TEMPLATE = "/sdcard/Android/data/%s/files/";
private _device: Mobile.IAndroidDevice = null;
constructor(private $devicesServices: Mobile.IDevicesServices,
private $platformService: IPlatformService,
private $platformsData: IPlatformsData,
private $projectData: IProjectData,
private $logger: ILogger,
private $options: IOptions,
private $childProcess: IChildProcess,
private $mobileHelper: Mobile.IMobileHelper,
private $hostInfo: IHostInfo,
private $errors: IErrors,
private $opener: IOpener,
private $staticConfig: IStaticConfig,
private $utils: IUtils,
private $config: IConfiguration) { }
private get platform() { return "android"; }
private get device(): Mobile.IAndroidDevice {
return this._device;
}
private set device(newDevice) {
this._device = newDevice;
}
public debug(): IFuture<void> {
return this.$options.emulator
? this.debugOnEmulator()
: this.debugOnDevice();
}
public debugOnEmulator(): IFuture<void> {
return (() => {
this.$platformService.deployOnEmulator(this.platform).wait();
this.debugOnDevice().wait();
}).future<void>()();
}
public debugOnDevice(): IFuture<void> {
return (() => {
let packageFile = "";
if(!this.$options.debugBrk && !this.$options.start && !this.$options.getPort && !this.$options.stop) {
this.$logger.warn("Neither --debug-brk nor --start option was specified. Defaulting to --debug-brk.");
this.$options.debugBrk = true;
}
if (this.$options.debugBrk && !this.$options.emulator) {
let cachedDeviceOption = this.$options.forDevice;
this.$options.forDevice = true;
this.$platformService.buildPlatform(this.platform).wait();
this.$options.forDevice = !!cachedDeviceOption;
let platformData = this.$platformsData.getPlatformData(this.platform);
packageFile = this.$platformService.getLatestApplicationPackageForDevice(platformData).wait().packageName;
this.$logger.out("Using ", packageFile);
}
this.$devicesServices.initialize({ platform: this.platform, deviceId: this.$options.device}).wait();
let action = (device: Mobile.IAndroidDevice): IFuture<void> => { return this.debugCore(device, packageFile, this.$projectData.projectId); };
this.$devicesServices.execute(action).wait();
}).future<void>()();
}
private debugCore(device: Mobile.IAndroidDevice, packageFile: string, packageName: string): IFuture<void> {
return (() => {
this.device = device;
if (this.$options.getPort) {
this.printDebugPort(packageName).wait();
} else if (this.$options.start) {
this.attachDebugger(packageName);
} else if (this.$options.stop) {
this.detachDebugger(packageName).wait();
} else if (this.$options.debugBrk) {
this.startAppWithDebugger(packageFile, packageName).wait();
}
}).future<void>()();
}
private printDebugPort(packageName: string): IFuture<void> {
return (() => {
let res = this.device.adb.executeShellCommand(["am", "broadcast", "-a", packageName + "-GetDbgPort"]).wait();
this.$logger.info(res);
}).future<void>()();
}
private attachDebugger(packageName: string): void {
let startDebuggerCommand = ["am", "broadcast", "-a", '\"${packageName}-Debug\"', "--ez", "enable", "true"];
let port = this.$options.debugPort;
if (port > 0) {
startDebuggerCommand.push("--ei", "debuggerPort", port.toString());
this.device.adb.executeShellCommand(startDebuggerCommand).wait();
} else {
let res = this.device.adb.executeShellCommand(["am", "broadcast", "-a", packageName + "-Debug", "--ez", "enable", "true"]).wait();
let match = res.match(/result=(\d)+/);
if (match) {
port = match[0].substring(7);
} else {
port = 0;
}
}
if ((0 < port) && (port < 65536)) {
this.tcpForward(port, port).wait();
this.startDebuggerClient(port).wait();
this.openDebuggerClient(AndroidDebugService.DEFAULT_NODE_INSPECTOR_URL + "?port=" + port);
} else {
this.$logger.info("Cannot detect debug port.");
}
}
private detachDebugger(packageName: string): IFuture<void> {
return this.device.adb.executeShellCommand(["am", "broadcast", "-a", `${packageName}-Debug`, "--ez", "enable", "false"]);
}
private startAppWithDebugger(packageFile: string, packageName: string): IFuture<void> {
return (() => {
if(!this.$options.emulator) {
this.device.applicationManager.uninstallApplication(packageName).wait();
this.device.applicationManager.installApplication(packageFile).wait();
}
let packageDir = util.format(AndroidDebugService.PACKAGE_EXTERNAL_DIR_TEMPLATE, packageName);
let envDebugOutFullpath = this.$mobileHelper.buildDevicePath(packageDir, AndroidDebugService.ENV_DEBUG_OUT_FILENAME);
this.device.adb.executeShellCommand(["rm", `${envDebugOutFullpath}`]).wait();
this.device.adb.executeShellCommand(["mkdir", "-p", `${packageDir}`]).wait();
let debugBreakPath = this.$mobileHelper.buildDevicePath(packageDir, "debugbreak");
this.device.adb.executeShellCommand([`cat /dev/null > ${debugBreakPath}`]).wait();
this.device.applicationManager.startApplication(packageName).wait();
let dbgPort = this.startAndGetPort(packageName).wait();
if (dbgPort > 0) {
this.tcpForward(dbgPort, dbgPort).wait();
this.startDebuggerClient(dbgPort).wait();
this.openDebuggerClient(AndroidDebugService.DEFAULT_NODE_INSPECTOR_URL + "?port=" + dbgPort);
}
}).future<void>()();
}
private tcpForward(src: Number, dest: Number): IFuture<void> {
return this.device.adb.executeCommand(["forward", `tcp:${src.toString()}`, `tcp:${dest.toString()}`]);
}
private startDebuggerClient(port: Number): IFuture<void> {
return (() => {
let nodeInspectorModuleFilePath = require.resolve("node-inspector");
let nodeInspectorModuleDir = path.dirname(nodeInspectorModuleFilePath);
let nodeInspectorFullPath = path.join(nodeInspectorModuleDir, "bin", "inspector");
this.$childProcess.spawn(process.argv[0], [nodeInspectorFullPath, "--debug-port", port.toString()], { stdio: "ignore", detached: true });
}).future<void>()();
}
private openDebuggerClient(url: string): void {
let defaultDebugUI = "chrome";
if(this.$hostInfo.isDarwin) {
defaultDebugUI = "Google Chrome";
}
if(this.$hostInfo.isLinux) {
defaultDebugUI = "google-chrome";
}
let debugUI = this.$config.ANDROID_DEBUG_UI || defaultDebugUI;
let child = this.$opener.open(url, debugUI);
if(!child) {
this.$errors.failWithoutHelp(`Unable to open ${debugUI}.`);
}
}
private checkIfRunning(packageName: string): boolean {
let packageDir = util.format(AndroidDebugService.PACKAGE_EXTERNAL_DIR_TEMPLATE, packageName);
let envDebugOutFullpath = packageDir + AndroidDebugService.ENV_DEBUG_OUT_FILENAME;
let isRunning = this.checkIfFileExists(envDebugOutFullpath).wait();
return isRunning;
}
private checkIfFileExists(filename: string): IFuture<boolean> {
return (() => {
let res = this.device.adb.executeShellCommand([`test -f ${filename} && echo 'yes' || echo 'no'`]).wait();
let exists = res.indexOf('yes') > -1;
return exists;
}).future<boolean>()();
}
private startAndGetPort(packageName: string): IFuture<number> {
return (() => {
let port = -1;
let timeout = this.$utils.getParsedTimeout(90);
let packageDir = util.format(AndroidDebugService.PACKAGE_EXTERNAL_DIR_TEMPLATE, packageName);
let envDebugInFullpath = packageDir + AndroidDebugService.ENV_DEBUG_IN_FILENAME;
this.device.adb.executeShellCommand(["rm", `${envDebugInFullpath}`]).wait();
let isRunning = false;
for (let i = 0; i < timeout; i++) {
helpers.sleep(1000 /* ms */);
isRunning = this.checkIfRunning(packageName);
if (isRunning) {
break;
}
}
if (isRunning) {
this.device.adb.executeShellCommand([`cat /dev/null > ${envDebugInFullpath}`]).wait();
for (let i = 0; i < timeout; i++) {
helpers.sleep(1000 /* ms */);
let envDebugOutFullpath = packageDir + AndroidDebugService.ENV_DEBUG_OUT_FILENAME;
let exists = this.checkIfFileExists(envDebugOutFullpath).wait();
if (exists) {
let res = this.device.adb.executeShellCommand(["cat", envDebugOutFullpath]).wait();
let match = res.match(/PORT=(\d)+/);
if (match) {
port = parseInt(match[0].substring(5), 10);
break;
}
}
}
}
return port;
}).future<number>()();
}
}
$injector.register("androidDebugService", AndroidDebugService);