-
Notifications
You must be signed in to change notification settings - Fork 17
/
ConvertVSProjects.cs
356 lines (305 loc) · 15.7 KB
/
ConvertVSProjects.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Windows.Forms;
using System.Xml.Linq;
namespace ProjectConverter
{
public class ConvertVSProjects
{
/// <summary>
/// Makes a backup copy of the existing Visual Studio project file
/// </summary>
/// <param name="FileName"></param>
/// <param name="ExistingVersion"></param>
public static void MakeBackup(string FileName, double ExistingVersion)
{
string Backup = string.Empty;
Dictionary<double, string> dictVSVersionString = new Dictionary<double, string>();
//Get the list of available Visual Studio version numbers
dictVSVersionString = VSUtils.PopulateVSVersionString();
if (dictVSVersionString.ContainsKey(ExistingVersion))
{
string strVSVersion = dictVSVersionString[ExistingVersion];
Backup = Path.Combine(Path.GetDirectoryName(FileName), string.Format("{0}{1}{2}", Path.GetFileNameWithoutExtension(FileName), strVSVersion, Path.GetExtension(FileName)));
}//if
else
{
// not likely
MessageBox.Show("Not a supported version", "Internal Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
File.Copy(FileName, Backup, true);
}
#region Project Conversion Methods
#region Convert Visual C# and VB.Net projects
/// <summary>
/// Convert VB.Net and C# Visual Studio project files
/// </summary>
/// <param name="ProjFile"></param>
/// <param name="ConvertTo"></param>
/// <param name="blnRemoveSccBindings"></param>
/// <returns></returns>
public static bool ConvertProject(string ProjFile, Versions ConvertTo, bool blnRemoveSccBindings = false)
{
//Declare the base namespace for Visual Studio project files
const string VSProjNamespace = "http://schemas.microsoft.com/developer/msbuild/2003";
const string TargetFrameworkNode = "TargetFrameworkVersion";
const string PropertyGroupNode = "PropertyGroup";
const string ToolsNode = "ToolsVersion";
const string ProductVersionNode = "ProductVersion";
const string OldToolsVersionNode = "OldToolsVersion";
//Get all of the settings for the appropriate version of Visual Studio
var vsProjectVersionInfo = VSProjectCreator.VSProjectFactory(ConvertTo);
//Read in the settings from the existing Visual Studio project file
VSProjectInfo vsProjectInfo = new VSProjectInfo(ProjFile);
//Load the Xml into a XElement
XElement xmlVSProjFile = XElement.Load(ProjFile);
//Declare the namespace for the Visual Studio project file
XNamespace xns = VSProjNamespace;
//Query for all of the necessary elements
var oldToolsElement = GetVSProjElement(xmlVSProjFile, OldToolsVersionNode);
//Use a Linq query to obtain the TargetFrameworkVersion
var targetFrameworkElement = GetVSProjElement(xmlVSProjFile, TargetFrameworkNode);
var productVersionElement = GetVSProjElement(xmlVSProjFile, ProductVersionNode);
var toolsVersionAttrib = xmlVSProjFile.Attribute("ToolsVersion");
//Get the 1st PropertyGroup in the project
var propGroup = xmlVSProjFile.Element(xns + PropertyGroupNode);
//Make sure that the root element of the file is a Visual Studio project file
if (xmlVSProjFile.Name.LocalName.Equals("Project"))
{
//TODO: Re-analyze this logic since it is not true in all cases, esp. VS 2012
if (toolsVersionAttrib != null)
{
// converting to VS2008, but project is already at VS2008
if (ConvertTo == Versions.Version9 && toolsVersionAttrib.Value.Equals(vsProjectVersionInfo.ToolsVersion))
{
// exit quietly
return false;
}
// converting to VS2010, but project is already at VS2010
if (ConvertTo == Versions.Version10 && toolsVersionAttrib.Value.Equals(vsProjectVersionInfo.ToolsVersion))
{
// exit quietly
return false;
}//if
}
else
{
// If converting to VS2005, but project is already at VS2005
if (ConvertTo == Versions.Version8)
{
// exit quietly
return false;
}
}
}
else
{
// no such node? That's bad, very bad...
throw new ApplicationException("Invalid project file");
}
// the OldToolsVersion element in the first PropertyGoup
string strOldToolsVersion = string.Empty;
//Local variable for storing the existing Target Framework Version
var existingTargetFrameworkVersion = string.Empty;
switch (ConvertTo)
{
case Versions.Version8:
// it gets removed
xmlVSProjFile.Attribute(ToolsNode).Remove();
//OldToolsVersion
oldToolsElement.Remove();
//TargetFrameworkVersion
targetFrameworkElement.Remove();
//Product Version
//Product Version element does not exist in VS 2012
if (productVersionElement.Count() == 0)
{
propGroup.Add(new XElement(xns + ProductVersionNode, Settings.Default.VS2005_Version));
}
else if (productVersionElement.Count() > 0)
{
productVersionElement.ElementAtOrDefault(0).Value = Settings.Default.VS2005_Version;
}
break;
case Versions.Version9:
if (toolsVersionAttrib != null)
{
xmlVSProjFile.SetAttributeValue(ToolsNode, vsProjectVersionInfo.ToolsVersion);
}//if
else
{
// add the attribute
xmlVSProjFile.Add(new XAttribute(ToolsNode, vsProjectVersionInfo.ToolsVersion));
}
//Product Version element does not exist in VS 2012
if (productVersionElement.Count() == 0)
{
propGroup.Add(new XElement(xns + ProductVersionNode, vsProjectVersionInfo.ProductVersion));
}
else if (productVersionElement.Count() > 0)
{
productVersionElement.ElementAtOrDefault(0).Value = vsProjectVersionInfo.ProductVersion;
}
// add a new element
//Use the coalescing operator to determine which value to use
strOldToolsVersion = vsProjectInfo.OldToolsVersion ?? vsProjectVersionInfo.OldToolsVersion;
if (oldToolsElement.ElementAtOrDefault(0) != null)
{
oldToolsElement.ElementAtOrDefault(0).Value = strOldToolsVersion;
}//if
else
{
propGroup.Add(new XElement(xns + OldToolsVersionNode, strOldToolsVersion));
}//else
existingTargetFrameworkVersion =
vsProjectVersionInfo.CheckFrameworkVersion(vsProjectInfo.TargetFrameworkVersion);
//Set the target Framework Version
if (targetFrameworkElement.ElementAtOrDefault(0) != null)
{
targetFrameworkElement.ElementAtOrDefault(0).Value = existingTargetFrameworkVersion;
}//if
else
{
propGroup.Add(new XElement(xns + TargetFrameworkNode, existingTargetFrameworkVersion));
}//else
break;
case Versions.Version10:
if (toolsVersionAttrib != null)
{
xmlVSProjFile.SetAttributeValue(ToolsNode, vsProjectInfo.ToolsVersion);
}//if
else
{
// add the attribute
xmlVSProjFile.Add(new XAttribute(ToolsNode, vsProjectVersionInfo.ToolsVersion));
}
if (productVersionElement.Count() > 0)
{
//Retain the product version if it exists, otherwise it is not needed for VS 2010 Support
productVersionElement.ElementAtOrDefault(0).Value = vsProjectVersionInfo.ProductVersion;
}//if
//Use the coalescing operator to determine which value to use
strOldToolsVersion = vsProjectInfo.OldToolsVersion ?? vsProjectVersionInfo.OldToolsVersion;
if (oldToolsElement.ElementAtOrDefault(0) != null)
{
oldToolsElement.ElementAtOrDefault(0).Value = strOldToolsVersion;
}//if
else
{
propGroup.Add(new XElement(xns + OldToolsVersionNode, strOldToolsVersion));
}//else
existingTargetFrameworkVersion = vsProjectVersionInfo.CheckFrameworkVersion(vsProjectInfo.TargetFrameworkVersion);
//Set the target Framework Version
if (targetFrameworkElement.ElementAtOrDefault(0) != null)
{
targetFrameworkElement.ElementAtOrDefault(0).Value = existingTargetFrameworkVersion;
}//if
else
{
propGroup.Add(new XElement(xns + TargetFrameworkNode, existingTargetFrameworkVersion));
}//else
break;
case Versions.Version11:
if (toolsVersionAttrib != null)
{
xmlVSProjFile.SetAttributeValue(ToolsNode, vsProjectVersionInfo.ToolsVersion);
}//if
else
{
// add the attribute
xmlVSProjFile.Add(new XAttribute(ToolsNode, vsProjectVersionInfo.ToolsVersion));
}
//Product Version information is no longer used in VS 2012
productVersionElement.Remove();
//Use the coalescing operator to determine which value to use
strOldToolsVersion = vsProjectInfo.OldToolsVersion ?? vsProjectVersionInfo.OldToolsVersion;
if (oldToolsElement.ElementAtOrDefault(0) != null)
{
oldToolsElement.ElementAtOrDefault(0).Value = strOldToolsVersion;
}//if
else
{
propGroup.Add(new XElement(xns + OldToolsVersionNode, strOldToolsVersion));
}//else
existingTargetFrameworkVersion = vsProjectVersionInfo.CheckFrameworkVersion(vsProjectInfo.TargetFrameworkVersion);
//Set the target Framework Version
if (targetFrameworkElement.ElementAtOrDefault(0) != null)
{
targetFrameworkElement.ElementAtOrDefault(0).Value = existingTargetFrameworkVersion;
}//if
else
{
propGroup.Add(new XElement(xns + TargetFrameworkNode, existingTargetFrameworkVersion));
}//else
break;
}//switch
// The MSBuildToolsPath vs MSBuildBinPath environmental variable. Oddly enough a fully patched VS2005
// uses the newer MSBuildToolsPath. So, this should only be required if you don't have VS2005 SP1 installed.
// However, I can't detect that, so we take the worst case scenario, and use the older version
var vsImportElement = from vsImportElements in xmlVSProjFile.Elements()
where vsImportElements.Name.LocalName.StartsWith("Import")
&& vsImportElements.FirstAttribute.Name.LocalName.Equals("Project")
&& vsImportElements.FirstAttribute.Value.StartsWith("$(MS")
select vsImportElements;
//Project should always be the first attribute for the Import element
var msBuildPathValue = vsImportElement.ElementAtOrDefault(0).FirstAttribute.Value;
string strMSBuildPath = string.Empty;
if (ConvertTo >= Versions.Version9)
{
// convert it to the newer MSBuildToolsPath
if (msBuildPathValue.Contains("MSBuildBinPath"))
{
strMSBuildPath = msBuildPathValue.Replace("MSBuildBinPath", "MSBuildToolsPath");
vsImportElement.ElementAtOrDefault(0).FirstAttribute.Value = strMSBuildPath;
}//if
}//if
else
{
// convert it to the older MSBuildBinPath
if (msBuildPathValue.Contains("MSBuildToolsPath"))
{
strMSBuildPath = msBuildPathValue.Replace("MSBuildToolsPath", "MSBuildBinPath");
vsImportElement.ElementAtOrDefault(0).FirstAttribute.Value = strMSBuildPath;
}//if
}//else
//Check if there is a requirement to remove the Scc Bindings
//from the Visual Studio project file
if (blnRemoveSccBindings)
{
//Obtain a handle to the Source Control elements in the Visual Studio project file
var sccProjElement =
from sccElements in xmlVSProjFile.Elements(xns + "PropertyGroup").Elements()
where sccElements.Name.LocalName.StartsWith("Scc")
select sccElements;
//Remove the SccElements from the collection
sccProjElement.Remove();
}//if
//Attempt to save the changes back to the Visual Studio file
try
{
//Save the changes back to the Visual Studio project file
xmlVSProjFile.Save(ProjFile);
}//try
catch (UnauthorizedAccessException ex)
{
FileOps.RemoveReadOnlyFlag(ProjFile);
//Save the changes back to the Visual Studio project file
xmlVSProjFile.Save(ProjFile);
} // catch
return true;
}
private static IEnumerable<XElement> GetVSProjElement(XElement xmlVSProjFile, string vsProjNode)
{
const string PropertyGroupNode = "PropertyGroup";
XNamespace xns = "http://schemas.microsoft.com/developer/msbuild/2003";
var vsProjElement = from vsProjElements in xmlVSProjFile.Elements(xns + PropertyGroupNode).Elements()
where vsProjElements.Name.LocalName.StartsWith(vsProjNode)
select vsProjElements;
return vsProjElement;
}
#endregion
#endregion
}
}