diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
commit | 01c8c35ca3d3424a9473dd22ea2456933b3e8d36 (patch) | |
tree | de582cb9ef3e449f24015dec09f80e998806b26a /csharp/src/ProtoGen/ProtocGenCs.cs | |
parent | 813d6d652d8091e9365bfbd01efb5f2ee8bdfbce (diff) | |
parent | 1eab56aee8dddcb656db67d9fb87a8cc6a2ac112 (diff) | |
download | protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.gz protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.bz2 protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.zip |
Merge pull request #2 from jtattermusch/csharp_protobuf_imported
Pulling protobuf-csharp-port into csharp/ directory
Diffstat (limited to 'csharp/src/ProtoGen/ProtocGenCs.cs')
-rw-r--r-- | csharp/src/ProtoGen/ProtocGenCs.cs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/csharp/src/ProtoGen/ProtocGenCs.cs b/csharp/src/ProtoGen/ProtocGenCs.cs new file mode 100644 index 00000000..29264200 --- /dev/null +++ b/csharp/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<string>();
+ 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<string> 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;
+ }
+ }
+}
|