From c8df7fa9895fdb7fc24d71d7b1924bfbd62d487d Mon Sep 17 00:00:00 2001 From: Minggang Wang Date: Thu, 30 May 2024 18:02:35 +0800 Subject: [PATCH] [Jazzy] Generate msg for a service --- rosidl_gen/generator.json | 2 +- rosidl_gen/index.js | 3 +- rosidl_gen/packages.js | 82 +++++++++++++++++++++++++++------------ test/blocklist.json | 3 +- 4 files changed, 62 insertions(+), 28 deletions(-) diff --git a/rosidl_gen/generator.json b/rosidl_gen/generator.json index 5cd4bd0a..722b6e9e 100644 --- a/rosidl_gen/generator.json +++ b/rosidl_gen/generator.json @@ -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": [ diff --git a/rosidl_gen/index.js b/rosidl_gen/index.js index fdaff6af..2e428160 100644 --- a/rosidl_gen/index.js +++ b/rosidl_gen/index.js @@ -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); @@ -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 diff --git a/rosidl_gen/packages.js b/rosidl_gen/packages.js index c580fef0..10eab8c0 100644 --- a/rosidl_gen/packages.js +++ b/rosidl_gen/packages.js @@ -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') { @@ -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 @@ -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; } /** diff --git a/test/blocklist.json b/test/blocklist.json index 7112afba..b4d20ce8 100644 --- a/test/blocklist.json +++ b/test/blocklist.json @@ -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" ] }