From 83f3410145eebc9ae5e79e916a17e95e3fc02223 Mon Sep 17 00:00:00 2001 From: Vladimir Enchev Date: Fri, 3 Jun 2016 12:07:07 +0300 Subject: [PATCH] No break by default implementation added for iOS and Android --- .../project/testing/debug-android.md | 3 + docs/man_pages/project/testing/debug-ios.md | 2 + lib/services/android-debug-service.ts | 57 ++++++++++--------- lib/services/ios-debug-service.ts | 25 ++++---- 4 files changed, 50 insertions(+), 37 deletions(-) diff --git a/docs/man_pages/project/testing/debug-android.md b/docs/man_pages/project/testing/debug-android.md index e4e8bc61fc..a64907e24a 100644 --- a/docs/man_pages/project/testing/debug-android.md +++ b/docs/man_pages/project/testing/debug-android.md @@ -1,6 +1,9 @@ debug android ========== +All you need to do to start debugging your app is to execute `tns debug android`. The NativeScript CLI will build, deploy, start your app, start Chrome DevTools and attach the debugger. +* You cannot debug if there are multiple devices available (emulators and/or real devices). You need to have started only one device or emulator. + Usage | Synopsis ---|--- Deploy on device, run the app and stop at the first breakpoint | `$ tns debug android --debug-brk [--device ] [--debug-port ] [--timeout ]` diff --git a/docs/man_pages/project/testing/debug-ios.md b/docs/man_pages/project/testing/debug-ios.md index 97ea34701e..f702bcdda3 100644 --- a/docs/man_pages/project/testing/debug-ios.md +++ b/docs/man_pages/project/testing/debug-ios.md @@ -1,5 +1,7 @@ debug ios ========== +All you need to do to start debugging your app is to execute `tns debug ios`. The NativeScript CLI will build, deploy, start your app, start Chrome DevTools and attach the debugger. +* You cannot debug if there are multiple devices available (emulators and/or real devices). You need to have started only one device or emulator. Usage | Synopsis ---|--- diff --git a/lib/services/android-debug-service.ts b/lib/services/android-debug-service.ts index ee13387473..e24f4f9b6f 100644 --- a/lib/services/android-debug-service.ts +++ b/lib/services/android-debug-service.ts @@ -106,12 +106,7 @@ class AndroidDebugService implements IDebugService { return (() => { let packageFile = ""; - if (!this.$options.debugBrk && !this.$options.start && !this.$options.getPort && !this.$options.stop) { - this.$logger.warn("Neither --debug-brk nor --start option was specified. Defaulting to --debug-brk."); - this.$options.debugBrk = true; - } - - if (this.$options.debugBrk && !this.$options.emulator) { + if (!this.$options.start && !this.$options.emulator) { let cachedDeviceOption = this.$options.forDevice; this.$options.forDevice = true; this.$platformService.buildPlatform(this.platform).wait(); @@ -141,6 +136,11 @@ class AndroidDebugService implements IDebugService { this.detachDebugger(packageName).wait(); } else if (this.$options.debugBrk) { this.startAppWithDebugger(packageFile, packageName).wait(); + } else { + this.startAppWithDebugger(packageFile, packageName).wait(); + //TODO: Find different way to make sure that the app is started. + sleep(500); + this.attachDebugger(device.deviceInfo.identifier, packageName).wait(); } }).future()(); } @@ -197,33 +197,38 @@ class AndroidDebugService implements IDebugService { // Arguments passed to executeShellCommand must be in array ([]), but it turned out adb shell "arg with intervals" still works correctly. // As we need to redirect output of a command on the device, keep using only one argument. // We could rewrite this with two calls - touch and rm -f , but -f flag is not available on old Android, so rm call will fail when file does not exist. - this.device.adb.executeShellCommand([`cat /dev/null > /data/local/tmp/${packageName}-debugbreak`]).wait(); + + if(this.$options.debugBrk) { + this.device.adb.executeShellCommand([`cat /dev/null > /data/local/tmp/${packageName}-debugbreak`]).wait(); + } this.device.applicationManager.stopApplication(packageName).wait(); this.device.applicationManager.startApplication(packageName).wait(); - let waitText: string = `0 /data/local/tmp/${packageName}-debugbreak`; - let maxWait = 12; - let debugerStarted: boolean = false; - while (maxWait > 0 && !debugerStarted) { - let forwardsResult = this.device.adb.executeShellCommand(["ls", "-s", `/data/local/tmp/${packageName}-debugbreak`]).wait(); - maxWait--; - debugerStarted = forwardsResult.indexOf(waitText) === -1; - if (!debugerStarted) { - sleep(500); + if(this.$options.debugBrk) { + let waitText: string = `0 /data/local/tmp/${packageName}-debugbreak`; + let maxWait = 12; + let debugerStarted: boolean = false; + while (maxWait > 0 && !debugerStarted) { + let forwardsResult = this.device.adb.executeShellCommand(["ls", "-s", `/data/local/tmp/${packageName}-debugbreak`]).wait(); + maxWait--; + debugerStarted = forwardsResult.indexOf(waitText) === -1; + if (!debugerStarted) { + sleep(500); + } } - } - if (debugerStarted) { - this.$logger.info("# NativeScript Debugger started #"); - } else { - this.$logger.warn("# NativeScript Debugger did not start in time #"); - } + if (debugerStarted) { + this.$logger.info("# NativeScript Debugger started #"); + } else { + this.$logger.warn("# NativeScript Debugger did not start in time #"); + } - if (this.$options.client) { - let localDebugPort = this.getForwardedLocalDebugPortForPackageName(this.device.deviceInfo.identifier, packageName).wait(); - this.startDebuggerClient(localDebugPort).wait(); - this.openDebuggerClient(AndroidDebugService.DEFAULT_NODE_INSPECTOR_URL + "?port=" + localDebugPort); + if (this.$options.client) { + let localDebugPort = this.getForwardedLocalDebugPortForPackageName(this.device.deviceInfo.identifier, packageName).wait(); + this.startDebuggerClient(localDebugPort).wait(); + this.openDebuggerClient(AndroidDebugService.DEFAULT_NODE_INSPECTOR_URL + "?port=" + localDebugPort); + } } }).future()(); } diff --git a/lib/services/ios-debug-service.ts b/lib/services/ios-debug-service.ts index e72f51bd6b..b591525dac 100644 --- a/lib/services/ios-debug-service.ts +++ b/lib/services/ios-debug-service.ts @@ -43,21 +43,23 @@ class IOSDebugService implements IDebugService { this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options."); } - if(!this.$options.debugBrk && !this.$options.start) { - this.$logger.warn("Neither --debug-brk nor --start option was specified. Defaulting to --debug-brk."); - this.$options.debugBrk = true; - } - if (this.$options.emulator) { if (this.$options.debugBrk) { - return this.emulatorDebugBrk(); + return this.emulatorDebugBrk(true); } else if (this.$options.start) { return this.emulatorStart(); + } else { + return this.emulatorDebugBrk(); } } else { if (this.$options.debugBrk) { - return this.deviceDebugBrk(); + return this.deviceDebugBrk(true); } else if (this.$options.start) { + return this.deviceStart(); + } else { + let deploy = this.$platformService.deployOnDevice(this.platform); + deploy.wait(); + return this.deviceStart(); } } @@ -72,14 +74,15 @@ class IOSDebugService implements IDebugService { }).future()(); } - private emulatorDebugBrk(): IFuture { + private emulatorDebugBrk(shouldBreak? : boolean): IFuture { return (() => { let platformData = this.$platformsData.getPlatformData(this.platform); this.$platformService.buildPlatform(this.platform).wait(); let emulatorPackage = this.$platformService.getLatestApplicationPackageForEmulator(platformData).wait(); + let args = shouldBreak ? "--nativescript-debug-brk" : "--nativescript-debug-start"; let child_process = this.$iOSEmulatorServices.runApplicationOnEmulator(emulatorPackage.packageName, { waitForDebugger: true, captureStdin: true, - args: "--nativescript-debug-brk", appId: this.$projectData.projectId }).wait(); + args: args, appId: this.$projectData.projectId }).wait(); let lineStream = byline(child_process.stdout); lineStream.on('data', (line: NodeBuffer) => { @@ -108,12 +111,12 @@ class IOSDebugService implements IDebugService { }).future()(); } - private deviceDebugBrk(): IFuture { + private deviceDebugBrk(shouldBreak? : boolean): IFuture { return (() => { this.$devicesService.initialize({ platform: this.platform, deviceId: this.$options.device }).wait(); this.$devicesService.execute((device: iOSDevice.IOSDevice) => (() => { if(device.isEmulator) { - return this.emulatorDebugBrk().wait(); + return this.emulatorDebugBrk(shouldBreak).wait(); } // we intentionally do not wait on this here, because if we did, we'd miss the AppLaunching notification let deploy = this.$platformService.deployOnDevice(this.platform);