-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathProjectFileInfo.ProjectData.cs
More file actions
393 lines (339 loc) · 22.7 KB
/
ProjectFileInfo.ProjectData.cs
File metadata and controls
393 lines (339 loc) · 22.7 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using NuGet.Packaging.Core;
using OmniSharp.Utilities;
using MSB = Microsoft.Build;
namespace OmniSharp.MSBuild.ProjectFile
{
internal partial class ProjectFileInfo
{
private class ProjectData
{
public Guid Guid { get; }
public string Name { get; }
public string AssemblyName { get; }
public string TargetPath { get; }
public string OutputPath { get; }
public string IntermediateOutputPath { get; }
public string ProjectAssetsFile { get; }
public string Configuration { get; }
public string Platform { get; }
public FrameworkName TargetFramework { get; }
public ImmutableArray<string> TargetFrameworks { get; }
public OutputKind OutputKind { get; }
public LanguageVersion LanguageVersion { get; }
public NullableContextOptions NullableContextOptions { get; }
public bool AllowUnsafeCode { get; }
public bool CheckForOverflowUnderflow { get; }
public string DocumentationFile { get; }
public ImmutableArray<string> PreprocessorSymbolNames { get; }
public ImmutableArray<string> SuppressedDiagnosticIds { get; }
public bool SignAssembly { get; }
public string AssemblyOriginatorKeyFile { get; }
public ImmutableArray<string> SourceFiles { get; }
public ImmutableArray<string> ProjectReferences { get; }
public ImmutableArray<string> References { get; }
public ImmutableArray<PackageReference> PackageReferences { get; }
public ImmutableArray<string> Analyzers { get; }
public ImmutableArray<string> AdditionalFiles { get; }
public RuleSet RuleSet { get; }
public ImmutableDictionary<string, string> ReferenceAliases { get; }
public ImmutableDictionary<string, string> ProjectReferenceAliases { get; }
public bool TreatWarningsAsErrors { get; }
public bool RunAnalyzers { get; }
public bool RunAnalyzersDuringLiveAnalysis { get; }
public string DefaultNamespace { get; }
private ProjectData()
{
// Be sure to initialize all collection properties with ImmutableArray<T>.Empty.
// Otherwise, Json.net won't be able to serialize the values.
TargetFrameworks = ImmutableArray<string>.Empty;
PreprocessorSymbolNames = ImmutableArray<string>.Empty;
SuppressedDiagnosticIds = ImmutableArray<string>.Empty;
SourceFiles = ImmutableArray<string>.Empty;
ProjectReferences = ImmutableArray<string>.Empty;
References = ImmutableArray<string>.Empty;
PackageReferences = ImmutableArray<PackageReference>.Empty;
Analyzers = ImmutableArray<string>.Empty;
AdditionalFiles = ImmutableArray<string>.Empty;
ReferenceAliases = ImmutableDictionary<string, string>.Empty;
ProjectReferenceAliases = ImmutableDictionary<string, string>.Empty;
}
private ProjectData(
Guid guid, string name,
string assemblyName, string targetPath, string outputPath, string intermediateOutputPath,
string projectAssetsFile,
string configuration, string platform,
FrameworkName targetFramework,
ImmutableArray<string> targetFrameworks,
OutputKind outputKind,
LanguageVersion languageVersion,
NullableContextOptions nullableContextOptions,
bool allowUnsafeCode,
bool checkForOverflowUnderflow,
string documentationFile,
ImmutableArray<string> preprocessorSymbolNames,
ImmutableArray<string> suppressedDiagnosticIds,
bool signAssembly,
string assemblyOriginatorKeyFile,
bool treatWarningsAsErrors,
string defaultNamespace,
bool runAnalyzers,
bool runAnalyzersDuringLiveAnalysis,
RuleSet ruleset)
: this()
{
Guid = guid;
Name = name;
AssemblyName = assemblyName;
TargetPath = targetPath;
OutputPath = outputPath;
IntermediateOutputPath = intermediateOutputPath;
ProjectAssetsFile = projectAssetsFile;
Configuration = configuration;
Platform = platform;
TargetFramework = targetFramework;
TargetFrameworks = targetFrameworks.EmptyIfDefault();
OutputKind = outputKind;
LanguageVersion = languageVersion;
NullableContextOptions = nullableContextOptions;
AllowUnsafeCode = allowUnsafeCode;
CheckForOverflowUnderflow = checkForOverflowUnderflow;
DocumentationFile = documentationFile;
PreprocessorSymbolNames = preprocessorSymbolNames.EmptyIfDefault();
SuppressedDiagnosticIds = suppressedDiagnosticIds.EmptyIfDefault();
SignAssembly = signAssembly;
AssemblyOriginatorKeyFile = assemblyOriginatorKeyFile;
TreatWarningsAsErrors = treatWarningsAsErrors;
RuleSet = ruleset;
DefaultNamespace = defaultNamespace;
RunAnalyzers = runAnalyzers;
RunAnalyzersDuringLiveAnalysis = runAnalyzersDuringLiveAnalysis;
}
private ProjectData(
Guid guid, string name,
string assemblyName, string targetPath, string outputPath, string intermediateOutputPath,
string projectAssetsFile,
string configuration, string platform,
FrameworkName targetFramework,
ImmutableArray<string> targetFrameworks,
OutputKind outputKind,
LanguageVersion languageVersion,
NullableContextOptions nullableContextOptions,
bool allowUnsafeCode,
bool checkForOverflowUnderflow,
string documentationFile,
ImmutableArray<string> preprocessorSymbolNames,
ImmutableArray<string> suppressedDiagnosticIds,
bool signAssembly,
string assemblyOriginatorKeyFile,
ImmutableArray<string> sourceFiles,
ImmutableArray<string> projectReferences,
ImmutableArray<string> references,
ImmutableArray<PackageReference> packageReferences,
ImmutableArray<string> analyzers,
ImmutableArray<string> additionalFiles,
bool treatWarningsAsErrors,
string defaultNamespace,
bool runAnalyzers,
bool runAnalyzersDuringLiveAnalysis,
RuleSet ruleset,
ImmutableDictionary<string, string> referenceAliases,
ImmutableDictionary<string, string> projectReferenceAliases)
: this(guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset)
{
SourceFiles = sourceFiles.EmptyIfDefault();
ProjectReferences = projectReferences.EmptyIfDefault();
References = references.EmptyIfDefault();
PackageReferences = packageReferences.EmptyIfDefault();
Analyzers = analyzers.EmptyIfDefault();
AdditionalFiles = additionalFiles.EmptyIfDefault();
ReferenceAliases = referenceAliases;
ProjectReferenceAliases = projectReferenceAliases;
}
public static ProjectData Create(MSB.Evaluation.Project project)
{
var guid = PropertyConverter.ToGuid(project.GetPropertyValue(PropertyNames.ProjectGuid));
var name = project.GetPropertyValue(PropertyNames.ProjectName);
var assemblyName = project.GetPropertyValue(PropertyNames.AssemblyName);
var targetPath = project.GetPropertyValue(PropertyNames.TargetPath);
var outputPath = project.GetPropertyValue(PropertyNames.OutputPath);
var intermediateOutputPath = project.GetPropertyValue(PropertyNames.IntermediateOutputPath);
var projectAssetsFile = project.GetPropertyValue(PropertyNames.ProjectAssetsFile);
var configuration = project.GetPropertyValue(PropertyNames.Configuration);
var platform = project.GetPropertyValue(PropertyNames.Platform);
var defaultNamespace = project.GetPropertyValue(PropertyNames.RootNamespace);
var targetFramework = new FrameworkName(project.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));
var targetFrameworkValue = project.GetPropertyValue(PropertyNames.TargetFramework);
var targetFrameworks = PropertyConverter.SplitList(project.GetPropertyValue(PropertyNames.TargetFrameworks), ';');
if (!string.IsNullOrWhiteSpace(targetFrameworkValue) && targetFrameworks.Length == 0)
{
targetFrameworks = ImmutableArray.Create(targetFrameworkValue);
}
var languageVersion = PropertyConverter.ToLanguageVersion(project.GetPropertyValue(PropertyNames.LangVersion));
var allowUnsafeCode = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.AllowUnsafeBlocks), defaultValue: false);
var checkForOverflowUnderflow = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.CheckForOverflowUnderflow), defaultValue: false);
var outputKind = PropertyConverter.ToOutputKind(project.GetPropertyValue(PropertyNames.OutputType));
var nullableContextOptions = PropertyConverter.ToNullableContextOptions(project.GetPropertyValue(PropertyNames.Nullable));
var documentationFile = project.GetPropertyValue(PropertyNames.DocumentationFile);
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(project.GetPropertyValue(PropertyNames.DefineConstants));
var suppressedDiagnosticIds = PropertyConverter.ToSuppressedDiagnosticIds(project.GetPropertyValue(PropertyNames.NoWarn));
var signAssembly = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
var assemblyOriginatorKeyFile = project.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);
var treatWarningsAsErrors = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
var runAnalyzers = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.RunAnalyzers), defaultValue: true);
var runAnalyzersDuringLiveAnalysis = PropertyConverter.ToBoolean(project.GetPropertyValue(PropertyNames.RunAnalyzersDuringLiveAnalysis), defaultValue: true);
return new ProjectData(
guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset: null);
}
public static ProjectData Create(MSB.Execution.ProjectInstance projectInstance)
{
var guid = PropertyConverter.ToGuid(projectInstance.GetPropertyValue(PropertyNames.ProjectGuid));
var name = projectInstance.GetPropertyValue(PropertyNames.ProjectName);
var assemblyName = projectInstance.GetPropertyValue(PropertyNames.AssemblyName);
var targetPath = projectInstance.GetPropertyValue(PropertyNames.TargetPath);
var outputPath = projectInstance.GetPropertyValue(PropertyNames.OutputPath);
var intermediateOutputPath = projectInstance.GetPropertyValue(PropertyNames.IntermediateOutputPath);
var projectAssetsFile = projectInstance.GetPropertyValue(PropertyNames.ProjectAssetsFile);
var configuration = projectInstance.GetPropertyValue(PropertyNames.Configuration);
var platform = projectInstance.GetPropertyValue(PropertyNames.Platform);
var defaultNamespace = projectInstance.GetPropertyValue(PropertyNames.RootNamespace);
var targetFramework = new FrameworkName(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));
var targetFrameworkValue = projectInstance.GetPropertyValue(PropertyNames.TargetFramework);
var targetFrameworks = PropertyConverter.SplitList(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworks), ';');
if (!string.IsNullOrWhiteSpace(targetFrameworkValue) && targetFrameworks.Length == 0)
{
targetFrameworks = ImmutableArray.Create(targetFrameworkValue);
}
var languageVersion = PropertyConverter.ToLanguageVersion(projectInstance.GetPropertyValue(PropertyNames.LangVersion));
var allowUnsafeCode = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.AllowUnsafeBlocks), defaultValue: false);
var checkForOverflowUnderflow = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.CheckForOverflowUnderflow), defaultValue: false);
var outputKind = PropertyConverter.ToOutputKind(projectInstance.GetPropertyValue(PropertyNames.OutputType));
var nullableContextOptions = PropertyConverter.ToNullableContextOptions(projectInstance.GetPropertyValue(PropertyNames.Nullable));
var documentationFile = projectInstance.GetPropertyValue(PropertyNames.DocumentationFile);
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
var suppressedDiagnosticIds = PropertyConverter.ToSuppressedDiagnosticIds(projectInstance.GetPropertyValue(PropertyNames.NoWarn));
var signAssembly = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
var treatWarningsAsErrors = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.TreatWarningsAsErrors), defaultValue: false);
var runAnalyzers = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzers), defaultValue: true);
var runAnalyzersDuringLiveAnalysis = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.RunAnalyzersDuringLiveAnalysis), defaultValue: true);
var assemblyOriginatorKeyFile = projectInstance.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);
var ruleset = ResolveRulesetIfAny(projectInstance);
var sourceFiles = GetFullPaths(
projectInstance.GetItems(ItemNames.Compile), filter: FileNameIsNotGenerated);
var projectReferences = ImmutableArray.CreateBuilder<string>();
var projectReferenceAliases = ImmutableDictionary.CreateBuilder<string, string>();
var projectReferencesAdded = new HashSet<string>();
foreach (var projectReferenceItem in projectInstance.GetItems(ItemNames.ProjectReference))
{
var fullPath = projectReferenceItem.GetMetadataValue(MetadataNames.FullPath);
if (IsCSharpProject(fullPath) && projectReferencesAdded.Add(fullPath))
{
projectReferences.Add(fullPath);
var aliases = projectReferenceItem.GetMetadataValue(MetadataNames.Aliases);
if (!string.IsNullOrEmpty(aliases))
{
projectReferenceAliases[fullPath] = aliases;
}
}
}
var references = ImmutableArray.CreateBuilder<string>();
var referenceAliases = ImmutableDictionary.CreateBuilder<string, string>();
foreach (var referencePathItem in projectInstance.GetItems(ItemNames.ReferencePath))
{
var referenceSourceTarget = referencePathItem.GetMetadataValue(MetadataNames.ReferenceSourceTarget);
if (StringComparer.OrdinalIgnoreCase.Equals(referenceSourceTarget, ItemNames.ProjectReference))
{
// If the reference was sourced from a project reference, we have two choices:
//
// 1. If the reference is a C# project reference, we shouldn't add it because it'll just duplicate
// the project reference.
// 2. If the reference is *not* a C# project reference, we should keep this reference because the
// project reference was already removed.
var originalItemSpec = referencePathItem.GetMetadataValue(MetadataNames.OriginalItemSpec);
if (originalItemSpec.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
{
continue;
}
}
var fullPath = referencePathItem.GetMetadataValue(MetadataNames.FullPath);
if (!string.IsNullOrEmpty(fullPath))
{
references.Add(fullPath);
var aliases = referencePathItem.GetMetadataValue(MetadataNames.Aliases);
if (!string.IsNullOrEmpty(aliases))
{
referenceAliases[fullPath] = aliases;
}
}
}
var packageReferences = GetPackageReferences(projectInstance.GetItems(ItemNames.PackageReference));
var analyzers = GetFullPaths(projectInstance.GetItems(ItemNames.Analyzer));
var additionalFiles = GetFullPaths(projectInstance.GetItems(ItemNames.AdditionalFiles));
return new ProjectData(guid, name,
assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks,
outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow, documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds,
signAssembly, assemblyOriginatorKeyFile,
sourceFiles, projectReferences.ToImmutable(), references.ToImmutable(), packageReferences, analyzers, additionalFiles, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset,
referenceAliases.ToImmutableDictionary(), projectReferenceAliases.ToImmutable());
}
private static RuleSet ResolveRulesetIfAny(MSB.Execution.ProjectInstance projectInstance)
{
var rulesetIfAny = projectInstance.Properties.FirstOrDefault(x => x.Name == "ResolvedCodeAnalysisRuleSet");
if (rulesetIfAny != null)
return RuleSet.LoadEffectiveRuleSetFromFile(Path.Combine(projectInstance.Directory, rulesetIfAny.EvaluatedValue));
return null;
}
private static bool IsCSharpProject(string filePath)
=> filePath.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase);
private static bool FileNameIsNotGenerated(string filePath)
=> !Path.GetFileName(filePath).StartsWith("TemporaryGeneratedFile_", StringComparison.OrdinalIgnoreCase);
private static ImmutableArray<string> GetFullPaths(IEnumerable<MSB.Execution.ProjectItemInstance> items, Func<string, bool> filter = null)
{
var builder = ImmutableArray.CreateBuilder<string>();
var addedSet = new HashSet<string>();
filter = filter ?? (_ => true);
foreach (var item in items)
{
var fullPath = item.GetMetadataValue(MetadataNames.FullPath);
if (filter(fullPath) && addedSet.Add(fullPath))
{
builder.Add(fullPath);
}
}
return builder.ToImmutable();
}
private static ImmutableArray<PackageReference> GetPackageReferences(ICollection<MSB.Execution.ProjectItemInstance> items)
{
var builder = ImmutableArray.CreateBuilder<PackageReference>(items.Count);
var addedSet = new HashSet<PackageReference>();
foreach (var item in items)
{
var name = item.EvaluatedInclude;
var versionValue = item.GetMetadataValue(MetadataNames.Version);
var versionRange = PropertyConverter.ToVersionRange(versionValue);
var dependency = new PackageDependency(name, versionRange);
var isImplicitlyDefinedValue = item.GetMetadataValue(MetadataNames.IsImplicitlyDefined);
var isImplicitlyDefined = PropertyConverter.ToBoolean(isImplicitlyDefinedValue, defaultValue: false);
var packageReference = new PackageReference(dependency, isImplicitlyDefined);
if (addedSet.Add(packageReference))
{
builder.Add(packageReference);
}
}
return builder.ToImmutable();
}
}
}
}