Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using Codice.Client.Common.Threading;
  6. using Codice.CM.Common;
  7. using Codice.CM.WorkspaceServer;
  8. using Codice.LogWrapper;
  9. using Unity.PlasticSCM.Editor.AssetUtils;
  10. using Unity.PlasticSCM.Editor.WebApi;
  11. using Unity.PlasticSCM.Editor.ProjectDownloader;
  12. namespace Unity.PlasticSCM.Editor.CollabMigration
  13. {
  14. public static class MigrateCollabProject
  15. {
  16. internal static void Initialize()
  17. {
  18. if (SessionState.GetInt(
  19. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  20. MIGRATED_NOT_CALCULATED) == MIGRATED_NOTHING_TO_DO)
  21. return;
  22. EditorApplication.update += RunOnceWhenAccessTokenAndProjectIdAreInitialized;
  23. }
  24. internal static void RunOnceWhenAccessTokenAndProjectIdAreInitialized()
  25. {
  26. if (string.IsNullOrEmpty(CloudProjectSettings.accessToken))
  27. return;
  28. if (!SetupCloudProjectId.HasCloudProjectId())
  29. return;
  30. if (!SessionState.GetBool(
  31. CloudProjectDownloader.IS_PROJECT_DOWNLOADER_ALREADY_EXECUTED_KEY, false))
  32. return;
  33. EditorApplication.update -= RunOnceWhenAccessTokenAndProjectIdAreInitialized;
  34. string projectPath = ProjectPath.FromApplicationDataPath(
  35. ApplicationDataPath.Get());
  36. string projectGuid = SetupCloudProjectId.GetCloudProjectId();
  37. if (!ShouldProjectBeMigrated(projectPath, projectGuid))
  38. {
  39. SessionState.SetInt(
  40. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  41. MIGRATED_NOTHING_TO_DO);
  42. return;
  43. }
  44. Execute(
  45. CloudProjectSettings.accessToken,
  46. projectPath,
  47. projectGuid);
  48. }
  49. static bool ShouldProjectBeMigrated(
  50. string projectPath,
  51. string projectGuid)
  52. {
  53. if (SessionState.GetBool(
  54. CloudProjectDownloader.SHOULD_PROJECT_BE_DOWNLOADED_KEY, false))
  55. {
  56. return false;
  57. }
  58. string collabPath = GetCollabSnapshotFile(
  59. projectPath, projectGuid);
  60. if (!File.Exists(collabPath))
  61. {
  62. return false;
  63. }
  64. if (FindWorkspace.HasWorkspace(ApplicationDataPath.Get()))
  65. {
  66. return false;
  67. }
  68. return true;
  69. }
  70. static void Execute(
  71. string unityAccessToken,
  72. string projectPath,
  73. string projectGuid)
  74. {
  75. string headCommitSha = GetCollabHeadCommitSha(projectPath, projectGuid);
  76. if (string.IsNullOrEmpty(headCommitSha))
  77. return;
  78. PlasticApp.InitializeIfNeeded();
  79. LaunchMigrationIfProjectIsArchivedAndMigrated(
  80. unityAccessToken,
  81. projectPath,
  82. projectGuid,
  83. headCommitSha);
  84. }
  85. internal static void DeletePlasticDirectoryIfExists(string projectPath)
  86. {
  87. WorkspaceInfo wkInfo = new WorkspaceInfo("wk", projectPath);
  88. string plasticDirectory = WorkspaceConfigFile.GetPlasticWkConfigPath(wkInfo);
  89. if (!Directory.Exists(plasticDirectory))
  90. return;
  91. Directory.Delete(plasticDirectory, true);
  92. }
  93. static void LaunchMigrationIfProjectIsArchivedAndMigrated(
  94. string unityAccessToken,
  95. string projectPath,
  96. string projectGuid,
  97. string headCommitSha)
  98. {
  99. IsCollabProjectMigratedResponse isMigratedResponse = null;
  100. ChangesetFromCollabCommitResponse changesetResponse = null;
  101. IThreadWaiter waiter = ThreadWaiter.GetWaiter(10);
  102. waiter.Execute(
  103. /*threadOperationDelegate*/ delegate
  104. {
  105. isMigratedResponse = WebRestApiClient.PlasticScm.
  106. IsCollabProjectMigrated(unityAccessToken, projectGuid);
  107. if (isMigratedResponse.Error != null)
  108. return;
  109. if (!isMigratedResponse.IsMigrated)
  110. return;
  111. OrganizationCredentials credentials = new OrganizationCredentials();
  112. credentials.User = isMigratedResponse.Credentials.Email;
  113. credentials.Password = isMigratedResponse.Credentials.Token;
  114. string webLoginAccessToken = WebRestApiClient.CloudServer.WebLogin(
  115. isMigratedResponse.WebServerUri,
  116. isMigratedResponse.PlasticCloudOrganizationName,
  117. credentials);
  118. changesetResponse = WebRestApiClient.CloudServer.
  119. GetChangesetFromCollabCommit(
  120. isMigratedResponse.WebServerUri,
  121. isMigratedResponse.PlasticCloudOrganizationName,
  122. webLoginAccessToken, projectGuid, headCommitSha);
  123. },
  124. /*afterOperationDelegate*/ delegate
  125. {
  126. if (waiter.Exception != null)
  127. {
  128. ExceptionsHandler.LogException(
  129. "IsCollabProjectArchivedAndMigrated",
  130. waiter.Exception);
  131. return;
  132. }
  133. if (isMigratedResponse.Error != null)
  134. {
  135. mLog.ErrorFormat(
  136. "Unable to get IsCollabProjectMigratedResponse: {0} [code {1}]",
  137. isMigratedResponse.Error.Message,
  138. isMigratedResponse.Error.ErrorCode);
  139. return;
  140. }
  141. if (!isMigratedResponse.IsMigrated)
  142. {
  143. SessionState.SetInt(
  144. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  145. MIGRATED_NOTHING_TO_DO);
  146. return;
  147. }
  148. if (changesetResponse.Error != null)
  149. {
  150. mLog.ErrorFormat(
  151. "Unable to get ChangesetFromCollabCommitResponse: {0} [code {1}]",
  152. changesetResponse.Error.Message,
  153. changesetResponse.Error.ErrorCode);
  154. return;
  155. }
  156. DeletePlasticDirectoryIfExists(projectPath);
  157. MigrationDialog.Show(
  158. null,
  159. unityAccessToken,
  160. projectPath,
  161. isMigratedResponse.Credentials.Email,
  162. isMigratedResponse.PlasticCloudOrganizationName,
  163. new RepId(
  164. changesetResponse.RepId,
  165. changesetResponse.RepModuleId),
  166. changesetResponse.ChangesetId,
  167. changesetResponse.BranchId,
  168. AfterWorkspaceMigrated);
  169. });
  170. }
  171. static void AfterWorkspaceMigrated()
  172. {
  173. SessionState.SetInt(
  174. IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY,
  175. MIGRATED_NOTHING_TO_DO);
  176. CollabPlugin.Disable();
  177. mLog.DebugFormat(
  178. "Disabled Collab Plugin after the migration for Project: {0}",
  179. ProjectPath.FromApplicationDataPath(ApplicationDataPath.Get()));
  180. }
  181. static string GetCollabHeadCommitSha(
  182. string projectPath,
  183. string projectGuid)
  184. {
  185. string collabPath = GetCollabSnapshotFile(
  186. projectPath, projectGuid);
  187. if (!File.Exists(collabPath))
  188. return null;
  189. string text = File.ReadAllText(collabPath);
  190. string[] chunks = text.Split(
  191. new string[] { "currRevisionID" },
  192. StringSplitOptions.None);
  193. string current = chunks[1].Substring(3, 40);
  194. if (!current.Contains("none"))
  195. return current;
  196. chunks = text.Split(
  197. new string[] { "headRevisionID" },
  198. StringSplitOptions.None);
  199. return chunks[1].Substring(3, 40);
  200. }
  201. static string GetCollabSnapshotFile(
  202. string projectPath,
  203. string projectGuid)
  204. {
  205. return projectPath + "/Library/Collab/CollabSnapshot_" + projectGuid + ".txt";
  206. }
  207. const string IS_PROJECT_MIGRATED_ALREADY_CALCULATED_KEY =
  208. "PlasticSCM.MigrateCollabProject.IsAlreadyCalculated";
  209. const int MIGRATED_NOT_CALCULATED = 0;
  210. const int MIGRATED_NOTHING_TO_DO = 1;
  211. static readonly ILog mLog = LogManager.GetLogger("MigrateCollabProject");
  212. }
  213. }