Skip to content

Add debug method to the public API #2677

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 2 commits into from
Apr 7, 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
1 change: 1 addition & 0 deletions lib/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ $injector.require("tnsModulesService", "./services/tns-modules-service");
$injector.require("platformsData", "./platforms-data");
$injector.require("platformService", "./services/platform-service");

$injector.require("debugDataService", "./services/debug-data-service");
$injector.require("iOSDebugService", "./services/ios-debug-service");
$injector.require("androidDebugService", "./services/android-debug-service");

Expand Down
38 changes: 27 additions & 11 deletions lib/commands/debug.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
export class DebugPlatformCommand implements ICommand {
import { EOL } from "os";

export abstract class DebugPlatformCommand implements ICommand {
public allowedParameters: ICommandParameter[] = [];

constructor(private debugService: IDebugService,
constructor(private debugService: IPlatformDebugService,
private $devicesService: Mobile.IDevicesService,
private $injector: IInjector,
private $logger: ILogger,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $config: IConfiguration,
private $usbLiveSyncService: ILiveSyncService,
private $debugDataService: IDebugDataService,
protected $platformService: IPlatformService,
protected $projectData: IProjectData,
protected $options: IOptions,
protected $platformsData: IPlatformsData) {
this.$projectData.initializeProjectData();
}
this.$projectData.initializeProjectData();
}

public async execute(args: string[]): Promise<void> {
const debugOptions = this.$options;
const deployOptions: IDeployPlatformOptions = {
clean: this.$options.clean,
device: this.$options.device,
Expand All @@ -29,8 +33,12 @@

const buildConfig: IBuildConfig = _.merge({ buildForDevice: this.$options.forDevice }, deployOptions);

const debugData = this.$debugDataService.createDebugData(this.debugService, this.$options, buildConfig);

await this.$platformService.trackProjectType(this.$projectData);

if (this.$options.start) {
return this.debugService.debug(this.$projectData, buildConfig);
return this.printDebugInformation(await this.debugService.debug(debugData, debugOptions));
}

const appFilesUpdaterOptions: IAppFilesUpdaterOptions = { bundle: this.$options.bundle, release: this.$options.release };
Expand All @@ -49,8 +57,9 @@

await deviceAppData.device.applicationManager.stopApplication(applicationId);

await this.debugService.debug(this.$projectData, buildConfig);
this.printDebugInformation(await this.debugService.debug(debugData, debugOptions));
};

return this.$usbLiveSyncService.liveSync(this.$devicesService.platform, this.$projectData, applicationReloadAction);
}

Expand All @@ -70,22 +79,29 @@

return true;
}

private printDebugInformation(information: string[]): void {
_.each(information, i => {
this.$logger.info(`To start debugging, open the following URL in Chrome:${EOL}${i}${EOL}`.cyan);
});
}
}

export class DebugIOSCommand extends DebugPlatformCommand {
constructor($iOSDebugService: IDebugService,
constructor($iOSDebugService: IPlatformDebugService,
$devicesService: Mobile.IDevicesService,
$injector: IInjector,
$logger: ILogger,
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
$config: IConfiguration,
$usbLiveSyncService: ILiveSyncService,
$debugDataService: IDebugDataService,
$platformService: IPlatformService,
$options: IOptions,
$projectData: IProjectData,
$platformsData: IPlatformsData,
$iosDeviceOperations: IIOSDeviceOperations) {
super($iOSDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $projectData, $options, $platformsData);
super($iOSDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $debugDataService, $platformService, $projectData, $options, $platformsData);
$iosDeviceOperations.setShouldDispose(this.$options.justlaunch);
}

Expand All @@ -97,19 +113,19 @@ export class DebugIOSCommand extends DebugPlatformCommand {
$injector.registerCommand("debug|ios", DebugIOSCommand);

export class DebugAndroidCommand extends DebugPlatformCommand {
constructor($androidDebugService: IDebugService,
constructor($androidDebugService: IPlatformDebugService,
$devicesService: Mobile.IDevicesService,
$injector: IInjector,
$logger: ILogger,
$devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
$config: IConfiguration,
$usbLiveSyncService: ILiveSyncService,
$debugDataService: IDebugDataService,
$platformService: IPlatformService,
$options: IOptions,
$projectData: IProjectData,
$platformsData: IPlatformsData) {

super($androidDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $platformService, $projectData, $options, $platformsData);
super($androidDebugService, $devicesService, $injector, $logger, $devicePlatformsConstants, $config, $usbLiveSyncService, $debugDataService, $platformService, $projectData, $options, $platformsData);
}

public async canExecute(args: string[]): Promise<boolean> {
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export class RunCommandBase {
justlaunch: this.$options.justlaunch,
};

return this.$platformService.startApplication(args[0], deployOpts, this.$projectData);
await this.$platformService.startApplication(args[0], deployOpts, this.$projectData.projectId);
return this.$platformService.trackProjectType(this.$projectData);
}

return this.$usbLiveSyncService.liveSync(args[0], this.$projectData);
Expand Down
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ export const ItunesConnectApplicationTypes = new ItunesConnectApplicationTypesCl
export const ANGULAR_NAME = "angular";
export const TYPESCRIPT_NAME = "typescript";
export const BUILD_OUTPUT_EVENT_NAME = "buildOutput";
export const CONNECTION_ERROR_EVENT_NAME = "connectionError";
export const VERSION_STRING = "version";
6 changes: 3 additions & 3 deletions lib/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ interface IAndroidToolsInfoData {
generateTypings: boolean;
}

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

interface IiOSNotification {
Expand Down
33 changes: 29 additions & 4 deletions lib/definitions/debug.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
interface IDebugService {
debug(projectData: IProjectData, buildConfig: IBuildConfig): Promise<void>;
debugStart(projectData: IProjectData, buildConfig: IBuildConfig): Promise<void>;
interface IDebugData {
deviceIdentifier: string;
applicationIdentifier: string;
pathToAppPackage: string;
projectName?: string;
projectDir?: string;
}

interface IDebugOptions {
chrome?: boolean;
start?: boolean;
stop?: boolean;
emulator?: boolean;
debugBrk?: boolean;
client?: boolean;
justlaunch?: boolean;
}

interface IDebugDataService {
createDebugData(debugService: IPlatformDebugService, options: IOptions, buildConfig: IBuildConfig): IDebugData;
}

interface IDebugService extends NodeJS.EventEmitter {
debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string[]>;
}

interface IPlatformDebugService extends IDebugService {
debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void>;
debugStop(): Promise<void>
platform: string;
}
}
2 changes: 1 addition & 1 deletion lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ interface IPlatformService extends NodeJS.EventEmitter {
* @param {IProjectData} projectData DTO with information about the project.
* @returns {void}
*/
startApplication(platform: string, runOptions: IRunPlatformOptions, projectData: IProjectData): Promise<void>;
startApplication(platform: string, runOptions: IRunPlatformOptions, projectId: string): Promise<void>;

/**
* The emulate command. In addition to `run --emulator` command, it handles the `--available-devices` option to show the available devices.
Expand Down
28 changes: 21 additions & 7 deletions lib/device-sockets/ios/socket-proxy-factory.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { EventEmitter } from "events";
import { CONNECTION_ERROR_EVENT_NAME } from "../../constants";
import { PacketStream } from "./packet-stream";
import * as net from "net";
import * as ws from "ws";
import temp = require("temp");

export class SocketProxyFactory implements ISocketProxyFactory {
export class SocketProxyFactory extends EventEmitter implements ISocketProxyFactory {
constructor(private $logger: ILogger,
private $errors: IErrors,
private $config: IConfiguration,
private $options: IOptions) { }
private $options: IOptions,
private $net: INet) {
super();
}

public createTCPSocketProxy(factory: () => Promise<net.Socket>): any {
public createTCPSocketProxy(factory: () => Promise<net.Socket>): net.Server {
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");

let server = net.createServer({
Expand All @@ -25,7 +31,7 @@ export class SocketProxyFactory implements ISocketProxyFactory {
}
});

const backendSocket: net.Socket = await factory();
const backendSocket = await factory();
this.$logger.info("Backend socket created.");

backendSocket.on("end", () => {
Expand Down Expand Up @@ -62,9 +68,9 @@ export class SocketProxyFactory implements ISocketProxyFactory {
return server;
}

public createWebSocketProxy(factory: () => Promise<net.Socket>): ws.Server {
public async createWebSocketProxy(factory: () => Promise<net.Socket>): Promise<ws.Server> {
// NOTE: We will try to provide command line options to select ports, at least on the localhost.
let localPort = 8080;
const localPort = await this.$net.getAvailablePortInRange(8080);

this.$logger.info("\nSetting up debugger proxy...\nPress Ctrl + C to terminate, or disconnect.\n");

Expand All @@ -77,7 +83,15 @@ export class SocketProxyFactory implements ISocketProxyFactory {
port: localPort,
verifyClient: async (info: any, callback: Function) => {
this.$logger.info("Frontend client connected.");
const _socket = await factory();
let _socket;
try {
_socket = await factory();
} catch (err) {
this.$logger.trace(err);
this.emit(CONNECTION_ERROR_EVENT_NAME, err);
this.$errors.failWithoutHelp("Cannot connect to device socket.");
}

this.$logger.info("Backend socket created.");
info.req["__deviceSocket"] = _socket;
callback(true);
Expand Down
1 change: 1 addition & 0 deletions lib/nativescript-cli-lib-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ $injector.requirePublic("companionAppsService", "./common/appbuilder/services/li
$injector.requirePublicClass("deviceEmitter", "./common/appbuilder/device-emitter");
$injector.requirePublicClass("deviceLogProvider", "./common/appbuilder/device-log-provider");
$injector.requirePublicClass("localBuildService", "./services/local-build-service");
$injector.requirePublicClass("debugService", "./services/debug-service");
$injector.require("iOSLogFilter", "./common/mobile/ios/ios-log-filter");

// We need this because some services check if (!$options.justlaunch) to start the device log after some operation.
Expand Down
Loading