ProtobufGenerator.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using Microsoft.VisualStudio.TextTemplating.VSHost;
  6. namespace ProtobufTool
  7. {
  8. [ComVisible(true)]
  9. [Guid("9cf79956-6dfb-4d5b-8d29-7b43f7306acf")]
  10. public class ProtobufGenerator : BaseCodeGeneratorWithSite
  11. {
  12. public override string GetDefaultExtension()
  13. {
  14. return ".pb.cs";
  15. }
  16. protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
  17. {
  18. var processStartInfo = new ProcessStartInfo("protogen.exe")
  19. {
  20. CreateNoWindow = true,
  21. WindowStyle = ProcessWindowStyle.Hidden,
  22. WorkingDirectory = Environment.CurrentDirectory,
  23. RedirectStandardError = true,
  24. RedirectStandardOutput = true
  25. };
  26. processStartInfo.CreateNoWindow = true;
  27. processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  28. var tempFile = Path.GetTempFileName();
  29. string inputDir = Path.GetFullPath(inputFileName);
  30. processStartInfo.Arguments = string.Format(
  31. " -q -writeErrors -t: csharp -i:{0} -w:{1} -o:{2} ",
  32. inputFileName, inputDir, tempFile);
  33. using(var process = Process.Start(processStartInfo))
  34. {
  35. process.WaitForExit();
  36. }
  37. byte[] bytes = File.ReadAllBytes(tempFile);
  38. File.Delete(tempFile);
  39. return bytes;
  40. }
  41. }
  42. }