Skip to content

Remove ios native dependencies #2495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ interface IAndroidToolsInfoData {

interface ISocketProxyFactory {
createTCPSocketProxy(factory: () => any): any;
createWebSocketProxy(factory: () => any): any;
createWebSocketProxy(factory: () => Promise<any>): any;
}

interface IiOSNotification {
Expand All @@ -304,11 +304,12 @@ interface IiOSNotification {
}

interface IiOSNotificationService {
awaitNotification(npc: Mobile.INotificationProxyClient, notification: string, timeout: number): Promise<string>;
awaitNotification(deviceIdentifier: string, socket: number, timeout: number): Promise<string>;
postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string>;
}

interface IiOSSocketRequestExecutor {
executeLaunchRequest(device: Mobile.IiOSDevice, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise<void>;
executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise<void>;
executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void>;
}

Expand Down
14 changes: 6 additions & 8 deletions lib/device-sockets/ios/socket-proxy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export class SocketProxyFactory implements ISocketProxyFactory {
return server;
}

public createWebSocketProxy(factory: () => net.Socket): ws.Server {
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
public createWebSocketProxy(factory: () => Promise<net.Socket>): ws.Server {
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
let localPort = 8080;

Expand All @@ -80,13 +79,12 @@ export class SocketProxyFactory implements ISocketProxyFactory {

let server = new ws.Server(<any>{
port: localPort,
verifyClient: (info: any, callback: Function) => {
verifyClient: async (info: any, callback: Function) => {
this.$logger.info("Frontend client connected.");
socketFactory((_socket: any) => {
this.$logger.info("Backend socket created.");
info.req["__deviceSocket"] = _socket;
callback(true);
});
const _socket = await factory();
this.$logger.info("Backend socket created.");
info.req["__deviceSocket"] = _socket;
callback(true);
}
});
server.on("connection", (webSocket) => {
Expand Down
76 changes: 48 additions & 28 deletions lib/device-sockets/ios/socket-request-executor.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import * as iOSProxyServices from "../../common/mobile/ios/device/ios-proxy-services";
import * as constants from "../../common/constants";

export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
constructor(private $errors: IErrors,
private $injector: IInjector,
private $iOSNotification: IiOSNotification,
private $iOSNotificationService: IiOSNotificationService,
private $logger: ILogger) { }
private $logger: ILogger,
private $iosDeviceOperations: IIOSDeviceOperations) {
this.$iosDeviceOperations.setShouldDispose(false);
}

public async executeAttachRequest(device: Mobile.IiOSDevice, timeout: number, projectId: string): Promise<void> {
let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector);
const deviceIdentifier = device.deviceInfo.identifier;

const observeNotificationSockets = [
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAlreadyConnected(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE),
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE),
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachAvailable(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE)
];

let data = [this.$iOSNotification.getAlreadyConnected(projectId), this.$iOSNotification.getReadyForAttach(projectId), this.$iOSNotification.getAttachAvailable(projectId)]
.map((notification) => this.$iOSNotificationService.awaitNotification(npc, notification, timeout)),
alreadyConnected = data[0],
readyForAttach = data[1],
attachAvailable = data[2];
const observeNotificationPromises = _(observeNotificationSockets)
.uniq()
.map(s => {
return this.$iOSNotificationService.awaitNotification(deviceIdentifier, +s, timeout);
})
.value();

npc.postNotificationAndAttachForData(this.$iOSNotification.getAttachAvailabilityQuery(projectId));
// Trigger the notifications update.
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachAvailabilityQuery(projectId));

let receivedNotification: string;
try {
receivedNotification = await Promise.race([alreadyConnected, readyForAttach, attachAvailable]);
receivedNotification = await Promise.race(observeNotificationPromises);
} catch (e) {
this.$errors.failWithoutHelp(`The application ${projectId} does not appear to be running on ${device.deviceInfo.displayName} or is not built with debugging enabled.`);
}
Expand All @@ -30,37 +40,47 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
this.$errors.failWithoutHelp("A client is already connected.");
break;
case this.$iOSNotification.getAttachAvailable(projectId):
await this.executeAttachAvailable(npc, timeout, projectId);
await this.executeAttachAvailable(deviceIdentifier, projectId, timeout);
break;
case this.$iOSNotification.getReadyForAttach(projectId):
break;
default:
this.$logger.trace("Response from attach availability query:");
this.$logger.trace(receivedNotification);
this.$errors.failWithoutHelp("No notification received while executing attach request.");
}
}

public async executeLaunchRequest(device: Mobile.IiOSDevice, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise<void> {
let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector);

public async executeLaunchRequest(deviceIdentifier: string, timeout: number, readyForAttachTimeout: number, projectId: string, shouldBreak?: boolean): Promise<void> {
try {
await this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.getAppLaunching(projectId), timeout);
process.nextTick(() => {
if (shouldBreak) {
npc.postNotificationAndAttachForData(this.$iOSNotification.getWaitForDebug(projectId));
}
const appLaunchingSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAppLaunching(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
await this.$iOSNotificationService.awaitNotification(deviceIdentifier, +appLaunchingSocket, timeout);

if (shouldBreak) {
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getWaitForDebug(projectId));
}

npc.postNotificationAndAttachForData(this.$iOSNotification.getAttachRequest(projectId));
});
// We need to send the ObserveNotification ReadyForAttach before we post the AttachRequest.
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, readyForAttachTimeout);

await this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.getReadyForAttach(projectId), readyForAttachTimeout);
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId));
await readyForAttachPromise;
} catch (e) {
this.$logger.trace(`Timeout error: ${e}`);
this.$errors.failWithoutHelp("Timeout waiting for response from NativeScript runtime.");
this.$logger.trace("Launch request error:");
this.$logger.trace(e);
this.$errors.failWithoutHelp("Error while waiting for response from NativeScript runtime.");
}
}

private async executeAttachAvailable(npc: Mobile.INotificationProxyClient, timeout: number, projectId: string): Promise<void> {
process.nextTick(() => npc.postNotificationAndAttachForData(this.$iOSNotification.getAttachRequest(projectId)));
private async executeAttachAvailable(deviceIdentifier: string, projectId: string, timeout: number): Promise<void> {
try {
await this.$iOSNotificationService.awaitNotification(npc, this.$iOSNotification.getReadyForAttach(projectId), timeout);
// We should create this promise here because we need to send the ObserveNotification on the device
// before we send the PostNotification.
const readyForAttachSocket = await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getReadyForAttach(projectId), constants.IOS_OBSERVE_NOTIFICATION_COMMAND_TYPE);
const readyForAttachPromise = this.$iOSNotificationService.awaitNotification(deviceIdentifier, +readyForAttachSocket, timeout);
await this.$iOSNotificationService.postNotification(deviceIdentifier, this.$iOSNotification.getAttachRequest(projectId));
await readyForAttachPromise;
} catch (e) {
this.$errors.failWithoutHelp(`The application ${projectId} timed out when performing the socket handshake.`);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/services/ios-debug-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class IOSDebugService implements IDebugService {

private async debugBrkCore(device: Mobile.IiOSDevice, projectData: IProjectData, shouldBreak?: boolean): Promise<void> {
let timeout = this.$utils.getMilliSecondsTimeout(TIMEOUT_SECONDS);
await this.$iOSSocketRequestExecutor.executeLaunchRequest(device, timeout, timeout, projectData.projectId, shouldBreak);
await this.$iOSSocketRequestExecutor.executeLaunchRequest(device.deviceInfo.identifier, timeout, timeout, projectData.projectId, shouldBreak);
await this.wireDebuggerClient(projectData, device);
}

Expand All @@ -174,8 +174,8 @@ class IOSDebugService implements IDebugService {
}

private async wireDebuggerClient(projectData: IProjectData, device?: Mobile.IiOSDevice): Promise<void> {
let factory = () => {
let socket = device ? device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort);
const factory = async () => {
let socket = device ? await device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort);
this._sockets.push(socket);
return socket;
};
Expand Down
36 changes: 18 additions & 18 deletions lib/services/ios-notification-service.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
export class IOSNotificationService implements IiOSNotificationService {
public async awaitNotification(npc: Mobile.INotificationProxyClient, notification: string, timeout: number): Promise<string> {
return new Promise<string>((resolve, reject) => {

let timeoutToken = setTimeout(() => {
detachObserver();
reject(new Error(`Timeout receiving ${notification} notification.`));
}, timeout);
import * as constants from "../common/constants";

function notificationObserver(_notification: string) {
clearTimeout(timeoutToken);
detachObserver();
resolve(_notification);
}
export class IOSNotificationService implements IiOSNotificationService {
constructor(private $iosDeviceOperations: IIOSDeviceOperations) { }

function detachObserver() {
process.nextTick(() => npc.removeObserver(notification, notificationObserver));
}
public async awaitNotification(deviceIdentifier: string, socket: number, timeout: number): Promise<string> {
const notificationResponse = await this.$iosDeviceOperations.awaitNotificationResponse([{
deviceId: deviceIdentifier,
socket: socket,
timeout: timeout,
responseCommandType: constants.IOS_RELAY_NOTIFICATION_COMMAND_TYPE,
responsePropertyName: "Name"
}]);

npc.addObserver(notification, notificationObserver);
return _.first(notificationResponse[deviceIdentifier]).response;
}

});
public async postNotification(deviceIdentifier: string, notification: string, commandType?: string): Promise<string> {
commandType = commandType || constants.IOS_POST_NOTIFICATION_COMMAND_TYPE;
const response = await this.$iosDeviceOperations.postNotification([{ deviceId: deviceIdentifier, commandType: commandType, notificationName: notification }]);
return _.first(response[deviceIdentifier]).response;
}
}

$injector.register("iOSNotificationService", IOSNotificationService);
10 changes: 4 additions & 6 deletions lib/services/livesync/ios-device-livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ class IOSLiveSyncService implements INativeScriptDeviceLiveSyncService {
private socket: net.Socket;
private device: Mobile.IiOSDevice;

constructor(_device: Mobile.IDevice,
constructor(_device: Mobile.IiOSDevice,
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
private $iOSNotification: IiOSNotification,
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
private $injector: IInjector,
private $logger: ILogger,
private $options: IOptions,
private $iOSDebugService: IDebugService,
private $fs: IFileSystem,
private $liveSyncProvider: ILiveSyncProvider,
private $processService: IProcessService) {

this.device = <Mobile.IiOSDevice>(_device);
this.device = _device;
}

public get debugService(): IDebugService {
Expand All @@ -49,7 +48,7 @@ class IOSLiveSyncService implements INativeScriptDeviceLiveSyncService {
} else {
let timeout = 9000;
await this.$iOSSocketRequestExecutor.executeAttachRequest(this.device, timeout, projectId);
this.socket = this.device.connectToPort(IOSLiveSyncService.BACKEND_PORT);
this.socket = await this.device.connectToPort(IOSLiveSyncService.BACKEND_PORT);
}

this.attachEventHandlers();
Expand Down Expand Up @@ -88,8 +87,7 @@ class IOSLiveSyncService implements INativeScriptDeviceLiveSyncService {
}

private async restartApplication(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
let projectData: IProjectData = this.$injector.resolve("projectData");
return this.device.applicationManager.restartApplication(deviceAppData.appIdentifier, projectData.projectName);
return this.device.applicationManager.restartApplication(deviceAppData.appIdentifier);
}

private async reloadPage(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<void> {
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
"colors": "1.1.2",
"email-validator": "1.0.4",
"esprima": "2.7.0",
"ffi": "https://github.com/icenium/node-ffi/tarball/v2.0.0.5",
"filesize": "3.1.2",
"gaze": "1.1.0",
"glob": "^7.0.3",
"iconv-lite": "0.4.11",
"inquirer": "0.9.0",
"ios-device-lib": "~0.3.0",
"ios-mobileprovision-finder": "1.0.9",
"ios-sim-portable": "~2.0.0",
"lockfile": "1.0.1",
Expand All @@ -62,8 +62,6 @@
"plistlib": "0.2.1",
"progress-stream": "1.1.1",
"properties-parser": "0.2.3",
"ref": "https://github.com/icenium/ref/tarball/v1.3.2.3",
"ref-struct": "https://github.com/telerik/ref-struct/tarball/v1.0.2.5",
"semver": "5.0.1",
"shelljs": "0.7.6",
"source-map": "0.5.6",
Expand Down
1 change: 1 addition & 0 deletions test/ios-project-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function createTestInjector(projectPath: string, projectName: string): IInjector
testInjector.register("utils", Utils);
testInjector.register("iTunesValidator", {});
testInjector.register("xcprojService", {});
testInjector.register("iosDeviceOperations", {});
testInjector.register("pluginVariablesService", PluginVariablesService);
testInjector.register("pluginVariablesHelper", PluginVariablesHelper);
testInjector.register("androidProcessService", {});
Expand Down