Bez popisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MigrationDialog.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using Codice.Client.BaseCommands;
  5. using Codice.Client.BaseCommands.EventTracking;
  6. using Codice.Client.BaseCommands.Sync;
  7. using Codice.Client.Common.Threading;
  8. using Codice.CM.Common;
  9. using Codice.LogWrapper;
  10. using CodiceApp.EventTracking;
  11. using PlasticGui;
  12. using PlasticGui.WorkspaceWindow;
  13. using Unity.PlasticSCM.Editor.UI;
  14. using Unity.PlasticSCM.Editor.UI.Progress;
  15. using Unity.PlasticSCM.Editor.WebApi;
  16. using Unity.PlasticSCM.Editor.Configuration;
  17. namespace Unity.PlasticSCM.Editor.CollabMigration
  18. {
  19. internal class MigrationDialog : PlasticDialog
  20. {
  21. protected override Rect DefaultRect
  22. {
  23. get
  24. {
  25. var baseRect = base.DefaultRect;
  26. return new Rect(baseRect.x, baseRect.y, 710, 260);
  27. }
  28. }
  29. protected override string GetTitle()
  30. {
  31. return "Upgrade your Collaborate project to Plastic SCM";
  32. }
  33. //TODO: localize the strings
  34. protected override void OnModalGUI()
  35. {
  36. GUILayout.BeginHorizontal();
  37. DoIconArea();
  38. GUILayout.Space(20);
  39. DoContentArea();
  40. GUILayout.EndHorizontal();
  41. DoButtonsArea();
  42. mProgressControls.UpdateDeterminateProgress(this);
  43. }
  44. internal static bool Show(
  45. EditorWindow parentWindow,
  46. string unityAccessToken,
  47. string projectPath,
  48. string user,
  49. string organizationName,
  50. RepId repId,
  51. long changesetId,
  52. long branchId,
  53. Action afterWorkspaceMigratedAction)
  54. {
  55. MigrationDialog dialog = Create(
  56. unityAccessToken,
  57. projectPath,
  58. user,
  59. organizationName,
  60. repId,
  61. changesetId,
  62. branchId,
  63. afterWorkspaceMigratedAction,
  64. new ProgressControlsForMigration());
  65. return dialog.RunModal(parentWindow) == ResponseType.Ok;
  66. }
  67. void DoIconArea()
  68. {
  69. GUILayout.BeginVertical();
  70. GUILayout.Space(30);
  71. Rect iconRect = GUILayoutUtility.GetRect(
  72. GUIContent.none, EditorStyles.label,
  73. GUILayout.Width(60), GUILayout.Height(60));
  74. GUI.DrawTexture(
  75. iconRect,
  76. Images.GetPlasticIcon(),
  77. ScaleMode.ScaleToFit);
  78. GUILayout.EndVertical();
  79. }
  80. void DoContentArea()
  81. {
  82. GUILayout.BeginVertical();
  83. Title("Upgrade your Collaborate project to Plastic SCM");
  84. GUILayout.Space(20);
  85. Paragraph("Your Unity project has been upgraded (from Collaborate) to Plastic SCM free" +
  86. " of charge by your administrator. Your local workspace will now be converted to a" +
  87. " Plastic SCM workspace in just a few minutes. Select “Migrate” to start the conversion process.");
  88. DrawProgressForMigration.For(
  89. mProgressControls.ProgressData);
  90. GUILayout.Space(40);
  91. GUILayout.EndVertical();
  92. }
  93. //TODO: localize the strings
  94. void DoButtonsArea()
  95. {
  96. using (new EditorGUILayout.HorizontalScope())
  97. {
  98. GUILayout.FlexibleSpace();
  99. if (Application.platform == RuntimePlatform.WindowsEditor)
  100. {
  101. DoOkButton();
  102. DoCloseButton();
  103. return;
  104. }
  105. DoCloseButton();
  106. DoOkButton();
  107. }
  108. }
  109. void DoOkButton()
  110. {
  111. if (mIsMigrationCompleted)
  112. {
  113. DoOpenPlasticButton();
  114. return;
  115. }
  116. DoMigrateButton();
  117. }
  118. void DoCloseButton()
  119. {
  120. GUI.enabled = !mProgressControls.ProgressData.IsOperationRunning;
  121. if (NormalButton(PlasticLocalization.GetString(
  122. PlasticLocalization.Name.CloseButton)))
  123. {
  124. if (mIsMigrationCompleted)
  125. TrackFeatureUseEvent.For(
  126. PlasticGui.Plastic.API.GetRepositorySpec(mWorkspaceInfo),
  127. TrackFeatureUseEvent.Features.CloseDialogAfterWorkspaceMigration);
  128. else
  129. TrackFeatureUseEvent.For(
  130. GetEventCloudOrganizationInfo(),
  131. TrackFeatureUseEvent.Features.DoNotMigrateWorkspace);
  132. CloseButtonAction();
  133. }
  134. GUI.enabled = true;
  135. }
  136. void DoOpenPlasticButton()
  137. {
  138. if (!NormalButton("Open Plastic SCM"))
  139. return;
  140. TrackFeatureUseEvent.For(
  141. PlasticGui.Plastic.API.GetRepositorySpec(mWorkspaceInfo),
  142. TrackFeatureUseEvent.Features.OpenPlasticAfterWorkspaceMigration);
  143. ((IPlasticDialogCloser)this).CloseDialog();
  144. ShowWindow.Plastic();
  145. }
  146. EventCloudOrganizationInfo GetEventCloudOrganizationInfo()
  147. {
  148. return new EventCloudOrganizationInfo()
  149. {
  150. Name = mOrganizationName,
  151. ServerType = EventCloudOrganizationInfo.GetServerType(true),
  152. User = mUser
  153. };
  154. }
  155. void DoMigrateButton()
  156. {
  157. GUI.enabled = !mProgressControls.ProgressData.IsOperationRunning;
  158. if (NormalButton("Migrate"))
  159. {
  160. if (EditorUtility.DisplayDialog(
  161. "Collab migration to Plastic SCM",
  162. "Are you sure to start the migration process?",
  163. PlasticLocalization.GetString(PlasticLocalization.Name.YesButton),
  164. PlasticLocalization.GetString(PlasticLocalization.Name.NoButton)))
  165. {
  166. TrackFeatureUseEvent.For(
  167. GetEventCloudOrganizationInfo(),
  168. TrackFeatureUseEvent.Features.MigrateWorkspace);
  169. LaunchMigration(
  170. mUnityAccessToken, mProjectPath,
  171. mOrganizationName, mRepId,
  172. mChangesetId, mBranchId,
  173. mAfterWorkspaceMigratedAction,
  174. mProgressControls);
  175. }
  176. else
  177. {
  178. TrackFeatureUseEvent.For(
  179. GetEventCloudOrganizationInfo(),
  180. TrackFeatureUseEvent.Features.DoNotMigrateWorkspace);
  181. }
  182. }
  183. GUI.enabled = true;
  184. }
  185. static void UpdateProgress(string wkPath,
  186. CreateWorkspaceFromCollab.Progress progress,
  187. ProgressControlsForMigration progressControls,
  188. BuildProgressSpeedAndRemainingTime.ProgressData progressData)
  189. {
  190. string header = MigrationProgressRender.FixNotificationPath(
  191. wkPath, progress.CurrentFile);
  192. string message = MigrationProgressRender.GetProgressString(
  193. progress,
  194. progressData,
  195. DateTime.Now,
  196. 0.05,
  197. "Calculating...",
  198. "Converted {0} of {1}bytes ({2} of 1 file){4}",
  199. "Converted {0} of {1}bytes ({2} of {3} files {4})",
  200. "remaining");
  201. float percent = GetProgressBarPercent.ForTransfer(
  202. progress.ProcessedSize, progress.TotalSize) / 100f;
  203. progressControls.ShowProgress(header, message, percent);
  204. }
  205. void LaunchMigration(
  206. string unityAccessToken,
  207. string projectPath,
  208. string organizationName,
  209. RepId repId,
  210. long changesetId,
  211. long branchId,
  212. Action afterWorkspaceMigratedAction,
  213. ProgressControlsForMigration progressControls)
  214. {
  215. string serverName = string.Format(
  216. "{0}@cloud", organizationName);
  217. TokenExchangeResponse tokenExchangeResponse = null;
  218. mWorkspaceInfo = null;
  219. CreateWorkspaceFromCollab.Progress progress = new CreateWorkspaceFromCollab.Progress();
  220. BuildProgressSpeedAndRemainingTime.ProgressData progressData =
  221. new BuildProgressSpeedAndRemainingTime.ProgressData(DateTime.Now);
  222. IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
  223. waiter.Execute(
  224. /*threadOperationDelegate*/
  225. delegate
  226. {
  227. tokenExchangeResponse = AutoConfig.PlasticCredentials(
  228. unityAccessToken,
  229. serverName,
  230. projectPath);
  231. if (tokenExchangeResponse.Error != null)
  232. {
  233. return;
  234. }
  235. RepositoryInfo repInfo = new BaseCommandsImpl().
  236. GetRepositoryInfo(repId, serverName);
  237. if (repInfo == null)
  238. {
  239. return;
  240. }
  241. repInfo.SetExplicitServer(serverName);
  242. mWorkspaceInfo = CreateWorkspaceFromCollab.Create(
  243. projectPath, repInfo.Name, repInfo,
  244. changesetId, branchId,
  245. progress);
  246. },
  247. /*afterOperationDelegate*/
  248. delegate
  249. {
  250. progressControls.HideProgress();
  251. if (waiter.Exception != null)
  252. {
  253. DisplayException(progressControls, waiter.Exception);
  254. TrackWorkspaceMigrationFinishedFailureEvent(mWorkspaceInfo);
  255. return;
  256. }
  257. if (tokenExchangeResponse.Error != null)
  258. {
  259. mLog.ErrorFormat(
  260. "Unable to get TokenExchangeResponse: {0} [code {1}]",
  261. tokenExchangeResponse.Error.Message,
  262. tokenExchangeResponse.Error.ErrorCode);
  263. }
  264. if (tokenExchangeResponse.Error != null ||
  265. mWorkspaceInfo == null)
  266. {
  267. progressControls.ShowError(
  268. "Failed to convert your workspace to Plastic SCM");
  269. TrackWorkspaceMigrationFinishedFailureEvent(mWorkspaceInfo);
  270. return;
  271. }
  272. progressControls.ShowSuccess(
  273. "Your workspace has been successfully converted to Plastic SCM");
  274. mIsMigrationCompleted = true;
  275. TrackFeatureUseEvent.For(
  276. PlasticGui.Plastic.API.GetRepositorySpec(mWorkspaceInfo),
  277. TrackFeatureUseEvent.Features.WorkspaceMigrationFinishedSuccess);
  278. afterWorkspaceMigratedAction();
  279. },
  280. /*timerTickDelegate*/
  281. delegate
  282. {
  283. UpdateProgress(projectPath, progress, progressControls, progressData);
  284. });
  285. }
  286. void TrackWorkspaceMigrationFinishedFailureEvent(WorkspaceInfo wkInfo)
  287. {
  288. if (wkInfo == null)
  289. {
  290. TrackFeatureUseEvent.For(
  291. GetEventCloudOrganizationInfo(),
  292. TrackFeatureUseEvent.Features.WorkspaceMigrationFinishedFailure);
  293. return;
  294. }
  295. TrackFeatureUseEvent.For(
  296. PlasticGui.Plastic.API.GetRepositorySpec(wkInfo),
  297. TrackFeatureUseEvent.Features.WorkspaceMigrationFinishedFailure);
  298. }
  299. static void DisplayException(
  300. ProgressControlsForMigration progressControls,
  301. Exception ex)
  302. {
  303. ExceptionsHandler.LogException(
  304. "MigrationDialog", ex);
  305. progressControls.ShowError(
  306. ExceptionsHandler.GetCorrectExceptionMessage(ex));
  307. }
  308. static MigrationDialog Create(
  309. string unityAccessToken,
  310. string projectPath,
  311. string user,
  312. string organizationName,
  313. RepId repId,
  314. long changesetId,
  315. long branchId,
  316. Action afterWorkspaceMigratedAction,
  317. ProgressControlsForMigration progressControls)
  318. {
  319. var instance = CreateInstance<MigrationDialog>();
  320. instance.IsResizable = false;
  321. instance.mUnityAccessToken = unityAccessToken;
  322. instance.mProjectPath = projectPath;
  323. instance.mUser = user;
  324. instance.mOrganizationName = organizationName;
  325. instance.mRepId = repId;
  326. instance.mChangesetId = changesetId;
  327. instance.mBranchId = branchId;
  328. instance.mAfterWorkspaceMigratedAction = afterWorkspaceMigratedAction;
  329. instance.mProgressControls = progressControls;
  330. instance.mEscapeKeyAction = instance.CloseButtonAction;
  331. return instance;
  332. }
  333. bool mIsMigrationCompleted;
  334. ProgressControlsForMigration mProgressControls;
  335. Action mAfterWorkspaceMigratedAction;
  336. long mChangesetId;
  337. long mBranchId;
  338. RepId mRepId;
  339. string mOrganizationName;
  340. string mUser;
  341. string mProjectPath;
  342. string mUnityAccessToken;
  343. WorkspaceInfo mWorkspaceInfo;
  344. static readonly ILog mLog = LogManager.GetLogger("MigrationDialog");
  345. }
  346. }