|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# License: Apache 2.0 |
| 4 | +# https://raw.github.com/rosjava/rosjava_build_tools/license/LICENSE |
| 5 | +# |
| 6 | + |
| 7 | +############################################################################## |
| 8 | +# Imports |
| 9 | +############################################################################## |
| 10 | + |
| 11 | +from __future__ import print_function |
| 12 | + |
| 13 | +import os |
| 14 | +import sys |
| 15 | +import argparse |
| 16 | +import xml.etree.ElementTree as ElementTree |
| 17 | + |
| 18 | +# local imports |
| 19 | +import utils |
| 20 | +import console |
| 21 | + |
| 22 | +############################################################################## |
| 23 | +# Methods |
| 24 | +############################################################################## |
| 25 | + |
| 26 | + |
| 27 | +def parse_arguments(): |
| 28 | + argv = sys.argv[1:] |
| 29 | + parser = argparse.ArgumentParser( |
| 30 | + description='Creates a new rosjava package based on catkin and gradle. \n') |
| 31 | + parser.add_argument('name', |
| 32 | + nargs=1, |
| 33 | + help='The name for the package') |
| 34 | + parser.add_argument('-a', '--author', |
| 35 | + action='store', |
| 36 | + default=utils.author_name(), |
| 37 | + help='A single author, may be used multiple times') |
| 38 | + args = parser.parse_args(argv) |
| 39 | + return args |
| 40 | + |
| 41 | + |
| 42 | +############################################################################## |
| 43 | +# Methods acting on classes |
| 44 | +############################################################################## |
| 45 | + |
| 46 | + |
| 47 | +# This inserts the labelled variables into the template wherever the corresponding |
| 48 | +# %package, %brief, %description and %depends is found. |
| 49 | +def instantiate_template(template, project_name, author): |
| 50 | + return template % locals() |
| 51 | + |
| 52 | + |
| 53 | +def create_gradle_package_files(args, template_directory): |
| 54 | + ''' |
| 55 | + This is almost a direct copy from catkin_create_pkg. |
| 56 | + ''' |
| 57 | + try: |
| 58 | + project_name = args.name[0].lower() |
| 59 | + package_path = os.path.abspath(os.path.join(os.getcwd(), project_name)) |
| 60 | + for template_name in ['build.gradle']: # 'CMakeLists.txt']: |
| 61 | + filename = os.path.join(package_path, template_name) |
| 62 | + template = utils.read_template_file(template_directory, template_name) |
| 63 | + contents = instantiate_template(template, project_name, args.author) |
| 64 | + try: |
| 65 | + f = open(filename, 'w') |
| 66 | + f.write(contents) |
| 67 | + console.pretty_print(' File : ', console.cyan) |
| 68 | + console.pretty_println(template_name, console.yellow) |
| 69 | + finally: |
| 70 | + f.close() |
| 71 | + except Exception: |
| 72 | + raise |
| 73 | + |
| 74 | + |
| 75 | +def add_to_root_gradle_settings(name): |
| 76 | + ''' |
| 77 | + Adds project name to the root level settings.gradle file. |
| 78 | + ''' |
| 79 | + for rel_path in ['.', '..']: |
| 80 | + settings_gradle_path = os.path.join(os.getcwd(), rel_path, 'settings.gradle') |
| 81 | + if os.path.isfile(settings_gradle_path): |
| 82 | + break |
| 83 | + else: |
| 84 | + settings_gradle_path = None |
| 85 | + if settings_gradle_path is None: |
| 86 | + console.pretty_println("\nCouldn't find the root level settings.gradle file - not adding to the superproject.") |
| 87 | + return |
| 88 | + with open(settings_gradle_path, 'a') as settings_gradle: |
| 89 | + console.pretty_print(' File : ', console.cyan) |
| 90 | + console.pretty_println('settings.gradle', console.yellow) |
| 91 | + settings_gradle.write("include '%s'\n" % name) |
| 92 | + |
| 93 | + |
| 94 | +def add_catkin_generate_tree_command(): |
| 95 | + for rel_path in ['.', '..']: |
| 96 | + build_gradle_path = os.path.join(os.getcwd(), rel_path, 'build.gradle') |
| 97 | + if os.path.isfile(build_gradle_path): |
| 98 | + break |
| 99 | + else: |
| 100 | + build_gradle_path = None |
| 101 | + if build_gradle_path is None: |
| 102 | + console.pretty_println("\nCouldn't find the root level build.gradle file - not adding to the superproject.") |
| 103 | + return |
| 104 | + with open(build_gradle_path, 'r') as build_gradle: |
| 105 | + console.pretty_print(' File : ', console.cyan) |
| 106 | + console.pretty_println('build.gradle (catkin_generate_tree update)', console.yellow) |
| 107 | + new_contents = build_gradle.read().replace("apply plugin: 'catkin'", "apply plugin: 'catkin'\nproject.catkin.tree.generate()\n") |
| 108 | + with open(build_gradle_path, 'w') as build_gradle: |
| 109 | + build_gradle.write(new_contents) |
| 110 | + |
| 111 | + |
| 112 | +def add_to_package_xml(name): |
| 113 | + ''' |
| 114 | + Adds project name to build_depends in package.xml (should be same name as the ros msg package name). |
| 115 | + ''' |
| 116 | + for rel_path in ['.', '..']: |
| 117 | + package_xml_path = os.path.join(os.getcwd(), rel_path, 'package.xml') |
| 118 | + if os.path.isfile(package_xml_path): |
| 119 | + break |
| 120 | + else: |
| 121 | + package_xml_path = None |
| 122 | + if package_xml_path is None: |
| 123 | + console.pretty_println("\nCouldn't find the root level package.xml file - not adding to the superproject.") |
| 124 | + return |
| 125 | + with open(package_xml_path, 'r') as package_xml: |
| 126 | + console.pretty_print(' File : ', console.cyan) |
| 127 | + console.pretty_println('package.xml (dependency update)', console.yellow) |
| 128 | + new_contents = package_xml.read().replace("</package>", "<build_depend>%s</build_depend>\n</package>" % name) |
| 129 | + with open(package_xml_path, 'w') as package_xml: |
| 130 | + package_xml.write(new_contents) |
| 131 | + |
| 132 | + |
| 133 | +def create_dummy_java_class(project_name): |
| 134 | + path = os.path.join(os.getcwd(), project_name.lower()) |
| 135 | + java_package_path = os.path.join(path, 'src', 'main', 'java', 'com', 'github', 'rosjava', project_name) |
| 136 | + utils.mkdir_p(java_package_path) |
| 137 | + filename = os.path.join(java_package_path, 'Dude.java') |
| 138 | + java_class = "package com.github.rosjava.%s.Dude;\n" % project_name |
| 139 | + java_class += "\n" |
| 140 | + java_class += "public class Dude {\n" |
| 141 | + java_class += "}\n" |
| 142 | + console.pretty_print(' File : ', console.cyan) |
| 143 | + console.pretty_println('Dude.class', console.yellow) |
| 144 | + with open(filename, 'w') as dude_class: |
| 145 | + dude_class.write(java_class) |
| 146 | + |
| 147 | + |
| 148 | +def ros_package_name(): |
| 149 | + for rel_path in ['.', '..']: |
| 150 | + package_xml_path = os.path.join(os.getcwd(), rel_path, 'package.xml') |
| 151 | + if os.path.isfile(package_xml_path): |
| 152 | + break |
| 153 | + else: |
| 154 | + package_xml_path = None |
| 155 | + if package_xml_path is None: |
| 156 | + console.pretty_println("\nCouldn't find the root level package.xml file - not adding to the superproject.") |
| 157 | + return |
| 158 | + tree = ElementTree.parse(package_xml_path) |
| 159 | + root = tree.getroot() |
| 160 | + name = root.find('name').text |
| 161 | + return name |
| 162 | + |
| 163 | + |
| 164 | +def create_rosjava_project_common(args, template_directory): |
| 165 | + project_name = args.name[0] |
| 166 | + console.pretty_println("\nCreating rosjava project ", console.bold) |
| 167 | + console.pretty_print(" Name : ", console.cyan) |
| 168 | + console.pretty_println("%s" % project_name, console.yellow) |
| 169 | + utils.mkdir_p(os.path.join(os.getcwd(), project_name.lower())) |
| 170 | + # This is in the old form, let's shovel the shit around to the new form |
| 171 | + create_gradle_package_files(args, template_directory) |
| 172 | + add_to_root_gradle_settings(args.name[0]) |
| 173 | + |
| 174 | + |
| 175 | +def create_rosjava_project(): |
| 176 | + args = parse_arguments() |
| 177 | + create_rosjava_project_common(args, 'rosjava_project') |
| 178 | + create_dummy_java_class(args.name[0]) |
| 179 | + |
| 180 | + |
| 181 | +def create_rosjava_msg_project(): |
| 182 | + args = parse_arguments() |
| 183 | + create_rosjava_project_common(args, 'rosjava_msg_project') |
| 184 | + add_catkin_generate_tree_command() |
| 185 | + add_to_package_xml(args.name[0]) |
0 commit comments