forked from dotnet/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectWorkspace.cs
576 lines (491 loc) · 24.2 KB
/
ProjectWorkspace.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DotNet.CodeAnalysis.CSharp;
using DotNet.CodeAnalysis.VisualBasic;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.Extensions.Logging;
using AnalysisProject = Microsoft.CodeAnalysis.Project;
using MSBProject = Microsoft.Build.Evaluation.Project;
using MSBProjectIntance = Microsoft.Build.Execution.ProjectInstance;
namespace DotNet.CodeAnalysis
{
/// Inspired by:
/// https://github.com/dotnet/roslyn/blob/main/src/Workspaces/Core/MSBuild/MSBuild/MSBuildWorkspace.cs
public class ProjectWorkspace : IDisposable
{
BuildManager _buildManager = BuildManager.DefaultBuildManager;
readonly AdhocWorkspace _workspace = new();
readonly HostWorkspaceServices _workspaceServices = null!;
readonly ProjectLoader _projectLoader;
readonly ILogger<ProjectWorkspace> _logger;
readonly Dictionary<string, ProjectItem> _documents = new(StringComparer.OrdinalIgnoreCase);
static readonly char[] s_directorySplitChars =
new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar };
static readonly ImmutableDictionary<string, string> s_defaultGlobalProperties =
new Dictionary<string, string>()
{
["DesignTimeBuild"] = bool.TrueString,
["NonExistentFile"] = "__NonExistentSubDir__\\__NonExistentFile__",
["BuildProjectReferences"] = bool.FalseString,
["BuildingProject"] = bool.FalseString,
["ProvideCommandLineArgs"] = bool.TrueString,
["SkipCompilerExecution"] = bool.TrueString,
["ContinueOnError"] = "ErrorAndContinue",
["ShouldUnsetParentConfigurationAndPlatform"] = bool.FalseString
}.ToImmutableDictionary();
public ProjectWorkspace(
ProjectLoader projectLoader, ILogger<ProjectWorkspace> logger) =>
(_projectLoader, _logger, _workspaceServices) = (projectLoader, logger, _workspace.Services);
public async Task<ImmutableArray<AnalysisProject>> LoadProjectAsync(
string path, CancellationToken cancellationToken)
{
if (File.Exists(path))
{
var projectExtension = Path.GetExtension(path);
var projectDirectory = Path.GetDirectoryName(path)!;
var language = projectExtension switch
{
".csproj" => LanguageNames.CSharp,
".vbproj" => LanguageNames.VisualBasic,
_ => throw new ArgumentException("Unknown project file, .")
};
var project = _projectLoader.LoadProject(path);
var builder = ImmutableArray.CreateBuilder<AnalysisProject>();
var buildProjectCollection = new ProjectCollection(s_defaultGlobalProperties);
var buildParameters = new BuildParameters(buildProjectCollection);
_buildManager.BeginBuild(buildParameters);
try
{
var projectInfos = await LoadProjectInfosAsync(project, language, projectDirectory, cancellationToken);
foreach (var projectInfo in projectInfos)
{
builder.Add(_workspace.AddProject(projectInfo));
}
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
}
finally
{
_buildManager.EndBuild();
}
return builder.ToImmutable();
}
return ImmutableArray<AnalysisProject>.Empty;
}
async Task<ImmutableArray<ProjectInfo>> LoadProjectInfosAsync(
MSBProject project, string language, string projectDirectory, CancellationToken cancellationToken)
{
var targetFrameworkValue = project.GetPropertyValue("TargetFramework");
var targetFrameworksValue = project.GetPropertyValue("TargetFrameworks");
if (string.IsNullOrEmpty(targetFrameworkValue) && !string.IsNullOrEmpty(targetFrameworksValue))
{
var targetFrameworks = targetFrameworksValue.Split(';');
var results = ImmutableArray.CreateBuilder<ProjectInfo>(targetFrameworks.Length);
if (!project.GlobalProperties.TryGetValue(
"TargetFramework", out var initialGlobalTargetFrameworkValue))
{
initialGlobalTargetFrameworkValue = null;
}
foreach (var targetFramework in targetFrameworks)
{
project.SetGlobalProperty("TargetFramework", targetFramework);
project.ReevaluateIfNecessary();
var projectFileInfo =
await BuildProjectFileInfoAsync(project, language, projectDirectory, cancellationToken).ConfigureAwait(false);
var projectInfo = await CreateProjectInfoAsync(projectFileInfo, projectDirectory);
results.Add(projectInfo);
}
if (initialGlobalTargetFrameworkValue is null)
{
project.RemoveGlobalProperty("TargetFramework");
}
else
{
project.SetGlobalProperty("TargetFramework", initialGlobalTargetFrameworkValue);
}
project.ReevaluateIfNecessary();
return results.ToImmutable();
}
else
{
var projectFileInfo =
await BuildProjectFileInfoAsync(project, language, projectDirectory, cancellationToken).ConfigureAwait(false);
var projectInfo = await CreateProjectInfoAsync(projectFileInfo, projectDirectory);
return ImmutableArray.Create(projectInfo);
}
}
async Task<ProjectInfo> CreateProjectInfoAsync(ProjectFileInfo projectFileInfo, string projectDirectory)
{
var projectId = ProjectId.CreateNewId(debugName: projectFileInfo.FilePath);
var language = projectFileInfo.Language;
var projectPath = projectFileInfo.FilePath;
var projectName = Path.GetFileNameWithoutExtension(projectPath);
if (!string.IsNullOrWhiteSpace(projectFileInfo.TargetFramework))
{
projectName += "(" + projectFileInfo.TargetFramework + ")";
}
var version = VersionStamp.Create(
FileUtilities.GetFileTimeStamp(projectPath));
if (projectFileInfo.IsEmpty)
{
var assembly = GetAssemblyNameFromProjectPath(projectPath);
return ProjectInfo.Create(
projectId,
version,
projectName,
assemblyName: assembly,
language: language,
filePath: projectPath,
outputFilePath: string.Empty,
outputRefFilePath: string.Empty,
compilationOptions: null, //compilationOptions,
parseOptions: null, //parseOptions,
documents: Enumerable.Empty<DocumentInfo>(),
projectReferences: Enumerable.Empty<ProjectReference>(),
metadataReferences: Enumerable.Empty<MetadataReference>(),
analyzerReferences: Enumerable.Empty<AnalyzerReference>(),
additionalDocuments: Enumerable.Empty<DocumentInfo>(),
isSubmission: false,
hostObjectType: null);
}
var isCSharp = language == LanguageNames.CSharp;
static CommandLineParser GetCommandLineParser(bool isCSharp) =>
isCSharp ? CSharpDefaults.CommandLineParser : VisualBasicDefaults.CommandLineParser;
var commandLineParser = GetCommandLineParser(isCSharp);
var commandLineArgs = commandLineParser.Parse(
args: projectFileInfo.CommandLineArgs,
baseDirectory: projectDirectory,
sdkDirectory: RuntimeEnvironment.GetRuntimeDirectory(),
additionalReferenceDirectories: null);
var assemblyName = commandLineArgs.CompilationName;
if (string.IsNullOrWhiteSpace(assemblyName))
{
assemblyName = GetAssemblyNameFromProjectPath(projectPath);
}
var parseOptions = commandLineArgs.ParseOptions;
if (parseOptions.DocumentationMode == DocumentationMode.None)
{
parseOptions = parseOptions.WithDocumentationMode(DocumentationMode.Parse);
}
static CompilationOptions GetCompilationOptions(bool isCSharp) =>
isCSharp ? CSharpDefaults.CompilationOptions : VisualBasicDefaults.CompilationOptions;
//// add all the extra options that are really behavior overrides
// var metadataService = GetWorkspaceService<IMetadataService>();
var compilationOptions = GetCompilationOptions(isCSharp)
.WithXmlReferenceResolver(new XmlFileResolver(projectDirectory))
.WithSourceReferenceResolver(new SourceFileResolver(ImmutableArray<string>.Empty, projectDirectory))
// // TODO: https://github.com/dotnet/roslyn/issues/4967
// .WithMetadataReferenceResolver(new WorkspaceMetadataFileReferenceResolver(metadataService, new RelativePathResolver(ImmutableArray<string>.Empty, projectDirectory)))
.WithStrongNameProvider(new DesktopStrongNameProvider(commandLineArgs.KeyFileSearchPaths))
.WithAssemblyIdentityComparer(DesktopAssemblyIdentityComparer.Default);
var documents = CreateDocumentInfos(projectFileInfo.Documents, projectId, commandLineArgs.Encoding);
var additionalDocuments = CreateDocumentInfos(projectFileInfo.AdditionalDocuments, projectId, commandLineArgs.Encoding);
var analyzerConfigDocuments = CreateDocumentInfos(projectFileInfo.AnalyzerConfigDocuments, projectId, commandLineArgs.Encoding);
CheckForDuplicateDocuments(documents.Concat(additionalDocuments).Concat(analyzerConfigDocuments).ToImmutableArray());
//
//var analyzerReferences = ResolveAnalyzerReferences(commandLineArgs);
//var resolvedReferences = await ResolveReferencesAsync(projectId, projectFileInfo, commandLineArgs, cancellationToken).ConfigureAwait(false);
await Task.CompletedTask;
return ProjectInfo.Create(
projectId,
version,
projectName,
assemblyName,
language,
projectPath,
outputFilePath: projectFileInfo.OutputFilePath,
outputRefFilePath: projectFileInfo.OutputRefFilePath,
compilationOptions: compilationOptions,
parseOptions: parseOptions,
documents: documents,
projectReferences: null, //resolvedReferences.ProjectReferences,
metadataReferences: null, //resolvedReferences.MetadataReferences,
analyzerReferences: null, //analyzerReferences,
additionalDocuments: null, //additionalDocuments,
isSubmission: false,
hostObjectType: null)
.WithDefaultNamespace(projectFileInfo.DefaultNamespace)
.WithAnalyzerConfigDocuments(analyzerConfigDocuments);
}
async Task<ProjectFileInfo> BuildProjectFileInfoAsync(
MSBProject loadedProject, string language, string projectDirectory, CancellationToken cancellationToken)
{
var project =
await BuildProjectAsync(loadedProject, cancellationToken).ConfigureAwait(false);
return project != null
? CreateProjectFileInfo(project, loadedProject, language, projectDirectory)
: ProjectFileInfo.CreateEmpty(language, loadedProject.FullPath);
}
async Task<MSBProjectIntance> BuildProjectAsync(
MSBProject project, CancellationToken cancellationToken)
{
var projectInstance = project.CreateProjectInstance();
var targets = new[] { "Compile", "CoreCompile" };
foreach (var target in targets)
{
if (!projectInstance.Targets.ContainsKey(target))
{
return projectInstance;
}
}
var buildRequestData = new BuildRequestData(projectInstance, targets);
var result = await BuildAsync(buildRequestData, cancellationToken).ConfigureAwait(false);
if (result.OverallResult == BuildResultCode.Failure)
{
if (result.Exception != null)
{
_logger.LogError(projectInstance.FullPath, result.Exception);
}
}
return projectInstance;
}
Task<BuildResult> BuildAsync(BuildRequestData requestData, CancellationToken cancellationToken)
{
TaskCompletionSource<BuildResult> taskSource = new();
CancellationTokenRegistration registration = default;
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.Register(() =>
{
taskSource.TrySetCanceled();
_buildManager.CancelAllSubmissions();
registration.Dispose();
});
}
try
{
_buildManager.PendBuildRequest(requestData).ExecuteAsync(sub =>
{
try
{
var result = sub.BuildResult;
registration.Dispose();
taskSource.TrySetResult(result);
}
catch (Exception e)
{
taskSource.TrySetException(e);
}
}, null);
}
catch (Exception e)
{
taskSource.SetException(e);
}
return taskSource.Task;
}
ProjectFileInfo CreateProjectFileInfo(
ProjectInstance projectInstance, MSBProject loadedProject, string language, string projectDirectory)
{
var commandLineArgs = GetCommandLineArgs(projectInstance, language);
var outputFilePath = projectInstance.GetPropertyValue("TargetPath");
if (!string.IsNullOrWhiteSpace(outputFilePath))
{
outputFilePath = GetAbsolutePathRelativeToProject(outputFilePath, projectDirectory);
}
var outputRefFilePath = projectInstance.GetPropertyValue("TargetRefPath");
if (!string.IsNullOrWhiteSpace(outputRefFilePath))
{
outputRefFilePath = GetAbsolutePathRelativeToProject(outputRefFilePath, projectDirectory);
}
var defaultNamespace = projectInstance.GetPropertyValue("RootNamespace") ?? string.Empty;
var targetFramework = projectInstance.GetPropertyValue("TargetFramework");
if (string.IsNullOrWhiteSpace(targetFramework))
{
targetFramework = null;
}
var docs = projectInstance.GetItems("Compile")
.Cast<ITaskItem>()
.Where(i => !Path.GetFileName(i.ItemSpec).StartsWith("TemporaryGeneratedFile_", StringComparison.Ordinal))
.Select(i => MakeDocumentFileInfo(loadedProject, i, projectDirectory))
.ToImmutableArray();
var additionalDocs = projectInstance.GetItems("AdditionalFiles")
.Cast<ITaskItem>()
.Select(i => MakeDocumentFileInfo(loadedProject, i, projectDirectory))
.ToImmutableArray();
//var analyzerConfigDocs = project.GetEditorConfigFiles()
// .Select(MakeNonSourceFileDocumentFileInfo)
// .ToImmutableArray();
return ProjectFileInfo.Create(
language,
projectInstance.FullPath,
outputFilePath,
outputRefFilePath,
defaultNamespace,
targetFramework ?? "<unknown>",
commandLineArgs,
docs,
additionalDocs,
ImmutableArray<DocumentFileInfo>.Empty, //analyzerConfigDocs,
projectInstance.GetItems("ProjectReference").Where(i =>
{
var referenceOutputAssemblyText = i.GetMetadataValue("ReferenceOutputAssembly");
return !string.IsNullOrWhiteSpace(referenceOutputAssemblyText)
? !string.Equals(referenceOutputAssemblyText, bool.FalseString, StringComparison.OrdinalIgnoreCase)
: true;
})
.Select(i =>
{
var aliasesText = i.GetMetadataValue("Aliases");
var aliases = !string.IsNullOrWhiteSpace(aliasesText)
? ImmutableArray.CreateRange(aliasesText.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(a => a.Trim()))
: ImmutableArray<string>.Empty;
return new ProjectFileReference(i.EvaluatedInclude, aliases);
}).ToImmutableArray());
}
ImmutableArray<string> GetCommandLineArgs(ProjectInstance project, string language)
{
var isCSharp = language == LanguageNames.CSharp;
var commandLineArgs = project.GetItems(isCSharp ? "CscCommandLineArgs" : "VbcCommandLineArgs")
.Cast<ITaskItem>()
.Select(item => item.ItemSpec)
.ToImmutableArray();
return commandLineArgs;
}
string GetAbsolutePathRelativeToProject(string path, string projectDirectory)
{
var absolutePath = FileUtilities.ResolveRelativePath(path, projectDirectory) ?? path;
return FileUtilities.TryNormalizeAbsolutePath(absolutePath) ?? absolutePath;
}
DocumentFileInfo MakeDocumentFileInfo(
MSBProject project, ITaskItem documentItem, string projectDirectory)
{
var filePath = GetAbsolutePathRelativeToProject(documentItem.ItemSpec, projectDirectory);
var logicalPath = GetDocumentLogicalPath(documentItem, projectDirectory);
var isLinked = !string.IsNullOrEmpty(documentItem.GetMetadata("Link"));
var isGenerated = IsDocumentGenerated(project, documentItem, projectDirectory);
return new DocumentFileInfo(filePath, logicalPath, isLinked, isGenerated, SourceCodeKind.Regular);
}
string GetDocumentLogicalPath(ITaskItem documentItem, string projectDirectory)
{
var link = documentItem.GetMetadata("Link");
if (!string.IsNullOrEmpty(link))
{
// if a specific link is specified in the project file then use it to form the logical path.
return link;
}
else
{
var filePath = documentItem.ItemSpec;
if (!PathUtilities.IsAbsolute(filePath))
{
return filePath;
}
var normalizedPath = FileUtilities.TryNormalizeAbsolutePath(filePath);
if (normalizedPath == null)
{
return filePath;
}
// If the document is within the current project directory (or subdirectory), then the logical path is the relative path
// from the project's directory.
if (normalizedPath.StartsWith(projectDirectory, StringComparison.OrdinalIgnoreCase))
{
return normalizedPath.Substring(projectDirectory.Length);
}
else
{
// if the document lies outside the project's directory (or subdirectory) then place it logically at the root of the project.
// if more than one document ends up with the same logical name then so be it (the workspace will survive.)
return PathUtilities.GetFileName(normalizedPath);
}
}
}
bool IsDocumentGenerated(MSBProject project, ITaskItem documentItem, string projectDirectory)
{
foreach (var item in project.GetItems("Compile"))
{
_documents[GetAbsolutePathRelativeToProject(item.EvaluatedInclude, projectDirectory)] = item;
}
return !_documents.ContainsKey(
GetAbsolutePathRelativeToProject(documentItem.ItemSpec, projectDirectory));
}
static string GetAssemblyNameFromProjectPath(string projectFilePath)
{
var assemblyName = Path.GetFileNameWithoutExtension(projectFilePath);
return string.IsNullOrWhiteSpace(assemblyName) ? "assembly" : assemblyName;
}
static ImmutableArray<DocumentInfo> CreateDocumentInfos(
IReadOnlyList<DocumentFileInfo> documentFileInfos, ProjectId projectId, Encoding? encoding)
{
var results = ImmutableArray.CreateBuilder<DocumentInfo>();
foreach (var info in documentFileInfos)
{
GetDocumentNameAndFolders(info.LogicalPath, out var name, out var folders);
var documentInfo = DocumentInfo.Create(
DocumentId.CreateNewId(projectId, debugName: info.FilePath),
name,
folders,
info.SourceCodeKind,
new FileTextLoader(info.FilePath, encoding),
info.FilePath,
info.IsGenerated);
results.Add(documentInfo);
}
return results.ToImmutable();
}
static void GetDocumentNameAndFolders(
string logicalPath, out string name, out ImmutableArray<string> folders)
{
var pathNames = logicalPath.Split(s_directorySplitChars, StringSplitOptions.RemoveEmptyEntries);
if (pathNames.Length > 0)
{
if (pathNames.Length > 1)
{
folders = pathNames.Take(pathNames.Length - 1).ToImmutableArray();
}
else
{
folders = ImmutableArray<string>.Empty;
}
name = pathNames[pathNames.Length - 1];
}
else
{
name = logicalPath;
folders = ImmutableArray<string>.Empty;
}
}
void CheckForDuplicateDocuments(ImmutableArray<DocumentInfo> documents)
{
var paths = new HashSet<string>(PathUtilities.Comparer);
foreach (var doc in documents)
{
if (doc.FilePath is null)
{
continue;
}
if (paths.Contains(doc.FilePath))
{
_logger.LogInformation($"Duplicate source file: {doc.FilePath}");
}
paths.Add(doc.FilePath);
}
}
TLanguageService? GetLanguageService<TLanguageService>(string languageName)
where TLanguageService : ILanguageService =>
_workspaceServices.GetLanguageServices(languageName)
.GetService<TLanguageService>();
TWorkspaceService? GetWorkspaceService<TWorkspaceService>()
where TWorkspaceService : IWorkspaceService =>
_workspaceServices.GetService<TWorkspaceService>();
public void Dispose()
{
_buildManager?.Dispose();
_buildManager = null!;
}
}
}