Skip to content

Commit ee426f7

Browse files
committed
Refactor debug configuration arguments
1 parent b67c459 commit ee426f7

File tree

5 files changed

+80
-35
lines changed

5 files changed

+80
-35
lines changed

src/custom-typings/debugProtocolExtensions.d.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,27 @@ declare module 'vscode-debugprotocol' {
44
namespace DebugProtocol {
55

66
type RequestArguments = LaunchRequestArguments | AttachRequestArguments;
7+
type RequestType = "launch" | "attach";
8+
type PlatformType = "android" | "ios";
79

8-
interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments {
9-
platform: string;
10-
appRoot?: string;
11-
stopOnEntry?: boolean;
10+
interface IRequestArgs {
11+
request: RequestType;
12+
platform: PlatformType;
13+
appRoot: string;
1214
sourceMaps?: boolean;
1315
diagnosticLogging?: boolean;
14-
request: string;
1516
tnsArgs?: string[];
1617
tnsOutput?: string;
18+
nativescriptCliPath?: string;
19+
}
20+
21+
interface ILaunchRequestArgs extends DebugProtocol.LaunchRequestArguments, IRequestArgs {
22+
stopOnEntry?: boolean;
1723
rebuild?: boolean;
1824
syncAllFiles?: boolean;
19-
nativescriptCliPath?: string;
2025
}
2126

22-
interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments {
23-
platform: string;
24-
appRoot?: string;
25-
sourceMaps?: boolean;
26-
diagnosticLogging?: boolean;
27-
request: string;
28-
tnsArgs?: string[];
29-
tnsOutput?: string;
30-
nativescriptCliPath?: string;
27+
interface IAttachRequestArgs extends DebugProtocol.AttachRequestArguments, IRequestArgs {
3128
}
3229

3330
interface ISetBreakpointsArgs extends DebugProtocol.SetBreakpointsArguments {

src/debug-adapter/adapter/sourceMaps/sourceMapTransformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class SourceMapTransformer implements DebugProtocol.IDebugTransformer {
3636
this.init(args);
3737
}
3838

39-
private init(args: DebugProtocol.ILaunchRequestArgs | DebugProtocol.IAttachRequestArgs): void {
39+
private init(args: DebugProtocol.IRequestArgs): void {
4040
if (args.sourceMaps) {
4141
this._webRoot = args.appRoot;
4242
this._sourceMaps = new SourceMaps(this._webRoot);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {DebugProtocol} from 'vscode-debugprotocol';
2+
3+
export class DebugConfiguration {
4+
private _requestArgs: DebugProtocol.IRequestArgs;
5+
6+
constructor(requestArgs: DebugProtocol.IRequestArgs) {
7+
this._requestArgs = requestArgs;
8+
}
9+
10+
public get isLaunch(): boolean {
11+
return this.args.request === "launch" && (<DebugProtocol.ILaunchRequestArgs>this._requestArgs).rebuild;
12+
}
13+
14+
public get isSync(): boolean {
15+
return this.args.request == "launch" && !(<DebugProtocol.ILaunchRequestArgs>this._requestArgs).rebuild;
16+
}
17+
18+
public get isAttach(): boolean {
19+
return this.args.request === "attach";
20+
}
21+
22+
public get isAndroid(): boolean {
23+
return this.args.platform == "android";
24+
}
25+
26+
public get isIos(): boolean {
27+
return this.args.platform == "ios";
28+
}
29+
30+
public get args(): DebugProtocol.IRequestArgs {
31+
return this._requestArgs;
32+
}
33+
34+
public get launchArgs(): DebugProtocol.ILaunchRequestArgs {
35+
return this.isLaunch ? <DebugProtocol.ILaunchRequestArgs>this.args : null;
36+
}
37+
38+
public get attachArgs(): DebugProtocol.IAttachRequestArgs {
39+
return this.isAttach ? <DebugProtocol.IAttachRequestArgs>this.args : null;
40+
}
41+
}

src/debug-adapter/webKitDebugAdapter.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as ns from '../services/NsCliService';
1717
import {AnalyticsService} from '../services/analytics/AnalyticsService';
1818
import {ExtensionClient} from '../services/ipc/ExtensionClient';
1919
import {Logger, LoggerHandler, Handlers, Tags} from '../services/Logger';
20+
import {DebugConfiguration} from './debugConfiguration';
2021

2122
interface IScopeVarHandle {
2223
objectId: string;
@@ -42,6 +43,7 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
4243
private platform: string;
4344
private _lastOutputEvent: OutputEvent;
4445
private _loggerFrontendHandler: LoggerHandler = args => this.fireEvent(new OutputEvent(` ›${args.message}\n`, args.type.toString()));
46+
private _debugConfig: DebugConfiguration;
4547

4648
public constructor() {
4749
this._variableHandles = new Handles<IScopeVarHandle>();
@@ -100,14 +102,15 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
100102
}
101103

102104
public launch(args: DebugProtocol.ILaunchRequestArgs): Promise<void> {
103-
return this._attach(args);
105+
return this.processRequest(args);
104106
}
105107

106108
public attach(args: DebugProtocol.IAttachRequestArgs): Promise<void> {
107-
return this._attach(args);
109+
return this.processRequest(args);
108110
}
109111

110-
private configureLoggingForRequest(name: string, args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): void {
112+
private configureLoggingForRequest(): void {
113+
let args = this._debugConfig.args;
111114
if (args.diagnosticLogging) {
112115
// The logger frontend handler is initially configured to handle messages with LoggerTagFrontendMessage tag only.
113116
// We remove the handler and add it again for all messages.
@@ -118,18 +121,19 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
118121
Logger.addHandler(Handlers.createStreamHandler(fs.createWriteStream(args.tnsOutput)));
119122
}
120123
Logger.log(`initialize(${JSON.stringify(this._initArgs) })`);
121-
Logger.log(`${name}(${JSON.stringify(args) })`);
124+
Logger.log(`${this._debugConfig.args.request}(${JSON.stringify(this._debugConfig.isAttach ? this._debugConfig.attachArgs : this._debugConfig.launchArgs) })`);
122125
}
123126

124-
private _attach(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs) {
127+
private processRequest(args: DebugProtocol.IRequestArgs) {
128+
this._debugConfig = new DebugConfiguration(args);
125129
ExtensionClient.setAppRoot(args.appRoot);
126-
let analyticsRequest = (args.request == "launch" && !(args as DebugProtocol.ILaunchRequestArgs).rebuild) ? "sync" : args.request;
130+
let analyticsRequest = this._debugConfig.isSync ? "sync" : args.request;
127131
ExtensionClient.getInstance().analyticsLaunchDebugger({ request: analyticsRequest, platform: args.platform });
128-
this.configureLoggingForRequest(args.request, args);
132+
this.configureLoggingForRequest();
129133
this.appRoot = args.appRoot;
130134
this.platform = args.platform;
131135

132-
return ((args.platform == 'ios') ? this._attachIos(args) : this._attachAndroid(args))
136+
return ((args.platform == 'ios') ? this._attachIos() : this._attachAndroid())
133137
.then(() => {
134138
this.fireEvent(new InitializedEvent());
135139
},
@@ -140,18 +144,20 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
140144
});
141145
}
142146

143-
private _attachIos(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): Promise<void> {
147+
private _attachIos(): Promise<void> {
148+
let args = this._debugConfig.args;
144149
let iosProject : ns.IosProject = new ns.IosProject(this.appRoot, args.tnsOutput);
145150

146151
return iosProject.debug(args)
147152
.then((socketFilePath) => {
148153
let iosConnection: IosConnection = new IosConnection();
149-
this.setConnection(iosConnection, args);
154+
this.setConnection(iosConnection);
150155
return iosConnection.attach(socketFilePath);
151156
});
152157
}
153158

154-
private _attachAndroid(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): Promise<void> {
159+
private _attachAndroid(): Promise<void> {
160+
let args = this._debugConfig.args;
155161
let androidProject: ns.AndroidProject = new ns.AndroidProject(this.appRoot, args.tnsOutput);
156162
let thisAdapter: WebKitDebugAdapter = this;
157163

@@ -166,7 +172,7 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
166172
port = debugPort;
167173
if (!thisAdapter._webKitConnection) {
168174
androidConnection = new AndroidConnection();
169-
this.setConnection(androidConnection, args);
175+
this.setConnection(androidConnection);
170176
}
171177
}).then(() => {
172178
Logger.log("Attaching to debug application");
@@ -175,7 +181,8 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
175181
});
176182
}
177183

178-
private setConnection(connection: INSDebugConnection, args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs) : INSDebugConnection {
184+
private setConnection(connection: INSDebugConnection) : INSDebugConnection {
185+
let args = this._debugConfig.args;
179186
connection.on('Debugger.paused', params => this.onDebuggerPaused(params));
180187
connection.on('Debugger.resumed', () => this.onDebuggerResumed());
181188
connection.on('Debugger.scriptParsed', params => this.onScriptParsed(params));
@@ -186,12 +193,12 @@ export class WebKitDebugAdapter implements DebugProtocol.IDebugAdapter {
186193
connection.on('Inspector.detached', () => this.terminateSession());
187194
connection.on('close', () => this.terminateSession());
188195
connection.on('error', () => this.terminateSession());
189-
connection.on('connect', () => this.onConnected(args))
196+
connection.on('connect', () => this.onConnected())
190197
this._webKitConnection = connection;
191198
return connection;
192199
}
193200

194-
private onConnected(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): void {
201+
private onConnected(): void {
195202
Logger.log("Debugger connected");
196203
}
197204

src/services/NsCliService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export abstract class NSProject extends EventEmitter {
8787

8888
public abstract run(): Promise<ChildProcess>;
8989

90-
public abstract debug(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): Promise<any>;
90+
public abstract debug(args: DebugProtocol.IRequestArgs): Promise<any>;
9191

9292
protected spawnProcess(commandPath: string, commandArgs: string[], tnsOutput?: string): ChildProcess {
9393
let options = { cwd: this.getProjectPath(), shell: true };
@@ -123,7 +123,7 @@ export class IosProject extends NSProject {
123123
return Promise.resolve(child);
124124
}
125125

126-
public debug(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): Promise<string> {
126+
public debug(args: DebugProtocol.IRequestArgs): Promise<string> {
127127
if (!this.isOSX()) {
128128
return Promise.reject('iOS platform is supported only on OS X.');
129129
}
@@ -211,7 +211,7 @@ export class AndroidProject extends NSProject {
211211
return Promise.resolve(child);
212212
}
213213

214-
public debug(params: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): Promise<void> {
214+
public debug(params: DebugProtocol.IRequestArgs): Promise<void> {
215215
if (params.request === "attach") {
216216
return Promise.resolve<void>();
217217
}
@@ -266,7 +266,7 @@ export class AndroidProject extends NSProject {
266266
}
267267
}
268268

269-
public getDebugPort(args: DebugProtocol.IAttachRequestArgs | DebugProtocol.ILaunchRequestArgs): Promise<number> {
269+
public getDebugPort(args: DebugProtocol.IRequestArgs): Promise<number> {
270270
//TODO: Call CLI to get the debug port
271271
//return Promise.resolve(40001);
272272

0 commit comments

Comments
 (0)