-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-device-debug-service.ts
235 lines (212 loc) · 6.47 KB
/
ios-device-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
import * as path from "path";
import { ChildProcess } from "child_process";
import { DebugServiceBase } from "./debug-service-base";
import {
CONNECTION_ERROR_EVENT_NAME,
DeviceConnectionType,
} from "../constants";
const inspectorAppName = "NativeScript Inspector.app";
const inspectorNpmPackageName = "tns-ios-inspector";
const inspectorUiDir = "WebInspectorUI/";
import { performanceLog } from "../common/decorators";
import { platform } from "os";
import {
IDeviceDebugService,
IDebugOptions,
IDebugResultInfo,
IDebugData,
} from "../definitions/debug";
import {
IPackageInstallationManager,
IAppDebugSocketProxyFactory,
} from "../declarations";
import { IProjectDataService } from "../definitions/project";
import { IChildProcess, IHostInfo, IErrors } from "../common/declarations";
import { injector } from "../common/yok";
export class IOSDeviceDebugService
extends DebugServiceBase
implements IDeviceDebugService {
private deviceIdentifier: string;
constructor(
protected device: Mobile.IiOSDevice,
protected $devicesService: Mobile.IDevicesService,
private $childProcess: IChildProcess,
private $hostInfo: IHostInfo,
private $logger: ILogger,
private $errors: IErrors,
private $packageInstallationManager: IPackageInstallationManager,
private $appDebugSocketProxyFactory: IAppDebugSocketProxyFactory,
private $projectDataService: IProjectDataService
) {
super(device, $devicesService);
this.$appDebugSocketProxyFactory.on(
CONNECTION_ERROR_EVENT_NAME,
(e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e)
);
this.deviceIdentifier = this.device.deviceInfo.identifier;
}
public get platform(): string {
return "ios";
}
@performanceLog()
public async debug(
debugData: IDebugData,
debugOptions: IDebugOptions
): Promise<IDebugResultInfo> {
await this.validateOptions(debugOptions);
const result: IDebugResultInfo = { debugUrl: null };
result.debugUrl = await this.wireDebuggerClient(debugData, debugOptions);
return result;
}
public async debugStop(): Promise<void> {
this.$appDebugSocketProxyFactory.removeAllProxies();
}
private async validateOptions(debugOptions: IDebugOptions) {
if (!this.$hostInfo.isWindows && !this.$hostInfo.isDarwin) {
this.$errors.fail(
`Debugging on iOS devices is not supported for ${platform()} yet.`
);
}
if (debugOptions.debugBrk && debugOptions.start) {
this.$errors.fail(
"Expected exactly one of the --debug-brk or --start options."
);
}
await this.validateUSBConnectedDevice();
}
private async validateUSBConnectedDevice() {
const device = await this.$devicesService.getDevice(this.deviceIdentifier);
if (
device.deviceInfo.connectionTypes.indexOf(DeviceConnectionType.USB) ===
-1 &&
device.deviceInfo.connectionTypes.indexOf(DeviceConnectionType.Local) ===
-1
) {
const deviceConnectionTypes = device.deviceInfo.connectionTypes
.map((type) => DeviceConnectionType[type])
.join(", ");
this.$errors.fail(
`Debugging application requires a USB or LOCAL connection while the target device "${this.deviceIdentifier}" has connection type "${deviceConnectionTypes}".`
);
}
}
private getProjectName(debugData: IDebugData): string {
let projectName = debugData.projectName;
if (!projectName && debugData.projectDir) {
const projectData = this.$projectDataService.getProjectData(
debugData.projectDir
);
projectName = projectData.projectName;
}
return projectName;
}
private async killProcess(childProcess: ChildProcess): Promise<void> {
if (childProcess) {
return new Promise<void>((resolve, reject) => {
childProcess.on("close", resolve);
childProcess.kill();
});
}
}
@performanceLog()
private async wireDebuggerClient(
debugData: IDebugData,
debugOptions: IDebugOptions
): Promise<string> {
if (
(debugOptions.inspector || !debugOptions.client) &&
this.$hostInfo.isDarwin
) {
return await this.setupTcpAppDebugProxy(debugData, debugOptions);
} else {
return await this.setupWebAppDebugProxy(debugOptions, debugData);
}
}
private async setupWebAppDebugProxy(
debugOptions: IDebugOptions,
debugData: IDebugData
): Promise<string> {
if (debugOptions.chrome) {
this.$logger.info(
"'--chrome' is the default behavior. Use --inspector to debug iOS applications using the Safari Web Inspector."
);
}
const projectName = this.getProjectName(debugData);
const webSocketProxy = await this.$appDebugSocketProxyFactory.ensureWebSocketProxy(
this.device,
debugData.applicationIdentifier,
projectName,
debugData.projectDir
);
return this.getChromeDebugUrl(debugOptions, webSocketProxy.options.port);
}
private async setupTcpAppDebugProxy(
debugData: IDebugData,
debugOptions: IDebugOptions
): Promise<string> {
const projectName = this.getProjectName(debugData);
const existingTcpProxy = this.$appDebugSocketProxyFactory.getTCPSocketProxy(
this.deviceIdentifier,
debugData.applicationIdentifier
);
const tcpSocketProxy =
existingTcpProxy ||
(await this.$appDebugSocketProxyFactory.addTCPSocketProxy(
this.device,
debugData.applicationIdentifier,
projectName,
debugData.projectDir
));
if (!existingTcpProxy) {
const inspectorProcess = await this.openAppInspector(
tcpSocketProxy.address(),
debugData,
debugOptions
);
if (inspectorProcess) {
tcpSocketProxy.on("close", async () => {
await this.killProcess(inspectorProcess);
});
}
}
return null;
}
@performanceLog()
private async openAppInspector(
fileDescriptor: string,
debugData: IDebugData,
debugOptions: IDebugOptions
): Promise<ChildProcess> {
if (debugOptions.client) {
const inspectorPath = await this.$packageInstallationManager.getInspectorFromCache(
inspectorNpmPackageName,
debugData.projectDir
);
const inspectorSourceLocation = path.join(
inspectorPath,
inspectorUiDir,
"Main.html"
);
const inspectorApplicationPath = path.join(
inspectorPath,
inspectorAppName,
"Contents",
"MacOS",
inspectorAppName,
"Contents",
"MacOS",
"NativeScript Inspector"
);
const inspectorProcess: ChildProcess = this.$childProcess.spawn(
inspectorApplicationPath,
[inspectorSourceLocation, debugData.projectName, fileDescriptor]
);
inspectorProcess.on("error", (e: Error) => this.$logger.trace(e));
return inspectorProcess;
} else {
this.$logger.info("Suppressing debugging client.");
return null;
}
}
}
injector.register("iOSDeviceDebugService", IOSDeviceDebugService, false);