Skip to content

Commit 9bea8ac

Browse files
committed
refactor: remove most of the emulator specific logic from the ios-debug-service
1 parent 6d6bce7 commit 9bea8ac

File tree

8 files changed

+41
-86
lines changed

8 files changed

+41
-86
lines changed

lib/common/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,7 @@ export class AndroidVirtualDevice {
156156
static GENYMOTION_DEFAULT_STDERR_STRING = "Logging activities to file";
157157

158158
static UNABLE_TO_START_EMULATOR_MESSAGE = "Cannot run the app in the selected native emulator. Try to restart the adb server by running the `adb kill-server` command in the Command Prompt, or increase the allocated RAM of the virtual device through the Android Virtual Device manager. NativeScript CLI users can try to increase the timeout of the operation by adding the `--timeout` flag.";
159+
159160
}
161+
162+
export const SOCKET_CONNECTION_TIMEOUT_MS = 30000;

lib/common/definitions/mobile.d.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,6 @@ declare module Mobile {
116116
openDeviceLogStream(options?: IiOSLogStreamOptions): Promise<void>;
117117
}
118118

119-
interface IiOSDeviceSocketsService {
120-
getSocket(deviceId: string): any;
121-
addSocket(deviceId: string, socket: any): void;
122-
}
123-
124119
interface IAndroidDevice extends IDevice {
125120
adb: Mobile.IDeviceAndroidDebugBridge;
126121
init(): Promise<void>;

lib/common/mobile/ios/device/ios-device.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as commonConstants from "../../../constants";
44
import * as constants from "../../../../constants";
55
import * as net from "net";
66
import { cache } from "../../../decorators";
7+
import * as helpers from "../../../../common/helpers";
78

89
export class IOSDevice implements Mobile.IiOSDevice {
910
public applicationManager: Mobile.IDeviceApplicationManager;
@@ -96,10 +97,15 @@ export class IOSDevice implements Mobile.IiOSDevice {
9697
}
9798

9899
const deviceId = this.deviceInfo.identifier;
99-
const deviceResponse = _.first((await this.$iosDeviceOperations.connectToPort([{ deviceId: deviceId, port: port }]))[deviceId]);
100+
this.socket = await helpers.connectEventuallyUntilTimeout(
101+
async () => {
102+
const deviceResponse = _.first((await this.$iosDeviceOperations.connectToPort([{ deviceId: deviceId, port: port }]))[deviceId]);
103+
const _socket = new net.Socket();
104+
_socket.connect(deviceResponse.port, deviceResponse.host);
105+
return _socket;
106+
},
107+
commonConstants.SOCKET_CONNECTION_TIMEOUT_MS);
100108

101-
this.socket = new net.Socket();
102-
this.socket.connect(deviceResponse.port, deviceResponse.host);
103109
this.socket.on("close", () => {
104110
this.socket = null;
105111
});

lib/common/mobile/ios/simulator/ios-simulator-device.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { cache } from "../../../decorators";
66
import * as helpers from "../../../../common/helpers";
77

88
export class IOSSimulator implements Mobile.IiOSDevice {
9-
private static socketConnectionTimeout = 30000;
109
private _applicationManager: Mobile.IDeviceApplicationManager;
1110
private _fileSystem: Mobile.IDeviceFileSystem;
1211
private socket: net.Socket;
@@ -45,7 +44,7 @@ export class IOSSimulator implements Mobile.IiOSDevice {
4544
try {
4645
this.socket = await helpers.connectEventuallyUntilTimeout(
4746
async () => { return this.$iOSEmulatorServices.connectToPort({ port }) },
48-
IOSSimulator.socketConnectionTimeout);
47+
constants.SOCKET_CONNECTION_TIMEOUT_MS);
4948
} catch (e) {
5049
this.$logger.warn(e);
5150
}

lib/definitions/debug.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ interface IDebugOptions {
3737
*/
3838
stop?: boolean;
3939

40+
// TODO: remove
4041
/**
4142
* Defines if debug process is for emulator (not for real device).
4243
*/

lib/device-sockets/ios/socket-request-executor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
1919
const observeNotificationPromises = _(observeNotificationSockets)
2020
.uniq()
2121
.map(s => {
22-
return this.$iOSNotificationService.awaitNotification(deviceIdentifier, +s, timeout);
22+
return this.$iOSNotificationService.awaitNotification(deviceIdentifier, s, timeout);
2323
})
2424
.value();
2525

lib/services/ios-debug-service.ts

Lines changed: 24 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as iOSDevice from "../common/mobile/ios/device/ios-device";
21
import * as path from "path";
32
import * as log4js from "log4js";
43
import { ChildProcess } from "child_process";
54
import { DebugServiceBase } from "./debug-service-base";
65
import { IOS_LOG_PREDICATE } from "../common/constants";
7-
import { CONNECTION_ERROR_EVENT_NAME, AWAIT_NOTIFICATION_TIMEOUT_SECONDS } from "../constants";
6+
import { CONNECTION_ERROR_EVENT_NAME } from "../constants";
87
import { getPidFromiOSSimulatorLogs } from "../common/helpers";
98
const inspectorAppName = "NativeScript Inspector.app";
109
const inspectorNpmPackageName = "tns-ios-inspector";
@@ -22,7 +21,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
2221
private $logger: ILogger,
2322
private $errors: IErrors,
2423
private $packageInstallationManager: IPackageInstallationManager,
25-
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
24+
private $iOSDebuggerPortService: IIOSDebuggerPortService,
2625
private $processService: IProcessService,
2726
private $socketProxyFactory: ISocketProxyFactory,
2827
private $projectDataService: IProjectDataService,
@@ -42,32 +41,28 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
4241
this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options.");
4342
}
4443

45-
if (this.$devicesService.isOnlyiOSSimultorRunning() || this.$devicesService.deviceCount === 0) {
46-
debugOptions.emulator = true;
47-
}
48-
4944
await this.startDeviceLogProcess(debugData, debugOptions);
45+
await this.$iOSDebuggerPortService.attachToDebuggerPortFoundEvent(this.device, debugData, debugOptions);
5046

51-
if (debugOptions.emulator) {
52-
if (debugOptions.start) {
53-
return this.emulatorStart(debugData, debugOptions);
47+
if (!debugOptions.start) { // not attach
48+
if (this.device.isEmulator) {
49+
await this.startAppOnSimulator(debugData, debugOptions);
5450
} else {
55-
return this.emulatorDebugBrk(debugData, debugOptions);
56-
}
57-
} else {
58-
if (debugOptions.start) {
59-
return this.deviceStart(debugData, debugOptions);
60-
} else {
61-
return this.deviceDebugBrk(debugData, debugOptions);
51+
await this.startAppOnDevice(debugData, debugOptions);
6252
}
6353
}
54+
55+
return this.wireDebuggerClient(debugData, debugOptions);
6456
}
6557

6658
public async debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
67-
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
68-
// TODO: this.device
69-
const action = async (device: Mobile.IiOSDevice) => device.isEmulator ? await this.emulatorDebugBrk(debugData, debugOptions) : await this.debugBrkCore(debugData, debugOptions);
70-
await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
59+
if (this.device.isEmulator) {
60+
await this.startAppOnSimulator(debugData, debugOptions);
61+
} else {
62+
await this.startAppOnDevice(debugData, debugOptions);
63+
}
64+
65+
await this.wireDebuggerClient(debugData, debugOptions);
7166
}
7267

7368
public async debugStop(): Promise<void> {
@@ -117,7 +112,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
117112
}
118113
}
119114

120-
private async emulatorDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
115+
private async startAppOnSimulator(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
121116
const args = debugOptions.debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start";
122117
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
123118
waitForDebugger: true,
@@ -130,67 +125,23 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
130125
timeout: debugOptions.timeout,
131126
sdk: debugOptions.sdk
132127
});
133-
134128
const pid = getPidFromiOSSimulatorLogs(debugData.applicationIdentifier, launchResult);
135129
this._lldbProcess = this.$childProcess.spawn("lldb", ["-p", pid]);
136130
if (log4js.levels.TRACE.isGreaterThanOrEqualTo(this.$logger.getLevel())) {
137131
this._lldbProcess.stdout.pipe(process.stdout);
138132
}
139133
this._lldbProcess.stderr.pipe(process.stderr);
140134
this._lldbProcess.stdin.write("process continue\n");
141-
142-
const debugUrl = await this.wireDebuggerClient(debugData, debugOptions);
143-
return debugUrl;
144135
}
145136

146-
private async emulatorStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
147-
const debugUrl = await this.wireDebuggerClient(debugData, debugOptions);
148-
return debugUrl;
149-
}
150-
151-
private async deviceDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
152-
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
153-
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
154-
const action = async (device: iOSDevice.IOSDevice): Promise<string> => {
155-
if (device.isEmulator) {
156-
return await this.emulatorDebugBrk(debugData, debugOptions);
157-
}
158-
159-
const runOptions: IRunPlatformOptions = {
160-
device: debugData.deviceIdentifier,
161-
emulator: debugOptions.emulator,
162-
justlaunch: debugOptions.justlaunch
163-
};
164-
165-
const promisesResults = await Promise.all<any>([
166-
this.$platformService.startApplication(this.platform, runOptions, { appId: debugData.applicationIdentifier, projectName: projectData.projectName }),
167-
this.debugBrkCore(debugData, debugOptions)
168-
]);
169-
170-
return _.last(promisesResults);
137+
private async startAppOnDevice(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
138+
const runOptions: IRunPlatformOptions = {
139+
device: debugData.deviceIdentifier,
140+
emulator: this.device.isEmulator,
141+
justlaunch: debugOptions.justlaunch
171142
};
172-
173-
// TODO: this.device
174-
const deviceActionResult = await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
175-
return deviceActionResult[0].result;
176-
}
177-
178-
private async debugBrkCore(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
179-
await this.$iOSSocketRequestExecutor.executeLaunchRequest(this.device.deviceInfo.identifier, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, AWAIT_NOTIFICATION_TIMEOUT_SECONDS, debugData.applicationIdentifier, debugOptions);
180-
const debugUrl = await this.wireDebuggerClient(debugData, debugOptions);
181-
return debugUrl;
182-
}
183-
184-
private async deviceStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
185-
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
186-
const action = async (device: Mobile.IiOSDevice) => device.isEmulator ? await this.emulatorStart(debugData, debugOptions) : await this.deviceStartCore(debugData, debugOptions);
187-
const deviceActionResult = await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
188-
return deviceActionResult[0].result;
189-
}
190-
191-
private async deviceStartCore(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
192-
const debugUrl = await this.wireDebuggerClient(debugData, debugOptions);
193-
return debugUrl;
143+
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
144+
await this.$platformService.startApplication(this.platform, runOptions, { appId: debugData.applicationIdentifier, projectName: projectData.projectName });
194145
}
195146

196147
private async wireDebuggerClient(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {

test/services/ios-debug-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ class IOSDebugServiceInheritor extends IOSDebugService {
1414
$logger: ILogger,
1515
$errors: IErrors,
1616
$packageInstallationManager: IPackageInstallationManager,
17-
$iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
17+
$iOSDebuggerPortService: IIOSDebuggerPortService,
1818
$processService: IProcessService,
1919
$socketProxyFactory: ISocketProxyFactory,
2020
$projectDataService: IProjectDataService,
2121
$deviceLogProvider: Mobile.IDeviceLogProvider) {
2222
super(<any>{}, $devicesService, $platformService, $iOSEmulatorServices, $childProcess, $hostInfo, $logger, $errors,
23-
$packageInstallationManager, $iOSSocketRequestExecutor,
23+
$packageInstallationManager, $iOSDebuggerPortService,
2424
$processService, $socketProxyFactory, $projectDataService, $deviceLogProvider);
2525
}
2626

0 commit comments

Comments
 (0)