Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(464)

Unified Diff: lib/file_generator.dart

Issue 2020483002: clean up API for file_generator.dart (Closed) Base URL: git@github.com:dart-lang/dart-protoc-plugin.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/code_generator.dart ('k') | test/file_generator_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/file_generator.dart
diff --git a/lib/file_generator.dart b/lib/file_generator.dart
index f1f78f2bd4cb24a8d44fad5d502d625a213849d2..148e2a6ebadfb5fd6e648d99937feb60a22e60cb 100644
--- a/lib/file_generator.dart
+++ b/lib/file_generator.dart
@@ -4,6 +4,9 @@
part of protoc;
+/// Generates the Dart output files for one .proto input file.
+///
+/// Outputs include .pb.dart, pbenum.dart, and .pbjson.dart.
class FileGenerator extends ProtobufContainer {
/// Returns the the mixin to use by default in this file,
/// or null for no mixin by default.
@@ -111,23 +114,32 @@ class FileGenerator extends ProtobufContainer {
return libraryName;
}
- CodeGeneratorResponse_File generateResponse(OutputConfiguration config) {
- IndentingWriter out = new IndentingWriter();
+ /// Generates all the Dart files for this .proto file.
+ List<CodeGeneratorResponse_File> generateFiles(OutputConfiguration config) {
+ if (!_linked) throw new StateError("not linked");
- generate(out, config);
+ makeFile(String extension, String content) {
+ Uri protoUrl = new Uri.file(_fileDescriptor.name);
+ Uri dartUrl = config.outputPathFor(protoUrl, extension);
+ return new CodeGeneratorResponse_File()
+ ..name = dartUrl.path
+ ..content = content;
+ }
- Uri filePath = new Uri.file(_fileDescriptor.name);
- return new CodeGeneratorResponse_File()
- ..name = config.outputPathFor(filePath, ".pb.dart").path
- ..content = out.toString();
+ return [
+ makeFile(".pb.dart", generateMainFile(config)),
+ makeFile(".pbenum.dart", generateEnumFile(config)),
+ makeFile(".pbjson.dart", generateJsonFile(config)),
+ ];
}
- /// Generates the Dart code for this .proto file.
- void generate(IndentingWriter out,
+ /// Returns the contents of the .pb.dart file for this .proto file.
+ String generateMainFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
if (!_linked) throw new StateError("not linked");
+ IndentingWriter out = new IndentingWriter();
- generateHeader(out, config);
+ writeMainHeader(out, config);
// Generate code.
for (MessageGenerator m in messageGenerators) {
@@ -158,10 +170,12 @@ class FileGenerator extends ProtobufContainer {
for (ServiceGenerator s in serviceGenerators) {
s.generate(out);
}
+
+ return out.toString();
}
- /// Prints header and imports.
- void generateHeader(IndentingWriter out,
+ /// Writes the header and imports for the .pb.dart file.
+ void writeMainHeader(IndentingWriter out,
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
String libraryName = _generateLibraryName(protoFileUri);
out.println('///\n'
@@ -180,7 +194,7 @@ class FileGenerator extends ProtobufContainer {
}
if (_needsProtobufImport) {
- out.println("import 'package:protobuf/protobuf.dart';");
+ out.println("import 'package:protobuf/protobuf.dart';");
out.println();
}
@@ -292,21 +306,10 @@ class FileGenerator extends ProtobufContainer {
return imports;
}
- CodeGeneratorResponse_File generateEnumResponse(OutputConfiguration config) {
- if (!_linked) throw new StateError("not linked");
-
- IndentingWriter out = new IndentingWriter();
-
- generateEnumFile(out, config);
-
- Uri filePath = new Uri.file(_fileDescriptor.name);
- return new CodeGeneratorResponse_File()
- ..name = config.outputPathFor(filePath, ".pbenum.dart").path
- ..content = out.toString();
- }
-
- void generateEnumFile(IndentingWriter out,
+ /// Returns the contents of the .pbenum.dart file for this .proto file.
+ String generateEnumFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
+ if (!_linked) throw new StateError("not linked");
Uri filePath = new Uri.file(_fileDescriptor.name);
if (filePath.isAbsolute) {
// protoc should never generate a file descriptor with an absolute path.
@@ -315,6 +318,7 @@ class FileGenerator extends ProtobufContainer {
var baseLibraryName = _generateLibraryName(filePath);
var libraryName = baseLibraryName + "_pbenum";
+ var out = new IndentingWriter();
out.print('''
///
// Generated code. Do not modify.
@@ -335,6 +339,8 @@ library $libraryName;
for (MessageGenerator m in messageGenerators) {
m.generateEnums(out);
}
+
+ return out.toString();
}
/// Returns the number of enum types generated in the .pbenum.dart file.
@@ -346,22 +352,10 @@ library $libraryName;
return count;
}
- CodeGeneratorResponse_File generateJsonDartResponse(
- OutputConfiguration config) {
- if (!_linked) throw new StateError("not linked");
-
- IndentingWriter out = new IndentingWriter();
-
- generateJsonDart(out, config);
-
- Uri filePath = new Uri.file(_fileDescriptor.name);
- return new CodeGeneratorResponse_File()
- ..name = config.outputPathFor(filePath, ".pbjson.dart").path
- ..content = out.toString();
- }
-
- void generateJsonDart(IndentingWriter out,
+ /// Returns the contents of the .pbjson.dart file for this .proto file.
+ String generateJsonFile(
[OutputConfiguration config = const DefaultOutputConfiguration()]) {
+ if (!_linked) throw new StateError("not linked");
Uri filePath = new Uri.file(_fileDescriptor.name);
if (filePath.isAbsolute) {
// protoc should never generate a file descriptor with an absolute path.
@@ -370,6 +364,7 @@ library $libraryName;
var baseLibraryName = _generateLibraryName(filePath);
var libraryName = baseLibraryName + "_pbjson";
+ var out = new IndentingWriter();
out.print('''
///
// Generated code. Do not modify.
@@ -400,6 +395,8 @@ library $libraryName;
for (ServiceGenerator s in serviceGenerators) {
s.generateConstants(out);
}
+
+ return out.toString();
}
/// Returns the generator for each .pbjson.dart file the generated
« no previous file with comments | « lib/code_generator.dart ('k') | test/file_generator_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698