THRIFT-1204: Ruby autogenerated files should require 'thrift' gem
Library: Ruby
Patch: Ilya Maykov
This patch does 2 things:
1) Adds a "require 'thrift'" line to the *_types.rb and *_constants.rb files,
to be consistent with *_service.rb which was generating the thrift require
already.
2) Adds a "rubygems" option to the --gen rb compiler target. When this option
is specified, the compiler will add a "require 'rubygems'" line to the top
of each gen-rb file. Defaults to false.
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1294949 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc
index 95404a0..a46dfe3 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -51,9 +51,10 @@
const std::string& option_string)
: t_oop_generator(program)
{
- (void) parsed_options;
(void) option_string;
out_dir_base_ = "gen-rb";
+
+ require_rubygems_ = (parsed_options.find("rubygems") != parsed_options.end());
}
/**
@@ -165,6 +166,7 @@
*/
std::string rb_autogen_comment();
+ std::string render_require_thrift();
std::string render_includes();
std::string declare_field(t_field* tfield);
std::string type_name(t_type* ttype);
@@ -209,6 +211,8 @@
std::ofstream f_consts_;
std::ofstream f_service_;
+ /** If true, add a "require 'rubygems'" line to the top of each gen-rb file. */
+ bool require_rubygems_;
};
@@ -231,12 +235,12 @@
// Print header
f_types_ <<
- rb_autogen_comment() << endl <<
+ rb_autogen_comment() << endl << render_require_thrift() <<
render_includes() << endl;
begin_namespace(f_types_, ruby_modules(program_));
f_consts_ <<
- rb_autogen_comment() << endl <<
+ rb_autogen_comment() << endl << render_require_thrift() <<
"require '" << underscore(program_name_) << "_types'" << endl <<
endl;
begin_namespace(f_consts_, ruby_modules(program_));
@@ -244,6 +248,17 @@
}
/**
+ * Renders the require of thrift itself, and possibly of the rubygems dependency.
+ */
+string t_rb_generator::render_require_thrift() {
+ if (require_rubygems_) {
+ return "require 'rubygems'\nrequire 'thrift'\n";
+ } else {
+ return "require 'thrift'\n";
+ }
+}
+
+/**
* Renders all the imports necessary for including another Thrift program
*/
string t_rb_generator::render_includes() {
@@ -698,8 +713,7 @@
f_service_.open(f_service_name.c_str());
f_service_ <<
- rb_autogen_comment() << endl <<
- "require 'thrift'" << endl;
+ rb_autogen_comment() << endl << render_require_thrift();
if (tservice->get_extends() != NULL) {
f_service_ <<
@@ -1191,5 +1205,5 @@
indent(out) << "end" << endl << endl;
}
-THRIFT_REGISTER_GENERATOR(rb, "Ruby", "")
-
+THRIFT_REGISTER_GENERATOR(rb, "Ruby",
+" rubygems: Add a \"require 'rubygems'\" line to the top of each generated file.\n")