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..4bde98a008b7c1b695cf0e17d9bb086d4210b83e 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) |
| @@ -44,7 +48,8 @@ def load_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) |
| @@ -86,39 +91,57 @@ def handle_single_schema(filename, dest_dir, root, root_namespace): |
| # 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++': |
|
benwells
2013/01/25 03:15:32
I don't think this is needed now, but it would be
|
| + 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 |
|
benwells
2013/01/25 03:15:32
You can factorize out the if dest_dir ... else: ..
|
| + 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: |
| @@ -141,38 +164,47 @@ def handle_bundle_schema(filenames, dest_dir, root, root_namespace): |
| 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: |
| + 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( |
| @@ -188,6 +220,12 @@ if __name__ == '__main__': |
| 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", |
|
benwells
2013/01/25 03:15:32
We should talk about custom hooks offline, I'm not
|
| + 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) |