| 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..ef01c010fdb77fe12056568e0b15a643a85b7532 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,15 @@ 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.
|
| +NODE_IGNORE_STR = {
|
| + 'c++': 'nocompile',
|
| +}
|
| +
|
| def load_schema(schema):
|
| schema_filename, schema_extension = os.path.splitext(schema)
|
|
|
| @@ -36,19 +46,22 @@ 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,
|
| + dart_overrides_dir):
|
| 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 = load_schema(schema)
|
| + if lang in NODE_IGNORE_STR:
|
| + api_defs = json_schema.DeleteNodes(api_defs, NODE_IGNORE_STR[lang])
|
|
|
| api_model = model.Model()
|
|
|
| @@ -83,45 +96,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
|
| + print h_code
|
| + print
|
| + print '%s.cc' % out_file
|
| + print
|
| + print cc_code
|
| +
|
| + elif lang.lower() == 'dart':
|
| + dart_code = (dart_generator.DartGenerator(namespace, dart_overrides_dir)
|
| + .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
|
| + 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
|
| - print h_code
|
| - print
|
| - print '%s.cc' % out_file
|
| - print
|
| - 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 +174,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
|
| + print api_h_code
|
| + print
|
| + print 'generated_schemas.h'
|
| + print
|
| + print schemas_h_code
|
| + print
|
| + print 'generated_schemas.cc'
|
| + print
|
| + print schemas_cc_code
|
| +
|
| + elif lang == 'dart':
|
| + raise Exception('Dart is not supported in bundle mode.')
|
| +
|
| else:
|
| - print 'generated_api.h'
|
| - print
|
| - print api_h_code
|
| - print
|
| - print 'generated_schemas.h'
|
| - print
|
| - print schemas_h_code
|
| - print
|
| - print 'generated_schemas.cc'
|
| - print
|
| - print schemas_cc_code
|
| + raise Exception("Unrecognised language %s. Supported languages are %s." %
|
| + (lang, SUPPORTED_LANG))
|
|
|
| if __name__ == '__main__':
|
| parser = optparse.OptionParser(
|
| @@ -190,9 +229,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='dart_overrides_dir',
|
| + help='Adds custom dart from files in the given folder (Dart only).')
|
|
|
| (opts, args) = parser.parse_args()
|
|
|
| @@ -202,6 +247,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.dart_overrides_dir)
|
|
|