Skip to content

Commit

Permalink
[Jazzy] Generate msg for a service
Browse files Browse the repository at this point in the history
  • Loading branch information
minggangw committed May 31, 2024
1 parent 2d3ccaa commit c8df7fa
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
2 changes: 1 addition & 1 deletion rosidl_gen/generator.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rosidl-generator",
"version": "0.3.7",
"version": "0.3.8",
"description": "Generate JavaScript object from ROS IDL(.msg) files",
"main": "index.js",
"authors": [
Expand Down
3 changes: 2 additions & 1 deletion rosidl_gen/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const packages = require('./packages.js');
const path = require('path');

const generatedRoot = path.join(__dirname, '../generated/');
const serviceMsgPath = path.join(generatedRoot, 'srv_msg');

function getInstalledPackagePaths() {
return process.env.AMENT_PREFIX_PATH.split(path.delimiter);
Expand Down Expand Up @@ -50,7 +51,7 @@ async function generateAll(forcedGenerating) {
path.join(__dirname, 'generator.json'),
path.join(generatedRoot, 'generator.json')
);

await fse.mkdir(serviceMsgPath);
// Process in AMENT_PREFIX_PATH in reverse order to
// such that interfaces defined earlier on the AMENX_PREFIX_PATH
// have higher priority over earlier versions and will override
Expand Down
82 changes: 57 additions & 25 deletions rosidl_gen/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const pkgFilters = require('../rosidl_gen/filter.js');
const fsp = fs.promises;

const generatedRoot = path.join(__dirname, '../generated/');
const serviceMsgPath = path.join(generatedRoot, 'srv_msg');

function getPackageName(filePath, amentExecuted) {
if (os.type() === 'Windows_NT') {
Expand Down Expand Up @@ -131,6 +132,58 @@ async function getPackageDefinitionsFiles(packageName, amentRoot) {
return rosFiles;
}

async function generateMsgForSrv(filePath, interfaceInfo, pkgMap) {
const requestMsgName = `${path.parse(filePath).name}_Request.msg`;
const responseMsgName = `${path.parse(filePath).name}_Response.msg`;
const data = await fsp.readFile(filePath, 'utf8');

const arr = data.split(/-{3,}/);
if (arr.length == 2) {
const packagePath = path.join(serviceMsgPath, interfaceInfo.pkgName);
if (!fs.existsSync(packagePath)) {
fs.mkdirSync(packagePath);
}

await fsp.writeFile(path.join(packagePath, requestMsgName), arr[0]);
await fsp.writeFile(path.join(packagePath, responseMsgName), arr[1]);
let requestInfo = Object.assign({}, interfaceInfo);
requestInfo.filePath = path.join(packagePath, requestMsgName);
requestInfo.interfaceName = requestInfo.interfaceName + "_Request"
let responseInfo = Object.assign({}, interfaceInfo);
responseInfo.filePath = path.join(packagePath, responseMsgName);
responseInfo.interfaceName = responseInfo.interfaceName + "_Response"

addInterfaceInfo(requestInfo, 'messages', pkgMap);
addInterfaceInfo(responseInfo, 'messages', pkgMap);
}
}

async function addInterfaceInfos(filePath, dir, pkgMap) {
const interfaceInfo = grabInterfaceInfo(filePath, true);
const ignore = pkgFilters.matchesAny(interfaceInfo);
if (ignore) {
console.log('Omitting filtered interface: ', interfaceInfo);
} else {
if (path.extname(filePath) === '.msg') {
// Some .msg files were generated prior to 0.3.2 for .action files,
// which has been disabled. So these files should be ignored here.
if (path.dirname(dir).split(path.sep).pop() !== 'action') {
addInterfaceInfo(interfaceInfo, 'messages', pkgMap);
}
} else if (path.extname(filePath) === '.srv') {
const requestMsgName = `${path.parse(filePath).name}_Request.msg`;
if (!fs.existsSync(path.join(path.dirname(filePath), requestMsgName))) {
await generateMsgForSrv(filePath, interfaceInfo, pkgMap);
}
addInterfaceInfo(interfaceInfo, 'services', pkgMap);
} else if (path.extname(filePath) === '.action') {
addInterfaceInfo(interfaceInfo, 'actions', pkgMap);
} else {
// we ignore all other files
}
}
}

/**
* Collects all packages in a directory by using the ament index.
* @param {string} dir - the directory to search in
Expand All @@ -144,32 +197,11 @@ async function findAmentPackagesInDirectory(dir) {

// Support flat() method for nodejs < 11.
const rosFiles = Array.prototype.flat ? files.flat() : flat(files);

const pkgMap = new Map();
return new Promise((resolve, reject) => {
rosFiles.forEach((filePath) => {
const interfaceInfo = grabInterfaceInfo(filePath, true);
const ignore = pkgFilters.matchesAny(interfaceInfo);
if (ignore) {
console.log('Omitting filtered interface: ', interfaceInfo);
} else {
if (path.extname(filePath) === '.msg') {
// Some .msg files were generated prior to 0.3.2 for .action files,
// which has been disabled. So these files should be ignored here.
if (path.dirname(dir).split(path.sep).pop() !== 'action') {
addInterfaceInfo(interfaceInfo, 'messages', pkgMap);
}
} else if (path.extname(filePath) === '.srv') {
addInterfaceInfo(interfaceInfo, 'services', pkgMap);
} else if (path.extname(filePath) === '.action') {
addInterfaceInfo(interfaceInfo, 'actions', pkgMap);
} else {
// we ignore all other files
}
}
});
resolve(pkgMap);
});
await Promise.all(
rosFiles.map(filePath => addInterfaceInfos(filePath, dir, pkgMap))
);
return pkgMap;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/blocklist.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"test-msg-type-cpp-node.js",
"test-cross-lang.js",
"test-message-generation-bin.js",
"test-subscription-content-filter.js"
"test-subscription-content-filter.js",
"test-guard-condition.js"
]
}

0 comments on commit c8df7fa

Please sign in to comment.