LaunchTool.cs 15 KB

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