LaunchTool.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using Codice.Client.BaseCommands.EventTracking;
  5. using Codice.CM.Common;
  6. using Codice.LogWrapper;
  7. using Codice.Utils;
  8. using PlasticGui;
  9. using Unity.PlasticSCM.Editor.Views;
  10. namespace Unity.PlasticSCM.Editor.Tool
  11. {
  12. internal static class LaunchTool
  13. {
  14. internal static void OpenGUIForMode(WorkspaceInfo wkInfo, bool isGluonMode)
  15. {
  16. if (ShowDownloadPlasticExeWindow(isGluonMode))
  17. return;
  18. mLog.DebugFormat(
  19. "Opening GUI on wkPath '{0}'.",
  20. wkInfo.ClientPath);
  21. TrackFeatureUseEvent.For(
  22. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo),
  23. isGluonMode ?
  24. TrackFeatureUseEvent.Features.LaunchGluonTool :
  25. TrackFeatureUseEvent.Features.LaunchPlasticTool);
  26. if (isGluonMode)
  27. {
  28. Process gluonProcess = ExecuteGUI(
  29. PlasticInstallPath.GetGluonExePath(),
  30. string.Format(
  31. ToolConstants.Gluon.GUI_WK_EXPLORER_ARG,
  32. wkInfo.ClientPath),
  33. ToolConstants.Gluon.GUI_COMMAND_FILE_ARG,
  34. ToolConstants.Gluon.GUI_COMMAND_FILE,
  35. mGluonProcessId);
  36. if (gluonProcess != null)
  37. mGluonProcessId = gluonProcess.Id;
  38. return;
  39. }
  40. if (PlatformIdentifier.IsMac())
  41. {
  42. Process plasticProcess = ExecuteGUI(
  43. PlasticInstallPath.GetPlasticExePath(),
  44. string.Format(
  45. ToolConstants.Plastic.GUI_MACOS_WK_EXPLORER_ARG,
  46. wkInfo.ClientPath),
  47. ToolConstants.Plastic.GUI_MACOS_COMMAND_FILE_ARG,
  48. ToolConstants.Plastic.GUI_MACOS_COMMAND_FILE,
  49. mPlasticProcessId);
  50. if (plasticProcess != null)
  51. mPlasticProcessId = plasticProcess.Id;
  52. return;
  53. }
  54. ExecuteProcess(
  55. PlasticInstallPath.GetPlasticExePath(),
  56. string.Format(
  57. ToolConstants.Plastic.GUI_WINDOWS_WK_ARG,
  58. wkInfo.ClientPath));
  59. }
  60. internal static void OpenBranchExplorer(WorkspaceInfo wkInfo, bool isGluonMode)
  61. {
  62. if (ShowDownloadPlasticExeWindow(isGluonMode))
  63. return;
  64. mLog.DebugFormat(
  65. "Opening Branch Explorer on wkPath '{0}'.",
  66. wkInfo.ClientPath);
  67. TrackFeatureUseEvent.For(
  68. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo),
  69. TrackFeatureUseEvent.Features.LaunchBranchExplorer);
  70. if (PlatformIdentifier.IsMac())
  71. {
  72. Process plasticProcess = ExecuteGUI(
  73. PlasticInstallPath.GetPlasticExePath(),
  74. string.Format(
  75. ToolConstants.Plastic.GUI_MACOS_BREX_ARG,
  76. wkInfo.ClientPath),
  77. ToolConstants.Plastic.GUI_MACOS_COMMAND_FILE_ARG,
  78. ToolConstants.Plastic.GUI_MACOS_COMMAND_FILE,
  79. mPlasticProcessId);
  80. if (plasticProcess != null)
  81. mPlasticProcessId = plasticProcess.Id;
  82. return;
  83. }
  84. Process brexProcess = ExecuteWindowsGUI(
  85. PlasticInstallPath.GetPlasticExePath(),
  86. string.Format(
  87. ToolConstants.Plastic.GUI_WINDOWS_BREX_ARG,
  88. wkInfo.ClientPath),
  89. mBrexProcessId);
  90. if (brexProcess != null)
  91. mBrexProcessId = brexProcess.Id;
  92. }
  93. internal static void OpenChangesetDiffs(string fullChangesetSpec, bool isGluonMode)
  94. {
  95. if (ShowDownloadPlasticExeWindow(isGluonMode))
  96. return;
  97. mLog.DebugFormat(
  98. "Launching changeset diffs for '{0}'",
  99. fullChangesetSpec);
  100. string exePath = (isGluonMode) ?
  101. PlasticInstallPath.GetGluonExePath() :
  102. PlasticInstallPath.GetPlasticExePath();
  103. string changesetDiffArg = (isGluonMode) ?
  104. ToolConstants.Gluon.GUI_CHANGESET_DIFF_ARG :
  105. ToolConstants.Plastic.GUI_CHANGESET_DIFF_ARG;
  106. ExecuteProcess(exePath,
  107. string.Format(
  108. changesetDiffArg, fullChangesetSpec));
  109. }
  110. internal static void OpenSelectedChangesetsDiffs(
  111. string srcFullChangesetSpec,
  112. string dstFullChangesetSpec,
  113. bool isGluonMode)
  114. {
  115. if (ShowDownloadPlasticExeWindow(isGluonMode))
  116. return;
  117. mLog.DebugFormat(
  118. "Launching selected changesets diffs for '{0}' and '{1}'",
  119. srcFullChangesetSpec,
  120. dstFullChangesetSpec);
  121. string exePath = (isGluonMode) ?
  122. PlasticInstallPath.GetGluonExePath() :
  123. PlasticInstallPath.GetPlasticExePath();
  124. string selectedChangesetsDiffArgs = (isGluonMode) ?
  125. ToolConstants.Gluon.GUI_SELECTED_CHANGESETS_DIFF_ARGS :
  126. ToolConstants.Plastic.GUI_SELECTED_CHANGESETS_DIFF_ARGS;
  127. ExecuteProcess(exePath,
  128. string.Format(
  129. selectedChangesetsDiffArgs,
  130. srcFullChangesetSpec,
  131. dstFullChangesetSpec));
  132. }
  133. internal static void OpenBranchDiffs(string fullBranchSpec, bool isGluonMode)
  134. {
  135. if (ShowDownloadPlasticExeWindow(isGluonMode))
  136. return;
  137. mLog.DebugFormat(
  138. "Launching branch diffs for '{0}'",
  139. fullBranchSpec);
  140. string exePath = (isGluonMode) ?
  141. PlasticInstallPath.GetGluonExePath() :
  142. PlasticInstallPath.GetPlasticExePath();
  143. string branchDiffArg = (isGluonMode) ?
  144. ToolConstants.Gluon.GUI_BRANCH_DIFF_ARG :
  145. ToolConstants.Plastic.GUI_BRANCH_DIFF_ARG;
  146. ExecuteProcess(exePath,
  147. string.Format(
  148. branchDiffArg, fullBranchSpec));
  149. }
  150. internal static void OpenWorkspaceConfiguration(WorkspaceInfo wkInfo, bool isGluonMode)
  151. {
  152. if (ShowDownloadPlasticExeWindow(isGluonMode))
  153. return;
  154. mLog.DebugFormat(
  155. "Opening Workspace Configuration on wkPath '{0}'.",
  156. wkInfo.ClientPath);
  157. TrackFeatureUseEvent.For(
  158. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo),
  159. TrackFeatureUseEvent.Features.LaunchPartialConfigure);
  160. Process gluonProcess = ExecuteGUI(
  161. PlasticInstallPath.GetGluonExePath(),
  162. string.Format(
  163. ToolConstants.Gluon.GUI_WK_CONFIGURATION_ARG,
  164. wkInfo.ClientPath),
  165. ToolConstants.Gluon.GUI_COMMAND_FILE_ARG,
  166. ToolConstants.Gluon.GUI_COMMAND_FILE,
  167. mGluonProcessId);
  168. if (gluonProcess == null)
  169. return;
  170. mGluonProcessId = gluonProcess.Id;
  171. }
  172. internal static void OpenMerge(string wkPath, bool isGluonMode)
  173. {
  174. if (ShowDownloadPlasticExeWindow(isGluonMode))
  175. return;
  176. mLog.DebugFormat(
  177. "Opening Merge on wkPath '{0}'.",
  178. wkPath);
  179. if (PlatformIdentifier.IsMac())
  180. {
  181. Process plasticProcess = ExecuteGUI(
  182. PlasticInstallPath.GetPlasticExePath(),
  183. string.Format(ToolConstants.Plastic.GUI_MACOS_MERGE_ARG, wkPath),
  184. ToolConstants.Plastic.GUI_MACOS_COMMAND_FILE_ARG,
  185. ToolConstants.Plastic.GUI_MACOS_COMMAND_FILE,
  186. mPlasticProcessId);
  187. if (plasticProcess != null)
  188. mPlasticProcessId = plasticProcess.Id;
  189. return;
  190. }
  191. ExecuteProcess(
  192. PlasticInstallPath.GetPlasticExePath(),
  193. string.Format(ToolConstants.Plastic.GUI_WINDOWS_MERGE_ARG, wkPath));
  194. }
  195. internal static bool ShowDownloadPlasticExeWindow(bool isGluonMode)
  196. {
  197. if (IsExeAvailable.ForMode(isGluonMode))
  198. return false;
  199. DownloadPlasticExeWindow.ShowWindow(isGluonMode);
  200. return true;
  201. }
  202. static Process ExecuteGUI(
  203. string program,
  204. string args,
  205. string commandFileArg,
  206. string commandFileName,
  207. int processId)
  208. {
  209. string commandFile = Path.Combine(
  210. Path.GetTempPath(), commandFileName);
  211. Process process = GetGUIProcess(program, processId);
  212. if (process == null)
  213. {
  214. mLog.DebugFormat("Executing {0} (new process).", program);
  215. return ExecuteProcess(
  216. program, args + string.Format(commandFileArg, commandFile));
  217. }
  218. mLog.DebugFormat("Executing {0} (reuse process pid:{1}).", program, processId);
  219. using (StreamWriter writer = new StreamWriter(new FileStream(
  220. commandFile, FileMode.Append, FileAccess.Write, FileShare.Read)))
  221. {
  222. writer.WriteLine(args);
  223. }
  224. return process;
  225. }
  226. static Process ExecuteWindowsGUI(
  227. string program,
  228. string args,
  229. int processId)
  230. {
  231. Process process = GetGUIProcess(program, processId);
  232. if (process == null)
  233. {
  234. mLog.DebugFormat("Executing {0} (new process).", program);
  235. return ExecuteProcess(program, args);
  236. }
  237. mLog.DebugFormat("Not executing {0} (existing process pid:{1}).", program, processId);
  238. BringWindowToFront.ForWindowsProcess(process.Id);
  239. return process;
  240. }
  241. static Process ExecuteProcess(string program, string args)
  242. {
  243. mLog.DebugFormat("Execute process: '{0} {1}'", program, args);
  244. Process process = BuildProcess(program, args);
  245. try
  246. {
  247. process.Start();
  248. return process;
  249. }
  250. catch (Exception ex)
  251. {
  252. mLog.ErrorFormat("Couldn't execute the program {0}: {1}",
  253. program, ex.Message);
  254. mLog.DebugFormat("Stack trace: {0}",
  255. ex.StackTrace);
  256. return null;
  257. }
  258. }
  259. static Process BuildProcess(string program, string args)
  260. {
  261. Process result = new Process();
  262. result.StartInfo.FileName = program;
  263. result.StartInfo.Arguments = args;
  264. result.StartInfo.CreateNoWindow = false;
  265. return result;
  266. }
  267. static Process GetGUIProcess(string program, int processId)
  268. {
  269. if (processId == -1)
  270. return null;
  271. mLog.DebugFormat("Checking {0} process [pid:{1}].", program, processId);
  272. try
  273. {
  274. Process process = Process.GetProcessById(processId);
  275. if (process == null)
  276. return null;
  277. return process.HasExited ? null : process;
  278. }
  279. catch
  280. {
  281. // process is not running
  282. return null;
  283. }
  284. }
  285. static int mPlasticProcessId = -1;
  286. static int mGluonProcessId = -1;
  287. static int mBrexProcessId = -1;
  288. static readonly ILog mLog = LogManager.GetLogger("LaunchTool");
  289. }
  290. }