|
5 | 5 | part of protoc; |
6 | 6 |
|
7 | 7 | final _dartIdentifier = new RegExp(r'^\w+$'); |
| 8 | +final _formatter = new DartFormatter(); |
8 | 9 |
|
9 | 10 | /// Generates the Dart output files for one .proto input file. |
10 | 11 | /// |
@@ -90,22 +91,23 @@ class FileGenerator extends ProtobufContainer { |
90 | 91 | } |
91 | 92 |
|
92 | 93 | final FileDescriptorProto descriptor; |
| 94 | + final GenerationOptions options; |
93 | 95 |
|
94 | 96 | // The relative path used to import the .proto file, as a URI. |
95 | 97 | final Uri protoFileUri; |
96 | 98 |
|
97 | | - final List<EnumGenerator> enumGenerators = <EnumGenerator>[]; |
98 | | - final List<MessageGenerator> messageGenerators = <MessageGenerator>[]; |
99 | | - final List<ExtensionGenerator> extensionGenerators = <ExtensionGenerator>[]; |
100 | | - final List<ClientApiGenerator> clientApiGenerators = <ClientApiGenerator>[]; |
101 | | - final List<ServiceGenerator> serviceGenerators = <ServiceGenerator>[]; |
| 99 | + final enumGenerators = <EnumGenerator>[]; |
| 100 | + final messageGenerators = <MessageGenerator>[]; |
| 101 | + final extensionGenerators = <ExtensionGenerator>[]; |
| 102 | + final clientApiGenerators = <ClientApiGenerator>[]; |
| 103 | + final serviceGenerators = <ServiceGenerator>[]; |
| 104 | + final grpcGenerators = <GrpcServiceGenerator>[]; |
102 | 105 |
|
103 | 106 | /// True if cross-references have been resolved. |
104 | 107 | bool _linked = false; |
105 | 108 |
|
106 | | - FileGenerator(FileDescriptorProto descriptor) |
107 | | - : descriptor = descriptor, |
108 | | - protoFileUri = new Uri.file(descriptor.name) { |
| 109 | + FileGenerator(this.descriptor, this.options) |
| 110 | + : protoFileUri = new Uri.file(descriptor.name) { |
109 | 111 | if (protoFileUri.isAbsolute) { |
110 | 112 | // protoc should never generate an import with an absolute path. |
111 | 113 | throw "FAILURE: Import with absolute path is not supported"; |
@@ -133,9 +135,13 @@ class FileGenerator extends ProtobufContainer { |
133 | 135 | extensionGenerators.add(new ExtensionGenerator(extension, this)); |
134 | 136 | } |
135 | 137 | for (ServiceDescriptorProto service in descriptor.service) { |
136 | | - var serviceGen = new ServiceGenerator(service, this); |
137 | | - serviceGenerators.add(serviceGen); |
138 | | - clientApiGenerators.add(new ClientApiGenerator(serviceGen)); |
| 138 | + if (options.useGrpc) { |
| 139 | + grpcGenerators.add(new GrpcServiceGenerator(service, this)); |
| 140 | + } else { |
| 141 | + var serviceGen = new ServiceGenerator(service, this); |
| 142 | + serviceGenerators.add(serviceGen); |
| 143 | + clientApiGenerators.add(new ClientApiGenerator(serviceGen)); |
| 144 | + } |
139 | 145 | } |
140 | 146 | } |
141 | 147 |
|
@@ -178,12 +184,19 @@ class FileGenerator extends ProtobufContainer { |
178 | 184 | ..content = content; |
179 | 185 | } |
180 | 186 |
|
181 | | - return [ |
| 187 | + final files = [ |
182 | 188 | makeFile(".pb.dart", generateMainFile(config)), |
183 | 189 | makeFile(".pbenum.dart", generateEnumFile(config)), |
184 | | - makeFile(".pbserver.dart", generateServerFile(config)), |
185 | 190 | makeFile(".pbjson.dart", generateJsonFile(config)), |
186 | 191 | ]; |
| 192 | + if (options.useGrpc) { |
| 193 | + if (grpcGenerators.isNotEmpty) { |
| 194 | + files.add(makeFile(".pbgrpc.dart", generateGrpcFile(config))); |
| 195 | + } |
| 196 | + } else { |
| 197 | + files.add(makeFile(".pbserver.dart", generateServerFile(config))); |
| 198 | + } |
| 199 | + return files; |
187 | 200 | } |
188 | 201 |
|
189 | 202 | /// Returns the contents of the .pb.dart file for this .proto file. |
@@ -417,6 +430,40 @@ import 'package:protobuf/protobuf.dart'; |
417 | 430 | return out.toString(); |
418 | 431 | } |
419 | 432 |
|
| 433 | + /// Returns the contents of the .pbgrpc.dart file for this .proto file. |
| 434 | + String generateGrpcFile( |
| 435 | + [OutputConfiguration config = const DefaultOutputConfiguration()]) { |
| 436 | + if (!_linked) throw new StateError("not linked"); |
| 437 | + var out = new IndentingWriter(); |
| 438 | + _writeLibraryHeading(out, "pbgrpc"); |
| 439 | + |
| 440 | + out.println(''' |
| 441 | +import 'dart:async'; |
| 442 | +
|
| 443 | +import 'package:grpc/grpc.dart'; |
| 444 | +'''); |
| 445 | + |
| 446 | + // Import .pb.dart files needed for requests and responses. |
| 447 | + var imports = new Set<FileGenerator>(); |
| 448 | + for (var generator in grpcGenerators) { |
| 449 | + generator.addImportsTo(imports); |
| 450 | + } |
| 451 | + for (var target in imports) { |
| 452 | + _writeImport(out, config, target, ".pb.dart"); |
| 453 | + } |
| 454 | + |
| 455 | + var resolvedImport = |
| 456 | + config.resolveImport(protoFileUri, protoFileUri, ".pb.dart"); |
| 457 | + out.println("export '$resolvedImport';"); |
| 458 | + out.println(); |
| 459 | + |
| 460 | + for (var generator in grpcGenerators) { |
| 461 | + generator.generate(out); |
| 462 | + } |
| 463 | + |
| 464 | + return _formatter.format(out.toString()); |
| 465 | + } |
| 466 | + |
420 | 467 | /// Returns the contents of the .pbjson.dart file for this .proto file. |
421 | 468 | String generateJsonFile( |
422 | 469 | [OutputConfiguration config = const DefaultOutputConfiguration()]) { |
|
0 commit comments