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 abstract class ProtobufContainer { | 7 abstract class ProtobufContainer { |
8 String get package; | 8 String get package; |
9 String get classname; | 9 String get classname; |
10 String get fqname; | 10 String get fqname; |
11 String get packageImportPrefix => package.replaceAll('.', r'$'); | 11 String get packageImportPrefix => package.replaceAll('.', r'$'); |
12 } | 12 } |
13 | 13 |
14 class CodeGenerator extends ProtobufContainer { | 14 class CodeGenerator extends ProtobufContainer { |
15 final Stream<List<int>> _streamIn; | 15 final Stream<List<int>> _streamIn; |
16 final IOSink _streamOut; | 16 final IOSink _streamOut; |
17 final IOSink _streamErr; | 17 final IOSink _streamErr; |
18 | 18 |
19 CodeGenerator(this._streamIn, this._streamOut, this._streamErr); | 19 CodeGenerator(this._streamIn, this._streamOut, this._streamErr); |
20 | 20 |
21 /// Runs the code generator. The optional [optionParsers] can be used to | 21 /// Runs the code generator. The optional [optionParsers] can be used to |
22 /// change how command line options are parsed (see [parseGenerationOptions] | 22 /// change how command line options are parsed (see [parseGenerationOptions] |
23 /// for details), and [outputConfiguration] can be used to override where | 23 /// for details), and [outputConfiguration] can be used to override where |
24 /// generated files are created and how imports between generated files are | 24 /// generated files are created and how imports between generated files are |
25 /// constructed (see [OutputConfiguration] for details). | 25 /// constructed (see [OutputConfiguration] for details). |
26 void generate({ | 26 void generate({ |
27 Map<String, SingleOptionParser> optionParsers, | 27 Map<String, SingleOptionParser> optionParsers, |
28 OutputConfiguration outputConfiguration}) { | 28 OutputConfiguration outputConfiguration}) { |
| 29 |
| 30 var extensions = new ExtensionRegistry(); |
| 31 Dart_options.registerAllExtensions(extensions); |
| 32 |
29 _streamIn | 33 _streamIn |
30 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) | 34 .fold(new BytesBuilder(), (builder, data) => builder..add(data)) |
31 .then((builder) => builder.takeBytes()) | 35 .then((builder) => builder.takeBytes()) |
32 .then((List<int> bytes) { | 36 .then((List<int> bytes) { |
33 var request = new CodeGeneratorRequest.fromBuffer(bytes); | 37 var request = |
| 38 new CodeGeneratorRequest.fromBuffer(bytes, extensions); |
34 var response = new CodeGeneratorResponse(); | 39 var response = new CodeGeneratorResponse(); |
35 | 40 |
36 // Parse the options in the request. Return the errors is any. | 41 // Parse the options in the request. Return the errors is any. |
37 var options = parseGenerationOptions( | 42 var options = parseGenerationOptions( |
38 request, response, optionParsers); | 43 request, response, optionParsers); |
39 if (options == null) { | 44 if (options == null) { |
40 _streamOut.add(response.writeToBuffer()); | 45 _streamOut.add(response.writeToBuffer()); |
41 return; | 46 return; |
42 } | 47 } |
43 | 48 |
44 var ctx = new GenerationContext(options, outputConfiguration == null | 49 var ctx = new GenerationContext(options, |
| 50 outputConfiguration == null |
45 ? new DefaultOutputConfiguration() : outputConfiguration); | 51 ? new DefaultOutputConfiguration() : outputConfiguration); |
46 List<FileGenerator> generators = <FileGenerator>[]; | 52 List<FileGenerator> generators = <FileGenerator>[]; |
47 for (FileDescriptorProto file in request.protoFile) { | 53 for (FileDescriptorProto file in request.protoFile) { |
48 var generator = new FileGenerator(file, this, ctx); | 54 var generator = new FileGenerator(file, this, ctx); |
49 if (request.fileToGenerate.contains(file.name)) { | 55 if (request.fileToGenerate.contains(file.name)) { |
50 generators.add(generator); | 56 generators.add(generator); |
51 } | 57 } |
52 } | 58 } |
53 | 59 |
54 response.file.addAll( | 60 response.file.addAll( |
55 generators.map((filegen) => filegen.generateResponse())); | 61 generators.map((filegen) => filegen.generateResponse())); |
56 _streamOut.add(response.writeToBuffer()); | 62 _streamOut.add(response.writeToBuffer()); |
57 }); | 63 }); |
58 } | 64 } |
59 | 65 |
60 String get package => ''; | 66 String get package => ''; |
61 String get classname => null; | 67 String get classname => null; |
62 String get fqname => ''; | 68 String get fqname => ''; |
63 } | 69 } |
OLD | NEW |