|
| 1 | +# Copyright 2021 Open Source Robotics Foundation, Inc. |
| 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 | +import pathlib |
| 16 | + |
| 17 | +from rosidl_cli.command import Command |
| 18 | + |
| 19 | +from .extensions import load_type_extensions |
| 20 | +from .extensions import load_typesupport_extensions |
| 21 | + |
| 22 | + |
| 23 | +class GenerateCommand(Command): |
| 24 | + """Generate source code from interface definition files.""" |
| 25 | + |
| 26 | + name = 'generate' |
| 27 | + |
| 28 | + def add_arguments(self, parser): |
| 29 | + parser.add_argument( |
| 30 | + '-o', '--output-path', type=pathlib.Path, |
| 31 | + metavar='PATH', default=pathlib.Path.cwd(), |
| 32 | + help=('Path to directory to hold generated source code files. ' |
| 33 | + "Defaults to '.'.")) |
| 34 | + parser.add_argument( |
| 35 | + '-t', '--type', metavar='TYPE_SPEC', |
| 36 | + dest='type_specs', action='append', default=[], |
| 37 | + help='Target type representations for generation.') |
| 38 | + parser.add_argument( |
| 39 | + '-ts', '--type-support', metavar='TYPESUPPORT_SPEC', |
| 40 | + dest='typesupport_specs', action='append', default=[], |
| 41 | + help='Target type supports for generation.') |
| 42 | + parser.add_argument( |
| 43 | + '-I', '--include-path', type=pathlib.Path, metavar='PATH', |
| 44 | + dest='include_paths', action='append', default=[], |
| 45 | + help='Paths to include dependency interface definition files from.') |
| 46 | + parser.add_argument( |
| 47 | + 'package_name', help='Name of the package to generate code for') |
| 48 | + parser.add_argument( |
| 49 | + 'interface_files', metavar='interface_file', nargs='+', |
| 50 | + help=('Normalized relative path to interface definition file. ' |
| 51 | + "If prefixed by another path followed by a colon ':', " |
| 52 | + 'path resolution is performed against such path.')) |
| 53 | + |
| 54 | + def main(self, *, args): |
| 55 | + extensions = [] |
| 56 | + |
| 57 | + unspecific_generation = \ |
| 58 | + not args.type_specs and not args.typesupport_specs |
| 59 | + |
| 60 | + if args.type_specs or unspecific_generation: |
| 61 | + extensions.extend(load_type_extensions( |
| 62 | + specs=args.type_specs, |
| 63 | + strict=not unspecific_generation)) |
| 64 | + |
| 65 | + if args.typesupport_specs or unspecific_generation: |
| 66 | + extensions.extend(load_typesupport_extensions( |
| 67 | + specs=args.typesupport_specs, |
| 68 | + strict=not unspecific_generation)) |
| 69 | + |
| 70 | + if unspecific_generation and not extensions: |
| 71 | + return 'No type nor typesupport extensions were found' |
| 72 | + |
| 73 | + if len(extensions) > 1: |
| 74 | + for extension in extensions: |
| 75 | + extension.generate( |
| 76 | + args.package_name, args.interface_files, args.include_paths, |
| 77 | + output_path=args.output_path / extension.name) |
| 78 | + else: |
| 79 | + extensions[0].generate( |
| 80 | + args.package_name, args.interface_files, |
| 81 | + args.include_paths, args.output_path |
| 82 | + ) |
0 commit comments