BashUtil.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Tasks;
  8. namespace HybridCLR.Editor.Installer
  9. {
  10. public static class BashUtil
  11. {
  12. public static int RunCommand(string workingDir, string program, string[] args, bool log = true)
  13. {
  14. using (Process p = new Process())
  15. {
  16. p.StartInfo.WorkingDirectory = workingDir;
  17. p.StartInfo.FileName = program;
  18. p.StartInfo.UseShellExecute = false;
  19. p.StartInfo.CreateNoWindow = true;
  20. string argsStr = string.Join(" ", args.Select(arg => "\"" + arg + "\""));
  21. p.StartInfo.Arguments = argsStr;
  22. if (log)
  23. {
  24. UnityEngine.Debug.Log($"[BashUtil] run => {program} {argsStr}");
  25. }
  26. p.Start();
  27. p.WaitForExit();
  28. return p.ExitCode;
  29. }
  30. }
  31. public static (int ExitCode, string StdOut, string StdErr) RunCommand2(string workingDir, string program, string[] args, bool log = true)
  32. {
  33. using (Process p = new Process())
  34. {
  35. p.StartInfo.WorkingDirectory = workingDir;
  36. p.StartInfo.FileName = program;
  37. p.StartInfo.UseShellExecute = false;
  38. p.StartInfo.CreateNoWindow = true;
  39. p.StartInfo.RedirectStandardOutput = true;
  40. p.StartInfo.RedirectStandardError = true;
  41. string argsStr = string.Join(" ", args.Select(arg => "\"" + arg + "\""));
  42. p.StartInfo.Arguments = argsStr;
  43. if (log)
  44. {
  45. UnityEngine.Debug.Log($"[BashUtil] run => {program} {argsStr}");
  46. }
  47. p.Start();
  48. p.WaitForExit();
  49. string stdOut = p.StandardOutput.ReadToEnd();
  50. string stdErr = p.StandardError.ReadToEnd();
  51. return (p.ExitCode, stdOut, stdErr);
  52. }
  53. }
  54. public static bool ExistProgram(string prog)
  55. {
  56. #if UNITY_EDITOR_WIN
  57. return RunCommand(".", "where", new string[] {prog}) == 0;
  58. #elif UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX
  59. return RunCommand(".", "which", new string[] {prog}) == 0;
  60. #endif
  61. }
  62. public static void RemoveDir(string dir, bool log = false)
  63. {
  64. if (log)
  65. {
  66. UnityEngine.Debug.Log($"[BashUtil] RemoveDir dir:{dir}");
  67. }
  68. if (!Directory.Exists(dir))
  69. {
  70. return;
  71. }
  72. foreach (var file in Directory.GetFiles(dir))
  73. {
  74. File.SetAttributes(file, FileAttributes.Normal);
  75. File.Delete(file);
  76. }
  77. foreach (var subDir in Directory.GetDirectories(dir))
  78. {
  79. RemoveDir(subDir);
  80. }
  81. Directory.Delete(dir);
  82. }
  83. public static void RecreateDir(string dir)
  84. {
  85. if(Directory.Exists(dir))
  86. {
  87. RemoveDir(dir, true);
  88. }
  89. Directory.CreateDirectory(dir);
  90. }
  91. public static void CopyDir(string src, string dst, bool log = false)
  92. {
  93. if (log)
  94. {
  95. UnityEngine.Debug.Log($"[BashUtil] CopyDir {src} => {dst}");
  96. }
  97. RemoveDir(dst);
  98. Directory.CreateDirectory(dst);
  99. foreach(var file in Directory.GetFiles(src))
  100. {
  101. File.Copy(file, $"{dst}/{Path.GetFileName(file)}");
  102. }
  103. foreach(var subDir in Directory.GetDirectories(src))
  104. {
  105. CopyDir(subDir, $"{dst}/{Path.GetFileName(subDir)}");
  106. }
  107. }
  108. }
  109. }