aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtoGen/ProtocGenCs.cs
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/ProtoGen/ProtocGenCs.cs')
-rw-r--r--csharp/src/ProtoGen/ProtocGenCs.cs76
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;
+ }
+ }
+}