00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 from __future__ import print_function
00012
00013 import os
00014 import sys
00015 import argparse
00016 import subprocess
00017 import shutil
00018
00019
00020 import utils
00021 import console
00022
00023
00024
00025
00026
00027
00028 def parse_arguments():
00029 argv = sys.argv[1:]
00030 parser = argparse.ArgumentParser(
00031 description='Creates a new android package based on catkin and gradle. \n')
00032 parser.add_argument('name',
00033 nargs=1,
00034 help='The name for the package')
00035 parser.add_argument('-s', '--sdk-version',
00036 action='store',
00037 default='17',
00038 help='Android sdk version [17]')
00039 parser.add_argument('-p', '--android-package-name',
00040 action='store',
00041 default='com.github.rosjava.android.pkg_name',
00042 help='Android package name (e.g. com.github.rosjava.android.pkg_name)')
00043 parser.add_argument('-a', '--author',
00044 action='store',
00045 default=utils.author_name(),
00046 help='A single author, may be used multiple times')
00047 args = parser.parse_args(argv)
00048 if args.android_package_name == "com.github.rosjava.android.pkg_name":
00049 args.android_package_name = "com.github.rosjava.android.%s" % args.name[0].lower()
00050 return args
00051
00052
00053 def create_android_project(package_name, sdk_version, java_package_name, is_library):
00054 path = os.path.join(os.getcwd(), package_name.lower())
00055 console.pretty_println("\nCreating android project ", console.bold)
00056 console.pretty_print(" Name : ", console.cyan)
00057 console.pretty_println("%s" % package_name, console.yellow)
00058 console.pretty_print(" Sdk Ver : ", console.cyan)
00059 console.pretty_println("%s" % sdk_version, console.yellow)
00060 console.pretty_print(" Java Name : ", console.cyan)
00061 console.pretty_println("%s" % java_package_name, console.yellow)
00062 if is_library:
00063 console.pretty_print(" Library : ", console.cyan)
00064 console.pretty_println("yes\n", console.yellow)
00065 cmd = ['android', 'create', 'lib-project', '-n', package_name, '-p', path, '-k', java_package_name, '-t', 'android-' + sdk_version, ]
00066 else:
00067 activity_name = utils.camel_case(package_name)
00068 console.pretty_print(" Activity : ", console.cyan)
00069 console.pretty_println("%s\n" % activity_name, console.yellow)
00070 cmd = ['android', 'create', 'project', '-n', package_name, '-p', path, '-k', java_package_name, '-t', 'android-' + sdk_version, '-a', activity_name]
00071 try:
00072 subprocess.check_call(cmd)
00073 except subprocess.CalledProcessError:
00074 raise subprocess.CalledProcessError("failed to create android project.")
00075
00076 utils.mkdir_p(os.path.join(path, 'src', 'main', 'java'))
00077 os.remove(os.path.join(path, 'local.properties'))
00078 os.remove(os.path.join(path, 'project.properties'))
00079 os.remove(os.path.join(path, 'ant.properties'))
00080 os.remove(os.path.join(path, 'proguard-project.txt'))
00081 os.remove(os.path.join(path, 'build.xml'))
00082 os.rmdir(os.path.join(path, 'bin'))
00083 os.rmdir(os.path.join(path, 'libs'))
00084 shutil.move(os.path.join(path, 'AndroidManifest.xml'), os.path.join(path, 'src', 'main'))
00085 shutil.move(os.path.join(path, 'res'), os.path.join(path, 'src', 'main'))
00086 if not is_library:
00087 shutil.move(os.path.join(path, 'src', java_package_name.split('.')[0]), os.path.join(path, 'src', 'main', 'java'))
00088
00089
00090
00091
00092
00093
00094
00095
00096 def instantiate_template(template, package_name, author, plugin_name, sdk_version):
00097 return template % locals()
00098
00099
00100 def create_gradle_package_files(args, author, is_library, sdk_version):
00101 '''
00102 This is almost a direct copy from catkin_create_pkg.
00103 '''
00104 plugin_name = "android-library" if is_library else "android"
00105 try:
00106 package_name = args.name[0].lower()
00107 package_path = os.path.abspath(os.path.join(os.getcwd(), package_name))
00108 console.pretty_println("\nCreating gradle files", console.bold)
00109 for template_name in ['build.gradle']:
00110 filename = os.path.join(package_path, template_name)
00111 template = read_template_file(template_name)
00112 contents = instantiate_template(template, package_name, author, plugin_name, sdk_version)
00113 if is_library:
00114 contents += extra_gradle_library_text()
00115 try:
00116 f = open(filename, 'w')
00117 f.write(contents)
00118 console.pretty_print(' File: ', console.cyan)
00119 console.pretty_println(template_name, console.yellow)
00120 finally:
00121 f.close()
00122 except Exception:
00123 raise
00124
00125
00126 def add_to_root_gradle_settings(name):
00127 '''
00128 Adds project name to the root level settings.gradle file.
00129 '''
00130 for rel_path in ['.', '..']:
00131 settings_gradle_path = os.path.join(os.getcwd(), rel_path, 'settings.gradle')
00132 if os.path.isfile(settings_gradle_path):
00133 break
00134 else:
00135 settings_gradle_path = None
00136 if settings_gradle_path is None:
00137 console.pretty_println("\nCouldn't find the root level settings.gradle file - not adding to the superproject.")
00138 return
00139 with open(settings_gradle_path, 'a') as settings_gradle:
00140 console.pretty_println("\nIncluding '%s' in the root gradle project configuration (settings.gradle).\n" % name, console.bold)
00141 settings_gradle.write("include '%s'\n" % name)
00142
00143
00144 def extra_gradle_library_text():
00145 text = "\n"
00146 text += "/* http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds */\n"
00147 text += "android.libraryVariants\n"
00148 text += "publishing {\n"
00149 text += " publications {\n"
00150 text += " maven(MavenPublication) {\n"
00151 text += " /* artifact bundleDebug */\n"
00152 text += " artifact bundleRelease\n"
00153 text += " }\n"
00154 text += " }\n"
00155 text += "}\n"
00156 return text
00157
00158
00159 def create_android_package(is_library=False):
00160 args = parse_arguments()
00161 create_android_project(args.name[0], args.sdk_version, args.android_package_name, is_library)
00162 create_gradle_package_files(args, args.author, is_library, args.sdk_version)
00163 add_to_root_gradle_settings(args.name[0])
00164
00165
00166
00167
00168
00169
00170 def read_template_file(filename):
00171 template_dir = os.path.join(os.path.dirname(__file__), 'templates', 'android_package')
00172 template = os.path.join(template_dir, '%s.in' % filename)
00173 if not os.path.isfile(template):
00174 raise IOError(
00175 "Could not read template [%s]" % template
00176 )
00177 with open(template, 'r') as fhand:
00178 template_contents = fhand.read()
00179 return template_contents