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 implements ProtobufContainer { | 7 class FileGenerator implements ProtobufContainer { |
8 final FileDescriptorProto _fileDescriptor; | 8 final FileDescriptorProto _fileDescriptor; |
9 final ProtobufContainer _parent; | 9 final ProtobufContainer _parent; |
10 final GenerationContext _context; | 10 final GenerationContext _context; |
(...skipping 10 matching lines...) Expand all Loading... |
21 } | 21 } |
22 for (DescriptorProto messageType in _fileDescriptor.messageType) { | 22 for (DescriptorProto messageType in _fileDescriptor.messageType) { |
23 messageGenerators.add(new MessageGenerator(messageType, this, _context)); | 23 messageGenerators.add(new MessageGenerator(messageType, this, _context)); |
24 } | 24 } |
25 for (FieldDescriptorProto extension in _fileDescriptor.extension) { | 25 for (FieldDescriptorProto extension in _fileDescriptor.extension) { |
26 extensionGenerators.add( | 26 extensionGenerators.add( |
27 new ExtensionGenerator(extension, this, _context)); | 27 new ExtensionGenerator(extension, this, _context)); |
28 } | 28 } |
29 } | 29 } |
30 | 30 |
| 31 String get package => _fileDescriptor.package; |
31 String get classname => ''; | 32 String get classname => ''; |
32 String get fqname => '.${_fileDescriptor.package}'; | 33 String get fqname => '.${_fileDescriptor.package}'; |
33 | 34 |
34 // Extract the filename from a URI and remove the extension. | 35 // Extract the filename from a URI and remove the extension. |
35 String _fileNameWithoutExtension(Uri filePath) { | 36 String _fileNameWithoutExtension(Uri filePath) { |
36 String fileName = filePath.pathSegments.last; | 37 String fileName = filePath.pathSegments.last; |
37 int index = fileName.lastIndexOf("."); | 38 int index = fileName.lastIndexOf("."); |
38 return index == -1 ? fileName : fileName.substring(0, index); | 39 return index == -1 ? fileName : fileName.substring(0, index); |
39 } | 40 } |
40 | 41 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 ); | 122 ); |
122 | 123 |
123 for (String import in _fileDescriptor.dependency) { | 124 for (String import in _fileDescriptor.dependency) { |
124 Uri importPath = new Uri.file(import); | 125 Uri importPath = new Uri.file(import); |
125 if (importPath.isAbsolute) { | 126 if (importPath.isAbsolute) { |
126 // protoc should never generate an import with an absolute path. | 127 // protoc should never generate an import with an absolute path. |
127 throw("FAILURE: Import with absolute path is not supported"); | 128 throw("FAILURE: Import with absolute path is not supported"); |
128 } | 129 } |
129 // Create a relative path from the current file to the import. | 130 // Create a relative path from the current file to the import. |
130 Uri relativeProtoPath = _relative(importPath, filePath); | 131 Uri relativeProtoPath = _relative(importPath, filePath); |
131 out.println("import '${_generatedFilePath(relativeProtoPath)}';"); | 132 // Find the file generator for this import as it contains the |
| 133 // package name. |
| 134 FileGenerator fileGenerator = _context.lookupFile(import); |
| 135 out.print("import '${_generatedFilePath(relativeProtoPath)}'"); |
| 136 if (package != fileGenerator.package) { |
| 137 out.print(' as ${fileGenerator.package}'); |
| 138 } |
| 139 out.println(';'); |
132 } | 140 } |
133 out.println(''); | 141 out.println(''); |
134 | 142 |
135 // Initialize Field. | 143 // Initialize Field. |
136 for (MessageGenerator m in messageGenerators) { | 144 for (MessageGenerator m in messageGenerators) { |
137 m.initializeFields(); | 145 m.initializeFields(); |
138 } | 146 } |
139 | 147 |
140 // Generate code. | 148 // Generate code. |
141 for (EnumGenerator e in enumGenerators) { | 149 for (EnumGenerator e in enumGenerators) { |
(...skipping 20 matching lines...) Expand all Loading... |
162 out.println('}'); | 170 out.println('}'); |
163 }); | 171 }); |
164 } | 172 } |
165 } | 173 } |
166 } | 174 } |
167 | 175 |
168 class GenerationContext { | 176 class GenerationContext { |
169 final GenerationOptions options; | 177 final GenerationOptions options; |
170 final Map<String, ProtobufContainer> _registry = | 178 final Map<String, ProtobufContainer> _registry = |
171 <String, ProtobufContainer>{}; | 179 <String, ProtobufContainer>{}; |
| 180 final Map<String, FileGenerator> _files = |
| 181 <String, FileGenerator>{}; |
172 | 182 |
173 GenerationContext(this.options); | 183 GenerationContext(this.options); |
174 | 184 |
175 void register(ProtobufContainer container) { | 185 void register(ProtobufContainer container) { |
176 _registry[container.fqname] = container; | 186 _registry[container.fqname] = container; |
| 187 if (container is FileGenerator) { |
| 188 _files[container._fileDescriptor.name] = container; |
| 189 } |
177 } | 190 } |
178 | 191 |
179 ProtobufContainer operator [](String fqname) => _registry[fqname]; | 192 ProtobufContainer operator [](String fqname) => _registry[fqname]; |
| 193 |
| 194 FileGenerator lookupFile(String name) => _files[name]; |
180 } | 195 } |
OLD | NEW |