Skip to content

Commit 4ccaa93

Browse files
committed
scripts working for rosjava packages and projects.
1 parent b110ae0 commit 4ccaa93

18 files changed

+472
-10
lines changed

scripts/catkin_create_android_pkg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import rosjava_build_tools.console as console
2020

2121
if __name__ == "__main__":
2222
try:
23-
sys.exit(init_android_repo())
23+
sys.exit(init_android_package())
2424
except Exception as e:
2525
console.logerror("%s : %s" % (str(e), type(e)))
2626
sys.exit(1)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
"""This script creates the skeleton of an android library package"""
4+
5+
##############################################################################
6+
# Imports
7+
##############################################################################
8+
9+
from __future__ import print_function
10+
import argparse
11+
import os
12+
import sys
13+
14+
from rosjava_build_tools import create_rosjava_msg_project
15+
import rosjava_build_tools.console as console
16+
17+
##############################################################################
18+
# Main
19+
##############################################################################
20+
21+
if __name__ == "__main__":
22+
try:
23+
sys.exit(create_rosjava_msg_project())
24+
except Exception as e:
25+
console.logerror("%s : %s" % (str(e), type(e)))
26+
sys.exit(1)

scripts/catkin_create_rosjava_pkg

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
"""This script creates the skeleton of an rosjava repo"""
4+
5+
##############################################################################
6+
# Imports
7+
##############################################################################
8+
9+
from __future__ import print_function
10+
import argparse
11+
import os
12+
import sys
13+
14+
from rosjava_build_tools import init_rosjava_package
15+
import rosjava_build_tools.console as console
16+
17+
##############################################################################
18+
# Main
19+
##############################################################################
20+
21+
if __name__ == "__main__":
22+
try:
23+
sys.exit(init_rosjava_package())
24+
except Exception as e:
25+
console.logerror("%s : %s" % (str(e), type(e)))
26+
sys.exit(1)

scripts/catkin_create_rosjava_project

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
"""This script creates the skeleton of an android library package"""
4+
5+
##############################################################################
6+
# Imports
7+
##############################################################################
8+
9+
from __future__ import print_function
10+
import argparse
11+
import os
12+
import sys
13+
14+
from rosjava_build_tools import create_rosjava_project
15+
import rosjava_build_tools.console as console
16+
17+
##############################################################################
18+
# Main
19+
##############################################################################
20+
21+
if __name__ == "__main__":
22+
try:
23+
sys.exit(create_rosjava_project())
24+
except Exception as e:
25+
console.logerror("%s : %s" % (str(e), type(e)))
26+
sys.exit(1)

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
scripts=['scripts/catkin_create_android_pkg',
1010
'scripts/catkin_create_android_project',
1111
'scripts/catkin_create_android_library_project',
12+
'scripts/catkin_create_rosjava_pkg',
13+
'scripts/catkin_create_rosjava_project',
14+
'scripts/catkin_create_rosjava_msg_project',
1215
],
1316
package_data = {'rosjava_build_tools': [
1417
'templates/android_package/*',

src/rosjava_build_tools/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
##############################################################################
1010

1111
import console
12-
from create_package import init_android_repo
13-
from create_project import create_android_project
12+
from create_package import init_android_package, init_rosjava_package
13+
from create_android_project import create_android_project
14+
from create_rosjava_project import create_rosjava_project, create_rosjava_msg_project
1415
from utils import which
1516
from release import scrape_for_release_message_packages

src/rosjava_build_tools/create_package.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
def parse_arguments():
2828
argv = sys.argv[1:]
2929
parser = argparse.ArgumentParser(
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')
30+
description='Creates a new rosjava/android repository based on catkin and gradle. \n\nNote that the path you provide will become the maven group for your repo.\n')
3131
parser.add_argument('path', nargs='?', default=os.getcwd(), help='path to the repository you wish to create (must not exist beforehand).')
3232
parser.add_argument('dependencies',
3333
nargs='*',
@@ -78,19 +78,19 @@ def instantiate_template(template, repo_name, author):
7878
return template % locals()
7979

8080

81-
def get_templates():
82-
template_dir = os.path.join(os.path.dirname(__file__), 'templates', 'init_repo')
81+
def get_templates(template_directory):
82+
template_dir = os.path.join(os.path.dirname(__file__), 'templates', template_directory)
8383
templates = {}
8484
templates['CMakeLists.txt'] = read_template(os.path.join(template_dir, 'CMakeLists.txt.in'))
8585
templates['build.gradle'] = read_template(os.path.join(template_dir, 'build.gradle.in'))
8686
templates['settings.gradle'] = read_template(os.path.join(template_dir, 'settings.gradle'))
8787
return templates
8888

8989

90-
def populate_repo(repo_path):
90+
def populate_repo(repo_path, package_type):
9191
author = utils.author_name()
9292
repo_name = os.path.basename(repo_path)
93-
templates = get_templates()
93+
templates = get_templates(package_type)
9494
for filename, template in templates.iteritems():
9595
contents = instantiate_template(template, repo_name, author)
9696
try:
@@ -154,13 +154,21 @@ def create_catkin_package_files(package_name, package_path, args):
154154
##############################################################################
155155

156156

157-
def init_android_repo():
157+
def init_package(package_type):
158158
args = parse_arguments()
159159
try:
160160
repo_path = utils.validate_path(args.path)
161161
repo_name = os.path.basename(os.path.normpath(repo_path)).lower()
162-
populate_repo(repo_path)
162+
populate_repo(repo_path, package_type)
163163
create_catkin_package_files(repo_name, repo_path, args)
164164
create_gradle_wrapper(repo_path)
165165
except Exception:
166166
raise
167+
168+
169+
def init_rosjava_package():
170+
init_package('rosjava_package')
171+
172+
173+
def init_android_package():
174+
init_package('android_package')
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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])

src/rosjava_build_tools/templates/init_repo/CMakeLists.txt.in renamed to src/rosjava_build_tools/templates/android_package/CMakeLists.txt.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ find_package(catkin REQUIRED rosjava_build_tools)
1313
# Set the gradle targets you want catkin's make to run by default
1414
catkin_android_setup(assembleRelease uploadArchives)
1515
catkin_package()
16+
17+
18+
##############################################################################
19+
# Installation
20+
##############################################################################
21+
22+
# If you are deploying android libraries (.aar's) uncomment this and
23+
# change this to match the maven group name you have specified in the
24+
# allprojects closure the root build.gradle
25+
#install(DIRECTORY ${CATKIN_DEVEL_PREFIX}/${CATKIN_GLOBAL_MAVEN_DESTINATION}/com/github/rosjava/${PROJECT_NAME}/
26+
# DESTINATION ${CATKIN_GLOBAL_MAVEN_DESTINATION}/com/github/rosjava/${PROJECT_NAME})

0 commit comments

Comments
 (0)