|
| 1 | +// Copyright (c) 2025, The Robot Web Tools Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +const fse = require('fs-extra'); |
| 16 | +const generateJSStructFromIDL = require('./idl_generator.js'); |
| 17 | +const packages = require('./packages.js'); |
| 18 | +const path = require('path'); |
| 19 | +const idlConvertor = require('../rosidl_convertor/idl_convertor.js'); |
| 20 | + |
| 21 | +const generatedRoot = path.join(__dirname, '../generated/'); |
| 22 | +const idlPath = path.join(generatedRoot, 'share'); |
| 23 | +const useIDL = !!process.argv.find((arg) => arg === '--idl'); |
| 24 | + |
| 25 | +// Get target path from environment variable instead of workerData |
| 26 | +const targetPath = process.env.WORKER_TARGET_PATH; |
| 27 | + |
| 28 | +async function generateInPath(targetPath) { |
| 29 | + let pkgsInfo = null; |
| 30 | + if (!useIDL) { |
| 31 | + pkgsInfo = Array.from( |
| 32 | + (await packages.findPackagesInDirectory(targetPath)).values() |
| 33 | + ); |
| 34 | + } else { |
| 35 | + const idlPkgs = await packages.findPackagesInDirectory(targetPath, useIDL); |
| 36 | + await fse.ensureDir(idlPath); |
| 37 | + const promises = []; |
| 38 | + idlPkgs.forEach((pkg) => { |
| 39 | + pkg.idls.forEach((idl) => { |
| 40 | + promises.push(idlConvertor(idl.pkgName, idl.filePath, idlPath)); |
| 41 | + }); |
| 42 | + }); |
| 43 | + await Promise.all(promises); |
| 44 | + const pkgsFromIdl = await packages.findPackagesInDirectory(idlPath, false); |
| 45 | + pkgsInfo = Array.from(pkgsFromIdl.values()); |
| 46 | + } |
| 47 | + |
| 48 | + await Promise.all( |
| 49 | + pkgsInfo.map((pkgInfo) => generateJSStructFromIDL(pkgInfo, generatedRoot)) |
| 50 | + ); |
| 51 | +} |
| 52 | + |
| 53 | +async function main() { |
| 54 | + try { |
| 55 | + await generateInPath(targetPath); |
| 56 | + process.exit(0); |
| 57 | + } catch (error) { |
| 58 | + console.error('Worker generation failed:', error.message); |
| 59 | + process.exit(1); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +main(); |
0 commit comments