Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: tools/json_schema_compiler/compiler.py

Issue 487533005: Add support for references in different paths in apis (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Renaming core_api::types->core_api::extension_types. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 """Generator for C++ structs from api json files. 5 """Generator for C++ structs from api json files.
6 6
7 The purpose of this tool is to remove the need for hand-written code that 7 The purpose of this tool is to remove the need for hand-written code that
8 converts to and from base::Value types when receiving javascript api calls. 8 converts to and from base::Value types when receiving javascript api calls.
9 Originally written for generating code for extension apis. Reference schemas 9 Originally written for generating code for extension apis. Reference schemas
10 are in chrome/common/extensions/api. 10 are in chrome/common/extensions/api.
11 11
12 Usage example: 12 Usage example:
13 compiler.py --root /home/Work/src --namespace extensions windows.json 13 compiler.py --root /home/Work/src --namespace extensions windows.json
14 tabs.json 14 tabs.json
15 compiler.py --destdir gen --root /home/Work/src 15 compiler.py --destdir gen --root /home/Work/src
16 --namespace extensions windows.json tabs.json 16 --namespace extensions windows.json tabs.json
17 """ 17 """
18 18
19 import optparse 19 import optparse
20 import os 20 import os
21 import sys 21 import sys
22 import shlex
not at google - send to devlin 2014/08/25 22:22:47 Alphabetic order.
lfg 2014/08/26 16:15:22 Done.
22 23
23 from cpp_bundle_generator import CppBundleGenerator 24 from cpp_bundle_generator import CppBundleGenerator
24 from cpp_generator import CppGenerator 25 from cpp_generator import CppGenerator
25 from cpp_type_generator import CppTypeGenerator 26 from cpp_type_generator import CppTypeGenerator
26 from dart_generator import DartGenerator 27 from dart_generator import DartGenerator
27 import json_schema 28 import json_schema
28 from model import Model 29 from model import Model
29 from schema_loader import SchemaLoader 30 from schema_loader import SchemaLoader
30 31
31 # Names of supported code generators, as specified on the command-line. 32 # Names of supported code generators, as specified on the command-line.
32 # First is default. 33 # First is default.
33 GENERATORS = ['cpp', 'cpp-bundle', 'dart'] 34 GENERATORS = ['cpp', 'cpp-bundle', 'dart']
34 35
35 def GenerateSchema(generator, 36 def GenerateSchema(generator,
36 file_paths, 37 file_paths,
37 root, 38 root,
38 destdir, 39 destdir,
39 cpp_namespace_pattern, 40 cpp_namespace_pattern,
40 dart_overrides_dir, 41 dart_overrides_dir,
41 impl_dir): 42 impl_dir,
43 includes):
42 # Merge the source files into a single list of schemas. 44 # Merge the source files into a single list of schemas.
43 api_defs = [] 45 api_defs = []
44 for file_path in file_paths: 46 for file_path in file_paths:
45 schema = os.path.normpath(file_path) 47 schema = os.path.normpath(file_path)
46 schema_loader = SchemaLoader( 48 schema_loader = SchemaLoader(
47 os.path.dirname(os.path.relpath(schema, root)), 49 os.path.dirname(os.path.relpath(schema, root)),
48 os.path.dirname(file_path)) 50 os.path.dirname(file_path),
51 includes,
52 cpp_namespace_pattern)
49 api_def = schema_loader.LoadSchema(os.path.split(schema)[1]) 53 api_def = schema_loader.LoadSchema(os.path.split(schema)[1])
50 54
51 # If compiling the C++ model code, delete 'nocompile' nodes. 55 # If compiling the C++ model code, delete 'nocompile' nodes.
52 if generator == 'cpp': 56 if generator == 'cpp':
53 api_def = json_schema.DeleteNodes(api_def, 'nocompile') 57 api_def = json_schema.DeleteNodes(api_def, 'nocompile')
54 api_defs.extend(api_def) 58 api_defs.extend(api_def)
55 59
56 api_model = Model() 60 api_model = Model()
57 61
58 # For single-schema compilation make sure that the first (i.e. only) schema 62 # For single-schema compilation make sure that the first (i.e. only) schema
59 # is the default one. 63 # is the default one.
60 default_namespace = None 64 default_namespace = None
61 65
62 # If we have files from multiple source paths, we'll use the common parent 66 # If we have files from multiple source paths, we'll use the common parent
63 # path as the source directory. 67 # path as the source directory.
64 src_path = None 68 src_path = None
65 69
66 # Load the actual namespaces into the model. 70 # Load the actual namespaces into the model.
67 for target_namespace, file_path in zip(api_defs, file_paths): 71 for target_namespace, file_path in zip(api_defs, file_paths):
68 relpath = os.path.relpath(os.path.normpath(file_path), root) 72 relpath = os.path.relpath(os.path.normpath(file_path), root)
69 namespace = api_model.AddNamespace(target_namespace, 73 namespace = api_model.AddNamespace(target_namespace,
70 relpath, 74 relpath,
75 cpp_namespace_pattern,
71 include_compiler_options=True) 76 include_compiler_options=True)
72 77
73 if default_namespace is None: 78 if default_namespace is None:
74 default_namespace = namespace 79 default_namespace = namespace
75 80
76 if src_path is None: 81 if src_path is None:
77 src_path = namespace.source_file_dir 82 src_path = namespace.source_file_dir
78 else: 83 else:
79 src_path = os.path.commonprefix((src_path, namespace.source_file_dir)) 84 src_path = os.path.commonprefix((src_path, namespace.source_file_dir))
80 85
(...skipping 12 matching lines...) Expand all
93 cpp_namespace_pattern, 98 cpp_namespace_pattern,
94 src_path, 99 src_path,
95 impl_dir) 100 impl_dir)
96 generators = [ 101 generators = [
97 ('generated_api.cc', cpp_bundle_generator.api_cc_generator), 102 ('generated_api.cc', cpp_bundle_generator.api_cc_generator),
98 ('generated_api.h', cpp_bundle_generator.api_h_generator), 103 ('generated_api.h', cpp_bundle_generator.api_h_generator),
99 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator), 104 ('generated_schemas.cc', cpp_bundle_generator.schemas_cc_generator),
100 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator) 105 ('generated_schemas.h', cpp_bundle_generator.schemas_h_generator)
101 ] 106 ]
102 elif generator == 'cpp': 107 elif generator == 'cpp':
103 cpp_generator = CppGenerator(type_generator, cpp_namespace_pattern) 108 cpp_generator = CppGenerator(type_generator)
104 generators = [ 109 generators = [
105 ('%s.h' % filename_base, cpp_generator.h_generator), 110 ('%s.h' % filename_base, cpp_generator.h_generator),
106 ('%s.cc' % filename_base, cpp_generator.cc_generator) 111 ('%s.cc' % filename_base, cpp_generator.cc_generator)
107 ] 112 ]
108 elif generator == 'dart': 113 elif generator == 'dart':
109 generators = [ 114 generators = [
110 ('%s.dart' % namespace.unix_name, DartGenerator( 115 ('%s.dart' % namespace.unix_name, DartGenerator(
111 dart_overrides_dir)) 116 dart_overrides_dir))
112 ] 117 ]
113 else: 118 else:
(...skipping 25 matching lines...) Expand all
139 parser.add_option('-n', '--namespace', default='generated_api_schemas', 144 parser.add_option('-n', '--namespace', default='generated_api_schemas',
140 help='C++ namespace for generated files. e.g extensions::api.') 145 help='C++ namespace for generated files. e.g extensions::api.')
141 parser.add_option('-g', '--generator', default=GENERATORS[0], 146 parser.add_option('-g', '--generator', default=GENERATORS[0],
142 choices=GENERATORS, 147 choices=GENERATORS,
143 help='The generator to use to build the output code. Supported values are' 148 help='The generator to use to build the output code. Supported values are'
144 ' %s' % GENERATORS) 149 ' %s' % GENERATORS)
145 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir', 150 parser.add_option('-D', '--dart-overrides-dir', dest='dart_overrides_dir',
146 help='Adds custom dart from files in the given directory (Dart only).') 151 help='Adds custom dart from files in the given directory (Dart only).')
147 parser.add_option('-i', '--impl-dir', dest='impl_dir', 152 parser.add_option('-i', '--impl-dir', dest='impl_dir',
148 help='The root path of all API implementations') 153 help='The root path of all API implementations')
154 parser.add_option('-I', '--include', #dest='include',
155 help='A list of paths to include when searching for referenced objects')
not at google - send to devlin 2014/08/25 22:22:47 Comment should also mention that convention of : s
lfg 2014/08/26 16:15:22 Done.
149 156
150 (opts, file_paths) = parser.parse_args() 157 (opts, file_paths) = parser.parse_args()
151 158
152 if not file_paths: 159 if not file_paths:
153 sys.exit(0) # This is OK as a no-op 160 sys.exit(0) # This is OK as a no-op
154 161
155 # Unless in bundle mode, only one file should be specified. 162 # Unless in bundle mode, only one file should be specified.
156 if opts.generator != 'cpp-bundle' and len(file_paths) > 1: 163 if opts.generator != 'cpp-bundle' and len(file_paths) > 1:
157 # TODO(sashab): Could also just use file_paths[0] here and not complain. 164 # TODO(sashab): Could also just use file_paths[0] here and not complain.
158 raise Exception( 165 raise Exception(
159 "Unless in bundle mode, only one file can be specified at a time.") 166 "Unless in bundle mode, only one file can be specified at a time.")
160 167
168 includes = []
169 if opts.include:
170 include_split = shlex.split(opts.include)
171 for i in include_split:
172 path, namespace = i.split(':', 1)
not at google - send to devlin 2014/08/25 22:22:47 This assumes that there is a ':' in the name. Mayb
lfg 2014/08/26 16:15:22 Done.
173 includes.append( { 'path': path, 'namespace': namespace } )
not at google - send to devlin 2014/08/25 22:22:47 How about: def split_path_and_namespace(path_and_
lfg 2014/08/26 16:15:21 Done.
174
161 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir, 175 result = GenerateSchema(opts.generator, file_paths, opts.root, opts.destdir,
162 opts.namespace, opts.dart_overrides_dir, 176 opts.namespace, opts.dart_overrides_dir,
163 opts.impl_dir) 177 opts.impl_dir, includes)
164 if not opts.destdir: 178 if not opts.destdir:
165 print result 179 print result
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698