-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathSolutionGenerator.cs
561 lines (492 loc) · 24.2 KB
/
SolutionGenerator.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
using Microsoft.SourceBrowser.Common;
namespace Microsoft.SourceBrowser.HtmlGenerator
{
public partial class SolutionGenerator : IDisposable
{
public string SolutionSourceFolder { get; private set; }
public string SolutionDestinationFolder { get; private set; }
public string ProjectFilePath { get; private set; }
public IReadOnlyDictionary<string, string> ServerPathMappings { get; set; }
private Federation Federation { get; set; }
public IEnumerable<string> PluginBlacklist { get; private set; }
private readonly HashSet<string> typeScriptFiles = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
public MEF.PluginAggregator PluginAggregator;
/// <summary>
/// List of all assembly names included in the index, from all solutions
/// </summary>
public HashSet<string> GlobalAssemblyList { get; set; }
private Solution solution;
private Workspace workspace;
public SolutionGenerator(
string solutionFilePath,
string solutionDestinationFolder,
ImmutableDictionary<string, string> properties = null,
Federation federation = null,
IReadOnlyDictionary<string, string> serverPathMappings = null,
IEnumerable<string> pluginBlacklist = null,
bool doNotIncludeReferencedProjects = false)
{
this.SolutionSourceFolder = Path.GetDirectoryName(solutionFilePath);
this.SolutionDestinationFolder = solutionDestinationFolder;
this.ProjectFilePath = solutionFilePath;
ServerPathMappings = serverPathMappings;
this.solution = CreateSolution(solutionFilePath, properties, doNotIncludeReferencedProjects);
this.Federation = federation ?? new Federation();
this.PluginBlacklist = pluginBlacklist ?? Enumerable.Empty<string>();
if (LoadPlugins)
{
SetupPluginAggregator();
}
}
public static bool LoadPlugins { get; set; }
public static bool ExcludeTests { get; set; }
private void SetupPluginAggregator()
{
if (!LoadPlugins)
{
return;
}
var settings = System.Configuration.ConfigurationManager.AppSettings;
var configs = settings
.AllKeys
.Where(k => k.Contains(':')) //Ignore keys that don't have a colon to indicate which plugin they go to
.Select(k => Tuple.Create(k.Split(':'), settings[k])) //Get the data -- split the key to get the plugin name and setting name, look up the key to get the value
.GroupBy(t => t.Item1[0]) //Group the settings based on which plugin they're for
.ToDictionary(
group => group.Key, //Index the outer dictionary based on plugin
group => group.ToDictionary(
t => t.Item1[1], //Index the inner dictionary based on setting name
t => t.Item2 //The actual value of the setting
)
);
PluginAggregator = new MEF.PluginAggregator(configs, new Utilities.PluginLogger(), PluginBlacklist);
FirstChanceExceptionHandler.IgnoreModules(PluginAggregator.Select(p => p.PluginModule));
PluginAggregator.Init();
}
public SolutionGenerator(
string projectFilePath,
string commandLineArguments,
string outputAssemblyPath,
string solutionSourceFolder,
string solutionDestinationFolder)
{
this.ProjectFilePath = projectFilePath;
string projectName = Path.GetFileNameWithoutExtension(projectFilePath);
string language = projectFilePath.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase) ?
LanguageNames.VisualBasic : LanguageNames.CSharp;
this.SolutionSourceFolder = solutionSourceFolder;
this.SolutionDestinationFolder = solutionDestinationFolder;
string projectSourceFolder = Path.GetDirectoryName(projectFilePath);
SetupPluginAggregator();
this.solution = CreateSolution(
commandLineArguments,
projectName,
language,
projectSourceFolder,
outputAssemblyPath);
}
public IEnumerable<string> GetAssemblyNames()
{
if (solution != null)
{
return solution.Projects.Select(p => p.AssemblyName);
}
else
{
return Enumerable.Empty<string>();
}
}
private static MSBuildWorkspace CreateWorkspace(ImmutableDictionary<string, string> propertiesOpt = null)
{
propertiesOpt = propertiesOpt ?? ImmutableDictionary<string, string>.Empty;
// Explicitly add "CheckForSystemRuntimeDependency = true" property to correctly resolve facade references.
// See https://github.com/dotnet/roslyn/issues/560
propertiesOpt = propertiesOpt.Add("CheckForSystemRuntimeDependency", "true");
propertiesOpt = propertiesOpt.Add("VisualStudioVersion", "15.0");
propertiesOpt = propertiesOpt.Add("AlwaysCompileMarkupFilesInSeparateDomain", "false");
var w = MSBuildWorkspace.Create(properties: propertiesOpt);
w.LoadMetadataForReferencedProjects = true;
w.AssociateFileExtensionWithLanguage("depproj", LanguageNames.CSharp);
return w;
}
private static Solution CreateSolution(
string commandLineArguments,
string projectName,
string language,
string projectSourceFolder,
string outputAssemblyPath)
{
var workspace = CreateWorkspace();
var projectInfo = CommandLineProject.CreateProjectInfo(
projectName,
language,
commandLineArguments,
projectSourceFolder,
workspace);
var solution = workspace.CurrentSolution.AddProject(projectInfo);
solution = RemoveNonExistingFiles(solution);
solution = AddAssemblyAttributesFile(language, outputAssemblyPath, solution);
solution = DisambiguateSameNameLinkedFiles(solution);
solution = DeduplicateProjectReferences(solution);
solution.Workspace.WorkspaceFailed += WorkspaceFailed;
return solution;
}
private static Solution DisambiguateSameNameLinkedFiles(Solution solution)
{
foreach (var projectId in solution.ProjectIds.ToArray())
{
var project = solution.GetProject(projectId);
solution = DisambiguateSameNameLinkedFiles(project);
}
return solution;
}
/// <summary>
/// If there are two linked files both outside the project cone, and they have same names,
/// they will logically appear as the same file in the project root. To disambiguate, we
/// remove both files from the project's root and re-add them each into a folder chain that
/// is formed from the full path of each document.
/// </summary>
private static Solution DisambiguateSameNameLinkedFiles(Project project)
{
var nameMap = project.Documents.Where(d => !d.Folders.Any()).ToLookup(d => d.Name);
foreach (var conflictedItemGroup in nameMap.Where(g => g.Count() > 1))
{
foreach (var conflictedDocument in conflictedItemGroup)
{
project = project.RemoveDocument(conflictedDocument.Id);
string filePath = conflictedDocument.FilePath;
DocumentId newId = DocumentId.CreateNewId(project.Id, filePath);
var folders = filePath.Split('\\').Select(p => p.TrimEnd(':'));
project = project.Solution.AddDocument(
newId,
conflictedDocument.Name,
conflictedDocument.GetTextAsync().Result,
folders,
filePath).GetProject(project.Id);
}
}
return project.Solution;
}
private static Solution RemoveNonExistingFiles(Solution solution)
{
foreach (var projectId in solution.ProjectIds.ToArray())
{
var project = solution.GetProject(projectId);
solution = RemoveNonExistingDocuments(project);
project = solution.GetProject(projectId);
solution = RemoveNonExistingReferences(project);
}
return solution;
}
private static Solution RemoveNonExistingDocuments(Project project)
{
foreach (var documentId in project.DocumentIds.ToArray())
{
var document = project.GetDocument(documentId);
if (!File.Exists(document.FilePath))
{
Log.Message("Document doesn't exist on disk: " + document.FilePath);
project = project.RemoveDocument(documentId);
}
}
return project.Solution;
}
private static Solution RemoveNonExistingReferences(Project project)
{
foreach (var metadataReference in project.MetadataReferences.ToArray())
{
if (!File.Exists(metadataReference.Display))
{
Log.Message("Reference assembly doesn't exist on disk: " + metadataReference.Display);
project = project.RemoveMetadataReference(metadataReference);
}
}
return project.Solution;
}
private static Solution AddAssemblyAttributesFile(string language, string outputAssemblyPath, Solution solution)
{
if (!File.Exists(outputAssemblyPath))
{
Log.Exception("AddAssemblyAttributesFile: assembly doesn't exist: " + outputAssemblyPath);
return solution;
}
var assemblyAttributesFileText = MetadataReading.GetAssemblyAttributesFileText(
assemblyFilePath: outputAssemblyPath,
language: language);
if (assemblyAttributesFileText != null)
{
var extension = language == LanguageNames.CSharp ? ".cs" : ".vb";
var newAssemblyAttributesDocumentName = MetadataAsSource.GeneratedAssemblyAttributesFileName + extension;
var existingAssemblyAttributesFileName = "AssemblyAttributes" + extension;
var project = solution.Projects.First();
if (project.Documents.All(d => d.Name != existingAssemblyAttributesFileName || d.Folders.Count != 0))
{
var document = project.AddDocument(
newAssemblyAttributesDocumentName,
assemblyAttributesFileText,
filePath: newAssemblyAttributesDocumentName);
solution = document.Project.Solution;
}
}
return solution;
}
private static Solution DeduplicateProjectReferences(Solution solution)
{
foreach (var projectId in solution.ProjectIds.ToArray())
{
var project = solution.GetProject(projectId);
var distinctProjectReferences = project.AllProjectReferences.Distinct().ToArray();
if (distinctProjectReferences.Length < project.AllProjectReferences.Count)
{
var duplicates = project.AllProjectReferences.GroupBy(p => p).Where(g => g.Count() > 1).Select(g => g.Key).ToArray();
foreach (var duplicate in duplicates)
{
Log.Write($"Duplicate project reference to {duplicate.ProjectId.ToString()} in project: {project.Name}", ConsoleColor.Yellow);
}
var newProject = project.WithProjectReferences(distinctProjectReferences);
solution = newProject.Solution;
}
}
return solution;
}
public static string CurrentAssemblyName = null;
/// <returns>true if only part of the solution was processed and the method needs to be called again, false if all done</returns>
public bool Generate(HashSet<string> processedAssemblyList = null, Folder<ProjectSkeleton> solutionExplorerRoot = null)
{
if (solution == null)
{
// we failed to open the solution earlier; just return
Log.Message("Solution is null: " + this.ProjectFilePath);
return false;
}
var allProjects = solution.Projects.ToArray();
if (allProjects.Length == 0)
{
Log.Exception("Solution " + this.ProjectFilePath + " has 0 projects - this is suspicious");
}
var projectsToProcess = allProjects
.Where(p => processedAssemblyList == null || processedAssemblyList.Add(p.AssemblyName))
.Where(p => !ExcludeTests || !IsTestProject(p))
.ToArray();
var currentBatch = projectsToProcess
.ToArray();
foreach (var project in currentBatch)
{
try
{
CurrentAssemblyName = project.AssemblyName;
var generator = new ProjectGenerator(this, project);
generator.Generate().GetAwaiter().GetResult();
File.AppendAllText(Paths.ProcessedAssemblies, project.AssemblyName + Environment.NewLine, Encoding.UTF8);
}
finally
{
CurrentAssemblyName = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
new TypeScriptSupport().Generate(typeScriptFiles, SolutionDestinationFolder);
AddProjectsToSolutionExplorer(
solutionExplorerRoot,
currentBatch);
return currentBatch.Length < projectsToProcess.Length;
}
private static bool IsTestProject(Project proj)
{
return
proj.MetadataReferences.Any(mdr =>
{
var peRef = mdr as PortableExecutableReference;
return
IsTestProject(peRef, "xunit.core.dll") ||
IsTestProject(peRef, "nunit.framework.dll") ||
IsTestProject(peRef, "Microsoft.VisualStudio.TestPlatform.TestFramework.dll");
}) ||
IsTestProject(proj, "xunit") ||
IsTestProject(proj, "nunit") ||
IsTestProject(proj, "MSTest.TestFramework");
}
private static IEnumerable<string> GetPackageRefs(Project proj)
{
var projRoot = XElement.Load(proj.FilePath);
var packageRefs = projRoot.Elements()
.Where(elem => elem.Name.LocalName == "ItemGroup")
.SelectMany(elem => elem.Elements())
.Where(elem => elem.Name.LocalName == "PackageReference")
.Select(elem => (string)elem.Attribute("Include"));
return packageRefs;
}
private static bool IsTestProject(Project proj, string marker)
{
return GetPackageRefs(proj).Any(pr => string.Equals(pr, marker, StringComparison.InvariantCultureIgnoreCase));
}
private static bool IsTestProject(PortableExecutableReference peRef, string marker)
{
return peRef?.FilePath.EndsWith(marker, StringComparison.InvariantCultureIgnoreCase) ?? false;
}
private void SetFieldValue(object instance, string fieldName, object value)
{
var type = instance.GetType();
var fieldInfo = type.GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
fieldInfo.SetValue(instance, null);
}
public void GenerateExternalReferences(HashSet<string> assemblyList)
{
var externalReferences = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var project in solution.Projects)
{
var references = project.MetadataReferences
.OfType<PortableExecutableReference>()
.Where(m => File.Exists(m.FilePath) &&
!assemblyList.Contains(Path.GetFileNameWithoutExtension(m.FilePath)) &&
!IsPartOfSolution(Path.GetFileNameWithoutExtension(m.FilePath)) &&
GetExternalAssemblyIndex(Path.GetFileNameWithoutExtension(m.FilePath)) == -1
)
.Select(m => Path.GetFullPath(m.FilePath));
foreach (var reference in references)
{
externalReferences[Path.GetFileNameWithoutExtension(reference)] = reference;
}
}
foreach (var externalReference in externalReferences)
{
Log.Write(externalReference.Key, ConsoleColor.Magenta);
var solutionGenerator = new SolutionGenerator(
externalReference.Value,
Paths.SolutionDestinationFolder,
pluginBlacklist: PluginBlacklist);
solutionGenerator.Generate(assemblyList);
}
}
public bool IsPartOfSolution(string assemblyName)
{
if (GlobalAssemblyList == null)
{
// if for some reason we don't know a global list, assume everything is in the solution
// this is better than the alternative
return true;
}
return GlobalAssemblyList.Contains(assemblyName);
}
public int GetExternalAssemblyIndex(string assemblyName)
{
if (Federation == null)
{
return -1;
}
return Federation.GetExternalAssemblyIndex(assemblyName);
}
private Solution CreateSolution(string solutionFilePath, ImmutableDictionary<string, string> properties = null, bool doNotIncludeReferencedProjects = false)
{
try
{
Solution solution = null;
if (solutionFilePath.EndsWith(".sln", StringComparison.OrdinalIgnoreCase))
{
properties = AddSolutionProperties(properties, solutionFilePath);
var workspace = CreateWorkspace(properties);
workspace.SkipUnrecognizedProjects = true;
workspace.WorkspaceFailed += WorkspaceFailed;
solution = workspace.OpenSolutionAsync(solutionFilePath).GetAwaiter().GetResult();
solution = DeduplicateProjectReferences(solution);
this.workspace = workspace;
}
else if (
solutionFilePath.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase) ||
solutionFilePath.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase))
{
var workspace = CreateWorkspace(properties);
workspace.WorkspaceFailed += WorkspaceFailed;
solution = workspace.OpenProjectAsync(solutionFilePath).GetAwaiter().GetResult().Solution;
solution = DeduplicateProjectReferences(solution);
if (doNotIncludeReferencedProjects)
{
var keepPrimaryProject = solution.Projects.First(p => string.Equals(p.FilePath, solutionFilePath, StringComparison.OrdinalIgnoreCase));
foreach (var projectIdToRemove in solution.ProjectIds.Where(id => id != keepPrimaryProject.Id).ToArray())
{
solution = solution.RemoveProject(projectIdToRemove);
}
}
this.workspace = workspace;
}
else if (
solutionFilePath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) ||
solutionFilePath.EndsWith(".winmd", StringComparison.OrdinalIgnoreCase) ||
solutionFilePath.EndsWith(".netmodule", StringComparison.OrdinalIgnoreCase))
{
solution = MetadataAsSource.LoadMetadataAsSourceSolution(solutionFilePath);
if (solution != null)
{
solution.Workspace.WorkspaceFailed += WorkspaceFailed;
workspace = solution.Workspace;
}
}
return solution;
}
catch (Exception ex)
{
Log.Exception(ex, "Failed to open solution: " + solutionFilePath);
return null;
}
}
private ImmutableDictionary<string, string> AddSolutionProperties(ImmutableDictionary<string, string> properties, string solutionFilePath)
{
// http://referencesource.microsoft.com/#MSBuildFiles/C/ProgramFiles(x86)/MSBuild/14.0/bin_/amd64/Microsoft.Common.CurrentVersion.targets,296
properties = properties ?? ImmutableDictionary<string, string>.Empty;
properties = properties.Add("SolutionName", Path.GetFileNameWithoutExtension(solutionFilePath));
properties = properties.Add("SolutionFileName", Path.GetFileName(solutionFilePath));
properties = properties.Add("SolutionPath", solutionFilePath);
properties = properties.Add("SolutionDir", Path.GetDirectoryName(solutionFilePath));
properties = properties.Add("SolutionExt", Path.GetExtension(solutionFilePath));
return properties;
}
private static void WorkspaceFailed(object sender, WorkspaceDiagnosticEventArgs e)
{
var message = e.Diagnostic.Message;
if (message.StartsWith("Could not find file", StringComparison.Ordinal) || message.StartsWith("Could not find a part of the path", StringComparison.Ordinal))
{
return;
}
if (message.StartsWith("The imported project ", StringComparison.Ordinal))
{
return;
}
if (message.Contains("because the file extension '.shproj'"))
{
return;
}
var project = ((Workspace)sender).CurrentSolution.Projects.FirstOrDefault();
if (project != null)
{
message = message + " Project: " + project.Name;
}
Log.Exception("Workspace failed: " + message);
Log.Write(message, ConsoleColor.Red);
}
public void AddTypeScriptFile(string filePath)
{
this.typeScriptFiles.Add(filePath);
}
public void Dispose()
{
if (workspace != null)
{
workspace.Dispose();
workspace = null;
}
}
}
}