-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathdebug-controller.ts
230 lines (187 loc) · 9.67 KB
/
debug-controller.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
import { performanceLog } from "../common/decorators";
import { EOL } from "os";
import { parse } from "url";
import { CONNECTED_STATUS } from "../common/constants";
import { TrackActionNames, DebugCommandErrors, CONNECTION_ERROR_EVENT_NAME, DebugTools, DEBUGGER_DETACHED_EVENT_NAME, DEBUGGER_ATTACHED_EVENT_NAME } from "../constants";
import { EventEmitter } from "events";
export class DebugController extends EventEmitter implements IDebugController {
private _platformDebugServices: IDictionary<IDeviceDebugService> = {};
constructor(
private $analyticsService: IAnalyticsService,
private $debugDataService: IDebugDataService,
private $devicesService: Mobile.IDevicesService,
private $errors: IErrors,
private $injector: IInjector,
private $liveSyncProcessDataService: ILiveSyncProcessDataService,
private $logger: ILogger,
private $mobileHelper: Mobile.IMobileHelper,
private $projectDataService: IProjectDataService
) {
super();
}
@performanceLog()
public async startDebug(debugData: IDebugData): Promise<IDebugInformation> {
const { debugOptions: options } = debugData;
const device = this.$devicesService.getDeviceByIdentifier(debugData.deviceIdentifier);
if (!device) {
this.$errors.fail(`Cannot find device with identifier ${debugData.deviceIdentifier}.`);
}
if (device.deviceInfo.status !== CONNECTED_STATUS) {
this.$errors.fail(`The device with identifier ${debugData.deviceIdentifier} is unreachable. Make sure it is Trusted and try again.`);
}
await this.$analyticsService.trackEventActionInGoogleAnalytics({
action: TrackActionNames.Debug,
device,
additionalData: this.$mobileHelper.isiOSPlatform(device.deviceInfo.platform) && options && options.inspector ? DebugTools.Inspector : DebugTools.Chrome,
projectDir: debugData.projectDir
});
if (!(await device.applicationManager.isApplicationInstalled(debugData.applicationIdentifier))) {
this.$errors.fail(`The application ${debugData.applicationIdentifier} is not installed on device with identifier ${debugData.deviceIdentifier}.`);
}
const debugService = this.getDeviceDebugService(device);
if (!debugService) {
this.$errors.fail(`Unsupported device OS: ${device.deviceInfo.platform}. You can debug your applications only on iOS or Android.`);
}
const debugOptions: IDebugOptions = _.cloneDeep(options);
const debugResultInfo = await debugService.debug(debugData, debugOptions);
return this.getDebugInformation(debugResultInfo, device.deviceInfo.identifier);
}
public enableDebugging(enableDebuggingData: IEnableDebuggingData): Promise<IDebugInformation>[] {
const { deviceIdentifiers } = enableDebuggingData;
return _.map(deviceIdentifiers, deviceIdentifier => this.enableDebuggingCore(enableDebuggingData.projectDir, deviceIdentifier, enableDebuggingData.debugOptions));
}
public async disableDebugging(disableDebuggingData: IDisableDebuggingData): Promise<void> {
const { deviceIdentifiers, projectDir } = disableDebuggingData;
for (const deviceIdentifier of deviceIdentifiers) {
const liveSyncProcessInfo = this.$liveSyncProcessDataService.getPersistedData(projectDir);
if (liveSyncProcessInfo.currentSyncAction) {
await liveSyncProcessInfo.currentSyncAction;
}
const currentDeviceDescriptor = this.getDeviceDescriptor(projectDir, deviceIdentifier);
if (currentDeviceDescriptor) {
currentDeviceDescriptor.debuggingEnabled = false;
} else {
this.$errors.fail(`Couldn't disable debugging for ${deviceIdentifier}`);
}
const currentDevice = this.$devicesService.getDeviceByIdentifier(currentDeviceDescriptor.identifier);
if (!currentDevice) {
this.$errors.fail(`Couldn't disable debugging for ${deviceIdentifier}. Could not find device.`);
}
await this.stopDebug(currentDevice.deviceInfo.identifier);
this.emit(DEBUGGER_DETACHED_EVENT_NAME, { deviceIdentifier });
}
}
public async attachDebugger(attachDebuggerData: IAttachDebuggerData): Promise<IDebugInformation> {
// Default values
if (attachDebuggerData.debugOptions) {
attachDebuggerData.debugOptions.chrome = attachDebuggerData.debugOptions.chrome === undefined ? true : attachDebuggerData.debugOptions.chrome;
attachDebuggerData.debugOptions.start = attachDebuggerData.debugOptions.start === undefined ? true : attachDebuggerData.debugOptions.start;
} else {
attachDebuggerData.debugOptions = {
chrome: true,
start: true
};
}
const projectData = this.$projectDataService.getProjectData(attachDebuggerData.projectDir);
const debugData = this.$debugDataService.getDebugData(attachDebuggerData.deviceIdentifier, projectData, attachDebuggerData.debugOptions);
// const platformData = this.$platformsDataService.getPlatformData(settings.platform, projectData);
// Of the properties below only `buildForDevice` and `release` are currently used.
// Leaving the others with placeholder values so that they may not be forgotten in future implementations.
const debugInfo = await this.startDebug(debugData);
const result = this.printDebugInformation(debugInfo, attachDebuggerData.debugOptions.forceDebuggerAttachedEvent);
return result;
}
@performanceLog()
public async enableDebuggingCoreWithoutWaitingCurrentAction(projectDir: string, deviceIdentifier: string, debugOptions: IDebugOptions): Promise<IDebugInformation> {
const deviceDescriptor = this.getDeviceDescriptor(projectDir, deviceIdentifier);
if (!deviceDescriptor) {
this.$errors.fail(`Couldn't enable debugging for ${deviceIdentifier}`);
}
deviceDescriptor.debuggingEnabled = true;
deviceDescriptor.debugOptions = debugOptions;
const currentDeviceInstance = this.$devicesService.getDeviceByIdentifier(deviceIdentifier);
const attachDebuggerData: IAttachDebuggerData = {
deviceIdentifier,
isEmulator: currentDeviceInstance.isEmulator,
outputPath: deviceDescriptor.buildData.outputPath,
platform: currentDeviceInstance.deviceInfo.platform,
projectDir,
debugOptions
};
let debugInformation: IDebugInformation;
try {
debugInformation = await this.attachDebugger(attachDebuggerData);
} catch (err) {
this.$logger.trace("Couldn't attach debugger, will modify options and try again.", err);
attachDebuggerData.debugOptions.start = false;
try {
debugInformation = await this.attachDebugger(attachDebuggerData);
} catch (innerErr) {
this.$logger.trace("Couldn't attach debugger with modified options.", innerErr);
throw err;
}
}
return debugInformation;
}
public printDebugInformation(debugInformation: IDebugInformation, fireDebuggerAttachedEvent: boolean = true): IDebugInformation {
if (!!debugInformation.url) {
if (fireDebuggerAttachedEvent) {
this.emit(DEBUGGER_ATTACHED_EVENT_NAME, debugInformation);
}
this.$logger.info(`To start debugging, open the following URL in Chrome:${EOL}${debugInformation.url}${EOL}`.green);
this.$logger.info(`If you're using an older Chrome version 82 and below, use this instead:${EOL}${debugInformation.legacyUrl}${EOL}`.cyan);
}
return debugInformation;
}
public async stopDebug(deviceIdentifier: string): Promise<void> {
const device = this.$devicesService.getDeviceByIdentifier(deviceIdentifier);
const debugService = this.getDeviceDebugService(device);
await debugService.debugStop();
}
private getDeviceDescriptor(projectDir: string, deviceIdentifier: string): ILiveSyncDeviceDescriptor {
const deviceDescriptors = this.$liveSyncProcessDataService.getDeviceDescriptors(projectDir);
const currentDeviceDescriptor = _.find(deviceDescriptors, d => d.identifier === deviceIdentifier);
return currentDeviceDescriptor;
}
private getDeviceDebugService(device: Mobile.IDevice): IDeviceDebugService {
if (!this._platformDebugServices[device.deviceInfo.identifier]) {
const devicePlatform = device.deviceInfo.platform;
if (this.$mobileHelper.isiOSPlatform(devicePlatform)) {
this._platformDebugServices[device.deviceInfo.identifier] = this.$injector.resolve("iOSDeviceDebugService", { device });
} else if (this.$mobileHelper.isAndroidPlatform(devicePlatform)) {
this._platformDebugServices[device.deviceInfo.identifier] = this.$injector.resolve("androidDeviceDebugService", { device });
} else {
this.$errors.fail(DebugCommandErrors.UNSUPPORTED_DEVICE_OS_FOR_DEBUGGING);
}
this.attachConnectionErrorHandlers(this._platformDebugServices[device.deviceInfo.identifier]);
}
return this._platformDebugServices[device.deviceInfo.identifier];
}
private attachConnectionErrorHandlers(platformDebugService: IDeviceDebugService) {
let connectionErrorHandler = (e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e);
connectionErrorHandler = connectionErrorHandler.bind(this);
platformDebugService.on(CONNECTION_ERROR_EVENT_NAME, connectionErrorHandler);
}
private getDebugInformation(debugResultInfo: IDebugResultInfo, deviceIdentifier: string): IDebugInformation {
const debugInfo: IDebugInformation = {
url: debugResultInfo.debugUrl,
port: 0,
deviceIdentifier
};
if (debugResultInfo.debugUrl) {
const parseQueryString = true;
const wsQueryParam = <string>parse(debugResultInfo.debugUrl, parseQueryString).query.ws;
const hostPortSplit = wsQueryParam && wsQueryParam.split(":");
debugInfo.port = hostPortSplit && +hostPortSplit[1];
}
return debugInfo;
}
private async enableDebuggingCore(projectDir: string, deviceIdentifier: string, debugOptions: IDebugOptions): Promise<IDebugInformation> {
const liveSyncProcessInfo = this.$liveSyncProcessDataService.getPersistedData(projectDir);
if (liveSyncProcessInfo && liveSyncProcessInfo.currentSyncAction) {
await liveSyncProcessInfo.currentSyncAction;
}
return this.enableDebuggingCoreWithoutWaitingCurrentAction(projectDir, deviceIdentifier, debugOptions);
}
}
$injector.register("debugController", DebugController);