3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 // Generates C# gRPC service interface out of Protobuf IDL.
23 #include "src/compiler/config.h"
24 #include "src/compiler/csharp_generator.h"
25 #include "src/compiler/csharp_generator_helpers.h"
27 class CSharpGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
29 CSharpGrpcGenerator() {}
30 ~CSharpGrpcGenerator() {}
32 bool Generate(const grpc::protobuf::FileDescriptor* file,
33 const grpc::string& parameter,
34 grpc::protobuf::compiler::GeneratorContext* context,
35 grpc::string* error) const {
36 std::vector<std::pair<grpc::string, grpc::string> > options;
37 grpc::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
39 bool generate_client = true;
40 bool generate_server = true;
41 bool internal_access = false;
42 for (size_t i = 0; i < options.size(); i++) {
43 if (options[i].first == "no_client") {
44 generate_client = false;
45 } else if (options[i].first == "no_server") {
46 generate_server = false;
47 } else if (options[i].first == "internal_access") {
48 internal_access = true;
50 *error = "Unknown generator option: " + options[i].first;
55 grpc::string code = grpc_csharp_generator::GetServices(
56 file, generate_client, generate_server, internal_access);
57 if (code.size() == 0) {
58 return true; // don't generate a file if there are no services
61 // Get output file name.
62 grpc::string file_name;
63 if (!grpc_csharp_generator::ServicesFilename(file, &file_name)) {
66 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
67 context->Open(file_name));
68 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
69 coded_out.WriteRaw(code.data(), code.size());
74 int main(int argc, char* argv[]) {
75 CSharpGrpcGenerator generator;
76 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);