-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathidl_generator.js
331 lines (299 loc) · 9.27 KB
/
idl_generator.js
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) 2018 Intel Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
const dot = require('dot');
const prettier = require('prettier');
const fse = require('fs-extra');
const path = require('path');
const parser = require('../rosidl_parser/rosidl_parser.js');
const actionMsgs = require('./action_msgs.js');
const DistroUtils = require('../lib/distro.js');
dot.templateSettings.strip = false;
dot.log = process.env.RCLNODEJS_LOG_VERBOSE || false;
const isDebug = !!process.argv.find((arg) => arg === '--debug');
const dots = dot.process({
path: path.join(__dirname, '../rosidl_gen/templates'),
});
function removeEmptyLines(str) {
return str.replace(/^\s*\n/gm, '');
}
/**
* Output generated code to disk. Do not overwrite
* an existing file. If file already exists do nothing.
* @param {string} dir
* @param {string} fileName
* @param {string} code
*/
async function writeGeneratedCode(dir, fileName, code) {
if (fileName.endsWith('.js')) {
code = await prettier.format(code, { parser: 'babel' });
}
await fse.mkdirs(dir);
await fse.writeFile(path.join(dir, fileName), code);
}
async function generateServiceJSStruct(
serviceInfo,
dir,
isActionService = true
) {
dir = path.join(dir, `${serviceInfo.pkgName}`);
const fileName =
serviceInfo.pkgName +
'__' +
serviceInfo.subFolder +
'__' +
serviceInfo.interfaceName +
'.js';
const generatedSrvCode = removeEmptyLines(
dots.service({ serviceInfo: serviceInfo })
);
// We are going to only generate the service JavaScript file if it meets one
// of the followings:
// 1. It's a action's request/response service.
// 2. For pre-Humble ROS 2 releases, because it doesn't support service
// introspection.
if (
isActionService ||
DistroUtils.getDistroId() <= DistroUtils.getDistroId('humble')
) {
return writeGeneratedCode(dir, fileName, generatedSrvCode);
}
return writeGeneratedCode(dir, fileName, generatedSrvCode).then(() => {
return generateServiceEventMsg(serviceInfo, dir);
});
}
async function generateServiceEventMsg(serviceInfo, dir) {
const fileName = serviceInfo.interfaceName + '.msg';
const generatedEvent = removeEmptyLines(
dots.service_event({ serviceInfo: serviceInfo })
);
return writeGeneratedCode(dir, fileName, generatedEvent).then(() => {
serviceInfo.interfaceName += '_Event';
serviceInfo.filePath = path.join(dir, fileName);
return generateServiceEventJSStruct(serviceInfo, dir);
});
}
async function generateServiceEventJSStruct(msgInfo, dir) {
const spec = await parser.parseMessageFile(msgInfo.pkgName, msgInfo.filePath);
// Pass `msgInfo.subFolder` to the `spec`, because some .srv files of the
// package may not be put under srv/ folder, e.g., slam_toolbox.
spec.subFolder = msgInfo.subFolder;
// Remove the `.msg` files generated in `generateServiceEventMsg()` to avoid
// being found later.
fse.removeSync(msgInfo.filePath);
const eventFileName =
msgInfo.pkgName +
'__' +
msgInfo.subFolder +
'__' +
msgInfo.interfaceName +
'.js';
// Set `msgInfo.isServiceEvent` to true, so when generating the JavaScript
// message files for the service event leveraging message.dot, it will use
// "__srv__" to require the JS files for the request/response of a specific
// service, e.g.,
// const AddTwoInts_RequestWrapper = require('../../generated/example_interfaces/example_interfaces__srv__AddTwoInts_Request.js');
// const AddTwoInts_ResponseWrapper = require('../../generated/example_interfaces/example_interfaces__srv__AddTwoInts_Response.js');
msgInfo.isServiceEvent = true;
const generatedCode = removeEmptyLines(
dots.message({
messageInfo: msgInfo,
spec: spec,
json: JSON.stringify(spec, null, ' '),
isDebug: isDebug,
})
);
return writeGeneratedCode(dir, eventFileName, generatedCode);
}
async function generateMessageJSStruct(messageInfo, dir) {
const spec = await parser.parseMessageFile(
messageInfo.pkgName,
messageInfo.filePath
);
await generateMessageJSStructFromSpec(messageInfo, dir, spec);
}
function generateMessageJSStructFromSpec(messageInfo, dir, spec) {
dir = path.join(dir, `${spec.baseType.pkgName}`);
const fileName =
spec.baseType.pkgName +
'__' +
messageInfo.subFolder +
'__' +
spec.msgName +
'.js';
const generatedCode = removeEmptyLines(
dots.message({
messageInfo: messageInfo,
spec: spec,
json: JSON.stringify(spec, null, ' '),
isDebug: isDebug,
})
);
return writeGeneratedCode(dir, fileName, generatedCode);
}
async function generateActionJSStruct(actionInfo, dir) {
const spec = await parser.parseActionFile(
actionInfo.pkgName,
actionInfo.filePath
);
const goalMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_Goal`,
},
dir,
spec.goal
);
const resultMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_Result`,
},
dir,
spec.result
);
const feedbackMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_Feedback`,
},
dir,
spec.feedback
);
const sendGoalRequestSpec = actionMsgs.createSendGoalRequestSpec(
actionInfo.pkgName,
actionInfo.interfaceName
);
const sendGoalRequestMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_SendGoal_Request`,
},
dir,
sendGoalRequestSpec
);
const sendGoalResponseSpec = actionMsgs.createSendGoalResponseSpec(
actionInfo.pkgName,
actionInfo.interfaceName
);
const sendGoalResponseMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_SendGoal_Response`,
},
dir,
sendGoalResponseSpec
);
const sendGoalSrv = generateServiceJSStruct(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_SendGoal`,
},
dir
);
const getResultRequestSpec = actionMsgs.createGetResultRequestSpec(
actionInfo.pkgName,
actionInfo.interfaceName
);
const getResultRequestMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_GetResult_Request`,
},
dir,
getResultRequestSpec
);
const getResultResponseSpec = actionMsgs.createGetResultResponseSpec(
actionInfo.pkgName,
actionInfo.interfaceName
);
const getResultResponseMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_GetResult_Response`,
},
dir,
getResultResponseSpec
);
const getResultSrv = generateServiceJSStruct(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_GetResult`,
},
dir
);
const feedbackMessageSpec = actionMsgs.createFeedbackMessageSpec(
actionInfo.pkgName,
actionInfo.interfaceName
);
const feedbackMessageMsg = generateMessageJSStructFromSpec(
{
pkgName: actionInfo.pkgName,
subFolder: actionInfo.subFolder,
interfaceName: `${actionInfo.interfaceName}_FeedbackMessage`,
},
dir,
feedbackMessageSpec
);
const fileName =
actionInfo.pkgName +
'__' +
actionInfo.subFolder +
'__' +
actionInfo.interfaceName +
'.js';
const generatedCode = removeEmptyLines(
dots.action({ actionInfo: actionInfo })
);
dir = path.join(dir, actionInfo.pkgName);
const action = writeGeneratedCode(dir, fileName, generatedCode);
await Promise.all([
goalMsg,
resultMsg,
feedbackMsg,
sendGoalRequestMsg,
sendGoalResponseMsg,
sendGoalSrv,
getResultRequestMsg,
getResultResponseMsg,
getResultSrv,
feedbackMessageMsg,
action,
]);
}
async function generateJSStructFromIDL(pkg, dir) {
const results = [];
pkg.messages.forEach((messageInfo) => {
results.push(generateMessageJSStruct(messageInfo, dir));
});
pkg.services.forEach((serviceInfo) => {
results.push(
generateServiceJSStruct(serviceInfo, dir, /*isActionService=*/ false)
);
});
pkg.actions.forEach((actionInfo) => {
results.push(generateActionJSStruct(actionInfo, dir));
});
await Promise.all(results);
}
module.exports = generateJSStructFromIDL;