murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | // Generates Objective C gRPC service interface out of Protobuf IDL. |
| 35 | |
| 36 | #include <memory> |
| 37 | |
| 38 | #include "src/compiler/config.h" |
| 39 | #include "src/compiler/objective_c_generator.h" |
| 40 | #include "src/compiler/objective_c_generator_helpers.h" |
| 41 | |
| 42 | class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { |
| 43 | public: |
| 44 | ObjectiveCGrpcGenerator() {} |
| 45 | virtual ~ObjectiveCGrpcGenerator() {} |
| 46 | |
| 47 | virtual bool Generate(const grpc::protobuf::FileDescriptor *file, |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 48 | const ::grpc::string ¶meter, |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 49 | grpc::protobuf::compiler::GeneratorContext *context, |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 50 | ::grpc::string *error) const { |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 51 | |
| 52 | if (file->service_count() == 0) { |
| 53 | // No services. Do nothing. |
| 54 | return true; |
| 55 | } |
| 56 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 57 | ::grpc::string file_name = grpc_generator::FileNameInUpperCamel(file); |
| 58 | ::grpc::string prefix = file->options().objc_class_prefix(); |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 59 | |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 60 | { |
| 61 | // Generate .pbrpc.h |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 62 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 63 | ::grpc::string imports = ::grpc::string("#import \"") + file_name + |
| 64 | ".pbobjc.h\"\n\n" |
Jorge Canizales | 9fce455 | 2015-07-18 00:50:07 -0700 | [diff] [blame] | 65 | "#import <ProtoRPC/ProtoService.h>\n" |
| 66 | "#import <RxLibrary/GRXWriteable.h>\n" |
| 67 | "#import <RxLibrary/GRXWriter.h>\n"; |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 68 | |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 69 | // TODO(jcanizales): Instead forward-declare the input and output types |
| 70 | // and import the files in the .pbrpc.m |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 71 | ::grpc::string proto_imports; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 72 | for (int i = 0; i < file->dependency_count(); i++) { |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 73 | ::grpc::string header = grpc_objective_c_generator::MessageHeaderName( |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 74 | file->dependency(i)); |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 75 | proto_imports += ::grpc::string("#import \"") + header + "\"\n"; |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 76 | } |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 77 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 78 | ::grpc::string declarations; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 79 | for (int i = 0; i < file->service_count(); i++) { |
| 80 | const grpc::protobuf::ServiceDescriptor *service = file->service(i); |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 81 | declarations += grpc_objective_c_generator::GetHeader(service); |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 82 | } |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 83 | |
| 84 | Write(context, file_name + ".pbrpc.h", |
| 85 | imports + '\n' + proto_imports + '\n' + declarations); |
| 86 | } |
| 87 | |
| 88 | { |
| 89 | // Generate .pbrpc.m |
| 90 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 91 | ::grpc::string imports = ::grpc::string("#import \"") + file_name + |
| 92 | ".pbrpc.h\"\n\n" |
Jorge Canizales | a92caa9 | 2015-06-21 17:55:43 -0700 | [diff] [blame] | 93 | "#import <ProtoRPC/ProtoRPC.h>\n" |
Jorge Canizales | a92caa9 | 2015-06-21 17:55:43 -0700 | [diff] [blame] | 94 | "#import <RxLibrary/GRXWriter+Immediate.h>\n"; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 95 | |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 96 | ::grpc::string definitions; |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 97 | for (int i = 0; i < file->service_count(); i++) { |
| 98 | const grpc::protobuf::ServiceDescriptor *service = file->service(i); |
Jorge Canizales | 52592fc | 2015-05-26 11:53:31 -0700 | [diff] [blame] | 99 | definitions += grpc_objective_c_generator::GetSource(service); |
Jorge Canizales | 1900dfc | 2015-05-15 09:44:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions); |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | private: |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 109 | // Write the given code into the given file. |
| 110 | void Write(grpc::protobuf::compiler::GeneratorContext *context, |
Jorge Canizales | b015dfb | 2015-08-13 18:41:29 -0700 | [diff] [blame] | 111 | const ::grpc::string &filename, const ::grpc::string &code) const { |
Jorge Canizales | 1a7918b | 2015-05-13 12:04:03 -0700 | [diff] [blame] | 112 | std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output( |
| 113 | context->Open(filename)); |
| 114 | grpc::protobuf::io::CodedOutputStream coded_out(output.get()); |
| 115 | coded_out.WriteRaw(code.data(), code.size()); |
| 116 | } |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | int main(int argc, char *argv[]) { |
| 120 | ObjectiveCGrpcGenerator generator; |
| 121 | return grpc::protobuf::compiler::PluginMain(argc, argv, &generator); |
| 122 | } |