nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 3 | * Copyright 2015 gRPC authors. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame^] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | // Generates Ruby gRPC service interface out of Protobuf IDL. |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 20 | |
| 21 | #include <memory> |
| 22 | |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 23 | #include "src/compiler/config.h" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 24 | #include "src/compiler/ruby_generator.h" |
| 25 | #include "src/compiler/ruby_generator_helpers-inl.h" |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 26 | |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 27 | class RubyGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 28 | public: |
| 29 | RubyGrpcGenerator() {} |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 30 | ~RubyGrpcGenerator() {} |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 31 | |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 32 | bool Generate(const grpc::protobuf::FileDescriptor *file, |
| 33 | const grpc::string ¶meter, |
| 34 | grpc::protobuf::compiler::GeneratorContext *context, |
| 35 | grpc::string *error) const { |
| 36 | grpc::string code = grpc_ruby_generator::GetServices(file); |
temiola | e5206aa | 2015-01-07 11:43:05 -0800 | [diff] [blame] | 37 | if (code.size() == 0) { |
| 38 | return true; // don't generate a file if there are no services |
| 39 | } |
| 40 | |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 41 | // Get output file name. |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 42 | grpc::string file_name; |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 43 | if (!grpc_ruby_generator::ServicesFilename(file, &file_name)) { |
| 44 | return false; |
| 45 | } |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 46 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
temiola | 5453555 | 2015-01-06 17:50:59 -0800 | [diff] [blame] | 47 | context->Open(file_name)); |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 48 | grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 49 | coded_out.WriteRaw(code.data(), code.size()); |
| 50 | return true; |
| 51 | } |
| 52 | }; |
| 53 | |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 54 | int main(int argc, char *argv[]) { |
nnoble | ebebb7e | 2014-12-10 16:31:01 -0800 | [diff] [blame] | 55 | RubyGrpcGenerator generator; |
Yang Gao | 478568e | 2015-03-23 22:09:22 -0700 | [diff] [blame] | 56 | return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 57 | } |