Skip to content

Commit 28b8540

Browse files
committed
Handle default watching on tns debug command execution
1 parent 5f62288 commit 28b8540

File tree

7 files changed

+39
-81
lines changed

7 files changed

+39
-81
lines changed

package.json

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -115,38 +115,23 @@
115115
"program": "./out/debug-adapter/webKitDebug.js",
116116
"runtime": "node",
117117
"initialConfigurations": [
118-
{
119-
"name": "Sync on iOS",
120-
"type": "nativescript",
121-
"request": "launch",
122-
"platform": "ios",
123-
"appRoot": "${workspaceRoot}",
124-
"sourceMaps": true
125-
},
126118
{
127119
"name": "Launch on iOS",
128120
"type": "nativescript",
129121
"request": "launch",
130122
"platform": "ios",
131123
"appRoot": "${workspaceRoot}",
132124
"sourceMaps": true,
133-
"rebuild": true
125+
"watch": true
134126
},
135127
{
136128
"name": "Attach on iOS",
137129
"type": "nativescript",
138130
"request": "attach",
139131
"platform": "ios",
140132
"appRoot": "${workspaceRoot}",
141-
"sourceMaps": true
142-
},
143-
{
144-
"name": "Sync on Android",
145-
"type": "nativescript",
146-
"request": "launch",
147-
"platform": "android",
148-
"appRoot": "${workspaceRoot}",
149-
"sourceMaps": true
133+
"sourceMaps": true,
134+
"watch": false
150135
},
151136
{
152137
"name": "Launch on Android",
@@ -155,15 +140,16 @@
155140
"platform": "android",
156141
"appRoot": "${workspaceRoot}",
157142
"sourceMaps": true,
158-
"rebuild": true
143+
"watch": true
159144
},
160145
{
161146
"name": "Attach on Android",
162147
"type": "nativescript",
163148
"request": "attach",
164149
"platform": "android",
165150
"appRoot": "${workspaceRoot}",
166-
"sourceMaps": true
151+
"sourceMaps": true,
152+
"watch": false
167153
}
168154
],
169155
"configurationAttributes": {
@@ -213,15 +199,10 @@
213199
"description": "If true the launch request will launch the program without enabling debugging",
214200
"default": false
215201
},
216-
"rebuild": {
202+
"watch": {
217203
"type": "boolean",
218-
"description": "Whether the app should be rebuild before start.",
204+
"description": "Whether the watch and reconnect on file change.",
219205
"default": true
220-
},
221-
"syncAllFiles": {
222-
"type": "boolean",
223-
"description": "Whether to sync files from node_modules folder.",
224-
"default": false
225206
}
226207
}
227208
},
@@ -260,6 +241,11 @@
260241
"type": "string",
261242
"description": "NativeScript platform",
262243
"default": null
244+
},
245+
"watch": {
246+
"type": "boolean",
247+
"description": "Whether the watch and reconnect on file change.",
248+
"default": true
263249
}
264250
}
265251
}

src/custom-typings/debugProtocolExtensions.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ declare module 'vscode-debugprotocol' {
1919

2020
interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IRequestArgs {
2121
stopOnEntry?: boolean;
22-
rebuild?: boolean;
23-
syncAllFiles?: boolean;
22+
watch?: boolean;
2423
}
2524

2625
interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments, IRequestArgs {

src/debug-adapter/debugRequest.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ export class DebugRequest {
1515
}
1616

1717
public get isLaunch(): boolean {
18-
return this.args.request === "launch" && (<DebugProtocol.ILaunchRequestArgs>this._requestArgs).rebuild;
19-
}
20-
21-
public get isSync(): boolean {
22-
return this.args.request == "launch" && !(<DebugProtocol.ILaunchRequestArgs>this._requestArgs).rebuild;
18+
return this.args.request === "launch";
2319
}
2420

2521
public get isAttach(): boolean {
@@ -39,7 +35,7 @@ export class DebugRequest {
3935
}
4036

4137
public get launchArgs(): DebugProtocol.ILaunchRequestArgs {
42-
return (this.isLaunch || this.isSync) ? <DebugProtocol.ILaunchRequestArgs>this.args : null;
38+
return this.isLaunch ? <DebugProtocol.ILaunchRequestArgs>this.args : null;
4339
}
4440

4541
public get attachArgs(): DebugProtocol.IAttachRequestArgs {

src/debug-adapter/webKitDebugAdapter.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,14 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
139139
return Services.extensionClient().getInitSettings().then(settings => {
140140
Services.cliPath = settings.tnsPath || Services.cliPath;
141141
this._request = new DebugRequest(args, Services.cli());
142-
Services.extensionClient().analyticsLaunchDebugger({ request: this._request.isSync ? "sync" : args.request, platform: args.platform });
142+
Services.extensionClient().analyticsLaunchDebugger({ request: args.request, platform: args.platform });
143143

144144
// Run CLI Command
145-
Services.logger().log(`[NSDebugAdapter] Using tns CLI on path '${this._request.project.cli.path}'`, Tags.FrontendMessage);
145+
Services.logger().log(`[NSDebugAdapter] Using tns CLI v${this._request.project.cli.version} on path '${this._request.project.cli.path}'`, Tags.FrontendMessage);
146146
Services.logger().log('[NSDebugAdapter] Running tns command...', Tags.FrontendMessage);
147147
let cliCommand: DebugResult;
148148
if (this._request.isLaunch) {
149-
cliCommand = this._request.project.debug({ stopOnEntry: this._request.launchArgs.stopOnEntry }, this._request.args.tnsArgs);
150-
}
151-
else if (this._request.isSync) {
152-
cliCommand = this._request.project.debugWithSync({ stopOnEntry: this._request.launchArgs.stopOnEntry, syncAllFiles: this._request.launchArgs.syncAllFiles }, this._request.args.tnsArgs);
149+
cliCommand = this._request.project.debug({ stopOnEntry: this._request.launchArgs.stopOnEntry, watch: this._request.launchArgs.watch }, this._request.args.tnsArgs);
153150
}
154151
else if (this._request.isAttach) {
155152
cliCommand = this._request.project.attach(this._request.args.tnsArgs);
@@ -158,33 +155,30 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
158155
if (cliCommand.tnsProcess) {
159156
cliCommand.tnsProcess.stdout.on('data', data => { Services.logger().log(data.toString(), Tags.FrontendMessage); });
160157
cliCommand.tnsProcess.stderr.on('data', data => { Services.logger().error(data.toString(), Tags.FrontendMessage); });
161-
cliCommand.tnsProcess.on('close', (code, signal) => { Services.logger().error(`The tns command finished its execution with code ${code}.`, Tags.FrontendMessage); });
158+
cliCommand.tnsProcess.on('close', (code, signal) => { Services.logger().error(`[NSDebugAdapter] The tns command finished its execution with code ${code}.`, Tags.FrontendMessage); });
162159
}
163160

164161
let promiseResolve = null;
165162
let promise: Promise<void> = new Promise<void>((res, rej) => { promiseResolve = res; });
166163
Services.logger().log('[NSDebugAdapter] Watching the tns CLI output to receive a connection token', Tags.FrontendMessage);
167164
// Attach to the running application
168165
cliCommand.tnsOutputEventEmitter.on('readyForConnection', (connectionToken: string | number) => {
169-
connectionToken = this._request.isAndroid ? this._request.androidProject.getDebugPortSync(this._request.args.tnsArgs) : connectionToken;
170-
Services.logger().log(`[NSDebugAdapter] Attaching to application on ${connectionToken}`, Tags.FrontendMessage);
166+
Services.logger().log(`[NSDebugAdapter] Ready to attach to application on ${connectionToken}`, Tags.FrontendMessage);
171167
let connection: INSDebugConnection = this._request.isAndroid ? new AndroidConnection() : new IosConnection();
172168

173169
connection.attach(connectionToken, 'localhost').then(() => {
170+
Services.logger().log(`[NSDebugAdapter] Connection to target application established on ${connectionToken}`, Tags.FrontendMessage);
174171
this.setConnection(connection);
175172
return connection.enable();
176173
}).then(() => {
177-
Services.logger().log(`[NSDebugAdapter] Successfully attached to the target application'`, Tags.FrontendMessage);
174+
Services.logger().log(`[NSDebugAdapter] Connection to target application successfully enabled`, Tags.FrontendMessage);
178175
this.fireEvent(new InitializedEvent());
179176
promiseResolve();
180-
}).then(() => {
181-
182-
});
177+
}).then(() => {});
183178
});
184179

185180
return promise;
186181
});
187-
188182
}
189183

190184
private setConnection(connection: INSDebugConnection) : INSDebugConnection {
@@ -216,8 +210,8 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
216210
private terminateSession(): void {
217211
this.clearEverything();
218212
// In case of a sync request the session is not terminated when the backend is detached
219-
if (!this._request.isSync) {
220-
Services.logger().log("Terminating debug session");
213+
if (!this._request.isLaunch || !this._request.launchArgs.watch) {
214+
Services.logger().log("[NSDebugAdapter] Terminating debug session");
221215
this.fireEvent(new TerminatedEvent());
222216
}
223217
}

src/project/androidProject.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,9 @@ export class AndroidProject extends Project {
2424
return { tnsProcess: null, tnsOutputEventEmitter: tnsOutputEventEmitter };
2525
}
2626

27-
public debugWithSync(options: { stopOnEntry: boolean, syncAllFiles: boolean }, tnsArgs?: string[]): DebugResult {
28-
let args: string[] = ["--watch"];
29-
if (options.syncAllFiles) { args.push("--syncAllFiles"); }
30-
args = args.concat(tnsArgs);
31-
32-
return this.debug({stopOnEntry: options.stopOnEntry}, args);
33-
}
34-
35-
public debug(options: { stopOnEntry: boolean }, tnsArgs?: string[]): DebugResult {
27+
public debug(options: { stopOnEntry: boolean, watch: boolean }, tnsArgs?: string[]): DebugResult {
3628
let args: string[] = [];
29+
args.push(options.watch ? "--watch" : "--no-watch");
3730
if (options.stopOnEntry) { args.push("--debug-brk"); }
3831
args = args.concat(tnsArgs);
3932

@@ -43,18 +36,15 @@ export class AndroidProject extends Project {
4336
return { tnsProcess: debugProcess, tnsOutputEventEmitter: tnsOutputEventEmitter };
4437
}
4538

46-
public getDebugPortSync(tnsArgs?: string[]): number {
47-
let args: string[] = [];
48-
args = args.concat(tnsArgs);
49-
let output = this.cli.executeSync(["debug", "android", "--get-port"].concat(args), this.appRoot);
50-
let port = parseInt(output.match("(?:debug port: )([\\d]{5})")[1]);
51-
return port;
52-
}
53-
5439
private configureReadyEvent(readableStream: stream.Readable, eventEmitter: EventEmitter): void {
40+
let debugPort = null;
41+
new scanner.StringMatchingScanner(readableStream).onEveryMatch(new RegExp("device: .* debug port: [0-9]+"), (match: scanner.MatchFound) => {
42+
//device: {device-name} debug port: {debug-port}
43+
debugPort = parseInt((<string>match.matches[0]).match("(?:debug port: )([\\d]{5})")[1]);
44+
});
5545
new scanner.StringMatchingScanner(readableStream).onEveryMatch('# NativeScript Debugger started #', (match: scanner.MatchFound) => {
5646
// wait a little before trying to connect, this gives a chance for adb to be able to connect to the debug socket
57-
setTimeout(() => { eventEmitter.emit('readyForConnection'); }, 500);
47+
setTimeout(() => { eventEmitter.emit('readyForConnection', debugPort); }, 500);
5848
});
5949
}
6050
}

src/project/iosProject.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {Project, DebugResult} from './project';
55
import * as scanner from './streamScanner';
66
import {Version} from '../common/version';
77
import {NativeScriptCli} from './nativeScriptCli';
8+
import {Services} from '../services/debugAdapterServices';
9+
import {Tags} from '../common/logger';
810

911
export class IosProject extends Project {
1012

@@ -30,16 +32,9 @@ export class IosProject extends Project {
3032
return { tnsProcess: debugProcess, tnsOutputEventEmitter: tnsOutputEventEmitter };
3133
}
3234

33-
public debugWithSync(options: { stopOnEntry: boolean, syncAllFiles: boolean }, tnsArgs?: string[]): DebugResult {
34-
let args: string[] = ["--watch"];
35-
if (options.syncAllFiles) { args.push("--syncAllFiles"); }
36-
args = args.concat(tnsArgs);
37-
38-
return this.debug({stopOnEntry: options.stopOnEntry}, args);
39-
}
40-
41-
public debug(options: { stopOnEntry: boolean }, tnsArgs?: string[]): DebugResult {
35+
public debug(options: { stopOnEntry: boolean, watch: boolean }, tnsArgs?: string[]): DebugResult {
4236
let args: string[] = [];
37+
args.push(options.watch ? "--watch" : "--no-watch");
4338
if (options.stopOnEntry) { args.push("--debug-brk"); }
4439
args = args.concat(tnsArgs);
4540

src/project/project.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ export abstract class Project {
2626

2727
public abstract attach(tnsArgs?: string[]): DebugResult;
2828

29-
public abstract debugWithSync(options: { stopOnEntry: boolean, syncAllFiles: boolean }, tnsArgs?: string[]): DebugResult;
30-
31-
public abstract debug(options: { stopOnEntry: boolean }, tnsArgs?: string[]): DebugResult;
29+
public abstract debug(options: { stopOnEntry: boolean, watch: boolean }, tnsArgs?: string[]): DebugResult;
3230

3331
protected executeRunCommand(args: string[]): ChildProcess {
3432
return this.cli.execute(["run", this.platformName()].concat(args), this._appRoot);

0 commit comments

Comments
 (0)