-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
553 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule common
updated
10 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
interface IIOSDebuggerPortInputData { | ||
deviceId: string; | ||
appId: string; | ||
} | ||
|
||
interface IIOSDebuggerPortData extends IIOSDebuggerPortInputData { | ||
port: number; | ||
} | ||
|
||
interface IIOSDebuggerPortStoredData { | ||
port: number; | ||
timer?: NodeJS.Timer; | ||
} | ||
|
||
interface IIOSDebuggerPortService { | ||
/** | ||
* Gets iOS debugger port for specified deviceId and appId | ||
* @param {IIOSDebuggerPortInputData} data - Describes deviceId and appId | ||
*/ | ||
getPort(data: IIOSDebuggerPortInputData): Promise<number>; | ||
/** | ||
* Attaches on DEBUGGER_PORT_FOUND event and STARTING_IOS_APPLICATION events | ||
* In case when DEBUGGER_PORT_FOUND event is emitted, stores the port and clears the timeout if such. | ||
* 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. | ||
* @param {Mobile.IDevice} device - Describes the device which logs should be parsed. | ||
*/ | ||
attachToDebuggerPortFoundEvent(device: Mobile.IDevice): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
interface IIOSLogParserService extends NodeJS.EventEmitter { | ||
/** | ||
* Starts looking for debugger port. Attaches on device logs and processes them. In case when port is found, DEBUGGER_PORT_FOUND event is emitted. | ||
* @param {Mobile.IDevice} device - Describes the device which logs will be processed. | ||
*/ | ||
startLookingForDebuggerPort(device: Mobile.IDevice): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { DEBUGGER_PORT_FOUND_EVENT_NAME, STARTING_IOS_APPLICATION_EVENT_NAME } from "../common/constants"; | ||
import { cache } from "../common/decorators"; | ||
import * as semver from "semver"; | ||
|
||
export class IOSDebuggerPortService implements IIOSDebuggerPortService { | ||
private mapDebuggerPortData: IDictionary<IIOSDebuggerPortStoredData> = {}; | ||
private static DEFAULT_PORT = 18181; | ||
private static MIN_REQUIRED_FRAMEWORK_VERSION = "4.0.0"; | ||
|
||
constructor(private $iOSLogParserService: IIOSLogParserService, | ||
private $iOSProjectService: IPlatformProjectService, | ||
private $logger: ILogger, | ||
private $projectData: IProjectData) { } | ||
|
||
public getPort(data: IIOSDebuggerPortInputData): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
if (!this.canStartLookingForDebuggerPort()) { | ||
return IOSDebuggerPortService.DEFAULT_PORT; | ||
} | ||
|
||
const key = `${data.deviceId}${data.appId}`; | ||
let retryCount: number = 10; | ||
|
||
const interval = setInterval(() => { | ||
let port = this.getPortByKey(key); | ||
if (port || retryCount === 0) { | ||
clearInterval(interval); | ||
resolve(port); | ||
} else { | ||
port = this.getPortByKey(key); | ||
retryCount--; | ||
} | ||
}, 500); | ||
}); | ||
} | ||
|
||
public attachToDebuggerPortFoundEvent(device: Mobile.IDevice): void { | ||
if (!this.canStartLookingForDebuggerPort()) { | ||
return; | ||
} | ||
|
||
this.attachToDebuggerPortFoundEventCore(); | ||
this.attachToStartingApplicationEvent(device); | ||
|
||
this.$iOSLogParserService.startLookingForDebuggerPort(device); | ||
} | ||
|
||
private canStartLookingForDebuggerPort(): boolean { | ||
const frameworkVersion = this.$iOSProjectService.getFrameworkVersion(this.$projectData); | ||
return semver.gte(frameworkVersion, IOSDebuggerPortService.MIN_REQUIRED_FRAMEWORK_VERSION); | ||
} | ||
|
||
@cache() | ||
private attachToDebuggerPortFoundEventCore(): void { | ||
this.$iOSLogParserService.on(DEBUGGER_PORT_FOUND_EVENT_NAME, (data: IIOSDebuggerPortData) => { | ||
this.$logger.trace(DEBUGGER_PORT_FOUND_EVENT_NAME, data); | ||
this.setData(data, { port: data.port }); | ||
this.clearTimeout(data); | ||
}); | ||
} | ||
|
||
@cache() | ||
private attachToStartingApplicationEvent(device: Mobile.IDevice): void { | ||
device.applicationManager.on(STARTING_IOS_APPLICATION_EVENT_NAME, (data: IIOSDebuggerPortData) => { | ||
this.$logger.trace(STARTING_IOS_APPLICATION_EVENT_NAME, data); | ||
const timer = setTimeout(() => { | ||
this.clearTimeout(data); | ||
if (!this.getPortByKey(`${data.deviceId}${data.appId}`)) { | ||
this.$logger.warn("NativeScript debugger was not able to get inspector socket port."); | ||
} | ||
}, 5000); | ||
|
||
this.setData(data, { port: null, timer }); | ||
}); | ||
} | ||
|
||
private getPortByKey(key: string): number { | ||
if (this.mapDebuggerPortData[key]) { | ||
return this.mapDebuggerPortData[key].port; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private setData(data: IIOSDebuggerPortData, storedData: IIOSDebuggerPortStoredData): void { | ||
const key = `${data.deviceId}${data.appId}`; | ||
|
||
if (!this.mapDebuggerPortData[key]) { | ||
this.mapDebuggerPortData[key] = <any>{}; | ||
} | ||
|
||
this.mapDebuggerPortData[key].port = storedData.port; | ||
this.mapDebuggerPortData[key].timer = storedData.timer; | ||
} | ||
|
||
private clearTimeout(data: IIOSDebuggerPortData): void { | ||
const storedData = this.mapDebuggerPortData[`${data.deviceId}${data.appId}`]; | ||
if (storedData && storedData.timer) { | ||
clearTimeout(storedData.timer); | ||
} | ||
} | ||
} | ||
$injector.register("iOSDebuggerPortService", IOSDebuggerPortService); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { DEBUGGER_PORT_FOUND_EVENT_NAME, DEVICE_LOG_EVENT_NAME } from "../common/constants"; | ||
import { cache } from "../common/decorators"; | ||
import { EventEmitter } from "events"; | ||
|
||
export class IOSLogParserService extends EventEmitter implements IIOSLogParserService { | ||
private static MESSAGE_REGEX = /NativeScript debugger has opened inspector socket on port (\d+?) for (.*)[.]/; | ||
|
||
constructor(private $deviceLogProvider: Mobile.IDeviceLogProvider, | ||
private $iosDeviceOperations: IIOSDeviceOperations, | ||
private $iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider, | ||
private $logger: ILogger, | ||
private $projectData: IProjectData) { | ||
super(); | ||
} | ||
|
||
public startLookingForDebuggerPort(device: Mobile.IDevice): void { | ||
this.$deviceLogProvider.setProjectNameForDevice(device.deviceInfo.identifier, this.$projectData.projectName); | ||
|
||
this.startLookingForDebuggerPortCore(device); | ||
this.startLogProcess(device); | ||
} | ||
|
||
@cache() | ||
private startLookingForDebuggerPortCore(device: Mobile.IDevice): void { | ||
const logProvider = device.isEmulator ? this.$iOSSimulatorLogProvider : this.$iosDeviceOperations; | ||
logProvider.on(DEVICE_LOG_EVENT_NAME, (response: IOSDeviceLib.IDeviceLogData) => this.processDeviceLogResponse(response)); | ||
} | ||
|
||
private processDeviceLogResponse(response: IOSDeviceLib.IDeviceLogData) { | ||
const matches = IOSLogParserService.MESSAGE_REGEX.exec(response.message); | ||
if (matches) { | ||
const data = { | ||
port: parseInt(matches[1]), | ||
appId: matches[2], | ||
deviceId: response.deviceId | ||
}; | ||
this.$logger.trace(`Emitting ${DEBUGGER_PORT_FOUND_EVENT_NAME} event`, data); | ||
this.emit(DEBUGGER_PORT_FOUND_EVENT_NAME, data); | ||
} | ||
} | ||
|
||
private startLogProcess(device: Mobile.IDevice): void { | ||
if (device.isEmulator) { | ||
return this.$iOSSimulatorLogProvider.startNewMutedLogProcess(device.deviceInfo.identifier); | ||
} | ||
|
||
return this.$iosDeviceOperations.startDeviceLog(device.deviceInfo.identifier); | ||
} | ||
} | ||
$injector.register("iOSLogParserService", IOSLogParserService); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.