/
VisualGitPackage.SolutionProperties.cs
358 lines (301 loc) · 14.1 KB
/
VisualGitPackage.SolutionProperties.cs
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// VisualGit.Package\VisualGitPackage.SolutionProperties.cs
//
// Copyright 2008-2011 The AnkhSVN Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Changes and additions made for VisualGit Copyright 2011 Pieter van Ginkel.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.OLE.Interop;
using VisualGit.Scc;
using VisualGit.VSPackage.Attributes;
using VisualGit.VS;
using System.IO;
namespace VisualGit.VSPackage
{
[ProvideSolutionProperties(VisualGitPackage.GitPropertyCategory)]
[ProvideSolutionProperties(VisualGitId.SccStructureName)]
partial class VisualGitPackage : IVsPersistSolutionProps
{
const string GitPropertyCategory = VisualGitId.GitSccName;
const string ManagedPropertyName = "Git-Managed";
const string ManagerPropertyName = "Manager";
public int OnProjectLoadFailure(IVsHierarchy pStubHierarchy, string pszProjectName, string pszProjectMk, string pszKey)
{
IVisualGitSccService scc = GetService<IVisualGitSccService>();
if (scc != null)
scc.IsSolutionDirty = true; // We should save our settings again
return VSConstants.S_OK;
}
public bool LoadUserProperties(string streamName)
{
IVsSolutionPersistence ps = GetService<IVsSolutionPersistence>(typeof(SVsSolutionPersistence));
if (ps == null)
return false;
return ErrorHandler.Succeeded(ps.LoadPackageUserOpts((IVsPersistSolutionOpts)GetService<VisualGit.UI.IVisualGitPackage>(), streamName));
}
// Global note:
// The same trick we do here for the solution (loading the package when encountering a solution property)
// can be done on several project types using IVsProjectStartupServices
public int QuerySaveSolutionProps(IVsHierarchy pHierarchy, VSQUERYSAVESLNPROPS[] pqsspSave)
{
// This function is called by the IDE to determine if something needs to be saved in the solution.
// If the package returns that it has dirty properties, the shell will callback on SaveSolutionProps
if (pHierarchy == null)
{
// We will write solution properties only for the solution
IVisualGitSccService scc = GetService<IVisualGitSccService>();
ISccSettingsStore translate = GetService<ISccSettingsStore>();
VSQUERYSAVESLNPROPS result = VSQUERYSAVESLNPROPS.QSP_HasNoProps;
if(scc != null && translate != null)
{
if (scc.IsSolutionDirty
|| translate.IsSolutionDirty)
{
result = VSQUERYSAVESLNPROPS.QSP_HasDirtyProps;
}
else if (!scc.HasSolutionData
&& !translate.HasSolutionData)
{
result = VSQUERYSAVESLNPROPS.QSP_HasNoProps;
}
else
result = VSQUERYSAVESLNPROPS.QSP_HasNoDirtyProps;
}
pqsspSave[0] = result;
}
return VSConstants.S_OK;
}
public int SaveSolutionProps(IVsHierarchy pHierarchy, IVsSolutionPersistence pPersistence)
{
// This function gets called by the shell after QuerySaveSolutionProps returned QSP_HasDirtyProps
// The package will pass in the key under which it wants to save its properties,
// and the IDE will call back on WriteSolutionProps
// The properties will be saved in the Pre-Load section
// When the solution will be reopened, the IDE will call our package to load them back before the projects in the solution are actually open
// This could help if the source control package needs to persist information like projects translation tables, that should be read from the suo file
// and should be available by the time projects are opened and the shell start calling IVsSccEnlistmentPathTranslation functions.
if (pHierarchy == null) // Only save the property on the solution itself
{
IVisualGitSccService scc = GetService<IVisualGitSccService>();
// SavePackageSolutionProps will call WriteSolutionProps with the specified key
if (scc != null && scc.HasSolutionData)
pPersistence.SavePackageSolutionProps(1 /* TRUE */, null, this, GitPropertyCategory);
ISccSettingsStore translate = GetService<ISccSettingsStore>();
if (translate != null && translate.HasSolutionData)
pPersistence.SavePackageSolutionProps(1 /* TRUE */, null, this, VisualGitId.SccStructureName);
// Once we saved our props, the solution is not dirty anymore
scc.IsSolutionDirty = false;
}
return VSConstants.S_OK;
}
int IVsPersistSolutionProps.WriteSolutionProps(IVsHierarchy pHierarchy, string pszKey, IPropertyBag pPropBag)
{
if (pHierarchy != null)
return VSConstants.S_OK; // Not send by our code!
else if(pPropBag == null)
return VSConstants.E_POINTER;
// This method is called from the VS implementation after a request from SaveSolutionProps
ISccSettingsStore translate = GetService<ISccSettingsStore>();
IVisualGitSccService scc = GetService<IVisualGitSccService>();
using (IPropertyMap map = translate.GetMap(pPropBag))
{
switch (pszKey)
{
case GitPropertyCategory:
map.SetRawValue(ManagedPropertyName, true.ToString());
// BH: Don't localize this text! Changing it will change all solutions marked as managed by VisualGit
map.SetRawValue(ManagerPropertyName, "VisualGit - Git Support for Visual Studio");
scc.WriteSolutionProperties(map);
break;
case VisualGitId.SccStructureName:
translate.WriteSolutionProperties(map);
break;
}
}
return VSConstants.S_OK;
}
public int ReadSolutionProps(IVsHierarchy pHierarchy, string pszProjectName, string pszProjectMk, string pszKey, int fPreLoad, IPropertyBag pPropBag)
{
if (pHierarchy != null)
return VSConstants.S_OK;
ISccSettingsStore translate = GetService<ISccSettingsStore>();
IVisualGitSccService scc = GetService<IVisualGitSccService>();
using (IPropertyMap map = translate.GetMap(pPropBag))
{
bool preload = (fPreLoad != 0);
switch (pszKey)
{
case GitPropertyCategory:
if (preload)
{
string value;
bool register;
if (!map.TryGetValue(ManagedPropertyName, out value))
register = false;
else if (string.IsNullOrEmpty(value) || !bool.TryParse(value, out register))
register = false;
if (register)
{
scc.RegisterAsPrimarySccProvider();
scc.LoadingManagedSolution(true);
}
scc.ReadSolutionProperties(map);
}
break;
case VisualGitId.SccStructureName:
translate.ReadSolutionProperties(map);
break;
}
}
return VSConstants.S_OK;
}
#region IVsPersistSolutionOpts
const string SccPendingChangeStream = VisualGitId.GitSccName + "Pending";
const string SccEnlistStream = VisualGitId.GitSccName + "Enlist";
public int LoadUserOptions(IVsSolutionPersistence pPersistence, uint grfLoadOpts)
{
if ((grfLoadOpts & (uint)__VSLOADUSEROPTS.LUO_OPENEDDSW) != 0)
{
return VSConstants.S_OK; // We only know .suo; let's ignore old style projects
}
try
{
pPersistence.LoadPackageUserOpts(this, SccPendingChangeStream);
pPersistence.LoadPackageUserOpts(this, SccEnlistStream);
return VSConstants.S_OK;
}
finally
{
Marshal.ReleaseComObject(pPersistence); // See Package.cs from MPF for reason
}
}
public int ReadUserOptions([In] IStream pOptionsStream, [In] string pszKey)
{
try
{
using (ComStreamWrapper wrapper = new ComStreamWrapper(pOptionsStream, true))
{
switch (pszKey)
{
case SccPendingChangeStream:
LoadPendingChanges(wrapper);
break;
case SccEnlistStream:
IVisualGitSccService scc = GetService<IVisualGitSccService>();
if (scc != null)
scc.SerializeEnlistData(wrapper, false);
break;
default:
// TODO: Add support for some service api for our services
break;
}
}
return VSConstants.S_OK; // Our data is in Git properties
}
finally
{
Marshal.ReleaseComObject(pOptionsStream); // See Package.cs from MPF for reason
}
}
public int SaveUserOptions([In] IVsSolutionPersistence pPersistence)
{
try
{
IVisualGitSccService scc = GetService<IVisualGitSccService>();
if (scc != null)
{
if (scc.IsActive && scc.IsSolutionManaged)
{
pPersistence.SavePackageUserOpts(this, SccPendingChangeStream);
pPersistence.SavePackageUserOpts(this, SccEnlistStream);
}
}
return VSConstants.S_OK;
}
finally
{
Marshal.ReleaseComObject(pPersistence); // See Package.cs from MPF for reason
}
}
public int WriteUserOptions([In] IStream pOptionsStream, [In] string pszKey)
{
try
{
using (ComStreamWrapper wrapper = new ComStreamWrapper(pOptionsStream))
{
IVisualGitSccService scc;
switch (pszKey)
{
case SccPendingChangeStream:
WritePendingChanges(wrapper);
break;
case SccEnlistStream:
scc = GetService<IVisualGitSccService>();
if (scc != null)
scc.SerializeEnlistData(wrapper, true);
break;
default:
// TODO: Add support for some service api for our services
break;
}
}
return VSConstants.S_OK;
}
finally
{
Marshal.ReleaseComObject(pOptionsStream); // See Package.cs from MPF for reason
}
}
private void WritePendingChanges(Stream storageStream)
{
IPendingChangesManager pendingChanges = GetService<IPendingChangesManager>();
using (BinaryWriter bw = new BinaryWriter(storageStream))
{
List<PendingChange> changes = (pendingChanges != null) ? new List<PendingChange>(pendingChanges.GetAll()) : null;
if (changes == null)
bw.Write((int)0);
else
{
bw.Write((int)changes.Count);
foreach (PendingChange pc in changes)
{
bw.Write(pc.FullPath);
}
}
}
}
private void LoadPendingChanges(Stream storageStream)
{
IFileStatusMonitor monitor = GetService<IFileStatusMonitor>();
if(monitor == null)
return;
using (BinaryReader br = new BinaryReader(storageStream))
{
int n = br.ReadInt32();
List<string> files = new List<string>();
for(int i = 0; i < n; i++)
{
files.Add(br.ReadString());
}
monitor.ScheduleMonitor(files);
}
}
#endregion
}
}