-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathios-device-debug-service.ts
139 lines (116 loc) · 5.87 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
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";
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);
return await this.wireDebuggerClient(debugData, debugOptions);
}
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<IDebugResultInfo> {
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<IDebugResultInfo> {
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 {
debugUrl: this.getChromeDebugUrl(debugOptions, webSocketProxy.options.port),
legacyDebugUrl: this.getChromeDebugUrl(debugOptions, webSocketProxy.options.port, true)
};
}
private async setupTcpAppDebugProxy(debugData: IDebugData, debugOptions: IDebugOptions): Promise<IDebugResultInfo> {
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);