From e5206aace91f9d833cd3a5cc0ef38accc2d78425 Mon Sep 17 00:00:00 2001 From: temiola Date: Wed, 7 Jan 2015 11:43:05 -0800 Subject: [PATCH] Updates the ruby generator so that it does not create files for empty protos. Previously, this alway generated a service file with a header. However, this does not work well, it means whenver the ruby service plugin is used, every proto file ends up with a service file. Change on 2015/01/07 by temiola ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=83448951 --- src/compiler/ruby_generator.cc | 11 +++++++---- src/compiler/ruby_plugin.cc | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/compiler/ruby_generator.cc b/src/compiler/ruby_generator.cc index e8d3c25e0b2..c5c58847363 100644 --- a/src/compiler/ruby_generator.cc +++ b/src/compiler/ruby_generator.cc @@ -126,7 +126,13 @@ string GetServices(const FileDescriptor* file) { StringOutputStream output_stream(&output); Printer out(&output_stream, '$'); - // Always write out a file header. + // Don't write out any output if there no services, to avoid empty service + // files being generated for proto files that don't declare any. + if (file->service_count() == 0) { + return output; + } + + // Write out a file header. map header_comment_vars = ListToDict({ "file.name", file->name(), "file.package", file->package(), @@ -134,9 +140,6 @@ string GetServices(const FileDescriptor* file) { out.Print("# Generated by the protocol buffer compiler. DO NOT EDIT!\n"); out.Print(header_comment_vars, "# Source: $file.name$ for package '$file.package$'\n"); - if (file->service_count() == 0) { - return output; - } out.Print("\n"); out.Print("require 'grpc'\n"); diff --git a/src/compiler/ruby_plugin.cc b/src/compiler/ruby_plugin.cc index 62229f92174..c1748cfaeb0 100644 --- a/src/compiler/ruby_plugin.cc +++ b/src/compiler/ruby_plugin.cc @@ -55,16 +55,19 @@ class RubyGrpcGenerator : public google::protobuf::compiler::CodeGenerator { const string& parameter, google::protobuf::compiler::GeneratorContext* context, string* error) const override { + string code = grpc_ruby_generator::GetServices(file); + if (code.size() == 0) { + return true; // don't generate a file if there are no services + } + // Get output file name. string file_name; if (!grpc_ruby_generator::ServicesFilename(file, &file_name)) { return false; } - std::unique_ptr output( context->Open(file_name)); google::protobuf::io::CodedOutputStream coded_out(output.get()); - string code = grpc_ruby_generator::GetServices(file); coded_out.WriteRaw(code.data(), code.size()); return true; }