ShellHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace ET
  7. {
  8. public static class ShellHelper
  9. {
  10. private static string shellApp
  11. {
  12. get
  13. {
  14. #if UNITY_EDITOR_WIN
  15. string app = "cmd.exe";
  16. #elif UNITY_EDITOR_OSX
  17. string app = "bash";
  18. #endif
  19. return app;
  20. }
  21. }
  22. private static volatile bool isFinish;
  23. public static void Run(string cmd, string workDirectory, List<string> environmentVars = null)
  24. {
  25. Process p = null;
  26. try
  27. {
  28. ProcessStartInfo start = new ProcessStartInfo(shellApp);
  29. #if UNITY_EDITOR_OSX
  30. string splitChar = ":";
  31. start.Arguments = "-c";
  32. #elif UNITY_EDITOR_WIN
  33. string splitChar = ";";
  34. start.Arguments = "/c";
  35. #endif
  36. if (environmentVars != null)
  37. {
  38. foreach (string var in environmentVars)
  39. {
  40. start.EnvironmentVariables["PATH"] += (splitChar + var);
  41. }
  42. }
  43. start.Arguments += (" \"" + cmd + "\"");
  44. start.CreateNoWindow = true;
  45. start.ErrorDialog = true;
  46. start.UseShellExecute = false;
  47. start.WorkingDirectory = workDirectory;
  48. if (start.UseShellExecute)
  49. {
  50. start.RedirectStandardOutput = false;
  51. start.RedirectStandardError = false;
  52. start.RedirectStandardInput = false;
  53. }
  54. else
  55. {
  56. start.RedirectStandardOutput = true;
  57. start.RedirectStandardError = true;
  58. start.RedirectStandardInput = true;
  59. start.StandardOutputEncoding = System.Text.Encoding.UTF8;
  60. start.StandardErrorEncoding = System.Text.Encoding.UTF8;
  61. }
  62. Barrier barrier = new Barrier(2);
  63. // 放到新线程启动进程,主线程循环读标准输出,直到进程结束
  64. Task.Run(() =>
  65. {
  66. p = Process.Start(start);
  67. barrier.RemoveParticipant();
  68. p.WaitForExit();
  69. isFinish = true;
  70. });
  71. // 这里要等待进程启动才能往下走,否则p将为null
  72. barrier.SignalAndWait();
  73. do
  74. {
  75. string line = p.StandardOutput.ReadLine();
  76. if (string.IsNullOrEmpty(line))
  77. {
  78. break;
  79. }
  80. line = line.Replace("\\", "/");
  81. UnityEngine.Debug.Log(line);
  82. }
  83. while (!isFinish);
  84. bool hasError = false;
  85. while (true)
  86. {
  87. string error = p.StandardError.ReadLine();
  88. if (string.IsNullOrEmpty(error))
  89. {
  90. break;
  91. }
  92. hasError = true;
  93. UnityEngine.Debug.LogError(error);
  94. }
  95. p.Close();
  96. if (hasError)
  97. {
  98. UnityEngine.Debug.LogError("has error!");
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. UnityEngine.Debug.LogException(e);
  104. if (p != null)
  105. {
  106. p.Close();
  107. }
  108. }
  109. }
  110. }
  111. }