forked from RobotWebTools/rclnodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
597 lines (502 loc) · 16.5 KB
/
index.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/* eslint-disable max-depth */
/* eslint-disable no-sync */
/* eslint-disable camelcase */
// 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.
// create interfaces.d.ts containing from each typeclass definition
/* Example output for std_msgs_msg_String
declare module "rclnodejs" {
namespace std_msgs {
namespace msg {
export type String = {
data: string
}
}
}
}
*/
'use strict';
const path = require('path');
const fs = require('fs');
const loader = require('../lib/interface_loader.js');
const pkgFilters = require('../rosidl_gen/filter.js');
async function generateAll() {
// load pkg and interface info (msgs and srvs)
const generatedPath = path.join(__dirname, '../generated/');
const pkgInfos = getPkgInfos(generatedPath);
// write message.d.ts file
const messagesFilePath = path.join(__dirname, '../types/interfaces.d.ts');
const fd = fs.openSync(messagesFilePath, 'w');
savePkgInfoAsTSD(pkgInfos, fd);
await wait(500); // hack to avoid random segfault
fs.closeSync(fd);
}
// scan generated files, i.e., rootDir, and collect pkg and ROS2 interface info
function getPkgInfos(rootDir) {
let pkgInfos = [];
let pkgs = fs.readdirSync(rootDir);
for (let pkg of pkgs) {
if (pkg.endsWith('.json')) continue;
const pkgInfo = {
name: pkg,
subfolders: new Map(),
};
const pkgPath = path.join(rootDir, pkg);
const files = fs.readdirSync(pkgPath).filter((fn) => fn.endsWith('.js'));
for (let filename of files) {
const typeClass = fileName2Typeclass(filename);
if (
!typeClass.type ||
pkgFilters.matchesAny({
pkgName: typeClass.package,
interfaceName: typeClass.name,
})
)
continue;
const rosInterface = loader.loadInterface(typeClass);
if (!pkgInfo.subfolders.has(typeClass.type)) {
pkgInfo.subfolders.set(typeClass.type, []);
}
pkgInfo.subfolders.get(typeClass.type).push(rosInterface);
}
pkgInfos.push(pkgInfo);
}
return pkgInfos;
}
function savePkgInfoAsTSD(pkgInfos, fd) {
const messagesMap = {
string: 'string',
};
const servicesMap = {};
const actionsMap = {};
fs.writeSync(fd, '/* eslint-disable camelcase */\n');
fs.writeSync(fd, '/* eslint-disable max-len */\n');
fs.writeSync(fd, '// DO NOT EDIT\n');
fs.writeSync(fd, '// This file is generated by the rostsd_gen script\n\n');
fs.writeSync(fd, "declare module 'rclnodejs' {\n");
for (const pkgInfo of pkgInfos) {
if (pkgInfo.subfolders.size === 0) continue;
// write namespaces heirarchy for package
fs.writeSync(fd, ` namespace ${pkgInfo.name} {\n`);
for (const subfolder of pkgInfo.subfolders.keys()) {
fs.writeSync(fd, ` namespace ${subfolder} {\n`);
for (const rosInterface of pkgInfo.subfolders.get(subfolder)) {
const type = rosInterface.type();
const fullInterfaceName = `${type.pkgName}/${type.subFolder}/${type.interfaceName}`;
const fullInterfacePath = `${type.pkgName}.${type.subFolder}.${type.interfaceName}`;
const fullInterfaceConstructor = fullInterfacePath + 'Constructor';
if (isMsgInterface(rosInterface)) {
// create message interface
saveMsgAsTSD(rosInterface, fd);
saveMsgConstructorAsTSD(rosInterface, fd);
messagesMap[fullInterfaceName] = fullInterfacePath;
} else if (isSrvInterface(rosInterface)) {
if (
!isValidService(rosInterface, pkgInfo.subfolders.get(subfolder))
) {
let type = rosInterface.type();
console.log(
`Incomplete service: ${type.pkgName}.${type.subFolder}.${type.interfaceName}.`
);
continue;
}
// create service interface
saveSrvAsTSD(rosInterface, fd);
if (!isInternalActionSrvInterface(rosInterface)) {
servicesMap[fullInterfaceName] = fullInterfaceConstructor;
}
} else if (isActionInterface(rosInterface)) {
if (!isValidAction(rosInterface, pkgInfo.subfolders.get(subfolder))) {
let type = rosInterface.type();
console.log(
`Incomplete action: ${type.pkgName}.${type.subFolder}.${type.interfaceName}.`
);
continue;
}
// create action interface
saveActionAsTSD(rosInterface, fd);
actionsMap[fullInterfaceName] = fullInterfaceConstructor;
}
}
// close namespace declare
fs.writeSync(fd, ' }\n');
}
// close pkg level namespace declare
fs.writeSync(fd, ' }\n\n');
}
// write messages type mappings
fs.writeSync(fd, ' type MessagesMap = {\n');
for (const key in messagesMap) {
fs.writeSync(fd, ` '${key}': ${messagesMap[key]},\n`);
}
fs.writeSync(fd, ' };\n');
fs.writeSync(fd, ' type MessageTypeClassName = keyof MessagesMap;\n');
fs.writeSync(fd, ' type Message = MessagesMap[MessageTypeClassName];\n');
fs.writeSync(
fd,
' type MessageType<T> = T extends MessageTypeClassName ? MessagesMap[T] : object;\n\n'
);
// write message contructor mappings
fs.writeSync(fd, ' type MessageTypeClassConstructorMap = {\n');
for (const key in messagesMap) {
if (key === 'string') {
fs.writeSync(fd, " 'string': never,\n");
continue;
}
fs.writeSync(fd, ` '${key}': ${messagesMap[key]}Constructor,\n`);
}
fs.writeSync(fd, ' };\n');
fs.writeSync(
fd,
' type MessageConstructorType<T> = ' +
'T extends MessageTypeClassName ? MessageTypeClassConstructorMap[T] : object;\n\n'
);
// write services type mappings
fs.writeSync(fd, ' type ServicesMap = {\n');
for (const key in servicesMap) {
fs.writeSync(fd, ` '${key}': ${servicesMap[key]},\n`);
}
fs.writeSync(fd, ' };\n');
fs.writeSync(fd, ' type ServiceTypeClassName = keyof ServicesMap;\n');
fs.writeSync(fd, ' type Service = ServicesMap[ServiceTypeClassName];\n');
fs.writeSync(
fd,
' type ServiceType<T> = T extends ServiceTypeClassName ? ServicesMap[T] : object;\n\n'
);
// write actions type mappings
fs.writeSync(fd, ' type ActionsMap = {\n');
for (const key in actionsMap) {
fs.writeSync(fd, ` '${key}': ${actionsMap[key]},\n`);
}
fs.writeSync(fd, ' };\n');
fs.writeSync(fd, ' type ActionTypeClassName = keyof ActionsMap;\n');
fs.writeSync(fd, ' type Action = ActionsMap[ActionTypeClassName];\n');
fs.writeSync(
fd,
' type ActionType<T> = T extends ActionTypeClassName ? ActionsMap[T] : object;\n\n'
);
fs.writeSync(
fd,
' type TypeClassName = MessageTypeClassName | ServiceTypeClassName | ActionTypeClassName;\n'
);
fs.writeSync(
fd,
' type InterfaceType<T> = T extends TypeClassName ? ' +
'(MessageTypeClassConstructorMap & ServicesMap & ActionsMap)[T] : object;\n'
);
// close module declare
fs.writeSync(fd, '}\n');
}
function saveMsgAsTSD(rosMsgInterface, fd) {
fs.writeSync(
fd,
` export interface ${rosMsgInterface.type().interfaceName} {\n`
);
const useSamePkg = isInternalActionMsgInterface(rosMsgInterface);
saveMsgFieldsAsTSD(rosMsgInterface, fd, 8, ';', '', useSamePkg);
fs.writeSync(fd, ' }\n');
}
/**
* Writes the message fields as typescript definitions.
*
* @param {*} rosMsgInterface ros message
* @param {*} fd file descriptor
* @param {string} indent The amount of indent, in spaces
* @param {string} lineEnd The character to put at the end of each line, usually ','
* or ';'
* @param {string} typePrefix The prefix to put before the type name for
* non-primitive types
* @param {boolean} useSamePackageSubFolder Indicates if the sub folder name should be taken from the message
* when the field type comes from the same package. This is needed for action interfaces. Defaults to false.
* @returns {undefined}
*/
function saveMsgFieldsAsTSD(
rosMsgInterface,
fd,
indent = 0,
lineEnd = ',',
typePrefix = '',
useSamePackageSubFolder = false
) {
let type = rosMsgInterface.type();
let fields = rosMsgInterface.ROSMessageDef.fields;
for (const field of fields) {
let subFolder =
useSamePackageSubFolder && field.type.pkgName === type.pkgName
? type.subFolder
: 'msg';
let fieldType = fieldType2JSName(field, subFolder);
let tp = field.type.isPrimitiveType ? '' : typePrefix;
if (typePrefix === 'rclnodejs.') {
fieldType = 'any';
tp = '';
}
const tmpl = indentString(`${field.name}: ${tp}${fieldType}`, indent);
fs.writeSync(fd, tmpl);
if (field.type.isArray) {
fs.writeSync(fd, '[]');
if (fieldType === 'number') {
// for number[] include alternate typed-array types, e.g., number[] | uint8[]
let jsTypedArrayName = fieldTypeArray2JSTypedArrayName(field.type.type);
if (jsTypedArrayName) {
fs.writeSync(fd, ` | ${jsTypedArrayName}`);
}
}
}
fs.writeSync(fd, lineEnd);
fs.writeSync(fd, '\n');
}
}
function saveMsgConstructorAsTSD(rosMsgInterface, fd) {
const type = rosMsgInterface.type();
const msgName = type.interfaceName;
fs.writeSync(fd, ` export interface ${msgName}Constructor {\n`);
for (const constant of rosMsgInterface.ROSMessageDef.constants) {
const constantType = primitiveType2JSName(constant.type);
fs.writeSync(fd, ` readonly ${constant.name}: ${constantType};\n`);
}
fs.writeSync(fd, ` new(other?: ${msgName}): ${msgName};\n`);
fs.writeSync(fd, ' }\n');
}
function saveSrvAsTSD(rosSrvInterface, fd) {
const serviceName = rosSrvInterface.type().interfaceName;
const interfaceTemplate = [
`export interface ${serviceName}Constructor extends ROSService {`,
` readonly Request: ${serviceName}_RequestConstructor;`,
` readonly Response: ${serviceName}_ResponseConstructor;`,
'}',
'',
];
fs.writeSync(fd, indentLines(interfaceTemplate, 6).join('\n'));
}
function saveActionAsTSD(rosActionInterface, fd) {
const actionName = rosActionInterface.type().interfaceName;
const interfaceTemplate = [
`export interface ${actionName}Constructor {`,
` readonly Goal: ${actionName}_GoalConstructor;`,
` readonly Result: ${actionName}_ResultConstructor;`,
` readonly Feedback: ${actionName}_FeedbackConstructor;`,
'}',
'',
];
fs.writeSync(fd, indentLines(interfaceTemplate, 6).join('\n'));
}
function isMsgInterface(rosInterface) {
return rosInterface.hasOwnProperty('ROSMessageDef');
}
function isServiceMsgInterface(rosMsgInterface) {
if (!isMsgInterface(rosMsgInterface)) return false;
let name = rosMsgInterface.type().interfaceName;
return name.endsWith('_Request') || name.endsWith('_Response');
}
function isInternalActionMsgInterface(rosMsgInterface) {
let name = rosMsgInterface.type().interfaceName;
return (
name.endsWith('_FeedbackMessage') ||
name.endsWith('_SendGoal_Request') ||
name.endsWith('_SendGoal_Response') ||
name.endsWith('_GetResult_Request') ||
name.endsWith('_GetResult_Response')
);
}
function isSrvInterface(rosInterface) {
return (
rosInterface.hasOwnProperty('Request') &&
rosInterface.hasOwnProperty('Response')
);
}
function isInternalActionSrvInterface(rosInterface) {
if (!isSrvInterface(rosInterface)) return false;
let name = rosInterface.type().interfaceName;
return name.endsWith('_GetResult') || name.endsWith('_SendGoal');
}
function isActionInterface(rosInterface, pkgInfos) {
return (
rosInterface.hasOwnProperty('Feedback') &&
rosInterface.hasOwnProperty('Goal') &&
rosInterface.hasOwnProperty('Result')
);
}
function isValidService(rosSrvInterface, infos) {
if (!isSrvInterface(rosSrvInterface)) return false;
let serviceName = rosSrvInterface.type().interfaceName;
let requestMsgName = serviceName + '_Request';
let responseMsgName = serviceName + '_Response';
let matches = infos.reduce((matchCnt, info) => {
let infoInterfaceName = info.type().interfaceName;
if (
requestMsgName === infoInterfaceName ||
responseMsgName === infoInterfaceName
) {
matchCnt++;
}
return matchCnt;
}, 0);
return matches === 2;
}
function isValidAction(rosActionInterface, infos) {
if (!isActionInterface(rosActionInterface)) return false;
let actionName = rosActionInterface.type().interfaceName;
let feedback = actionName + '_Feedback';
let feedbackMsg = actionName + '_FeedbackMessage';
let goalMsg = actionName + '_Goal';
let resultMsg = actionName + '_Result';
let getResultSrv = actionName + '_GetResult';
let sendGoalSrv = actionName + '_SendGoal';
let searches = [actionName, feedback, feedbackMsg, goalMsg, resultMsg];
const SUCCESS_MATCH_COUNT = searches.length + 2;
let matches = infos.reduce((matchCnt, info) => {
let infoInterfaceName = info.type().interfaceName;
if (
searches.indexOf(infoInterfaceName) >= 0 ||
(getResultSrv === infoInterfaceName && isValidService(info, infos)) ||
(sendGoalSrv === infoInterfaceName && isValidService(info, infos))
) {
matchCnt++;
}
return matchCnt;
}, 0);
return matches === SUCCESS_MATCH_COUNT;
}
function fieldType2JSName(fieldInfo, subFolder = 'msg') {
return fieldInfo.type.isPrimitiveType
? primitiveType2JSName(fieldInfo.type.type)
: `${fieldInfo.type.pkgName}.${subFolder}.${fieldInfo.type.type}`;
}
// https://design.ros2.org/articles/idl_interface_definition.html
// https://github.com/ros2/rosidl/blob/master/rosidl_parser/rosidl_parser/definition.py
function primitiveType2JSName(type) {
let jsName;
switch (type) {
case 'char':
case 'byte':
case 'octet':
// signed explicit integer types
case 'short':
case 'long':
case 'long long':
// unsigned nonexplicit integer types
case 'unsigned short':
case 'unsigned long':
case 'unsigned long long':
// float point types
case 'float':
case 'double':
case 'long double':
// signed explicit integer types
case 'int8':
case 'int16':
case 'int32':
case 'int64':
// signed explicit float types
case 'float32':
case 'float64':
// unsigned explicit integer types
case 'uint8':
case 'uint16':
case 'uint32':
case 'uint64':
jsName = 'number';
break;
case 'bool':
case 'boolean':
jsName = 'boolean';
break;
case 'string':
case 'wstring':
jsName = 'string';
break;
}
return jsName;
}
function fieldTypeArray2JSTypedArrayName(type) {
let jsName;
switch (type) {
case 'byte':
case 'octet':
case 'uint8':
jsName = 'Uint8Array';
break;
case 'char':
case 'int8':
jsName = 'Int8Array';
break;
case 'int16':
case 'short':
jsName = 'Int16Array';
break;
case 'uint16':
case 'unsigned short':
jsName = 'Uint16Array';
break;
case 'int32':
case 'long':
jsName = 'Int32Array';
break;
case 'uint32':
case 'unsigned long':
jsName = 'Uint32Array';
break;
case 'float':
case 'float32':
jsName = 'Float32Array';
break;
case 'double':
case 'float64':
jsName = 'Float64Array';
break;
case 'long long':
case 'unsigned long long':
case 'int64':
case 'uint64':
// number
break;
}
return jsName;
}
// example filename: std_msgs_msg_String, sensor_msgs_msg_LaserScan
// result {package: 'std_msgs', type: 'msg', name: 'String'}
function fileName2Typeclass(filename) {
const regex = /(.+)__(\w+)__(\w+)\.js/;
const array = filename.split(regex).filter(Boolean);
if (!array || array.length != 3) {
// todo: throw error
console.log('ERRORRROOROR', array);
return;
}
return {
package: array[0],
type: array[1],
name: array[2],
};
}
function indentString(string, amount) {
if (!string) {
return '';
}
return ' '.repeat(amount) + string;
}
function indentLines(lines, amount) {
if (!Array.isArray(lines)) {
throw new Error('lines must be an array');
}
return lines.map((line) => indentString(line, amount));
}
async function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
const tsdGenerator = {
generateAll,
};
module.exports = tsdGenerator;