Skip to content

Commit e0f81b6

Browse files
author
Fatme
authored
Merge pull request #3540 from NativeScript/fatme/ios-debugger-port
Get debugger port from device logs
2 parents ae30c84 + 7dd8689 commit e0f81b6

23 files changed

+587
-96
lines changed

lib/bootstrap.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,5 @@ $injector.requireCommand("resources|generate|splashes", "./commands/generate-ass
167167
$injector.requirePublic("assetsGenerationService", "./services/assets-generation/assets-generation-service");
168168

169169
$injector.require("filesHashService", "./services/files-hash-service");
170+
$injector.require("iOSLogParserService", "./services/ios-log-parser-service");
171+
$injector.require("iOSDebuggerPortService", "./services/ios-debugger-port-service");

lib/commands/debug.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ export class DebugIOSCommand implements ICommand {
143143
private $injector: IInjector,
144144
private $projectData: IProjectData,
145145
private $platformsData: IPlatformsData,
146-
$iosDeviceOperations: IIOSDeviceOperations) {
146+
$iosDeviceOperations: IIOSDeviceOperations,
147+
$iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider) {
147148
this.$projectData.initializeProjectData();
148149
// Do not dispose ios-device-lib, so the process will remain alive and the debug application (NativeScript Inspector or Chrome DevTools) will be able to connect to the socket.
149150
// In case we dispose ios-device-lib, the socket will be closed and the code will fail when the debug application tries to read/send data to device socket.
150151
// That's why the `$ tns debug ios --justlaunch` command will not release the terminal.
151152
// In case we do not set it to false, the dispose will be called once the command finishes its execution, which will prevent the debugging.
152153
$iosDeviceOperations.setShouldDispose(false);
154+
$iOSSimulatorLogProvider.setShouldDispose(false);
153155
}
154156

155157
public execute(args: string[]): Promise<void> {

lib/declarations.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ interface ISocketProxyFactory extends NodeJS.EventEmitter {
645645

646646
interface IiOSNotification {
647647
getWaitForDebug(projectId: string): string;
648-
getAttachRequest(projectId: string): string;
648+
getAttachRequest(projectId: string, deviceId: string): string;
649649
getAppLaunching(projectId: string): string;
650650
getReadyForAttach(projectId: string): string;
651651
getAttachAvailabilityQuery(projectId: string): string;

lib/definitions/debug.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ interface IDebugOptions {
8787
* Defines if the iOS App Inspector should be used instead of providing URL to debug the application with Chrome DevTools
8888
*/
8989
inspector?: boolean;
90+
/**
91+
* Defines if should print all availableDevices
92+
*/
93+
availableDevices?: boolean;
9094
}
9195

9296
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
interface IIOSDebuggerPortInputData {
2+
deviceId: string;
3+
appId: string;
4+
}
5+
6+
interface IIOSDebuggerPortData extends IIOSDebuggerPortInputData {
7+
port: number;
8+
}
9+
10+
interface IIOSDebuggerPortStoredData {
11+
port: number;
12+
timer?: NodeJS.Timer;
13+
}
14+
15+
interface IIOSDebuggerPortService {
16+
/**
17+
* Gets iOS debugger port for specified deviceId and appId
18+
* @param {IIOSDebuggerPortInputData} data - Describes deviceId and appId
19+
*/
20+
getPort(data: IIOSDebuggerPortInputData): Promise<number>;
21+
/**
22+
* Attaches on DEBUGGER_PORT_FOUND event and STARTING_IOS_APPLICATION events
23+
* In case when DEBUGGER_PORT_FOUND event is emitted, stores the port and clears the timeout if such.
24+
* In case when STARTING_IOS_APPLICATION event is emiited, sets the port to null and add timeout for 5000 miliseconds which checks if port is null and prints a warning.
25+
* @param {Mobile.IDevice} device - Describes the device which logs should be parsed.
26+
*/
27+
attachToDebuggerPortFoundEvent(device: Mobile.IDevice): void;
28+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
interface IIOSLogParserService extends NodeJS.EventEmitter {
2+
/**
3+
* Starts looking for debugger port. Attaches on device logs and processes them. In case when port is found, DEBUGGER_PORT_FOUND event is emitted.
4+
* @param {Mobile.IDevice} device - Describes the device which logs will be processed.
5+
*/
6+
startParsingLog(device: Mobile.IDevice): void;
7+
}

lib/definitions/project.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ interface IProjectTemplatesService {
218218

219219
interface IPlatformProjectServiceBase {
220220
getPluginPlatformsFolderPath(pluginData: IPluginData, platform: string): string;
221+
getFrameworkVersion(projectData: IProjectData): string;
221222
}
222223

223224
interface IBuildForDevice {
@@ -270,7 +271,7 @@ interface ILocalBuildService {
270271

271272
interface ICleanNativeAppData extends IProjectDir, IPlatform { }
272273

273-
interface IPlatformProjectService extends NodeJS.EventEmitter {
274+
interface IPlatformProjectService extends NodeJS.EventEmitter, IPlatformProjectServiceBase {
274275
getPlatformData(projectData: IProjectData): IPlatformData;
275276
validate(projectData: IProjectData): Promise<void>;
276277
createProject(frameworkDir: string, frameworkVersion: string, projectData: IProjectData, config: ICreateProjectOptions): Promise<void>;

lib/device-sockets/ios/notification.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
export class IOSNotification implements IiOSNotification {
1+
import { EventEmitter } from "events";
2+
import { ATTACH_REQUEST_EVENT_NAME } from "../../common/constants";
3+
4+
export class IOSNotification extends EventEmitter implements IiOSNotification {
25
private static WAIT_FOR_DEBUG_NOTIFICATION_NAME = "WaitForDebugger";
36
private static ATTACH_REQUEST_NOTIFICATION_NAME = "AttachRequest";
47
private static APP_LAUNCHING_NOTIFICATION_NAME = "AppLaunching";
@@ -11,8 +14,9 @@ export class IOSNotification implements IiOSNotification {
1114
return this.formatNotification(IOSNotification.WAIT_FOR_DEBUG_NOTIFICATION_NAME, projectId);
1215
}
1316

14-
public getAttachRequest(projectId: string): string {
15-
return this.formatNotification(IOSNotification.ATTACH_REQUEST_NOTIFICATION_NAME, projectId);
17+
public getAttachRequest(appId: string, deviceId: string): string {
18+
this.emit(ATTACH_REQUEST_EVENT_NAME, { deviceId, appId });
19+
return this.formatNotification(IOSNotification.ATTACH_REQUEST_NOTIFICATION_NAME, appId);
1620
}
1721

1822
public getAppLaunching(projectId: string): string {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
6161
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
6262
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, readyForAttachTimeout);
6363

64-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId));
64+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId, deviceIdentifier));
6565
await readyForAttachPromise;
6666
} catch (e) {
6767
this.$logger.trace("Launch request error:");
@@ -76,7 +76,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
7676
// before we send the PostNotification.
7777
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
7878
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, timeout);
79-
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId));
79+
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId, deviceIdentifier));
8080
await readyForAttachPromise;
8181
} catch (e) {
8282
this.$errors.failWithoutHelp(`The application ${projectId} timed out when performing the socket handshake.`);

0 commit comments

Comments
 (0)