-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrubyDebug.ts
More file actions
81 lines (76 loc) · 2.53 KB
/
rubyDebug.ts
File metadata and controls
81 lines (76 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as vscode from 'vscode';
import * as Net from 'net';
import { rubySpawn } from 'ruby-spawn';
import { ChildProcess } from 'child_process';
export class RubyDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(session: vscode.DebugSession, executable: vscode.DebugAdapterExecutable | undefined): vscode.ProviderResult<vscode.DebugAdapterDescriptor> {
return new Promise((resolve, reject) => {
let socket = new Net.Socket();
let child: ChildProcess;
socket.on('connect', () => {
if (socket.remotePort && socket.remoteAddress) {
resolve(new vscode.DebugAdapterServer(socket.remotePort, socket.remoteAddress));
} else {
reject(new Error("Connection to debugger could not be resolved"));
}
});
socket.on('error', (err) => {
if (child) {
child.kill();
}
reject(err);
});
Object.assign(process.env, session.configuration.env);
if (session.configuration.request === 'attach') {
let host = session.configuration.host || '127.0.0.1';
let port = session.configuration.port || 1234;
socket.connect(port, host);
} else {
let opts = {};
if (session.workspaceFolder) {
opts['cwd'] = session.workspaceFolder.uri.fsPath;
}
let dbg = (session.configuration.debugger || 'readapt');
let dbgArgs = ['serve'].concat(session.configuration.debuggerArgs || []);
if (session.configuration.useBundler) {
dbgArgs.unshift(dbg);
dbgArgs.unshift('exec');
dbg = 'bundle';
}
child = rubySpawn(dbg, dbgArgs, opts, true);
let started = false;
child.stderr.on('data', (buffer: Buffer) => {
let text = buffer.toString();
if (!started) {
if (text.match(/^Readapt Debugger/)) {
let match = text.match(/HOST=([^\s]*)[\s]+PORT=([0-9]*)/);
if (match) {
started = true;
socket.connect(parseInt(match[2]), match[1]);
} else {
reject(new Error("Unable to determine debugger host and port"));
}
}
}
vscode.debug.activeDebugConsole.append(text);
});
child.stdout.on('data', (buffer: Buffer) => {
let text = buffer.toString();
vscode.debug.activeDebugConsole.append(text);
});
child.on('exit', (code) => {
if (!started) {
let message = `Debugger exited without connecting (exit code ${code})`;
if (session.configuration.useBundler) {
message += "\nIs readapt included in your Gemfile?"
}
reject(new Error(message));
}
});
}
});
}
dispose() {
// Nothing to do
}
}