|
8 | 8 | # Imports
|
9 | 9 | ##############################################################################
|
10 | 10 |
|
11 |
| -from __future__ import print_function |
12 |
| - |
13 | 11 | import os
|
14 | 12 | import sys
|
15 | 13 | import argparse
|
16 | 14 | import subprocess
|
17 |
| -import shutil |
| 15 | +import catkin_pkg |
| 16 | +from catkin_pkg.package_templates import create_package_xml, PackageTemplate |
18 | 17 |
|
19 | 18 | # local imports
|
20 | 19 | import utils
|
|
28 | 27 | def parse_arguments():
|
29 | 28 | argv = sys.argv[1:]
|
30 | 29 | parser = argparse.ArgumentParser(
|
31 |
| - description='Creates a new android package based on catkin and gradle. \n') |
32 |
| - parser.add_argument('name', |
33 |
| - nargs=1, |
34 |
| - help='The name for the package') |
35 |
| - parser.add_argument('-s', '--sdk-version', |
36 |
| - action='store', |
37 |
| - default='17', |
38 |
| - help='Android sdk version [17]') |
39 |
| - parser.add_argument('-p', '--android-package-name', |
40 |
| - action='store', |
41 |
| - default='com.github.rosjava.android.pkg_name', |
42 |
| - help='Android package name (e.g. com.github.rosjava.android.pkg_name)') |
| 30 | + description='Creates a new android repository based on catkin and gradle. \n\nNote that the path you provide will become the maven group for your repo.\n') |
| 31 | + parser.add_argument('path', nargs='?', default=os.getcwd(), help='path to the repository you wish to create (must not exist beforehand).') |
| 32 | + parser.add_argument('dependencies', |
| 33 | + nargs='*', |
| 34 | + help='Dependency list') |
| 35 | + parser.add_argument('-l', '--license', |
| 36 | + action='append', |
| 37 | + default=["Apache 2.0"], |
| 38 | + help='Name for License, (e.g. BSD, MIT, GPLv3...)[BSD]') |
43 | 39 | parser.add_argument('-a', '--author',
|
44 |
| - action='store', |
45 |
| - default=utils.author_name(), |
| 40 | + action='append', |
46 | 41 | help='A single author, may be used multiple times')
|
| 42 | + parser.add_argument('-m', '--maintainer', |
| 43 | + action='append', |
| 44 | + help='A single maintainer, may be used multiple times') |
| 45 | + parser.add_argument('-V', '--pkg_version', |
| 46 | + action='store', |
| 47 | + default="0.1.0", |
| 48 | + help='Initial Package version [0.1.0]') |
| 49 | + parser.add_argument('-D', '--description', |
| 50 | + action='store', |
| 51 | + help='Description') |
47 | 52 | args = parser.parse_args(argv)
|
48 |
| - if args.android_package_name == "com.github.rosjava.android.pkg_name": |
49 |
| - args.android_package_name = "com.github.rosjava.android.%s" % args.name[0].lower() |
| 53 | +# if not args.author: |
| 54 | +# args.author = [] |
| 55 | +# args.author.append(utils.author_name) |
| 56 | +# if not args.maintainer: |
| 57 | +# args.maintainer = [] |
| 58 | +# args.maintainer.append(catkin_pkg.package.Person(utils.author_name)) |
| 59 | +# else: |
| 60 | +# args.maintainer = [] |
| 61 | +# args.maintainer.append(catkin_pkg.package.Person(utils.author_name)) |
50 | 62 | return args
|
51 | 63 |
|
52 | 64 |
|
53 |
| -def create_android_project(package_name, sdk_version, java_package_name, is_library): |
54 |
| - path = os.path.join(os.getcwd(), package_name.lower()) |
55 |
| - console.pretty_println("\nCreating android project ", console.bold) |
56 |
| - console.pretty_print(" Name : ", console.cyan) |
57 |
| - console.pretty_println("%s" % package_name, console.yellow) |
58 |
| - console.pretty_print(" Sdk Ver : ", console.cyan) |
59 |
| - console.pretty_println("%s" % sdk_version, console.yellow) |
60 |
| - console.pretty_print(" Java Name : ", console.cyan) |
61 |
| - console.pretty_println("%s" % java_package_name, console.yellow) |
62 |
| - if is_library: |
63 |
| - console.pretty_print(" Library : ", console.cyan) |
64 |
| - console.pretty_println("yes\n", console.yellow) |
65 |
| - cmd = ['android', 'create', 'lib-project', '-n', package_name, '-p', path, '-k', java_package_name, '-t', 'android-' + sdk_version, ] |
66 |
| - else: |
67 |
| - activity_name = utils.camel_case(package_name) |
68 |
| - console.pretty_print(" Activity : ", console.cyan) |
69 |
| - console.pretty_println("%s\n" % activity_name, console.yellow) |
70 |
| - cmd = ['android', 'create', 'project', '-n', package_name, '-p', path, '-k', java_package_name, '-t', 'android-' + sdk_version, '-a', activity_name] |
| 65 | +# Finds and reads one of the templates. |
| 66 | +def read_template(tmplf): |
| 67 | + f = open(tmplf, 'r') |
71 | 68 | try:
|
72 |
| - subprocess.check_call(cmd) |
73 |
| - except subprocess.CalledProcessError: |
74 |
| - raise subprocess.CalledProcessError("failed to create android project.") |
75 |
| - # This is in the old form, let's shovel the shit around to the new form |
76 |
| - utils.mkdir_p(os.path.join(path, 'src', 'main', 'java')) |
77 |
| - os.remove(os.path.join(path, 'local.properties')) |
78 |
| - os.remove(os.path.join(path, 'project.properties')) |
79 |
| - os.remove(os.path.join(path, 'ant.properties')) |
80 |
| - os.remove(os.path.join(path, 'proguard-project.txt')) |
81 |
| - os.remove(os.path.join(path, 'build.xml')) |
82 |
| - os.rmdir(os.path.join(path, 'bin')) |
83 |
| - os.rmdir(os.path.join(path, 'libs')) |
84 |
| - shutil.move(os.path.join(path, 'AndroidManifest.xml'), os.path.join(path, 'src', 'main')) |
85 |
| - shutil.move(os.path.join(path, 'res'), os.path.join(path, 'src', 'main')) |
86 |
| - if not is_library: |
87 |
| - shutil.move(os.path.join(path, 'src', java_package_name.split('.')[0]), os.path.join(path, 'src', 'main', 'java')) |
88 |
| - |
89 |
| -############################################################################## |
90 |
| -# Methods acting on classes |
91 |
| -############################################################################## |
| 69 | + t = f.read() |
| 70 | + finally: |
| 71 | + f.close() |
| 72 | + return t |
92 | 73 |
|
93 | 74 |
|
94 | 75 | # This inserts the labelled variables into the template wherever the corresponding
|
95 | 76 | # %package, %brief, %description and %depends is found.
|
96 |
| -def instantiate_template(template, package_name, author, plugin_name, sdk_version): |
| 77 | +def instantiate_template(template, repo_name, author): |
97 | 78 | return template % locals()
|
98 | 79 |
|
99 | 80 |
|
100 |
| -def create_gradle_package_files(args, author, is_library, sdk_version): |
| 81 | +def get_templates(): |
| 82 | + template_dir = os.path.join(os.path.dirname(__file__), 'templates', 'init_repo') |
| 83 | + templates = {} |
| 84 | + templates['CMakeLists.txt'] = read_template(os.path.join(template_dir, 'CMakeLists.txt.in')) |
| 85 | + templates['build.gradle'] = read_template(os.path.join(template_dir, 'build.gradle.in')) |
| 86 | + templates['settings.gradle'] = read_template(os.path.join(template_dir, 'settings.gradle')) |
| 87 | + return templates |
| 88 | + |
| 89 | + |
| 90 | +def populate_repo(repo_path): |
| 91 | + author = utils.author_name() |
| 92 | + repo_name = os.path.basename(repo_path) |
| 93 | + templates = get_templates() |
| 94 | + for filename, template in templates.iteritems(): |
| 95 | + contents = instantiate_template(template, repo_name, author) |
| 96 | + try: |
| 97 | + p = os.path.abspath(os.path.join(repo_path, filename)) |
| 98 | + f = open(p, 'w') |
| 99 | + f.write(contents) |
| 100 | + console.pretty_print("Created repo file: ", console.cyan) |
| 101 | + console.pretty_println("%s" % p, console.yellow) |
| 102 | + finally: |
| 103 | + f.close() |
| 104 | + |
| 105 | + |
| 106 | +def create_gradle_wrapper(repo_path): |
| 107 | + gradle_binary = os.path.join(os.path.dirname(__file__), 'gradle', 'gradlew') |
| 108 | + cmd = [gradle_binary, '-p', repo_path, 'wrapper'] |
| 109 | + console.pretty_print("Creating gradle wrapper: ", console.cyan) |
| 110 | + console.pretty_println("%s" % ' '.join(cmd), console.yellow) |
| 111 | + try: |
| 112 | + subprocess.check_call(cmd) |
| 113 | + except subprocess.CalledProcessError: |
| 114 | + raise subprocess.CalledProcessError("failed to create the gradle wrapper.") |
| 115 | + |
| 116 | + |
| 117 | +def create_catkin_package_files(package_name, package_path, args): |
101 | 118 | '''
|
102 | 119 | This is almost a direct copy from catkin_create_pkg.
|
103 | 120 | '''
|
104 |
| - plugin_name = "android-library" if is_library else "android" |
105 | 121 | try:
|
106 |
| - package_name = args.name[0].lower() |
107 |
| - package_path = os.path.abspath(os.path.join(os.getcwd(), package_name)) |
108 |
| - console.pretty_println("\nCreating gradle files", console.bold) |
109 |
| - for template_name in ['build.gradle']: # 'CMakeLists.txt']: |
110 |
| - filename = os.path.join(package_path, template_name) |
111 |
| - template = read_template_file(template_name) |
112 |
| - contents = instantiate_template(template, package_name, author, plugin_name, sdk_version) |
113 |
| - if is_library: |
114 |
| - contents += extra_gradle_library_text() |
115 |
| - try: |
116 |
| - f = open(filename, 'w') |
117 |
| - f.write(contents) |
118 |
| - console.pretty_print(' File: ', console.cyan) |
119 |
| - console.pretty_println(template_name, console.yellow) |
120 |
| - finally: |
121 |
| - f.close() |
| 122 | + build_depends = [] |
| 123 | + if 'rosjava_build_tools' not in args.dependencies: |
| 124 | + build_depends.append(catkin_pkg.package.Dependency('rosjava_build_tools')) |
| 125 | + for depend_name in args.dependencies: |
| 126 | + build_depends.append(catkin_pkg.package.Dependency(depend_name)) |
| 127 | + package_template = PackageTemplate._create_package_template( |
| 128 | + package_name=package_name, |
| 129 | + description=args.description, |
| 130 | + licenses=args.license or [], |
| 131 | + maintainer_names=args.maintainer, |
| 132 | + author_names=args.author, |
| 133 | + version=args.pkg_version, |
| 134 | + catkin_deps=[], |
| 135 | + system_deps=[], |
| 136 | + boost_comps=None) |
| 137 | + package_template.exports = [] |
| 138 | + package_template.build_depends = build_depends |
| 139 | + distro_version = utils.distro_version() |
| 140 | + package_xml = create_package_xml(package_template=package_template, rosdistro=distro_version) |
| 141 | + try: |
| 142 | + filename = os.path.join(package_path, 'package.xml') |
| 143 | + f = open(filename, 'w') |
| 144 | + f.write(package_xml) |
| 145 | + console.pretty_print('Created repo file: ', console.cyan) |
| 146 | + console.pretty_println('%s' % filename, console.yellow) |
| 147 | + finally: |
| 148 | + f.close() |
122 | 149 | except Exception:
|
123 | 150 | raise
|
124 | 151 |
|
125 |
| - |
126 |
| -def add_to_root_gradle_settings(name): |
127 |
| - ''' |
128 |
| - Adds project name to the root level settings.gradle file. |
129 |
| - ''' |
130 |
| - for rel_path in ['.', '..']: |
131 |
| - settings_gradle_path = os.path.join(os.getcwd(), rel_path, 'settings.gradle') |
132 |
| - if os.path.isfile(settings_gradle_path): |
133 |
| - break |
134 |
| - else: |
135 |
| - settings_gradle_path = None |
136 |
| - if settings_gradle_path is None: |
137 |
| - console.pretty_println("\nCouldn't find the root level settings.gradle file - not adding to the superproject.") |
138 |
| - return |
139 |
| - with open(settings_gradle_path, 'a') as settings_gradle: |
140 |
| - console.pretty_println("\nIncluding '%s' in the root gradle project configuration (settings.gradle).\n" % name, console.bold) |
141 |
| - settings_gradle.write("include '%s'\n" % name) |
142 |
| - |
143 |
| - |
144 |
| -def extra_gradle_library_text(): |
145 |
| - text = "\n" |
146 |
| - text += "/* http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds */\n" |
147 |
| - text += "android.libraryVariants\n" |
148 |
| - text += "publishing {\n" |
149 |
| - text += " publications {\n" |
150 |
| - text += " maven(MavenPublication) {\n" |
151 |
| - text += " /* artifact bundleDebug */\n" |
152 |
| - text += " artifact bundleRelease\n" |
153 |
| - text += " }\n" |
154 |
| - text += " }\n" |
155 |
| - text += "}\n" |
156 |
| - return text |
157 |
| - |
158 |
| - |
159 |
| -def create_android_package(is_library=False): |
160 |
| - args = parse_arguments() |
161 |
| - create_android_project(args.name[0], args.sdk_version, args.android_package_name, is_library) |
162 |
| - create_gradle_package_files(args, args.author, is_library, args.sdk_version) |
163 |
| - add_to_root_gradle_settings(args.name[0]) |
164 |
| - |
165 | 152 | ##############################################################################
|
166 |
| -# Borrowed from catkin_pkg.package_templates |
| 153 | +# Methods acting on classes |
167 | 154 | ##############################################################################
|
168 | 155 |
|
169 | 156 |
|
170 |
| -def read_template_file(filename): |
171 |
| - template_dir = os.path.join(os.path.dirname(__file__), 'templates', 'android_package') |
172 |
| - template = os.path.join(template_dir, '%s.in' % filename) |
173 |
| - if not os.path.isfile(template): |
174 |
| - raise IOError( |
175 |
| - "Could not read template [%s]" % template |
176 |
| - ) |
177 |
| - with open(template, 'r') as fhand: |
178 |
| - template_contents = fhand.read() |
179 |
| - return template_contents |
| 157 | +def init_android_repo(): |
| 158 | + args = parse_arguments() |
| 159 | + try: |
| 160 | + repo_path = utils.validate_path(args.path) |
| 161 | + repo_name = os.path.basename(os.path.normpath(repo_path)).lower() |
| 162 | + populate_repo(repo_path) |
| 163 | + create_catkin_package_files(repo_name, repo_path, args) |
| 164 | + create_gradle_wrapper(repo_path) |
| 165 | + except Exception: |
| 166 | + raise |
0 commit comments