Skip to content

Commit d0a8e2a

Browse files
authored
Merge pull request #65 from NativeScript/raikov/livesync-debug2
Added support for debug with livesync
2 parents c6e3e9a + 7c4c417 commit d0a8e2a

File tree

3 files changed

+68
-52
lines changed

3 files changed

+68
-52
lines changed

package.json

+20-31
Original file line numberDiff line numberDiff line change
@@ -105,84 +105,68 @@
105105
"runtime": "node",
106106
"initialConfigurations": [
107107
{
108-
"name": "Launch on iOS Device",
108+
"name": "Sync on iOS",
109109
"type": "nativescript",
110110
"platform": "ios",
111111
"request": "launch",
112112
"appRoot": "${workspaceRoot}",
113113
"sourceMaps": true,
114114
"diagnosticLogging": false,
115-
"emulator": false
116-
},
117-
{
118-
"name": "Attach on iOS Device",
119-
"type": "nativescript",
120-
"platform": "ios",
121-
"request": "attach",
122-
"appRoot": "${workspaceRoot}",
123-
"sourceMaps": true,
124-
"diagnosticLogging": false,
125-
"emulator": false
115+
"emulator": false,
116+
"rebuild": false
126117
},
127118
{
128-
"name": "Launch on iOS Emulator",
119+
"name": "Launch on iOS",
129120
"type": "nativescript",
130121
"platform": "ios",
131122
"request": "launch",
132123
"appRoot": "${workspaceRoot}",
133124
"sourceMaps": true,
134125
"diagnosticLogging": false,
135-
"emulator": true
126+
"emulator": false,
127+
"rebuild": true
136128
},
137129
{
138-
"name": "Attach on iOS Emulator",
130+
"name": "Attach on iOS",
139131
"type": "nativescript",
140132
"platform": "ios",
141133
"request": "attach",
142134
"appRoot": "${workspaceRoot}",
143135
"sourceMaps": true,
144136
"diagnosticLogging": false,
145-
"emulator": true
137+
"emulator": false
146138
},
147139
{
148-
"name": "Launch on Android Device",
140+
"name": "Sync on Android",
149141
"type": "nativescript",
150142
"platform": "android",
151143
"request": "launch",
152144
"appRoot": "${workspaceRoot}",
153145
"sourceMaps": true,
154146
"diagnosticLogging": false,
155-
"emulator": false
147+
"emulator": false,
148+
"rebuild": false
156149
},
157150
{
158-
"name": "Launch on Android Emulator",
151+
"name": "Launch on Android",
159152
"type": "nativescript",
160153
"platform": "android",
161154
"request": "launch",
162155
"appRoot": "${workspaceRoot}",
163156
"sourceMaps": true,
164157
"diagnosticLogging": false,
165-
"emulator": true
158+
"emulator": false,
159+
"rebuild": true
166160
},
167161
{
168-
"name": "Attach on Android Device",
162+
"name": "Attach on Android",
169163
"type": "nativescript",
170164
"platform": "android",
171165
"request": "attach",
172166
"appRoot": "${workspaceRoot}",
173167
"sourceMaps": true,
174168
"diagnosticLogging": false,
175169
"emulator": false
176-
},
177-
{
178-
"name": "Attach on Android Emulator",
179-
"type": "nativescript",
180-
"platform": "android",
181-
"request": "attach",
182-
"appRoot": "${workspaceRoot}",
183-
"sourceMaps": true,
184-
"diagnosticLogging": false,
185-
"emulator": true
186170
}
187171
],
188172
"configurationAttributes": {
@@ -234,6 +218,11 @@
234218
"type": "boolean",
235219
"description": "Whether the app to run in emulator or on a physical device.",
236220
"default": false
221+
},
222+
"rebuild": {
223+
"type": "boolean",
224+
"description": "Whether the app should be rebuild before start.",
225+
"default": true
237226
}
238227
}
239228
},

src/debug-adapter/webKitAdapterInterfaces.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments
1212
request: string;
1313
tnsArgs?: string[];
1414
tnsOutput?: string;
15+
rebuild?: boolean;
1516
}
1617

1718
export interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments {

src/services/NsCliService.ts

+47-21
Original file line numberDiff line numberDiff line change
@@ -137,35 +137,51 @@ export class IosProject extends NSProject {
137137
return Promise.reject('iOS platform is supported only on OS X.');
138138
}
139139

140+
let rebuild = (args.request == "launch") ? (args as ILaunchRequestArgs).rebuild : true;
140141
// build command to execute
141142
let command = new CommandBuilder()
142143
.appendParam("debug")
143144
.appendParam(this.platform())
144145
.appendParamIf("--emulator", args.emulator)
145146
.appendParamIf("--start", args.request === "attach")
146147
.appendParamIf("--debug-brk", args.request === "launch")
148+
.appendParamIf("--no-rebuild", !rebuild)
147149
.appendParam("--no-client")
148150
.appendParams(args.tnsArgs)
149151
.build();
150152

151153
let socketPathPrefix = 'socket-file-location: ';
152154
let socketPathPattern: RegExp = new RegExp(socketPathPrefix + '.*\.sock');
153-
let readyToConnect: boolean = false;
155+
156+
let isSocketOpened = (cliOutput: string): string => {
157+
let matches: RegExpMatchArray = cliOutput.match(socketPathPattern);
158+
if(matches && matches.length > 0) {
159+
return matches[0].substr(socketPathPrefix.length);
160+
}
161+
return null;
162+
};
163+
164+
let isAppSynced = (cliOutput: string) => {
165+
return cliOutput.indexOf('Successfully synced application') > -1;
166+
};
154167

155168
return new Promise<string>((resolve, reject) => {
156169
// run NativeScript CLI command
157170
let child: ChildProcess = this.spawnProcess(command.path, command.args, args.tnsOutput);
158171

172+
let appSynced = false;
173+
let socketPath: string = null;
174+
159175
child.stdout.on('data', (data) => {
160-
let strData: string = data.toString();
161-
this.emit('TNS.outputMessage', strData, 'log');
162-
this.writeToTnsOutputFile(strData);
163-
if(!readyToConnect) {
164-
let matches: RegExpMatchArray = strData.match(socketPathPattern);
165-
if(matches && matches.length > 0) {
166-
readyToConnect = true;
167-
resolve(matches[0].substr(socketPathPrefix.length));
168-
}
176+
let cliOutput: string = data.toString();
177+
this.emit('TNS.outputMessage', cliOutput, 'log');
178+
this.writeToTnsOutputFile(cliOutput);
179+
180+
socketPath = socketPath || isSocketOpened(cliOutput);
181+
appSynced = rebuild ? false : (appSynced || isAppSynced(cliOutput));
182+
183+
if ((rebuild && socketPath) || (!rebuild && socketPath && appSynced)) {
184+
resolve(socketPath);
169185
}
170186
});
171187

@@ -207,11 +223,12 @@ export class AndroidProject extends NSProject {
207223
return Promise.resolve(child);
208224
}
209225

210-
public debug(args: IAttachRequestArgs | ILaunchRequestArgs): Promise<void> {
211-
if (args.request === "attach") {
226+
public debug(params: IAttachRequestArgs | ILaunchRequestArgs): Promise<void> {
227+
if (params.request === "attach") {
212228
return Promise.resolve<void>();
213229
}
214-
else if (args.request === "launch") {
230+
else if (params.request === "launch") {
231+
let args: ILaunchRequestArgs = params as ILaunchRequestArgs;
215232
let that = this;
216233
let launched = false;
217234

@@ -220,6 +237,7 @@ export class AndroidProject extends NSProject {
220237
.appendParam("debug")
221238
.appendParam(this.platform())
222239
.appendParamIf("--emulator", args.emulator)
240+
.appendParamIf("--no-rebuild", args.rebuild !== true)
223241
.appendParam("--debug-brk")
224242
.appendParam("--no-client")
225243
.appendParams(args.tnsArgs)
@@ -233,13 +251,14 @@ export class AndroidProject extends NSProject {
233251
let strData: string = data.toString();
234252
that.emit('TNS.outputMessage', data.toString(), 'log');
235253
that.writeToTnsOutputFile(strData);
236-
if (!launched && args.request === "launch" && strData.indexOf('# NativeScript Debugger started #') > -1) {
237-
launched = true;
238-
239-
//wait a little before trying to connect, this gives a changes for adb to be able to connect to the debug socket
240-
setTimeout(() => {
241-
resolve();
242-
}, 500);
254+
if (!launched) {
255+
if (args.request === "launch" && ((strData.indexOf('# NativeScript Debugger started #') > -1) || strData.indexOf('Successfully synced application') > -1)) {
256+
launched = true;
257+
//wait a little before trying to connect, this gives a changes for adb to be able to connect to the debug socket
258+
setTimeout(() => {
259+
resolve();
260+
}, 500);
261+
}
243262
}
244263
});
245264

@@ -249,7 +268,14 @@ export class AndroidProject extends NSProject {
249268
});
250269

251270
child.on('close', function(code) {
252-
reject("The debug process exited unexpectedly code:" + code);
271+
if (!args.rebuild) {
272+
setTimeout(() => {
273+
reject("The debug process exited unexpectedly code:" + code);
274+
}, 3000);
275+
}
276+
else {
277+
reject("The debug process exited unexpectedly code:" + code);
278+
}
253279
});
254280
});
255281
}

0 commit comments

Comments
 (0)