| OLD | NEW |
| 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 | 22 |
| 23 from cpp_bundle_generator import CppBundleGenerator | 23 from cpp_bundle_generator import CppBundleGenerator |
| 24 from cpp_generator import CppGenerator | 24 from cpp_generator import CppGenerator |
| 25 from cpp_type_generator import CppTypeGenerator | 25 from cpp_type_generator import CppTypeGenerator |
| 26 from dart_generator import DartGenerator | 26 from dart_generator import DartGenerator |
| 27 from js_externs_generator import JsExternsGenerator |
| 27 import json_schema | 28 import json_schema |
| 28 from model import Model, UnixName | 29 from model import Model, UnixName |
| 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', 'externs'] |
| 34 | 35 |
| 35 def GenerateSchema(generator, | 36 def GenerateSchema(generator, |
| 36 filenames, | 37 filenames, |
| 37 root, | 38 root, |
| 38 destdir, | 39 destdir, |
| 39 root_namespace, | 40 root_namespace, |
| 40 dart_overrides_dir): | 41 dart_overrides_dir): |
| 41 schema_loader = SchemaLoader(os.path.dirname(os.path.relpath( | 42 schema_loader = SchemaLoader(os.path.dirname(os.path.relpath( |
| 42 os.path.normpath(filenames[0]), root))) | 43 os.path.normpath(filenames[0]), root))) |
| 43 # Merge the source files into a single list of schemas. | 44 # Merge the source files into a single list of schemas. |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 cpp_generator = CppGenerator(type_generator, root_namespace) | 104 cpp_generator = CppGenerator(type_generator, root_namespace) |
| 104 generators = [ | 105 generators = [ |
| 105 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), | 106 ('%s.h' % namespace.unix_name, cpp_generator.h_generator), |
| 106 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) | 107 ('%s.cc' % namespace.unix_name, cpp_generator.cc_generator) |
| 107 ] | 108 ] |
| 108 elif generator == 'dart': | 109 elif generator == 'dart': |
| 109 generators = [ | 110 generators = [ |
| 110 ('%s.dart' % namespace.unix_name, DartGenerator( | 111 ('%s.dart' % namespace.unix_name, DartGenerator( |
| 111 dart_overrides_dir)) | 112 dart_overrides_dir)) |
| 112 ] | 113 ] |
| 114 elif generator == 'externs': |
| 115 generators = [ |
| 116 ('%s_externs.js' % namespace.unix_name, JsExternsGenerator()) |
| 117 ] |
| 113 else: | 118 else: |
| 114 raise Exception('Unrecognised generator %s' % generator) | 119 raise Exception('Unrecognised generator %s' % generator) |
| 115 | 120 |
| 116 output_code = [] | 121 output_code = [] |
| 117 for filename, generator in generators: | 122 for filename, generator in generators: |
| 118 code = generator.Generate(namespace).Render() | 123 code = generator.Generate(namespace).Render() |
| 119 if destdir: | 124 if destdir: |
| 120 with open(os.path.join(destdir, namespace.source_file_dir, | 125 with open(os.path.join(destdir, namespace.source_file_dir, |
| 121 filename), 'w') as f: | 126 filename), 'w') as f: |
| 122 f.write(code) | 127 f.write(code) |
| (...skipping 27 matching lines...) Expand all Loading... |
| 150 # Unless in bundle mode, only one file should be specified. | 155 # Unless in bundle mode, only one file should be specified. |
| 151 if opts.generator != 'cpp-bundle' and len(filenames) > 1: | 156 if opts.generator != 'cpp-bundle' and len(filenames) > 1: |
| 152 # TODO(sashab): Could also just use filenames[0] here and not complain. | 157 # TODO(sashab): Could also just use filenames[0] here and not complain. |
| 153 raise Exception( | 158 raise Exception( |
| 154 "Unless in bundle mode, only one file can be specified at a time.") | 159 "Unless in bundle mode, only one file can be specified at a time.") |
| 155 | 160 |
| 156 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, | 161 result = GenerateSchema(opts.generator, filenames, opts.root, opts.destdir, |
| 157 opts.namespace, opts.dart_overrides_dir) | 162 opts.namespace, opts.dart_overrides_dir) |
| 158 if not opts.destdir: | 163 if not opts.destdir: |
| 159 print result | 164 print result |
| OLD | NEW |