Index: tools/json_schema_compiler/compiler.py |
diff --git a/tools/json_schema_compiler/compiler.py b/tools/json_schema_compiler/compiler.py |
index 38899286d215158c7701ff7cda84285f927e0f01..7a2e4dd6f1593ed3a4ae95b03631d3a483ffd5c0 100755 |
--- a/tools/json_schema_compiler/compiler.py |
+++ b/tools/json_schema_compiler/compiler.py |
@@ -18,6 +18,7 @@ Usage example: |
import optparse |
import os |
+import shlex |
import sys |
from cpp_bundle_generator import CppBundleGenerator |
@@ -25,6 +26,7 @@ from cpp_generator import CppGenerator |
from cpp_type_generator import CppTypeGenerator |
from dart_generator import DartGenerator |
import json_schema |
+from cpp_namespace_environment import CppNamespaceEnvironment |
from model import Model |
from schema_loader import SchemaLoader |
@@ -38,15 +40,18 @@ def GenerateSchema(generator_name, |
destdir, |
cpp_namespace_pattern, |
dart_overrides_dir, |
- impl_dir): |
+ impl_dir, |
+ include_rules): |
# Merge the source files into a single list of schemas. |
api_defs = [] |
for file_path in file_paths: |
- schema = os.path.normpath(file_path) |
+ schema = os.path.relpath(file_path, root) |
schema_loader = SchemaLoader( |
- os.path.dirname(os.path.relpath(schema, root)), |
- os.path.dirname(file_path)) |
- api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) |
+ root, |
+ os.path.dirname(schema), |
+ include_rules, |
+ cpp_namespace_pattern) |
+ api_def = schema_loader.LoadSchema(schema) |
# If compiling the C++ model code, delete 'nocompile' nodes. |
if generator_name == 'cpp': |
@@ -68,7 +73,9 @@ def GenerateSchema(generator_name, |
relpath = os.path.relpath(os.path.normpath(file_path), root) |
namespace = api_model.AddNamespace(target_namespace, |
relpath, |
- include_compiler_options=True) |
+ include_compiler_options=True, |
+ environment=CppNamespaceEnvironment( |
+ cpp_namespace_pattern)) |
if default_namespace is None: |
default_namespace = namespace |
@@ -105,7 +112,7 @@ def GenerateSchema(generator_name, |
('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) |
] |
elif generator_name == 'cpp': |
- cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) |
+ cpp_generator = CppGenerator(type_generator) |
generators = [ |
('%s.h' % filename_base, cpp_generator.h_generator), |
('%s.cc' % filename_base, cpp_generator.cc_generator) |
@@ -156,6 +163,10 @@ if __name__ == '__main__': |
help='Adds custom dart from files in the given directory (Dart only).') |
parser.add_option('-i', '--impl-dir', dest='impl_dir', |
help='The root path of all API implementations') |
+ parser.add_option('-I', '--include-rules', |
+ help='A list of paths to include when searching for referenced objects,' |
+ ' with the namespace separated by a \':\'. Example: ' |
+ '/foo/bar:Foo::Bar::%(namespace)s') |
(opts, file_paths) = parser.parse_args() |
@@ -169,8 +180,19 @@ if __name__ == '__main__': |
raise Exception( |
"Unless in bundle mode, only one file can be specified at a time.") |
+ def split_path_and_namespace(path_and_namespace): |
+ if ':' not in path_and_namespace: |
+ raise ValueError('Invalid include rule "%s". Rules must be of ' |
+ 'the form path:namespace' % path_and_namespace) |
+ return path_and_namespace.split(':', 1) |
+ |
+ include_rules = [] |
+ if opts.include_rules: |
+ include_rules = map(split_path_and_namespace, |
+ shlex.split(opts.include_rules)) |
+ |
result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, |
opts.namespace, opts.dart_overrides_dir, |
- opts.impl_dir) |
+ opts.impl_dir, include_rules) |
if not opts.destdir: |
print result |