BashUtil.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace HybridCLR.Editor.Installer
  10. {
  11. public static class BashUtil
  12. {
  13. public static int RunCommand(string workingDir, string program, string[] args, bool log = true)
  14. {
  15. using (Process p = new Process())
  16. {
  17. p.StartInfo.WorkingDirectory = workingDir;
  18. p.StartInfo.FileName = program;
  19. p.StartInfo.UseShellExecute = false;
  20. p.StartInfo.CreateNoWindow = true;
  21. string argsStr = string.Join(" ", args.Select(arg => "\"" + arg + "\""));
  22. p.StartInfo.Arguments = argsStr;
  23. if (log)
  24. {
  25. UnityEngine.Debug.Log($"[BashUtil] run => {program} {argsStr}");
  26. }
  27. p.Start();
  28. p.WaitForExit();
  29. return p.ExitCode;
  30. }
  31. }
  32. public static (int ExitCode, string StdOut, string StdErr) RunCommand2(string workingDir, string program, string[] args, bool log = true)
  33. {
  34. using (Process p = new Process())
  35. {
  36. p.StartInfo.WorkingDirectory = workingDir;
  37. p.StartInfo.FileName = program;
  38. p.StartInfo.UseShellExecute = false;
  39. p.StartInfo.CreateNoWindow = true;
  40. p.StartInfo.RedirectStandardOutput = true;
  41. p.StartInfo.RedirectStandardError = true;
  42. string argsStr = string.Join(" ", args);
  43. p.StartInfo.Arguments = argsStr;
  44. if (log)
  45. {
  46. UnityEngine.Debug.Log($"[BashUtil] run => {program} {argsStr}");
  47. }
  48. p.Start();
  49. p.WaitForExit();
  50. string stdOut = p.StandardOutput.ReadToEnd();
  51. string stdErr = p.StandardError.ReadToEnd();
  52. return (p.ExitCode, stdOut, stdErr);
  53. }
  54. }
  55. public static void RemoveDir(string dir, bool log = false)
  56. {
  57. if (log)
  58. {
  59. UnityEngine.Debug.Log($"[BashUtil] RemoveDir dir:{dir}");
  60. }
  61. int maxTryCount = 5;
  62. for (int i = 0; i < maxTryCount; ++i)
  63. {
  64. try
  65. {
  66. if (!Directory.Exists(dir))
  67. {
  68. return;
  69. }
  70. foreach (var file in Directory.GetFiles(dir))
  71. {
  72. File.SetAttributes(file, FileAttributes.Normal);
  73. File.Delete(file);
  74. }
  75. foreach (var subDir in Directory.GetDirectories(dir))
  76. {
  77. RemoveDir(subDir);
  78. }
  79. Directory.Delete(dir);
  80. break;
  81. }
  82. catch (Exception e)
  83. {
  84. UnityEngine.Debug.LogError($"[BashUtil] RemoveDir:{dir} with exception:{e}. try count:{i}");
  85. Thread.Sleep(100);
  86. }
  87. }
  88. }
  89. public static void RecreateDir(string dir)
  90. {
  91. if(Directory.Exists(dir))
  92. {
  93. RemoveDir(dir, true);
  94. }
  95. Directory.CreateDirectory(dir);
  96. }
  97. private static void CopyWithCheckLongFile(string srcFile, string dstFile)
  98. {
  99. var maxPathLength = 255;
  100. #if UNITY_EDITOR_OSX
  101. maxPathLength = 1024;
  102. #endif
  103. if (srcFile.Length > maxPathLength)
  104. {
  105. UnityEngine.Debug.LogError($"srcFile:{srcFile} path is too long. copy ignore!");
  106. return;
  107. }
  108. if (dstFile.Length > maxPathLength)
  109. {
  110. UnityEngine.Debug.LogError($"dstFile:{dstFile} path is too long. copy ignore!");
  111. return;
  112. }
  113. File.Copy(srcFile, dstFile);
  114. }
  115. public static void CopyDir(string src, string dst, bool log = false)
  116. {
  117. if (log)
  118. {
  119. UnityEngine.Debug.Log($"[BashUtil] CopyDir {src} => {dst}");
  120. }
  121. RemoveDir(dst);
  122. Directory.CreateDirectory(dst);
  123. foreach(var file in Directory.GetFiles(src))
  124. {
  125. CopyWithCheckLongFile(file, $"{dst}/{Path.GetFileName(file)}");
  126. }
  127. foreach(var subDir in Directory.GetDirectories(src))
  128. {
  129. CopyDir(subDir, $"{dst}/{Path.GetFileName(subDir)}");
  130. }
  131. }
  132. }
  133. }