Improve CLI interface of C++ enums to Java generator
1. Print error if input file doesn't have any annotated enums. 2. Convert "mandatory option" 'output_dir' into a positional argument. 3. Fix 'print_output_only' when running from command line. BUG=458562 Review URL: https://codereview.chromium.org/934873002 Cr-Original-Commit-Position: refs/heads/master@{#317783} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 4e3301c82c48ac22439d1b9dc34ba05a0599e3e9
This commit is contained in:
Родитель
aa2673d7a2
Коммит
e1c04e42ea
|
@ -228,16 +228,21 @@ def GetScriptName():
|
|||
return os.sep.join(script_components[build_index:])
|
||||
|
||||
|
||||
def DoGenerate(options, source_paths):
|
||||
def DoGenerate(output_dir, source_paths, print_output_only=False):
|
||||
output_paths = []
|
||||
for source_path in source_paths:
|
||||
enum_definitions = DoParseHeaderFile(source_path)
|
||||
if not enum_definitions:
|
||||
raise Exception('No enums found in %s\n'
|
||||
'Did you forget prefixing enums with '
|
||||
'"// GENERATED_JAVA_ENUM_PACKAGE: foo"?' %
|
||||
source_path)
|
||||
for enum_definition in enum_definitions:
|
||||
package_path = enum_definition.enum_package.replace('.', os.path.sep)
|
||||
file_name = enum_definition.class_name + '.java'
|
||||
output_path = os.path.join(options.output_dir, package_path, file_name)
|
||||
output_path = os.path.join(output_dir, package_path, file_name)
|
||||
output_paths.append(output_path)
|
||||
if not options.print_output_only:
|
||||
if not print_output_only:
|
||||
build_utils.MakeDirectory(os.path.dirname(output_path))
|
||||
DoWriteOutput(source_path, output_path, enum_definition)
|
||||
return output_paths
|
||||
|
@ -300,24 +305,32 @@ def AssertFilesList(output_paths, assert_files_list):
|
|||
'add %s and remove %s.' % (need_to_add, need_to_remove))
|
||||
|
||||
def DoMain(argv):
|
||||
parser = optparse.OptionParser()
|
||||
usage = 'usage: %prog [options] output_dir input_file(s)...'
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
|
||||
parser.add_option('--assert_file', action="append", default=[],
|
||||
dest="assert_files_list", help='Assert that the given '
|
||||
'file is an output. There can be multiple occurrences of '
|
||||
'this flag.')
|
||||
parser.add_option('--output_dir', help='Base path for generated files.')
|
||||
parser.add_option('--print_output_only', help='Only print output paths.',
|
||||
action='store_true')
|
||||
parser.add_option('--verbose', help='Print more information.',
|
||||
action='store_true')
|
||||
|
||||
options, args = parser.parse_args(argv)
|
||||
|
||||
output_paths = DoGenerate(options, args)
|
||||
if len(args) < 2:
|
||||
parser.error('Need to specify output directory and at least one input file')
|
||||
output_paths = DoGenerate(args[0], args[1:],
|
||||
print_output_only=options.print_output_only)
|
||||
|
||||
if options.assert_files_list:
|
||||
AssertFilesList(output_paths, options.assert_files_list)
|
||||
|
||||
return " ".join(output_paths)
|
||||
if options.verbose:
|
||||
print 'Output paths:'
|
||||
print '\n'.join(output_paths)
|
||||
|
||||
return ' '.join(output_paths)
|
||||
|
||||
if __name__ == '__main__':
|
||||
DoMain(sys.argv[1:])
|
||||
|
|
|
@ -14,6 +14,7 @@ import os
|
|||
import sys
|
||||
import unittest
|
||||
|
||||
import java_cpp_enum
|
||||
from java_cpp_enum import EnumDefinition, GenerateOutput, GetScriptName
|
||||
from java_cpp_enum import HeaderParser
|
||||
|
||||
|
@ -151,6 +152,14 @@ public class ClassName {
|
|||
with self.assertRaises(Exception):
|
||||
HeaderParser(test_data).ParseDefinitions()
|
||||
|
||||
def testParseReturnsEmptyListWithoutDirectives(self):
|
||||
test_data = """
|
||||
enum EnumName {
|
||||
VALUE_ONE,
|
||||
};
|
||||
""".split('\n')
|
||||
self.assertEqual([], HeaderParser(test_data).ParseDefinitions())
|
||||
|
||||
def testParseEnumClass(self):
|
||||
test_data = """
|
||||
// GENERATED_JAVA_ENUM_PACKAGE: test.namespace
|
||||
|
@ -403,6 +412,15 @@ public class ClassName {
|
|||
definition.Finalize()
|
||||
self.assertEqual(['A', 'B', 'NAME_LAST'], definition.entries.keys())
|
||||
|
||||
def testGenerateThrowsOnEmptyInput(self):
|
||||
with self.assertRaises(Exception):
|
||||
original_do_parse = java_cpp_enum.DoParseHeaderFile
|
||||
try:
|
||||
java_cpp_enum.DoParseHeaderFile = lambda _: []
|
||||
java_cpp_enum.DoGenerate('dir', ['file'])
|
||||
finally:
|
||||
java_cpp_enum.DoParseHeaderFile = original_do_parse
|
||||
|
||||
def main(argv):
|
||||
parser = optparse.OptionParser()
|
||||
parser.add_option("--stamp", help="File to touch on success.")
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
# Location where all generated Java sources will be placed.
|
||||
'output_dir': '<(SHARED_INTERMEDIATE_DIR)/enums/<(_target_name)',
|
||||
'generator_path': '<(DEPTH)/build/android/gyp/java_cpp_enum.py',
|
||||
'generator_args': '--output_dir=<(output_dir) <(source_file)',
|
||||
'generator_args': '<(output_dir) <(source_file)',
|
||||
},
|
||||
'direct_dependent_settings': {
|
||||
'variables': {
|
||||
|
|
|
@ -335,8 +335,8 @@ template("java_cpp_template") {
|
|||
#
|
||||
# outputs: list of outputs, relative to the output_dir. These paths are
|
||||
# verified at build time by the script. To get the list programatically run:
|
||||
# python build/android/gyp/java_cpp_enum.py --output_dir=. \
|
||||
# --print_output_only path/to/header/file.h
|
||||
# python build/android/gyp/java_cpp_enum.py \
|
||||
# --print_output_only . path/to/header/file.h
|
||||
#
|
||||
# Example
|
||||
# java_cpp_enum("foo_generated_enum") {
|
||||
|
@ -366,16 +366,14 @@ template("java_cpp_enum") {
|
|||
outputs =
|
||||
get_path_info(rebase_path(invoker.outputs, ".", gen_dir), "abspath")
|
||||
|
||||
args = [
|
||||
"--output_dir",
|
||||
rebase_path(gen_dir, root_build_dir),
|
||||
]
|
||||
args = []
|
||||
foreach(output, rebase_path(outputs, root_build_dir)) {
|
||||
args += [
|
||||
"--assert_file",
|
||||
output,
|
||||
]
|
||||
}
|
||||
args += [ rebase_path(gen_dir, root_build_dir) ]
|
||||
args += rebase_path(invoker.sources, root_build_dir)
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче