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