5
5
import * as os from 'os' ;
6
6
import * as fs from 'fs' ;
7
7
import * as path from 'path' ;
8
- import { spawn , ChildProcess } from 'child_process' ;
9
8
import { Handles , StoppedEvent , InitializedEvent , TerminatedEvent , OutputEvent } from 'vscode-debugadapter' ;
10
9
import { DebugProtocol } from 'vscode-debugprotocol' ;
11
10
import { INSDebugConnection } from './connection/INSDebugConnection' ;
12
11
import { IosConnection } from './connection/iosConnection' ;
13
12
import { AndroidConnection } from './connection/androidConnection' ;
14
- import * as utils from './utilities' ;
13
+ import { Project , DebugResult } from '../project/project' ;
14
+ import { IosProject } from '../project/iosProject' ;
15
+ import { AndroidProject } from '../project/androidProject' ;
16
+ import * as utils from '../common/utilities' ;
15
17
import { formatConsoleMessage } from './consoleHelper' ;
16
- import * as ns from '../project/NsCliService' ;
17
18
import { DebugAdapterServices as Services } from '../services/debugAdapterServices' ;
18
19
import { LoggerHandler , Handlers , Tags } from '../common/Logger' ;
19
- import { DebugConfiguration } from './debugConfiguration ' ;
20
+ import { DebugRequest } from './debugRequest ' ;
20
21
21
22
interface IScopeVarHandle {
22
23
objectId : string ;
@@ -38,11 +39,9 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
38
39
private _setBreakpointsRequestQ : Promise < any > ;
39
40
private _webKitConnection : INSDebugConnection ;
40
41
private _eventHandler : ( event : DebugProtocol . Event ) => void ;
41
- private appRoot : string ;
42
- private platform : string ;
43
42
private _lastOutputEvent : OutputEvent ;
44
43
private _loggerFrontendHandler : LoggerHandler = args => this . fireEvent ( new OutputEvent ( ` ›${ args . message } \n` , args . type . toString ( ) ) ) ;
45
- private _debugConfig : DebugConfiguration ;
44
+ private _request : DebugRequest ;
46
45
47
46
public constructor ( ) {
48
47
this . _variableHandles = new Handles < IScopeVarHandle > ( ) ;
@@ -51,7 +50,7 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
51
50
Services . logger . addHandler ( this . _loggerFrontendHandler , [ Tags . FrontendMessage ] ) ;
52
51
Services . logger . log ( `OS: ${ os . platform ( ) } ${ os . arch ( ) } ` ) ;
53
52
Services . logger . log ( 'Node version: ' + process . version ) ;
54
- Services . logger . log ( 'Adapter version: ' + require ( '../../package.json' ) . version ) ;
53
+ Services . logger . log ( 'Adapter version: ' + utils . getInstalledExtensionVersion ( ) . toString ( ) ) ;
55
54
56
55
this . clearEverything ( ) ;
57
56
}
@@ -108,8 +107,7 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
108
107
return this . processRequest ( args ) ;
109
108
}
110
109
111
- private configureLoggingForRequest ( ) : void {
112
- let args = this . _debugConfig . args ;
110
+ private configureLoggingForRequest ( args : DebugProtocol . IRequestArgs ) : void {
113
111
if ( args . diagnosticLogging ) {
114
112
// The logger frontend handler is initially configured to handle messages with LoggerTagFrontendMessage tag only.
115
113
// We remove the handler and add it again for all messages.
@@ -120,68 +118,63 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
120
118
Services . logger . addHandler ( Handlers . createStreamHandler ( fs . createWriteStream ( args . tnsOutput ) ) ) ;
121
119
}
122
120
Services . logger . log ( `initialize(${ JSON . stringify ( this . _initArgs ) } )` ) ;
123
- Services . logger . log ( `${ this . _debugConfig . args . request } (${ JSON . stringify ( this . _debugConfig . isAttach ? this . _debugConfig . attachArgs : this . _debugConfig . launchArgs ) } )` ) ;
121
+ Services . logger . log ( `${ args . request } (${ JSON . stringify ( args ) } )` ) ;
124
122
}
125
123
126
124
private processRequest ( args : DebugProtocol . IRequestArgs ) {
127
- this . _debugConfig = new DebugConfiguration ( args ) ;
125
+ this . configureLoggingForRequest ( args ) ;
126
+ // Initialize the request
128
127
Services . appRoot = args . appRoot ;
129
- let analyticsRequest = this . _debugConfig . isSync ? "sync" : args . request ;
130
- Services . extensionClient . analyticsLaunchDebugger ( { request : analyticsRequest , platform : args . platform } ) ;
131
- this . configureLoggingForRequest ( ) ;
132
- this . appRoot = args . appRoot ;
133
- this . platform = args . platform ;
134
-
135
- return ( ( args . platform == 'ios' ) ? this . _attachIos ( ) : this . _attachAndroid ( ) )
136
- . then ( ( ) => {
137
- this . fireEvent ( new InitializedEvent ( ) ) ;
138
- } ,
139
- e => {
140
- Services . logger . error ( "Command failed: " + e , Tags . FrontendMessage ) ;
141
- this . clearEverything ( ) ;
142
- return utils . errP ( e ) ;
143
- } ) ;
144
- }
128
+ Services . cliPath = args . nativescriptCliPath || Services . cliPath ;
129
+ this . _request = new DebugRequest ( args , Services . cli ) ;
130
+ Services . extensionClient . analyticsLaunchDebugger ( { request : this . _request . isSync ? "sync" : args . request , platform : args . platform } ) ;
131
+
132
+ // Run CLI Command
133
+ let cliCommand : DebugResult ;
134
+ if ( this . _request . isLaunch ) {
135
+ cliCommand = this . _request . project . debug ( { stopOnEntry : this . _request . launchArgs . stopOnEntry } , this . _request . args . tnsArgs ) ;
136
+ }
137
+ else if ( this . _request . isSync ) {
138
+ cliCommand = this . _request . project . debugWithSync ( { stopOnEntry : this . _request . launchArgs . stopOnEntry , syncAllFiles : this . _request . launchArgs . syncAllFiles } , this . _request . args . tnsArgs ) ;
139
+ }
140
+ else if ( this . _request . isAttach ) {
141
+ cliCommand = this . _request . project . attach ( this . _request . args . tnsArgs ) ;
142
+ }
145
143
146
- private _attachIos ( ) : Promise < void > {
147
- let args = this . _debugConfig . args ;
148
- let iosProject : ns . IosProject = new ns . IosProject ( this . appRoot , args . tnsOutput ) ;
144
+ if ( cliCommand . tnsProcess ) {
145
+ cliCommand . tnsProcess . stdout . on ( 'data' , data => { Services . logger . log ( data . toString ( ) , Tags . FrontendMessage ) ; } ) ;
146
+ cliCommand . tnsProcess . stderr . on ( 'data' , data => { Services . logger . error ( data . toString ( ) , Tags . FrontendMessage ) ; } ) ;
147
+ cliCommand . tnsProcess . on ( 'close' , ( code , signal ) => { Services . logger . error ( `The tns command finished its execution with code ${ code } .` , Tags . FrontendMessage ) ; } ) ;
148
+ }
149
149
150
- return iosProject . debug ( args )
151
- . then ( ( socketFilePath ) => {
152
- let iosConnection : IosConnection = new IosConnection ( ) ;
153
- this . setConnection ( iosConnection ) ;
154
- return iosConnection . attach ( socketFilePath ) ;
150
+ // Attach to the running application
151
+ let connectionEstablished = cliCommand . backendIsReadyForConnection . then ( ( connectionToken : string | number ) => {
152
+ if ( this . _request . isAndroid ) {
153
+ Services . logger . log ( `Attaching to application on port ${ connectionToken } ` ) ;
154
+ let androidConnection = new AndroidConnection ( ) ;
155
+ this . setConnection ( androidConnection ) ;
156
+ let port : number = < number > connectionToken ;
157
+ return androidConnection . attach ( port , 'localhost' ) ;
158
+ }
159
+ if ( this . _request . isIos ) {
160
+ Services . logger . log ( `Attaching to application on socket path ${ connectionToken } ` ) ;
161
+ let iosConnection = new IosConnection ( ) ;
162
+ this . setConnection ( iosConnection ) ;
163
+ let socketFilePath : string = < string > connectionToken ;
164
+ return iosConnection . attach ( socketFilePath ) ;
165
+ }
155
166
} ) ;
156
- }
157
-
158
- private _attachAndroid ( ) : Promise < void > {
159
- let args = this . _debugConfig . args ;
160
- let androidProject : ns . AndroidProject = new ns . AndroidProject ( this . appRoot , args . tnsOutput ) ;
161
- let thisAdapter : WebKitDebugAdapter = this ;
162
-
163
- Services . logger . log ( "Getting debug port" ) ;
164
- let androidConnection : AndroidConnection = null ;
165
167
166
- let runDebugCommand : Promise < any > = ( args . request == 'launch' ) ? androidProject . debug ( args ) : Promise . resolve ( ) ;
167
-
168
- return runDebugCommand . then ( _ => {
169
- let port : number ;
170
- return androidProject . getDebugPort ( args ) . then ( debugPort => {
171
- port = debugPort ;
172
- if ( ! thisAdapter . _webKitConnection ) {
173
- androidConnection = new AndroidConnection ( ) ;
174
- this . setConnection ( androidConnection ) ;
175
- }
176
- } ) . then ( ( ) => {
177
- Services . logger . log ( "Attaching to debug application" ) ;
178
- return androidConnection . attach ( port , 'localhost' ) ;
179
- } ) ;
168
+ // Send InitializedEvent
169
+ return connectionEstablished . then ( ( ) => this . fireEvent ( new InitializedEvent ( ) ) , e => {
170
+ Services . logger . error ( `Error: ${ e } ` , Tags . FrontendMessage ) ;
171
+ this . clearEverything ( ) ;
172
+ return utils . errP ( e ) ;
180
173
} ) ;
181
174
}
182
175
183
176
private setConnection ( connection : INSDebugConnection ) : INSDebugConnection {
184
- let args = this . _debugConfig . args ;
177
+ let args = this . _request . args ;
185
178
connection . on ( 'Debugger.paused' , params => this . onDebuggerPaused ( params ) ) ;
186
179
connection . on ( 'Debugger.resumed' , ( ) => this . onDebuggerResumed ( ) ) ;
187
180
connection . on ( 'Debugger.scriptParsed' , params => this . onScriptParsed ( params ) ) ;
@@ -308,7 +301,7 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
308
301
let isClientPath = false ;
309
302
if ( localMessage . url )
310
303
{
311
- const clientPath = utils . webkitUrlToClientPath ( this . appRoot , this . platform , localMessage . url ) ;
304
+ const clientPath = utils . webkitUrlToClientPath ( this . _request . args . appRoot , this . _request . args . platform , localMessage . url ) ;
312
305
if ( clientPath !== '' ) {
313
306
localMessage . url = clientPath ;
314
307
isClientPath = true ;
0 commit comments