From 92d6214e0f76cc92f35f3a44aa89e0b0e60df5e8 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Sat, 2 Aug 2014 21:57:54 +0100 Subject: Add protoc plugin. Original patch by igorgatis@gmail.com, tweaked a little before commit. Fixes issue 90. --- src/ProtoGen/ProtocGenCs.cs | 76 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/ProtoGen/ProtocGenCs.cs (limited to 'src/ProtoGen/ProtocGenCs.cs') diff --git a/src/ProtoGen/ProtocGenCs.cs b/src/ProtoGen/ProtocGenCs.cs new file mode 100644 index 00000000..29264200 --- /dev/null +++ b/src/ProtoGen/ProtocGenCs.cs @@ -0,0 +1,76 @@ +using Google.ProtocolBuffers.Compiler.PluginProto; +using Google.ProtocolBuffers.DescriptorProtos; +using System; +using System.Collections.Generic; + +// Usage example: +// protoc.exe +// --plugin=path\to\protoc-gen-cs.exe +// --cs_out="-generated_code_attributes=true umbrella_namespace=TutorialProto :." +// --proto_path=.\protos\ +// protos\tutorial\addressbook.proto + +namespace Google.ProtocolBuffers.ProtoGen +{ + public static class ProtocGenCs + { + internal static void Run(CodeGeneratorRequest request, CodeGeneratorResponse.Builder response) + { + var arguments = new List(); + foreach (var arg in request.Parameter.Split(' ')) + { + var timmedArg = (arg ?? "").Trim(); + if (!string.IsNullOrEmpty(timmedArg)) + { + arguments.Add(timmedArg); + } + } + // Adding fake input file to make TryValidate happy. + arguments.Add(System.Reflection.Assembly.GetExecutingAssembly().Location); + + GeneratorOptions options = new GeneratorOptions + { + Arguments = arguments + }; + IList validationFailures; + if (!options.TryValidate(out validationFailures)) + { + response.Error += new InvalidOptionsException(validationFailures).Message; + return; + } + + Generator generator = Generator.CreateGenerator(options); + generator.Generate(request, response); + } + + public static int Main(string[] args) + { + // Hack to make sure everything's initialized + DescriptorProtoFile.Descriptor.ToString(); + ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance(); + CSharpOptions.RegisterAllExtensions(extensionRegistry); + + CodeGeneratorRequest request; + var response = new CodeGeneratorResponse.Builder(); + try + { + using (var input = Console.OpenStandardInput()) + { + request = CodeGeneratorRequest.ParseFrom(input, extensionRegistry); + } + Run(request, response); + } + catch (Exception e) + { + response.Error += e.ToString(); + } + + using (var output = Console.OpenStandardOutput()) + { + response.Build().WriteTo(output); + output.Flush(); + } + return 0; + } + } +} -- cgit v1.2.3