Repository navigation
Expand file tree
/
Copy pathSolutionGeneration.cs
More file actions
370 lines (322 loc) · 13.4 KB
/
Copy pathSolutionGeneration.cs
File metadata and controls
370 lines (322 loc) · 13.4 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
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
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using FileList = System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>>;
using StringPair = System.Collections.Generic.KeyValuePair<string, object>;
namespace Microsoft.CodeAnalysis.UnitTests
{
/// <summary>
/// Flexible and extensible API to generate MSBuild projects and solutions without external files or resources.
/// </summary>
public static class SolutionGeneration
{
public const string NS = "http://schemas.microsoft.com/developer/msbuild/2003";
private const string CSharpProjectTemplate =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<Project ToolsVersion=""12.0"" DefaultTargets=""Build"" xmlns=""http://schemas.microsoft.com/developer/msbuild/2003"">
<Import Project=""$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props"" Condition=""Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')"" />
<PropertyGroup>
<Configuration Condition="" '$(Configuration)' == '' "">Debug</Configuration>
<Platform Condition="" '$(Platform)' == '' "">AnyCPU</Platform>
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "">
</PropertyGroup>
<PropertyGroup Condition="" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "">
</PropertyGroup>
<Import Project=""$(MSBuildToolsPath)\Microsoft.CSharp.targets"" />
</Project>";
private const string SolutionTemplate =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.30110.0
MinimumVisualStudioVersion = 10.0.40219.1
{0}Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
public const string PublicKey = "00240000048000009400000006020000002400005253413100040000010001003bb5de1b79bee9bf5ba44bdb42974c6f40fdc4b329c8e1b833fa798cf0859529485b2bfc359a08e16f025fe57efd293c4dc3541cb2e0929b1c4a92db87eed7a9454dbd08beb7c7308941384b3bfb088de781b51caef23677f8f6defb671e97e1fc5e0979858e52828c86aca1d4ea1797f1f1254bf64073a28e5be520d5397fb0";
public const string PublicKeyToken = "39d7e8ec38707fde";
public static readonly byte[] KeySnk = MSBuildWorkspaceTests.GetResourceBytes("key.snk");
public static FileList GetSolutionFiles(params IBuilder[] inputs)
{
List<StringPair> list = new List<StringPair>();
var projectBuilders = inputs.OfType<ProjectBuilder>();
var files = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
int fileIndex = 1;
int projectIndex = 1;
// first make sure all projects have names, as a separate loop
foreach (var project in projectBuilders)
{
if (project.Name == null)
{
project.Name = "Project" + projectIndex;
projectIndex++;
}
}
foreach (var project in projectBuilders)
{
foreach (var document in project.Documents)
{
if (document.FilePath == null)
{
document.FilePath = "Document" + fileIndex + (project.Language == LanguageNames.VisualBasic ? ".vb" : ".cs");
fileIndex++;
}
}
foreach (var projectReference in project.ProjectReferences)
{
if (projectReference.Guid == Guid.Empty)
{
var referencedProject = projectBuilders.First(p => p.Name == projectReference.ProjectName);
projectReference.Guid = referencedProject.Guid;
projectReference.ProjectFileName = referencedProject.Name + referencedProject.Extension;
}
}
foreach (var kvp in project.Files)
{
if (files.Add(kvp.Key + kvp.Value))
{
list.Add(new StringPair(kvp.Key, kvp.Value));
}
}
}
list.Add(new KeyValuePair<string, object>("Solution.sln", GetSolutionContent(projectBuilders)));
return list;
}
public static IBuilder Project(params IBuilder[] inputs)
{
var projectReferences = inputs.OfType<ProjectReferenceBuilder>();
var documents = inputs.OfType<DocumentBuilder>().ToList();
var projectName = inputs.OfType<ProjectNameBuilder>().FirstOrDefault();
var properties = inputs.OfType<PropertyBuilder>().ToList();
var sign = inputs.OfType<SignBuilder>();
if (sign != null)
{
properties.Add((PropertyBuilder)Property("SignAssembly", "true"));
properties.Add((PropertyBuilder)Property("AssemblyOriginatorKeyFile", "key.snk"));
documents.Add((DocumentBuilder)Document(KeySnk, "key.snk", "None"));
}
return new ProjectBuilder
{
Name = projectName != null ? projectName.Name : null,
Documents = documents,
ProjectReferences = projectReferences,
Properties = properties
};
}
public static IBuilder ProjectReference(string projectName)
{
return new ProjectReferenceBuilder
{
ProjectName = projectName
};
}
public static IBuilder ProjectName(string projectName)
{
return new ProjectNameBuilder
{
Name = projectName
};
}
public static IBuilder Property(string propertyName, string propertyValue)
{
return new PropertyBuilder
{
Name = propertyName,
Value = propertyValue
};
}
public static IBuilder Sign
{
get
{
return new SignBuilder();
}
}
public static IBuilder Document(
object content = null,
string filePath = null,
string itemType = "Compile")
{
return new DocumentBuilder
{
FilePath = filePath,
Content = content,
ItemType = itemType
};
}
private static string GetSolutionContent(IEnumerable<ProjectBuilder> projects)
{
var sb = new StringBuilder();
foreach (var project in projects)
{
var fileName = project.Name + project.Extension;
var languageGuid = project.Language == LanguageNames.VisualBasic ?
"{F184B08F-C81C-45F6-A57F-5ABD9991F28F}" :
"{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
sb.AppendLine(
string.Format(
@"Project(""{0}"") = ""{1}"", ""{2}"", ""{3}""",
languageGuid,
project.Name,
fileName,
project.Guid.ToString("B")));
sb.AppendLine("EndProject");
}
return string.Format(SolutionTemplate, sb.ToString());
}
public interface IBuilder
{
}
private class ProjectBuilder : IBuilder
{
public string Name { get; set; }
public string Language { get; set; }
public Guid Guid { get; set; }
public string OutputType { get; set; }
public string OutputPath { get; set; }
public IEnumerable<DocumentBuilder> Documents { get; set; }
public IEnumerable<ProjectReferenceBuilder> ProjectReferences { get; set; }
public IEnumerable<PropertyBuilder> Properties { get; set; }
public string Extension
{
get
{
return Language == LanguageNames.VisualBasic ? ".vbproj" : ".csproj";
}
}
public FileList Files
{
get
{
foreach (var document in Documents)
{
yield return new StringPair(document.FilePath, document.Content);
}
yield return new StringPair(Name + Extension, GetProjectContent());
}
}
private string GetProjectContent()
{
if (Language == LanguageNames.VisualBasic)
{
throw new NotImplementedException("Need VB support");
}
if (Guid == Guid.Empty)
{
Guid = Guid.NewGuid();
}
if (string.IsNullOrEmpty(OutputType))
{
OutputType = "Library";
}
if (string.IsNullOrEmpty(OutputPath))
{
OutputPath = ".";
}
var document = XDocument.Parse(CSharpProjectTemplate);
var propertyGroup = document.Root.Descendants(XName.Get("PropertyGroup", NS)).First();
AddXElement(propertyGroup, "ProjectGuid", Guid.ToString("B"));
AddXElement(propertyGroup, "OutputType", OutputType);
AddXElement(propertyGroup, "OutputPath", OutputPath);
AddXElement(propertyGroup, "AssemblyName", Name);
if (Properties != null)
{
foreach (var property in Properties)
{
AddXElement(propertyGroup, property.Name, property.Value);
}
}
var importTargets = document.Root.Elements().Last();
if (ProjectReferences != null && ProjectReferences.Any())
{
AddItemGroup(
importTargets,
_ => "ProjectReference",
ProjectReferences,
i => i.ProjectFileName,
(projectReference, xmlElement) =>
{
if (projectReference.Guid != Guid.Empty)
{
AddXElement(xmlElement, "Project", projectReference.Guid.ToString("B"));
}
AddXElement(xmlElement, "Name", Path.GetFileNameWithoutExtension(projectReference.ProjectName));
});
}
if (Documents != null)
{
AddItemGroup(
importTargets,
i => i.ItemType,
Documents,
i => i.FilePath);
}
return document.ToString();
}
private void AddItemGroup<T>(
XElement addBefore,
Func<T, string> itemTypeSelector,
IEnumerable<T> items,
Func<T, string> attributeValueGetter,
Action<T, XElement> elementModifier = null)
{
var itemGroup = CreateXElement("ItemGroup");
addBefore.AddBeforeSelf(itemGroup);
foreach (var item in items)
{
var itemElement = CreateXElement(itemTypeSelector(item));
itemElement.SetAttributeValue("Include", attributeValueGetter(item));
if (elementModifier != null)
{
elementModifier(item, itemElement);
}
itemGroup.Add(itemElement);
}
}
private XElement CreateXElement(string name)
{
return new XElement(XName.Get(name, NS));
}
private void AddXElement(XElement element, string elementName, string elementValue)
{
element.Add(new XElement(XName.Get(elementName, NS), elementValue));
}
}
private class ProjectReferenceBuilder : IBuilder
{
public string ProjectName { get; set; }
public Guid Guid { get; set; }
public string ProjectFileName { get; set; }
}
private class ProjectNameBuilder : IBuilder
{
public string Name { get; set; }
}
private class PropertyBuilder : IBuilder
{
public string Name { get; set; }
public string Value { get; set; }
}
private class SignBuilder : IBuilder { }
private class DocumentBuilder : IBuilder
{
public string FilePath { get; set; }
public object Content { get; set; }
public string ItemType { get; set; }
}
}
}