-
Notifications
You must be signed in to change notification settings - Fork 420
/
ProjectFileInfo.cs
312 lines (258 loc) · 13.9 KB
/
ProjectFileInfo.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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.Versioning;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.Extensions.Logging;
using NuGet.Packaging.Core;
using OmniSharp.MSBuild.Models.Events;
using OmniSharp.Options;
using OmniSharp.Utilities;
namespace OmniSharp.MSBuild.ProjectFile
{
public partial class ProjectFileInfo
{
private readonly ProjectData _data;
public string FilePath { get; }
public string Directory { get; }
public ProjectId Id { get; }
public Guid Guid => _data.Guid;
public string Name => _data.Name;
public string AssemblyName => _data.AssemblyName;
public string TargetPath => _data.TargetPath;
public string OutputPath => _data.OutputPath;
public string ProjectAssetsFile => _data.ProjectAssetsFile;
public FrameworkName TargetFramework => _data.TargetFramework;
public ImmutableArray<string> TargetFrameworks => _data.TargetFrameworks;
public OutputKind OutputKind => _data.OutputKind;
public LanguageVersion LanguageVersion => _data.LanguageVersion;
public bool AllowUnsafeCode => _data.AllowUnsafeCode;
public string DocumentationFile => _data.DocumentationFile;
public IList<string> PreprocessorSymbolNames => _data.PreprocessorSymbolNames;
public IList<string> SuppressedDiagnosticIds => _data.SuppressedDiagnosticIds;
public bool SignAssembly => _data.SignAssembly;
public string AssemblyOriginatorKeyFile => _data.AssemblyName;
public ImmutableArray<string> SourceFiles => _data.SourceFiles;
public ImmutableArray<string> References => _data.References;
public ImmutableArray<string> ProjectReferences => _data.ProjectReferences;
public ImmutableArray<PackageReference> PackageReferences => _data.PackageReferences;
public ImmutableArray<string> Analyzers => _data.Analyzers;
internal ProjectFileInfo(string filePath)
{
this.FilePath = filePath;
}
private ProjectFileInfo(
ProjectId id,
string filePath,
ProjectData data)
{
this.Id = id;
this.FilePath = filePath;
this.Directory = Path.GetDirectoryName(filePath);
_data = data;
}
public static ProjectFileInfo Create(
string filePath, string solutionDirectory, string sdksPath, ILogger logger,
MSBuildOptions options = null, ICollection<MSBuildDiagnosticsMessage> diagnostics = null)
{
if (!File.Exists(filePath))
{
return null;
}
var projectInstance = LoadProject(filePath, solutionDirectory, sdksPath, logger, options, diagnostics, out var targetFrameworks);
if (projectInstance == null)
{
return null;
}
var id = ProjectId.CreateNewId(debugName: filePath);
var data = CreateProjectData(projectInstance, targetFrameworks);
return new ProjectFileInfo(id, filePath, data);
}
private static ProjectInstance LoadProject(
string filePath, string solutionDirectory, string sdksPath, ILogger logger,
MSBuildOptions options, ICollection<MSBuildDiagnosticsMessage> diagnostics, out ImmutableArray<string> targetFrameworks)
{
options = options ?? new MSBuildOptions();
var globalProperties = GetGlobalProperties(options, solutionDirectory, sdksPath, logger);
const string MSBuildSDKsPath = "MSBuildSDKsPath";
var oldSdksPathValue = Environment.GetEnvironmentVariable(MSBuildSDKsPath);
try
{
if (globalProperties.TryGetValue(MSBuildSDKsPath, out var sdksPathValue))
{
Environment.SetEnvironmentVariable(MSBuildSDKsPath, sdksPathValue);
}
var collection = new ProjectCollection(globalProperties);
// Evaluate the MSBuild project
var project = string.IsNullOrEmpty(options.ToolsVersion)
? collection.LoadProject(filePath)
: collection.LoadProject(filePath, options.ToolsVersion);
var targetFramework = project.GetPropertyValue(PropertyNames.TargetFramework);
targetFrameworks = PropertyConverter.SplitList(project.GetPropertyValue(PropertyNames.TargetFrameworks), ';');
// If the project supports multiple target frameworks and specific framework isn't
// selected, we must pick one before execution. Otherwise, the ResolveReferences
// target might not be available to us.
if (string.IsNullOrWhiteSpace(targetFramework) && targetFrameworks.Length > 0)
{
// For now, we'll just pick the first target framework. Eventually, we'll need to
// do better and potentially allow OmniSharp hosts to select a target framework.
targetFramework = targetFrameworks[0];
project.SetProperty(PropertyNames.TargetFramework, targetFramework);
}
else if (!string.IsNullOrWhiteSpace(targetFramework) && targetFrameworks.Length == 0)
{
targetFrameworks = ImmutableArray.Create(targetFramework);
}
var projectInstance = project.CreateProjectInstance();
var buildResult = projectInstance.Build(TargetNames.ResolveReferences,
new[] { new MSBuildLogForwarder(logger, diagnostics) });
return buildResult
? projectInstance
: null;
}
finally
{
Environment.SetEnvironmentVariable(MSBuildSDKsPath, oldSdksPathValue);
}
}
private static ProjectData CreateProjectData(ProjectInstance projectInstance, ImmutableArray<string> targetFrameworks)
{
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 projectAssetsFile = projectInstance.GetPropertyValue(PropertyNames.ProjectAssetsFile);
var targetFramework = new FrameworkName(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));
var languageVersion = PropertyConverter.ToLanguageVersion(projectInstance.GetPropertyValue(PropertyNames.LangVersion));
var allowUnsafeCode = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.AllowUnsafeBlocks), defaultValue: false);
var outputKind = PropertyConverter.ToOutputKind(projectInstance.GetPropertyValue(PropertyNames.OutputType));
var documentationFile = projectInstance.GetPropertyValue(PropertyNames.DocumentationFile);
var preprocessorSymbolNames = PropertyConverter.ToPreprocessorSymbolNames(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
var suppressDiagnosticIds = PropertyConverter.ToSuppressDiagnosticIds(projectInstance.GetPropertyValue(PropertyNames.NoWarn));
var signAssembly = PropertyConverter.ToBoolean(projectInstance.GetPropertyValue(PropertyNames.SignAssembly), defaultValue: false);
var assemblyOriginatorKeyFile = projectInstance.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);
var sourceFiles = GetFullPaths(projectInstance.GetItems(ItemNames.Compile));
var projectReferences = GetFullPaths(projectInstance.GetItems(ItemNames.ProjectReference));
var references = GetFullPaths(
projectInstance.GetItems(ItemNames.ReferencePath).Where(ReferenceSourceTargetIsNotProjectReference));
var packageReferences = GetPackageReferences(projectInstance.GetItems(ItemNames.PackageReference));
var analyzers = GetFullPaths(projectInstance.GetItems(ItemNames.Analyzer));
return new ProjectData(guid, name,
assemblyName, targetPath, outputPath, projectAssetsFile,
targetFramework, targetFrameworks,
outputKind, languageVersion, allowUnsafeCode, documentationFile, preprocessorSymbolNames, suppressDiagnosticIds,
signAssembly, assemblyOriginatorKeyFile,
sourceFiles, projectReferences, references, packageReferences, analyzers);
}
public ProjectFileInfo Reload(
string solutionDirectory, string sdksPath, ILogger logger,
MSBuildOptions options = null, ICollection<MSBuildDiagnosticsMessage> diagnostics = null)
{
var projectInstance = LoadProject(FilePath, solutionDirectory, sdksPath, logger, options, diagnostics, out var targetFrameworks);
if (projectInstance == null)
{
return null;
}
var data = CreateProjectData(projectInstance, targetFrameworks);
return new ProjectFileInfo(Id, FilePath, data);
}
public bool IsUnityProject()
{
return References.Any(filePath =>
{
var fileName = Path.GetFileName(filePath);
return string.Equals(fileName, "UnityEngine.dll", StringComparison.OrdinalIgnoreCase)
|| string.Equals(fileName, "UnityEditor.dll", StringComparison.OrdinalIgnoreCase);
});
}
private static Dictionary<string, string> GetGlobalProperties(MSBuildOptions options, string solutionDirectory, string sdksPath, ILogger logger)
{
var globalProperties = new Dictionary<string, string>
{
{ PropertyNames.DesignTimeBuild, "true" },
{ PropertyNames.BuildProjectReferences, "false" },
{ PropertyNames._ResolveReferenceDependencies, "true" },
{ PropertyNames.SolutionDir, solutionDirectory + Path.DirectorySeparatorChar }
};
globalProperties.AddPropertyIfNeeded(
logger,
PropertyNames.MSBuildExtensionsPath,
userOptionValue: options.MSBuildExtensionsPath,
environmentValue: MSBuildEnvironment.MSBuildExtensionsPath);
globalProperties.AddPropertyIfNeeded(
logger,
PropertyNames.MSBuildSDKsPath,
userOptionValue: options.MSBuildSDKsPath,
environmentValue: sdksPath);
globalProperties.AddPropertyIfNeeded(
logger,
PropertyNames.VisualStudioVersion,
userOptionValue: options.VisualStudioVersion,
environmentValue: null);
globalProperties.AddPropertyIfNeeded(
logger,
PropertyNames.Configuration,
userOptionValue: options.Configuration,
environmentValue: null);
globalProperties.AddPropertyIfNeeded(
logger,
PropertyNames.Platform,
userOptionValue: options.Platform,
environmentValue: null);
if (PlatformHelper.IsMono)
{
var monoXBuildFrameworksDirPath = PlatformHelper.MonoXBuildFrameworksDirPath;
if (monoXBuildFrameworksDirPath != null)
{
logger.LogDebug($"Using TargetFrameworkRootPath: {monoXBuildFrameworksDirPath}");
globalProperties.Add(PropertyNames.TargetFrameworkRootPath, monoXBuildFrameworksDirPath);
}
}
return globalProperties;
}
private static bool ReferenceSourceTargetIsNotProjectReference(ProjectItemInstance item)
{
return item.GetMetadataValue(MetadataNames.ReferenceSourceTarget) != ItemNames.ProjectReference;
}
private static ImmutableArray<string> GetFullPaths(IEnumerable<ProjectItemInstance> items)
{
var builder = ImmutableArray.CreateBuilder<string>();
var addedSet = new HashSet<string>();
foreach (var item in items)
{
var fullPath = item.GetMetadataValue(MetadataNames.FullPath);
if (addedSet.Add(fullPath))
{
builder.Add(fullPath);
}
}
return builder.ToImmutable();
}
private static ImmutableArray<PackageReference> GetPackageReferences(ICollection<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();
}
}
}