Skip to content

Commit 670f1c8

Browse files
committed
refactor: unify deviceId usage in ios-debug-service
1 parent 9bea8ac commit 670f1c8

File tree

2 files changed

+48
-37
lines changed

2 files changed

+48
-37
lines changed

lib/definitions/debug.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO: remove IDeviceIdentifier
12
/**
23
* Describes information for starting debug process.
34
*/

lib/services/ios-debug-service.ts

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const inspectorUiDir = "WebInspectorUI/";
1111

1212
export class IOSDebugService extends DebugServiceBase implements IPlatformDebugService {
1313
private _lldbProcess: ChildProcess;
14+
private deviceIdentifier: string;
1415

1516
constructor(protected device: Mobile.IiOSDevice,
1617
protected $devicesService: Mobile.IDevicesService,
@@ -26,25 +27,24 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
2627
private $socketProxyFactory: ISocketProxyFactory,
2728
private $projectDataService: IProjectDataService,
2829
private $deviceLogProvider: Mobile.IDeviceLogProvider) {
30+
2931
super(device, $devicesService);
3032
this.$processService.attachToProcessExitSignals(this, this.debugStop);
3133
this.$socketProxyFactory.on(CONNECTION_ERROR_EVENT_NAME, (e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e));
34+
this.deviceIdentifier = this.device.deviceInfo.identifier;
3235
}
3336

3437
public get platform(): string {
3538
return "ios";
3639
}
3740

3841
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
39-
40-
if (debugOptions.debugBrk && debugOptions.start) {
41-
this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options.");
42-
}
42+
this.validateOptions(debugOptions);
4343

4444
await this.startDeviceLogProcess(debugData, debugOptions);
4545
await this.$iOSDebuggerPortService.attachToDebuggerPortFoundEvent(this.device, debugData, debugOptions);
4646

47-
if (!debugOptions.start) { // not attach
47+
if (!debugOptions.start) {
4848
if (this.device.isEmulator) {
4949
await this.startAppOnSimulator(debugData, debugOptions);
5050
} else {
@@ -67,12 +67,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
6767

6868
public async debugStop(): Promise<void> {
6969
this.$socketProxyFactory.removeAllProxies();
70-
71-
if (this._lldbProcess) {
72-
this._lldbProcess.stdin.write("process detach\n");
73-
await this.killProcess(this._lldbProcess);
74-
this._lldbProcess = undefined;
75-
}
70+
await this.stopAppDebuggerOnSimulator();
7671
}
7772

7873
protected getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
@@ -83,11 +78,17 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
8378
return chromeDebugUrl;
8479
}
8580

81+
private validateOptions(debugOptions: IDebugOptions) {
82+
if (debugOptions.debugBrk && debugOptions.start) {
83+
this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options.");
84+
}
85+
}
86+
8687
private async startDeviceLogProcess(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
8788
if (debugOptions.justlaunch) {
8889
// No logs should be printed on console when `--justlaunch` option is passed.
8990
// On the other side we need to start log process in order to get debugger port from logs.
90-
this.$deviceLogProvider.muteLogsForDevice(debugData.deviceIdentifier);
91+
this.$deviceLogProvider.muteLogsForDevice(this.deviceIdentifier);
9192
}
9293

9394
let projectName = debugData.projectName;
@@ -97,21 +98,12 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
9798
}
9899

99100
if (projectName) {
100-
this.$deviceLogProvider.setProjectNameForDevice(debugData.deviceIdentifier, projectName);
101+
this.$deviceLogProvider.setProjectNameForDevice(this.deviceIdentifier, projectName);
101102
}
102103

103104
await this.device.openDeviceLogStream({ predicate: IOS_LOG_PREDICATE });
104105
}
105106

106-
private async killProcess(childProcess: ChildProcess): Promise<void> {
107-
if (childProcess) {
108-
return new Promise<void>((resolve, reject) => {
109-
childProcess.on("close", resolve);
110-
childProcess.kill();
111-
});
112-
}
113-
}
114-
115107
private async startAppOnSimulator(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
116108
const args = debugOptions.debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start";
117109
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
@@ -120,38 +112,56 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
120112
args: args,
121113
appId: debugData.applicationIdentifier,
122114
skipInstall: true,
123-
device: debugData.deviceIdentifier,
115+
device: this.deviceIdentifier,
124116
justlaunch: debugOptions.justlaunch,
125117
timeout: debugOptions.timeout,
126118
sdk: debugOptions.sdk
127119
});
128120
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, launchResult);
129-
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
130-
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
131-
this._lldbProcess.stdout.pipe(process.stdout);
132-
}
133-
this._lldbProcess.stderr.pipe(process.stderr);
134-
this._lldbProcess.stdin.write("process continue\n");
121+
this.startAppDebuggerOnSimulator(pid);
135122
}
136123

137124
private async startAppOnDevice(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
138125
const runOptions: IRunPlatformOptions = {
139-
device: debugData.deviceIdentifier,
126+
device: this.deviceIdentifier,
140127
emulator: this.device.isEmulator,
141128
justlaunch: debugOptions.justlaunch
142129
};
143130
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
144131
await this.$platformService.startApplication(this.platform, runOptions, { appId: debugData.applicationIdentifier, projectName: projectData.projectName });
145132
}
146133

134+
private startAppDebuggerOnSimulator(pid: string) {
135+
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
136+
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
137+
this._lldbProcess.stdout.pipe(process.stdout);
138+
}
139+
this._lldbProcess.stderr.pipe(process.stderr);
140+
this._lldbProcess.stdin.write("process continue\n");
141+
}
142+
143+
private async stopAppDebuggerOnSimulator() {
144+
if (this._lldbProcess) {
145+
this._lldbProcess.stdin.write("process detach\n");
146+
await this.killProcess(this._lldbProcess);
147+
this._lldbProcess = undefined;
148+
}
149+
}
150+
151+
private async killProcess(childProcess: ChildProcess): Promise<void> {
152+
if (childProcess) {
153+
return new Promise<void>((resolve, reject) => {
154+
childProcess.on("close", resolve);
155+
childProcess.kill();
156+
});
157+
}
158+
}
159+
147160
private async wireDebuggerClient(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
148-
// the VSCode Ext starts `tns debug ios --no-client` to start/attach to debug sessions
149-
// check if --no-client is passed - default to opening a tcp socket (versus Chrome DevTools (websocket))
150-
const deviceIdentifier = this.device ? this.device.deviceInfo.identifier : debugData.deviceIdentifier;
151161
if ((debugOptions.inspector || !debugOptions.client) && this.$hostInfo.isDarwin) {
152-
const existingTcpProxy = this.$socketProxyFactory.getTCPSocketProxy(deviceIdentifier);
162+
const existingTcpProxy = this.$socketProxyFactory.getTCPSocketProxy(this.deviceIdentifier);
153163
const getDeviceSocket = async () => await this.device.getDebugSocket(debugData.applicationIdentifier, debugData.projectDir);
154-
const tcpSocketProxy = existingTcpProxy || await this.$socketProxyFactory.addTCPSocketProxy(getDeviceSocket, deviceIdentifier);
164+
const tcpSocketProxy = existingTcpProxy || await this.$socketProxyFactory.addTCPSocketProxy(getDeviceSocket, this.deviceIdentifier);
155165
if (!existingTcpProxy) {
156166
await this.openAppInspector(tcpSocketProxy.address(), debugData, debugOptions);
157167
}
@@ -162,9 +172,9 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
162172
this.$logger.info("'--chrome' is the default behavior. Use --inspector to debug iOS applications using the Safari Web Inspector.");
163173
}
164174

165-
const existingWebProxy = this.$socketProxyFactory.getWebSocketProxy(deviceIdentifier);
175+
const existingWebProxy = this.$socketProxyFactory.getWebSocketProxy(this.deviceIdentifier);
166176
const getDeviceSocket = async () => await this.device.getDebugSocket(debugData.applicationIdentifier, debugData.projectDir);
167-
const webSocketProxy = existingWebProxy || await this.$socketProxyFactory.addWebSocketProxy(getDeviceSocket, deviceIdentifier);
177+
const webSocketProxy = existingWebProxy || await this.$socketProxyFactory.addWebSocketProxy(getDeviceSocket, this.deviceIdentifier);
168178

169179
return this.getChromeDebugUrl(debugOptions, webSocketProxy.options.port);
170180
}

0 commit comments

Comments
 (0)