-
Notifications
You must be signed in to change notification settings - Fork 418
/
Copy pathProjectFileInfoExtensions.cs
92 lines (77 loc) · 4.28 KB
/
ProjectFileInfoExtensions.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
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
using OmniSharp.Helpers;
namespace OmniSharp.MSBuild.ProjectFile
{
internal static class ProjectFileInfoExtensions
{
public static CSharpCompilationOptions CreateCompilationOptions(this ProjectFileInfo projectFileInfo)
{
var compilationOptions = new CSharpCompilationOptions(projectFileInfo.OutputKind);
compilationOptions = compilationOptions.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default)
.WithSpecificDiagnosticOptions(projectFileInfo.GetDiagnosticOptions())
.WithOverflowChecks(projectFileInfo.CheckForOverflowUnderflow);
if (projectFileInfo.AllowUnsafeCode)
{
compilationOptions = compilationOptions.WithAllowUnsafe(true);
}
if (projectFileInfo.TreatWarningsAsErrors)
{
compilationOptions = compilationOptions.WithGeneralDiagnosticOption(ReportDiagnostic.Error);
}
if (projectFileInfo.NullableContextOptions != compilationOptions.NullableContextOptions)
{
compilationOptions = compilationOptions.WithNullableContextOptions(projectFileInfo.NullableContextOptions);
}
if (projectFileInfo.SignAssembly && !string.IsNullOrEmpty(projectFileInfo.AssemblyOriginatorKeyFile))
{
var keyFile = Path.Combine(projectFileInfo.Directory, projectFileInfo.AssemblyOriginatorKeyFile);
compilationOptions = compilationOptions.WithStrongNameProvider(new DesktopStrongNameProvider())
.WithCryptoKeyFile(keyFile);
}
if (!string.IsNullOrWhiteSpace(projectFileInfo.DocumentationFile))
{
compilationOptions = compilationOptions.WithXmlReferenceResolver(XmlFileResolver.Default);
}
return compilationOptions;
}
public static ImmutableDictionary<string, ReportDiagnostic> GetDiagnosticOptions(this ProjectFileInfo projectFileInfo)
{
var defaultSuppressions = CompilationOptionsHelper.GetDefaultSuppressedDiagnosticOptions(projectFileInfo.SuppressedDiagnosticIds);
var specificRules = projectFileInfo.RuleSet?.SpecificDiagnosticOptions ?? ImmutableDictionary<string, ReportDiagnostic>.Empty;
return specificRules.Concat(defaultSuppressions.Where(x => !specificRules.Keys.Contains(x.Key))).ToImmutableDictionary();
}
public static ProjectInfo CreateProjectInfo(this ProjectFileInfo projectFileInfo, IAnalyzerAssemblyLoader analyzerAssemblyLoader)
{
var analyzerReferences = ResolveAnalyzerReferencesForProject(projectFileInfo, analyzerAssemblyLoader);
return ProjectInfo.Create(
id: projectFileInfo.Id,
version: VersionStamp.Create(),
name: projectFileInfo.Name,
assemblyName: projectFileInfo.AssemblyName,
language: LanguageNames.CSharp,
filePath: projectFileInfo.FilePath,
outputFilePath: projectFileInfo.TargetPath,
compilationOptions: projectFileInfo.CreateCompilationOptions(),
analyzerReferences: analyzerReferences).WithDefaultNamespace(projectFileInfo.DefaultNamespace);
}
private static IEnumerable<AnalyzerReference> ResolveAnalyzerReferencesForProject(ProjectFileInfo projectFileInfo, IAnalyzerAssemblyLoader analyzerAssemblyLoader)
{
if (!projectFileInfo.RunAnalyzers || !projectFileInfo.RunAnalyzersDuringLiveAnalysis)
{
return Enumerable.Empty<AnalyzerReference>();
}
foreach(var analyzerAssemblyPath in projectFileInfo.Analyzers.Distinct())
{
analyzerAssemblyLoader.AddDependencyLocation(analyzerAssemblyPath);
}
return projectFileInfo.Analyzers.Select(analyzerCandicatePath => new AnalyzerFileReference(analyzerCandicatePath, analyzerAssemblyLoader));
}
}
}