1- import * as iOSDevice from "../common/mobile/ios/device/ios-device" ;
21import * as path from "path" ;
32import * as log4js from "log4js" ;
43import { ChildProcess } from "child_process" ;
54import { DebugServiceBase } from "./debug-service-base" ;
65import { IOS_LOG_PREDICATE } from "../common/constants" ;
7- import { CONNECTION_ERROR_EVENT_NAME , AWAIT_NOTIFICATION_TIMEOUT_SECONDS } from "../constants" ;
6+ import { CONNECTION_ERROR_EVENT_NAME } from "../constants" ;
87import { getPidFromiOSSimulatorLogs } from "../common/helpers" ;
98const inspectorAppName = "NativeScript Inspector.app" ;
109const inspectorNpmPackageName = "tns-ios-inspector" ;
@@ -22,7 +21,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
2221 private $logger : ILogger ,
2322 private $errors : IErrors ,
2423 private $packageInstallationManager : IPackageInstallationManager ,
25- private $iOSSocketRequestExecutor : IiOSSocketRequestExecutor ,
24+ private $iOSDebuggerPortService : IIOSDebuggerPortService ,
2625 private $processService : IProcessService ,
2726 private $socketProxyFactory : ISocketProxyFactory ,
2827 private $projectDataService : IProjectDataService ,
@@ -42,32 +41,28 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
4241 this . $errors . failWithoutHelp ( "Expected exactly one of the --debug-brk or --start options." ) ;
4342 }
4443
45- if ( this . $devicesService . isOnlyiOSSimultorRunning ( ) || this . $devicesService . deviceCount === 0 ) {
46- debugOptions . emulator = true ;
47- }
48-
4944 await this . startDeviceLogProcess ( debugData , debugOptions ) ;
45+ await this . $iOSDebuggerPortService . attachToDebuggerPortFoundEvent ( this . device , debugData , debugOptions ) ;
5046
51- if ( debugOptions . emulator ) {
52- if ( debugOptions . start ) {
53- return this . emulatorStart ( debugData , debugOptions ) ;
47+ if ( ! debugOptions . start ) { // not attach
48+ if ( this . device . isEmulator ) {
49+ await this . startAppOnSimulator ( debugData , debugOptions ) ;
5450 } else {
55- return this . emulatorDebugBrk ( debugData , debugOptions ) ;
56- }
57- } else {
58- if ( debugOptions . start ) {
59- return this . deviceStart ( debugData , debugOptions ) ;
60- } else {
61- return this . deviceDebugBrk ( debugData , debugOptions ) ;
51+ await this . startAppOnDevice ( debugData , debugOptions ) ;
6252 }
6353 }
54+
55+ return this . wireDebuggerClient ( debugData , debugOptions ) ;
6456 }
6557
6658 public async debugStart ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < void > {
67- await this . $devicesService . initialize ( { platform : this . platform , deviceId : debugData . deviceIdentifier } ) ;
68- // TODO: this.device
69- const action = async ( device : Mobile . IiOSDevice ) => device . isEmulator ? await this . emulatorDebugBrk ( debugData , debugOptions ) : await this . debugBrkCore ( debugData , debugOptions ) ;
70- await this . $devicesService . execute ( action , this . getCanExecuteAction ( debugData . deviceIdentifier ) ) ;
59+ if ( this . device . isEmulator ) {
60+ await this . startAppOnSimulator ( debugData , debugOptions ) ;
61+ } else {
62+ await this . startAppOnDevice ( debugData , debugOptions ) ;
63+ }
64+
65+ await this . wireDebuggerClient ( debugData , debugOptions ) ;
7166 }
7267
7368 public async debugStop ( ) : Promise < void > {
@@ -117,7 +112,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
117112 }
118113 }
119114
120- private async emulatorDebugBrk ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
115+ private async startAppOnSimulator ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < void > {
121116 const args = debugOptions . debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start" ;
122117 const launchResult = await this . $iOSEmulatorServices . runApplicationOnEmulator ( debugData . pathToAppPackage , {
123118 waitForDebugger : true ,
@@ -130,67 +125,23 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
130125 timeout : debugOptions . timeout ,
131126 sdk : debugOptions . sdk
132127 } ) ;
133-
134128 const pid = getPidFromiOSSimulatorLogs ( debugData . applicationIdentifier , launchResult ) ;
135129 this . _lldbProcess = this . $childProcess . spawn ( "lldb" , [ "-p" , pid ] ) ;
136130 if ( log4js . levels . TRACE . isGreaterThanOrEqualTo ( this . $logger . getLevel ( ) ) ) {
137131 this . _lldbProcess . stdout . pipe ( process . stdout ) ;
138132 }
139133 this . _lldbProcess . stderr . pipe ( process . stderr ) ;
140134 this . _lldbProcess . stdin . write ( "process continue\n" ) ;
141-
142- const debugUrl = await this . wireDebuggerClient ( debugData , debugOptions ) ;
143- return debugUrl ;
144135 }
145136
146- private async emulatorStart ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
147- const debugUrl = await this . wireDebuggerClient ( debugData , debugOptions ) ;
148- return debugUrl ;
149- }
150-
151- private async deviceDebugBrk ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
152- await this . $devicesService . initialize ( { platform : this . platform , deviceId : debugData . deviceIdentifier } ) ;
153- const projectData = this . $projectDataService . getProjectData ( debugData . projectDir ) ;
154- const action = async ( device : iOSDevice . IOSDevice ) : Promise < string > => {
155- if ( device . isEmulator ) {
156- return await this . emulatorDebugBrk ( debugData , debugOptions ) ;
157- }
158-
159- const runOptions : IRunPlatformOptions = {
160- device : debugData . deviceIdentifier ,
161- emulator : debugOptions . emulator ,
162- justlaunch : debugOptions . justlaunch
163- } ;
164-
165- const promisesResults = await Promise . all < any > ( [
166- this . $platformService . startApplication ( this . platform , runOptions , { appId : debugData . applicationIdentifier , projectName : projectData . projectName } ) ,
167- this . debugBrkCore ( debugData , debugOptions )
168- ] ) ;
169-
170- return _ . last ( promisesResults ) ;
137+ private async startAppOnDevice ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < void > {
138+ const runOptions : IRunPlatformOptions = {
139+ device : debugData . deviceIdentifier ,
140+ emulator : this . device . isEmulator ,
141+ justlaunch : debugOptions . justlaunch
171142 } ;
172-
173- // TODO: this.device
174- const deviceActionResult = await this . $devicesService . execute ( action , this . getCanExecuteAction ( debugData . deviceIdentifier ) ) ;
175- return deviceActionResult [ 0 ] . result ;
176- }
177-
178- private async debugBrkCore ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
179- await this . $iOSSocketRequestExecutor . executeLaunchRequest ( this . device . deviceInfo . identifier , AWAIT_NOTIFICATION_TIMEOUT_SECONDS , AWAIT_NOTIFICATION_TIMEOUT_SECONDS , debugData . applicationIdentifier , debugOptions ) ;
180- const debugUrl = await this . wireDebuggerClient ( debugData , debugOptions ) ;
181- return debugUrl ;
182- }
183-
184- private async deviceStart ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
185- await this . $devicesService . initialize ( { platform : this . platform , deviceId : debugData . deviceIdentifier } ) ;
186- const action = async ( device : Mobile . IiOSDevice ) => device . isEmulator ? await this . emulatorStart ( debugData , debugOptions ) : await this . deviceStartCore ( debugData , debugOptions ) ;
187- const deviceActionResult = await this . $devicesService . execute ( action , this . getCanExecuteAction ( debugData . deviceIdentifier ) ) ;
188- return deviceActionResult [ 0 ] . result ;
189- }
190-
191- private async deviceStartCore ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
192- const debugUrl = await this . wireDebuggerClient ( debugData , debugOptions ) ;
193- return debugUrl ;
143+ const projectData = this . $projectDataService . getProjectData ( debugData . projectDir ) ;
144+ await this . $platformService . startApplication ( this . platform , runOptions , { appId : debugData . applicationIdentifier , projectName : projectData . projectName } ) ;
194145 }
195146
196147 private async wireDebuggerClient ( debugData : IDebugData , debugOptions : IDebugOptions ) : Promise < string > {
0 commit comments