Skip to content

Commit d0ae86b

Browse files
wayneparrottMinggang Wang
authored andcommitted
improve message generation
package.json - renamed binary script to `generate-ros-messages` generate_messages.js - minor change in console.log message README.md - updated to reference the generate-ros-messages script and how to run it using `npx` test-generate-messages-bin.js - added test to run `generate-ros-messages` using `npx` test-interactive.js - fixed minor lint issues. fix #776
1 parent a96e8a9 commit d0ae86b

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

README.md

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,18 @@ rclnodejs.init().then(() => {
9999
});
100100
```
101101

102-
The benefits of using TypeScript become evident when working with more complex use-cases. ROS messages are defined in the `types/interfaces.d.ts` module. This module is updated as part of the `generate-messages` process described in the next section.
102+
The benefits of using TypeScript become evident when working with more complex use-cases. ROS messages are defined in the `types/interfaces.d.ts` module. This module is updated as part of the `generate-ros-messages` process described in the next section.
103103

104104
## ROS2 Interface Message Generation (important)
105105

106106
ROS components communicate by sending and receiving messages described
107107
by the interface definition language (IDL). ROS client libraries such as
108108
rclnodejs are responsible for converting these IDL message descriptions
109109
into source code of their target language. For this, rclnodejs provides
110-
the `generate-messages` npm script that reads in the IDL
111-
messages files of a ROS environment and generates corresponding JavaScript
110+
the npm binary`generate-ros-messages` script that reads the IDL
111+
message files of a ROS environment and generates corresponding JavaScript
112112
message interface files. Additionally, the tool generates the TypeScript
113-
`interface.d.ts` file containing declarations for every IDL message file
114-
processed.
113+
`interface.d.ts` file containing declarations for each IDL message file.
115114

116115
Learn more about ROS interfaces and IDL [here](https://index.ros.org/doc/ros2/Concepts/About-ROS-Interfaces/).
117116

@@ -127,25 +126,15 @@ stringMsgObject.data = 'hello world';
127126

128127
Message files are generated as a post-install step of the rclnodejs
129128
installation process. Thereafter, you will need to manually run the
130-
message generation script when new ROS message packages are installed
129+
rclnodejs message generation script when new ROS message packages are installed
131130
for which your ROS2-nodejs project has a dependency.
132131

133-
### Running `generate-messages` Utility
132+
### Running `generate-ros-messages` Utility
134133

135-
To use `generate-messages` from your Nodejs package, create an npm
136-
script entry in your package.json file as shown:
134+
To run the `generate-ros-messages` script from your Nodejs package, use the `npx` utility included in your Nodejs installation.
137135

138136
```
139-
"scripts": {
140-
"generate-messages": "generate-messages"
141-
// your other scripts here
142-
}
143-
```
144-
145-
To run the script use `npm` as follows:
146-
147-
```
148-
npm run generate-messages
137+
npx generate-ros-messages
149138
```
150139

151140
The newly generated JavaScript files can be found at

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"format": "clang-format -i -style=file ./src/*.cpp ./src/*.hpp && prettier --write \"{lib,rosidl_gen,rostsd_gen,rosidl_parser,types,example,test,scripts,benchmark}/**/*.{js,md,ts}\" ./*.{js,md,ts}"
2323
},
2424
"bin": {
25-
"generate-messages": "./scripts/generate_messages.js"
25+
"generate-ros-messages": "./scripts/generate_messages.js"
2626
},
2727
"authors": [
2828
"Minggang Wang <[email protected]>",

scripts/generate_messages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const generator = require('../rosidl_gen/index.js');
2121
const tsdGenerator = require('../rostsd_gen/index.js');
2222

2323
async function main() {
24-
console.log('Start JavaScript message generation...');
24+
console.log('Start generation of ROS2 JavaScript messages...');
2525

2626
try {
2727
await generator.generateAll(true);

test/test-generate-messages-bin.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const path = require('path');
2222
const rclnodejs = require('../index.js');
2323

2424
const GEN_FOLDER = 'generated';
25-
const SCRIPT_NAME = 'generate-messages';
25+
const SCRIPT_NAME = 'generate-ros-messages';
2626

2727
describe('rclnodejs generate-messages binary-script tests', function () {
2828
let cwd;
@@ -87,14 +87,14 @@ describe('rclnodejs generate-messages binary-script tests', function () {
8787
fs.rmdirSync(this.tmpPkg, { recursive: true });
8888
});
8989

90-
it('test generate-messages script installation', function (done) {
90+
it('test generate-ros-messages script installation', function (done) {
9191
// confirm script is installed at <pgk>/node_modules/.bin/<script>
9292
let script = createScriptFolderPath(this.tmpPkg);
9393
assert.ok(fs.existsSync(script));
9494
done();
9595
});
9696

97-
it('test generate-messages script operation', function (done) {
97+
it('test generate-ros-messages script operation', function (done) {
9898
let script = createScriptFolderPath(this.tmpPkg);
9999
childProcess.spawnSync(script, [], { stdio: 'inherit', shell: true });
100100

@@ -109,6 +109,25 @@ describe('rclnodejs generate-messages binary-script tests', function () {
109109
);
110110
done();
111111
});
112+
113+
it('test npx generate-ros-messages script operation', function (done) {
114+
childProcess.spawnSync('npx', [SCRIPT_NAME], {
115+
stdio: 'inherit',
116+
shell: true,
117+
cwd: this.tmpPkg,
118+
});
119+
120+
let generatedFolderPath = createGeneratedFolderPath(this.tmpPkg);
121+
assert.ok(
122+
fs.existsSync(generatedFolderPath),
123+
'No generated message folder found'
124+
);
125+
assert.ok(
126+
fs.existsSync(path.join(generatedFolderPath, 'std_msgs')),
127+
'std_msgs folder found'
128+
);
129+
done();
130+
});
112131
});
113132

114133
function createGeneratedFolderPath(pkgFolder) {

test/test-interactive.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,18 @@ describe('rclnodejs interactive testing', function () {
3535
const RclString = 'std_msgs/msg/String';
3636
var publisher = childProcess.fork(`${__dirname}/publisher_setup.js`);
3737
var destroy = false;
38-
var subscription = node.createSubscription(
39-
RclString,
40-
'topic',
41-
function (msg) {
42-
assert.deepStrictEqual(typeof msg, 'object');
43-
assert.ok('data' in msg);
44-
assert.deepStrictEqual(msg.data, 'Greeting from publisher');
38+
var subscription = node.createSubscription(RclString, 'topic', (msg) => {
39+
assert.deepStrictEqual(typeof msg, 'object');
40+
assert.ok('data' in msg);
41+
assert.deepStrictEqual(msg.data, 'Greeting from publisher');
4542

46-
if (!destroy) {
47-
publisher.kill('SIGINT');
48-
node.destroy();
49-
destroy = true;
50-
done();
51-
}
43+
if (!destroy) {
44+
publisher.kill('SIGINT');
45+
node.destroy();
46+
destroy = true;
47+
done();
5248
}
53-
);
49+
});
5450
rclnodejs.spin(node);
5551
});
5652
});

0 commit comments

Comments
 (0)