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 ae9237336f41af191a39fe1dbe87791ee3e16c09..f372b339241dd317e2feee51b7e844b5b098362e 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,9 @@ import optparse |
| import os.path |
| import sys |
| +# list of supported languages to build; first is default |
| +SUPPORTED_LANGS = ['c++', 'dart'] |
| + |
| def load_schema(schema): |
| schema_filename, schema_extension = os.path.splitext(schema) |
| @@ -36,19 +40,20 @@ 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, |
| + hooks_file): |
| 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.DeleteNocompileNodes(load_schema(schema), lang) |
| api_model = model.Model() |
| @@ -80,45 +85,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++': |
|
not at google - send to devlin
2013/01/25 18:14:33
Unfortunately, now compiler.py has three personali
sashab
2013/01/30 05:26:03
Unfortunately, it doesn't seem that simple, for a
not at google - send to devlin
2013/01/31 02:08:48
I think these are surmountable. If a generator nee
|
| + 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, hooks_file) |
| + .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: |
| @@ -138,41 +161,50 @@ 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( |
| - 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( |
| + 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: |
|
not at google - send to devlin
2013/01/25 18:14:33
I think the time has come to delete this branch
sashab
2013/02/04 05:09:27
Done.
|
| + 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_LANGS)) |
| + |
| if __name__ == '__main__': |
| parser = optparse.OptionParser( |
| @@ -185,9 +217,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('-H', '--hooks-file', dest='hooks_file', |
| + help='Adds custom hooks from the given file (Dart only).', metavar='FILE') |
| (opts, args) = parser.parse_args() |
| @@ -197,6 +235,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.hooks_file) |