forked from ajshort/vscode-ros
-
Notifications
You must be signed in to change notification settings - Fork 93
/
launch.ts
309 lines (278 loc) · 12.5 KB
/
launch.ts
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) Andrew Short. All rights reserved.
// Licensed under the MIT License.
import * as child_process from "child_process";
import * as fs from "fs";
import * as yaml from "js-yaml";
import * as os from "os";
import * as path from "path";
import * as readline from "readline";
import * as shell_quote from "shell-quote";
import * as tmp from "tmp";
import * as util from "util";
import * as vscode from "vscode";
import * as extension from "../../../../extension";
import * as requests from "../../../requests";
import * as utils from "../../../utils";
import { rosApi } from "../../../../ros/ros";
import { env } from "../../../../extension";
const promisifiedExec = util.promisify(child_process.exec);
interface ILaunchRequest {
nodeName: string;
executable: string;
arguments: string[];
cwd: string;
env: { [key: string]: string };
}
export class LaunchResolver implements vscode.DebugConfigurationProvider {
// tslint:disable-next-line: max-line-length
public async resolveDebugConfigurationWithSubstitutedVariables(folder: vscode.WorkspaceFolder | undefined, config: requests.ILaunchRequest, token?: vscode.CancellationToken) {
if (!path.isAbsolute(config.target) || (path.extname(config.target) !== ".launch" && path.extname(config.target) !== ".test")) {
throw new Error("Launch request requires an absolute path as target.");
}
const delay = ms => new Promise(res => setTimeout(res, ms));
switch (env.ROS_VERSION.trim()) {
case "1": {
// Manage the status of the ROS core, starting one if not present
// The ROS core will continue to run until the VSCode window is closed
if (await rosApi.getCoreStatus() == false) {
console.log("ROS Core is not active, attempting to start automatically");
rosApi.startCore();
// Wait for the core to start up to a timeout
const timeout_ms: number = 30000;
const interval_ms: number = 100;
let timeWaited: number = 0;
while (await rosApi.getCoreStatus() == false &&
timeWaited < timeout_ms) {
timeWaited += interval_ms;
await delay(interval_ms);
}
console.log("Waited " + timeWaited + " for ROS Core to start");
if (timeWaited >= timeout_ms) {
throw new Error('Timed out (' + timeWaited / 1000 + ' seconds) waiting for ROS Core to start. Start ROSCore manually to avoid this error.');
}
}
break;
}
case "2": {
// TODO(#431), support starting the ROS2 daemon automatically
break;
}
}
const rosExecOptions: child_process.ExecOptions = {
env: await extension.resolvedEnv(),
};
let result = await promisifiedExec(`roslaunch --dump-params ${config.target}`, rosExecOptions);
if (result.stderr) {
throw (new Error(`Error from roslaunch:\r\n ${result.stderr}`));
} else if (result.stdout.length == 0) {
throw (new Error(`roslaunch unexpectedly produced no output, please test by running \"roslaunch --dump-params ${config.target}\" in a ros terminal.`));
}
const parameters = Object.keys(yaml.load(result.stdout));
if (parameters && parameters.length) {
// only call into rosparam when necessary
const tmpFile = tmp.fileSync();
fs.writeFile(`${tmpFile.name}`, result.stdout, async (error) => {
if (error) {
throw error;
}
await promisifiedExec(`rosparam load ${tmpFile.name}`, rosExecOptions);
tmpFile.removeCallback();
});
}
result = await promisifiedExec(`roslaunch --nodes ${config.target}`, rosExecOptions);
if (result.stderr) {
throw (new Error(`Error from roslaunch:\r\n ${result.stderr}`));
} else if (result.stdout.length == 0) {
throw (new Error(`roslaunch unexpectedly produced no output, please test by running \"roslaunch --dump-params ${config.target}\" in a ros terminal.`));
}
const nodes = result.stdout.trim().split(os.EOL);
await Promise.all(nodes.map((node: string) => {
return promisifiedExec(`roslaunch --args ${node} ${config.target}`, rosExecOptions);
})).then((commands: Array<{ stdout: string; stderr: string; }>) => {
commands.forEach((command, index) => {
const launchRequest = this.generateLaunchRequest(nodes[index], command.stdout);
this.executeLaunchRequest(launchRequest, false);
});
});
// @todo: error handling for Promise.all
// Return null as we have spawned new debug requests
return null;
}
private generateLaunchRequest(nodeName: string, command: string): ILaunchRequest {
let parsedArgs: shell_quote.ParseEntry[];
const isWindows = os.platform() === "win32";
if (isWindows) {
// https://github.com/ros/ros_comm/pull/1809
// escape backslash in file path
parsedArgs = shell_quote.parse(command.replace(/[\\]/g, "\\$&"));
parsedArgs = shell_quote.parse(parsedArgs[2].toString().replace(/[\\]/g, "\\$&"));
} else {
parsedArgs = shell_quote.parse(command);
}
const envConfig: { [key: string]: string; } = {};
while (parsedArgs) {
// https://github.com/ros/ros_comm/pull/1809
if (isWindows && parsedArgs[0].toString() === "set") {
parsedArgs.shift();
}
if (parsedArgs[0].toString().includes("=")) {
const arg = parsedArgs.shift().toString();
envConfig[arg.substring(0, arg.indexOf("="))] = arg.substring(arg.indexOf("=") + 1);
// https://github.com/ros/ros_comm/pull/1809
// "&&" is treated as Object
if (isWindows && parsedArgs[0] instanceof Object) {
parsedArgs.shift();
}
} else {
break;
}
}
const request: ILaunchRequest = {
nodeName: nodeName,
executable: parsedArgs.shift().toString(),
arguments: parsedArgs.map((arg) => {
return arg.toString();
}),
cwd: ".",
env: {
...extension.env,
...envConfig,
},
};
return request;
}
private async executeLaunchRequest(request: ILaunchRequest, stopOnEntry: boolean) {
let debugConfig: ICppvsdbgLaunchConfiguration | ICppdbgLaunchConfiguration | IPythonLaunchConfiguration;
if (os.platform() === "win32") {
if (request.executable.toLowerCase().endsWith("python") ||
request.executable.toLowerCase().endsWith("python.exe")) {
const pythonScript: string = request.arguments.shift();
const pythonLaunchConfig: IPythonLaunchConfiguration = {
name: request.nodeName,
type: "python",
request: "launch",
program: pythonScript,
args: request.arguments,
env: request.env,
stopOnEntry: stopOnEntry,
justMyCode: false,
};
debugConfig = pythonLaunchConfig;
} else if (request.executable.endsWith(".exe")) {
interface ICppEnvConfig {
name: string;
value: string;
}
const envConfigs: ICppEnvConfig[] = [];
for (const key in request.env) {
if (request.env.hasOwnProperty(key)) {
envConfigs.push({
name: key,
value: request.env[key],
});
}
}
const cppvsdbgLaunchConfig: ICppvsdbgLaunchConfiguration = {
name: request.nodeName,
type: "cppvsdbg",
request: "launch",
cwd: ".",
program: request.executable,
args: request.arguments,
environment: envConfigs,
stopAtEntry: stopOnEntry,
};
debugConfig = cppvsdbgLaunchConfig;
}
if (!debugConfig) {
throw (new Error(`Failed to create a debug configuration!`));
}
const launched = await vscode.debug.startDebugging(undefined, debugConfig);
if (!launched) {
throw (new Error(`Failed to start debug session!`));
}
} else {
try {
// this should be guaranteed by roslaunch
fs.accessSync(request.executable, fs.constants.X_OK);
} catch (errNotExecutable) {
throw (new Error(`Error! ${request.executable} is not executable!`));
}
try {
// need to be readable to check shebang line
fs.accessSync(request.executable, fs.constants.R_OK);
} catch (errNotReadable) {
throw (new Error(`Error! ${request.executable} is not readable!`));
}
const fileStream = fs.createReadStream(request.executable);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
// we only want to read 1 line to check for shebang line
let linesToRead: number = 1;
rl.on("line", async (line) => {
if (linesToRead <= 0) {
return;
}
linesToRead--;
if (!linesToRead) {
rl.close();
}
// look for Python in shebang line
if (line.startsWith("#!") && line.toLowerCase().indexOf("python") !== -1) {
const pythonLaunchConfig: IPythonLaunchConfiguration = {
name: request.nodeName,
type: "python",
request: "launch",
program: request.executable,
args: request.arguments,
env: request.env,
stopOnEntry: stopOnEntry,
justMyCode: false,
};
debugConfig = pythonLaunchConfig;
} else {
interface ICppEnvConfig {
name: string;
value: string;
}
const envConfigs: ICppEnvConfig[] = [];
for (const key in request.env) {
if (request.env.hasOwnProperty(key)) {
envConfigs.push({
name: key,
value: request.env[key],
});
}
}
const cppdbgLaunchConfig: ICppdbgLaunchConfiguration = {
name: request.nodeName,
type: "cppdbg",
request: "launch",
cwd: ".",
program: request.executable,
args: request.arguments,
environment: envConfigs,
stopAtEntry: stopOnEntry,
setupCommands: [
{
text: "-enable-pretty-printing",
description: "Enable pretty-printing for gdb",
ignoreFailures: true
}
]
};
debugConfig = cppdbgLaunchConfig;
}
if (!debugConfig) {
throw (new Error(`Failed to create a debug configuration!`));
}
const launched = await vscode.debug.startDebugging(undefined, debugConfig);
if (!launched) {
throw (new Error(`Failed to start debug session!`));
}
});
}
}
}