Chromium Code Reviews| Index: tools/json_schema_compiler/compiler.py | 
| diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py | 
| index a270756bf31bd127fa8d4a7203d44fad1b1dc94a..4422f594c7b91bd4899b923459a0b9fdc21c1c73 100755 | 
| --- a/tools/json_schema_compiler/compiler.py | 
| +++ b/tools/json_schema_compiler/compiler.py | 
| @@ -19,6 +19,7 @@ Usage example: | 
| import cc_generator | 
| import cpp_type_generator | 
| import h_generator | 
| +import dart_generator | 
| import idl_schema | 
| import json_schema | 
| import model | 
| @@ -28,6 +29,18 @@ import optparse | 
| import os.path | 
| import sys | 
| +# List of supported languages to build; first is default. | 
| +SUPPORTED_LANGS = ['c++', 'dart'] | 
| + | 
| +# For each language, the corresponding "ignore string" to remove that node from | 
| +# the returned namespace. | 
| +# TODO(sashab): Remove nodart from this list - all dart overrides must use the | 
| +# custom_dart_directory option. | 
| 
 
not at google - send to devlin
2013/01/29 16:37:08
sgtm
 
sashab
2013/01/30 05:26:03
Done.
 
 | 
| +NODE_IGNORE_STR = { | 
| + 'c++': 'nocompile', | 
| + 'dart': 'nodart', | 
| +} | 
| + | 
| def load_schema(schema): | 
| schema_filename, schema_extension = os.path.splitext(schema) | 
| @@ -36,19 +49,21 @@ def load_schema(schema): | 
| elif schema_extension == '.idl': | 
| api_defs = idl_schema.Load(schema) | 
| else: | 
| - sys.exit("Did not recognize file extension %s for schema %s" % | 
| + sys.exit('Did not recognize file extension %s for schema %s' % | 
| (schema_extension, schema)) | 
| if len(api_defs) != 1: | 
| - sys.exit("File %s has multiple schemas. Files are only allowed to contain a" | 
| - " single schema." % schema) | 
| + sys.exit('File %s has multiple schemas. Files are only allowed to contain a' | 
| + ' single schema.' % schema) | 
| return api_defs | 
| -def handle_single_schema(filename, dest_dir, root, root_namespace): | 
| +def handle_single_schema(filename, dest_dir, root, root_namespace, lang, | 
| + custom_dart_folder): | 
| schema = os.path.normpath(filename) | 
| schema_filename, schema_extension = os.path.splitext(schema) | 
| path, short_filename = os.path.split(schema_filename) | 
| - api_defs = json_schema.DeleteNocompileNodes(load_schema(schema)) | 
| + api_defs = json_schema.DeleteNodes(load_schema(schema), | 
| + NODE_IGNORE_STR[lang]) | 
| api_model = model.Model() | 
| @@ -83,45 +98,63 @@ def handle_single_schema(filename, dest_dir, root, root_namespace): | 
| continue | 
| if short_filename != namespace.unix_name: | 
| - sys.exit("Filename %s is illegal. Name files using unix_hacker style." % | 
| + sys.exit('Filename %s is illegal. Name files using unix_hacker style.' % | 
| filename) | 
| # The output filename must match the input filename for gyp to deal with it | 
| # properly. | 
| out_file = namespace.unix_name | 
| - type_generator = cpp_type_generator.CppTypeGenerator( | 
| - root_namespace, namespace, namespace.unix_name) | 
| - for referenced_namespace in api_model.namespaces.values(): | 
| - if referenced_namespace == namespace: | 
| - continue | 
| - type_generator.AddNamespace( | 
| - referenced_namespace, | 
| - referenced_namespace.unix_name) | 
| - h_code = (h_generator.HGenerator(namespace, type_generator) | 
| - .Generate().Render()) | 
| - cc_code = (cc_generator.CCGenerator(namespace, type_generator) | 
| - .Generate().Render()) | 
| + if lang.lower() == 'c++': | 
| + type_generator = cpp_type_generator.CppTypeGenerator( | 
| + root_namespace, namespace, namespace.unix_name) | 
| + for referenced_namespace in api_model.namespaces.values(): | 
| + if referenced_namespace == namespace: | 
| + continue | 
| + type_generator.AddNamespace( | 
| + referenced_namespace, | 
| + referenced_namespace.unix_name) | 
| + | 
| + h_code = (h_generator.HGenerator(namespace, type_generator) | 
| + .Generate().Render()) | 
| + cc_code = (cc_generator.CCGenerator(namespace, type_generator) | 
| + .Generate().Render()) | 
| + | 
| + if dest_dir: | 
| + with open( | 
| + os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | 
| + 'w') as cc_file: | 
| + cc_file.write(cc_code) | 
| + with open( | 
| + os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), | 
| + 'w') as h_file: | 
| + h_file.write(h_code) | 
| + else: | 
| + print '%s.h' % out_file | 
| + print h_code | 
| + print '%s.cc' % out_file | 
| + print cc_code | 
| + | 
| + elif lang.lower() == 'dart': | 
| + dart_code = (dart_generator.DartGenerator(namespace, custom_dart_folder) | 
| + .Generate().Render()) | 
| + | 
| + if dest_dir: | 
| + with open(os.path.join(dest_dir, out_file + '.dart'), 'w') as dart_file: | 
| + dart_file.write(dart_code) | 
| + else: | 
| + print '%s.dart' % out_file | 
| + print dart_code | 
| - if dest_dir: | 
| - with open( | 
| - os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'), | 
| - 'w') as cc_file: | 
| - cc_file.write(cc_code) | 
| - with open( | 
| - os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'), | 
| - 'w') as h_file: | 
| - h_file.write(h_code) | 
| else: | 
| - print '%s.h' % out_file | 
| - print h_code | 
| - print '%s.cc' % out_file | 
| - print cc_code | 
| + raise Exception('Unrecognised language %s. Supported languages are %s.' % | 
| + (lang, SUPPORTED_LANGS)) | 
| -def handle_bundle_schema(filenames, dest_dir, root, root_namespace): | 
| +def handle_bundle_schema(filenames, dest_dir, root, root_namespace, lang): | 
| # Merge the source files into a single list of schemas. | 
| api_defs = [] | 
| for filename in filenames: | 
| @@ -143,41 +176,49 @@ def handle_bundle_schema(filenames, dest_dir, root, root_namespace): | 
| # generate because the gyp uses the names of the JSON files to generate | 
| # the names of the .cc and .h files. We want these to be using unix_names. | 
| if namespace.unix_name != short_filename: | 
| - sys.exit("Filename %s is illegal. Name files using unix_hacker style." % | 
| + sys.exit('Filename %s is illegal. Name files using unix_hacker style.' % | 
| schema_filename) | 
| - type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 
| - for referenced_namespace in api_model.namespaces.values(): | 
| - type_generator.AddNamespace( | 
| - referenced_namespace, | 
| - referenced_namespace.unix_name) | 
| - | 
| - generator = schema_bundle_generator.SchemaBundleGenerator( | 
| - root, api_model, api_defs, type_generator) | 
| - api_h_code = generator.GenerateAPIHeader().Render() | 
| - schemas_h_code = generator.GenerateSchemasHeader().Render() | 
| - schemas_cc_code = generator.GenerateSchemasCC().Render() | 
| - | 
| - if dest_dir: | 
| - basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') | 
| - with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: | 
| - h_file.write(api_h_code) | 
| - with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: | 
| - h_file.write(schemas_h_code) | 
| - with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: | 
| - cc_file.write(schemas_cc_code) | 
| + if lang == 'c++': | 
| + type_generator = cpp_type_generator.CppTypeGenerator(root_namespace) | 
| + for referenced_namespace in api_model.namespaces.values(): | 
| + type_generator.AddNamespace( | 
| + referenced_namespace, | 
| + referenced_namespace.unix_name) | 
| + | 
| + generator = schema_bundle_generator.SchemaBundleGenerator( | 
| + root, api_model, api_defs, type_generator) | 
| + api_h_code = generator.GenerateAPIHeader().Render() | 
| + schemas_h_code = generator.GenerateSchemasHeader().Render() | 
| + schemas_cc_code = generator.GenerateSchemasCC().Render() | 
| + | 
| + if dest_dir: | 
| + basedir = os.path.join(dest_dir, 'chrome/common/extensions/api') | 
| + with open(os.path.join(basedir, 'generated_api.h'), 'w') as h_file: | 
| + h_file.write(api_h_code) | 
| + with open(os.path.join(basedir, 'generated_schemas.h'), 'w') as h_file: | 
| + h_file.write(schemas_h_code) | 
| + with open(os.path.join(basedir, 'generated_schemas.cc'), 'w') as cc_file: | 
| + cc_file.write(schemas_cc_code) | 
| + else: | 
| + print 'generated_api.h' | 
| + print api_h_code | 
| + print 'generated_schemas.h' | 
| + print schemas_h_code | 
| + print 'generated_schemas.cc' | 
| + print schemas_cc_code | 
| + | 
| + elif lang == 'dart': | 
| + raise Exception('Dart is not supported in bundle mode.') | 
| + | 
| else: | 
| - print 'generated_api.h' | 
| - print api_h_code | 
| - print 'generated_schemas.h' | 
| - print schemas_h_code | 
| - print 'generated_schemas.cc' | 
| - print schemas_cc_code | 
| + raise Exception("Unrecognised language %s. Supported languages are %s." % | 
| + (lang, SUPPORTED_LANG)) | 
| if __name__ == '__main__': | 
| parser = optparse.OptionParser( | 
| @@ -190,9 +231,15 @@ if __name__ == '__main__': | 
| help='root directory to output generated files.') | 
| parser.add_option('-n', '--namespace', default='generated_api_schemas', | 
| help='C++ namespace for generated files. e.g extensions::api.') | 
| - parser.add_option('-b', '--bundle', action="store_true", help= | 
| + parser.add_option('-b', '--bundle', action='store_true', help= | 
| '''if supplied, causes compiler to generate bundle files for the given set of | 
| source files.''') | 
| + parser.add_option('-l', '--lang', default=SUPPORTED_LANGS[0], | 
| + help= | 
| +'''The language to generate the output in. Currently supported options are | 
| +%s.''' % SUPPORTED_LANGS) | 
| + parser.add_option('-D', '--custom-dart-folder', dest='custom_dart_folder', | 
| + help='Adds custom dart from files in the given folder (Dart only).') | 
| (opts, args) = parser.parse_args() | 
| @@ -202,6 +249,7 @@ source files.''') | 
| root_namespace = opts.namespace | 
| if opts.bundle: | 
| - handle_bundle_schema(args, dest_dir, opts.root, root_namespace) | 
| + handle_bundle_schema(args, dest_dir, opts.root, root_namespace, opts.lang) | 
| else: | 
| - handle_single_schema(args[0], dest_dir, opts.root, root_namespace) | 
| + handle_single_schema(args[0], dest_dir, opts.root, root_namespace, | 
| + opts.lang, opts.custom_dart_folder) |