blob: 9c234bd7cbc1e9f8d7f9ced8ebf512f95350e496 [file] [log] [blame]
nnobleebebb7e2014-12-10 16:31:01 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
nnobleebebb7e2014-12-10 16:31:01 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
nnobleebebb7e2014-12-10 16:31:01 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
nnobleebebb7e2014-12-10 16:31:01 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
nnobleebebb7e2014-12-10 16:31:01 -080016 *
17 */
18
19// Generates Ruby gRPC service interface out of Protobuf IDL.
nnobleebebb7e2014-12-10 16:31:01 -080020
21#include <memory>
22
Yang Gao478568e2015-03-23 22:09:22 -070023#include "src/compiler/config.h"
nnobleebebb7e2014-12-10 16:31:01 -080024#include "src/compiler/ruby_generator.h"
25#include "src/compiler/ruby_generator_helpers-inl.h"
nnobleebebb7e2014-12-10 16:31:01 -080026
Yang Gao478568e2015-03-23 22:09:22 -070027class RubyGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
nnobleebebb7e2014-12-10 16:31:01 -080028 public:
29 RubyGrpcGenerator() {}
Craig Tillercf133f42015-02-26 14:05:56 -080030 ~RubyGrpcGenerator() {}
nnobleebebb7e2014-12-10 16:31:01 -080031
Craig Tillerbaa14a92017-11-03 09:09:36 -070032 bool Generate(const grpc::protobuf::FileDescriptor* file,
33 const grpc::string& parameter,
34 grpc::protobuf::compiler::GeneratorContext* context,
35 grpc::string* error) const {
Yang Gao478568e2015-03-23 22:09:22 -070036 grpc::string code = grpc_ruby_generator::GetServices(file);
temiolae5206aa2015-01-07 11:43:05 -080037 if (code.size() == 0) {
38 return true; // don't generate a file if there are no services
39 }
40
nnobleebebb7e2014-12-10 16:31:01 -080041 // Get output file name.
Yang Gao478568e2015-03-23 22:09:22 -070042 grpc::string file_name;
nnobleebebb7e2014-12-10 16:31:01 -080043 if (!grpc_ruby_generator::ServicesFilename(file, &file_name)) {
44 return false;
45 }
Yang Gao478568e2015-03-23 22:09:22 -070046 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
temiola54535552015-01-06 17:50:59 -080047 context->Open(file_name));
Yang Gao478568e2015-03-23 22:09:22 -070048 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
nnobleebebb7e2014-12-10 16:31:01 -080049 coded_out.WriteRaw(code.data(), code.size());
50 return true;
51 }
52};
53
Craig Tillerbaa14a92017-11-03 09:09:36 -070054int main(int argc, char* argv[]) {
nnobleebebb7e2014-12-10 16:31:01 -080055 RubyGrpcGenerator generator;
Yang Gao478568e2015-03-23 22:09:22 -070056 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
Craig Tiller190d3602015-02-18 09:23:38 -080057}