OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of protoc; | 5 part of protoc; |
6 | 6 |
7 class FileGenerator extends ProtobufContainer { | 7 class FileGenerator extends ProtobufContainer { |
8 | 8 |
9 /// Returns the the mixin to use by default in this file, | 9 /// Returns the the mixin to use by default in this file, |
10 /// or null for no mixin by default. | 10 /// or null for no mixin by default. |
(...skipping 10 matching lines...) Expand all Loading... |
21 return mixin; | 21 return mixin; |
22 } | 22 } |
23 | 23 |
24 final FileDescriptorProto _fileDescriptor; | 24 final FileDescriptorProto _fileDescriptor; |
25 final ProtobufContainer _parent; | 25 final ProtobufContainer _parent; |
26 final GenerationContext _context; | 26 final GenerationContext _context; |
27 | 27 |
28 final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; | 28 final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; |
29 final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; | 29 final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; |
30 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; | 30 final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; |
| 31 final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[]; |
| 32 final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[]; |
31 | 33 |
32 FileGenerator(this._fileDescriptor, this._parent, this._context) { | 34 FileGenerator(this._fileDescriptor, this._parent, this._context) { |
33 _context.register(this); | 35 _context.register(this); |
34 | 36 |
35 var defaultMixin = _getDefaultMixin(_fileDescriptor); | 37 var defaultMixin = _getDefaultMixin(_fileDescriptor); |
36 | 38 |
37 // Load and register all enum and message types. | 39 // Load and register all enum and message types. |
38 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) { | 40 for (EnumDescriptorProto enumType in _fileDescriptor.enumType) { |
39 enumGenerators.add(new EnumGenerator(enumType, this, _context)); | 41 enumGenerators.add(new EnumGenerator(enumType, this, _context)); |
40 } | 42 } |
41 for (DescriptorProto messageType in _fileDescriptor.messageType) { | 43 for (DescriptorProto messageType in _fileDescriptor.messageType) { |
42 messageGenerators.add( | 44 messageGenerators.add( |
43 new MessageGenerator(messageType, this, _context, defaultMixin)); | 45 new MessageGenerator(messageType, this, _context, defaultMixin)); |
44 } | 46 } |
45 for (FieldDescriptorProto extension in _fileDescriptor.extension) { | 47 for (FieldDescriptorProto extension in _fileDescriptor.extension) { |
46 extensionGenerators.add( | 48 extensionGenerators.add( |
47 new ExtensionGenerator(extension, this, _context)); | 49 new ExtensionGenerator(extension, this, _context)); |
48 } | 50 } |
| 51 for (ServiceDescriptorProto service in _fileDescriptor.service) { |
| 52 serviceGenerators.add(new ServiceGenerator(service, this, _context)); |
| 53 clientApiGenerators.add(new ClientApiGenerator(service, this, _context)); |
| 54 } |
49 } | 55 } |
50 | 56 |
51 String get package => _fileDescriptor.package; | 57 String get package => _fileDescriptor.package; |
52 String get classname => ''; | 58 String get classname => ''; |
53 String get fqname => '.${_fileDescriptor.package}'; | 59 String get fqname => '.${_fileDescriptor.package}'; |
54 | 60 |
55 // Extract the filename from a URI and remove the extension. | 61 // Extract the filename from a URI and remove the extension. |
56 String _fileNameWithoutExtension(Uri filePath) { | 62 String _fileNameWithoutExtension(Uri filePath) { |
57 String fileName = filePath.pathSegments.last; | 63 String fileName = filePath.pathSegments.last; |
58 int index = fileName.lastIndexOf("."); | 64 int index = fileName.lastIndexOf("."); |
(...skipping 24 matching lines...) Expand all Loading... |
83 | 89 |
84 void generate(IndentingWriter out) { | 90 void generate(IndentingWriter out) { |
85 Uri filePath = new Uri.file(_fileDescriptor.name); | 91 Uri filePath = new Uri.file(_fileDescriptor.name); |
86 if (filePath.isAbsolute) { | 92 if (filePath.isAbsolute) { |
87 // protoc should never generate a file descriptor with an absolute path. | 93 // protoc should never generate a file descriptor with an absolute path. |
88 throw("FAILURE: File with an absolute path is not supported"); | 94 throw("FAILURE: File with an absolute path is not supported"); |
89 } | 95 } |
90 | 96 |
91 String libraryName = _generateLibraryName(filePath); | 97 String libraryName = _generateLibraryName(filePath); |
92 | 98 |
| 99 // Print header and imports. We only add the dart:async import if there |
| 100 // are services in the FileDescriptorProto. |
93 out.println( | 101 out.println( |
94 '///\n' | 102 '///\n' |
95 '// Generated code. Do not modify.\n' | 103 '// Generated code. Do not modify.\n' |
96 '///\n' | 104 '///\n' |
97 'library $libraryName;\n' | 105 'library $libraryName;\n'); |
98 '\n' | 106 if (_fileDescriptor.service.isNotEmpty) { |
| 107 out.println("import 'dart:async';\n"); |
| 108 } |
| 109 out.println( |
99 "import 'package:fixnum/fixnum.dart';\n" | 110 "import 'package:fixnum/fixnum.dart';\n" |
100 "import 'package:protobuf/protobuf.dart';" | 111 "import 'package:protobuf/protobuf.dart';" |
101 ); | 112 ); |
102 | 113 |
103 var mixinImports = findMixinsToImport(); | 114 var mixinImports = findMixinsToImport(); |
104 var importNames = mixinImports.keys.toList(); | 115 var importNames = mixinImports.keys.toList(); |
105 importNames.sort(); | 116 importNames.sort(); |
106 for (var imp in importNames) { | 117 for (var imp in importNames) { |
107 var symbols = mixinImports[imp]; | 118 var symbols = mixinImports[imp]; |
108 out.println("import '${imp}' show ${symbols.join(', ')};"); | 119 out.println("import '${imp}' show ${symbols.join(', ')};"); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 x.generate(out); | 162 x.generate(out); |
152 } | 163 } |
153 out.println('static void registerAllExtensions(ExtensionRegistry ' | 164 out.println('static void registerAllExtensions(ExtensionRegistry ' |
154 'registry) {'); | 165 'registry) {'); |
155 for (ExtensionGenerator x in extensionGenerators) { | 166 for (ExtensionGenerator x in extensionGenerators) { |
156 out.println(' registry.add(${x.name});'); | 167 out.println(' registry.add(${x.name});'); |
157 } | 168 } |
158 out.println('}'); | 169 out.println('}'); |
159 }); | 170 }); |
160 } | 171 } |
| 172 |
| 173 for (ClientApiGenerator c in clientApiGenerators) { |
| 174 c.generate(out); |
| 175 } |
| 176 for (ServiceGenerator s in serviceGenerators) { |
| 177 s.generate(out); |
| 178 } |
161 } | 179 } |
162 | 180 |
163 /// Returns a map from import names to the Dart symbols to be imported. | 181 /// Returns a map from import names to the Dart symbols to be imported. |
164 Map<String, List<String>> findMixinsToImport() { | 182 Map<String, List<String>> findMixinsToImport() { |
165 var mixins = new Set<PbMixin>(); | 183 var mixins = new Set<PbMixin>(); |
166 for (MessageGenerator m in messageGenerators) { | 184 for (MessageGenerator m in messageGenerators) { |
167 m.addMixinsTo(mixins); | 185 m.addMixinsTo(mixins); |
168 } | 186 } |
169 | 187 |
170 var imports = {}; | 188 var imports = {}; |
(...skipping 29 matching lines...) Expand all Loading... |
200 _registry[container.fqname] = container; | 218 _registry[container.fqname] = container; |
201 if (container is FileGenerator) { | 219 if (container is FileGenerator) { |
202 _files[container._fileDescriptor.name] = container; | 220 _files[container._fileDescriptor.name] = container; |
203 } | 221 } |
204 } | 222 } |
205 | 223 |
206 ProtobufContainer operator [](String fqname) => _registry[fqname]; | 224 ProtobufContainer operator [](String fqname) => _registry[fqname]; |
207 | 225 |
208 FileGenerator lookupFile(String name) => _files[name]; | 226 FileGenerator lookupFile(String name) => _files[name]; |
209 } | 227 } |
OLD | NEW |