-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathProjectLoadListener.cs
137 lines (120 loc) · 5.88 KB
/
ProjectLoadListener.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
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using Microsoft.Build.Execution;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using OmniSharp.Eventing;
using OmniSharp.Models;
using OmniSharp.MSBuild.Notification;
using OmniSharp.MSBuild.ProjectFile;
namespace OmniSharp.MSBuild
{
[Export(typeof(IMSBuildEventSink))]
public class ProjectLoadListener : IMSBuildEventSink
{
internal const string TargetFramework = nameof(TargetFramework);
internal const string TargetFrameworkVersion = nameof(TargetFrameworkVersion);
internal const string TargetFrameworks = nameof(TargetFrameworks);
private const string MSBuildProjectFullPath = nameof(MSBuildProjectFullPath);
private readonly ILogger _logger;
private readonly IEventEmitter _eventEmitter;
private static readonly VsTfmAndFileExtHashingAlgorithm _tfmAndFileHashingAlgorithm = new VsTfmAndFileExtHashingAlgorithm();
private static readonly VsReferenceHashingAlgorithm _referenceHashingAlgorithm = new VsReferenceHashingAlgorithm();
[ImportingConstructor]
public ProjectLoadListener(ILoggerFactory loggerFactory, IEventEmitter eventEmitter)
{
_logger = loggerFactory.CreateLogger<ProjectLoadListener>();
_eventEmitter = eventEmitter;
}
public void ProjectLoaded(ProjectLoadedEventArgs args)
{
try
{
var projectId = GetProjectId(args);
var sessionId = GetSessionId(args);
var targetFrameworks = GetTargetFrameworks(args.ProjectInstance);
var sdkVersion = GetSdkVersion(args);
var outputKind = GetOutputKind(args);
var projectCapabilities = GetProjectCapabilities(args.ProjectInstance);
if (args.References == null)
{
return;
}
var hashedReferences = GetHashedReferences(args);
var (hashedFileExtensions, fileCounts) = GetUniqueHashedFileExtensionsAndCounts(args);
_eventEmitter.ProjectInformation(projectId, sessionId, (int)outputKind, projectCapabilities, targetFrameworks, sdkVersion, hashedReferences, hashedFileExtensions, fileCounts);
}
catch (Exception ex)
{
_logger.LogError("Unexpected exception got thrown from project load listener: " + ex);
}
}
private static (IEnumerable<HashedString> Extensions, IEnumerable<int> Counts) GetUniqueHashedFileExtensionsAndCounts(ProjectLoadedEventArgs args)
{
var contentFiles = args.ProjectInstance
.GetItems(ItemNames.Content)
.Select(item => item.GetMetadataValue(MetadataNames.FullPath));
var allFiles = args.SourceFiles.Concat(contentFiles);
var filesCounts = allFiles.GroupBy(file => Path.GetExtension(file)).ToDictionary(kvp => kvp.Key, kvp => kvp.Count());
IEnumerable<string> fileExtensions = filesCounts.Select(kvp => kvp.Key);
IEnumerable<int> fileCounts = filesCounts.Select(kvp => kvp.Value);
return (fileExtensions.Select(_tfmAndFileHashingAlgorithm.HashInput), fileCounts);
}
private static HashedString GetProjectId(ProjectLoadedEventArgs args)
{
if (args.ProjectIdIsDefinedInSolution)
{
//If we are getting a raw guid we should not hash it
return new HashedString(args.Id.Id.ToString());
}
var projectFilePath = args.ProjectInstance.GetPropertyValue(MSBuildProjectFullPath);
var content = File.ReadAllText(projectFilePath);
//create a hash from the filename and the content
return _referenceHashingAlgorithm.HashInput($"Filename: {Path.GetFileName(projectFilePath)}\n{content}");
}
private static HashedString GetSdkVersion(ProjectLoadedEventArgs args)
{
return _tfmAndFileHashingAlgorithm.HashInput($"{args.SdkVersion}");
}
private static HashedString GetSessionId(ProjectLoadedEventArgs args)
{
return _tfmAndFileHashingAlgorithm.HashInput(args.SessionId.ToString());
}
private static OutputKind GetOutputKind(ProjectLoadedEventArgs args)
{
return PropertyConverter.ToOutputKind(args.ProjectInstance.GetPropertyValue(PropertyNames.OutputType));
}
private static IEnumerable<HashedString> GetHashedReferences(ProjectLoadedEventArgs args)
{
var referenceNames = args.References.Select(reference => Path.GetFileNameWithoutExtension(reference).ToLower());
return referenceNames.Select(_referenceHashingAlgorithm.HashInput);
}
private IEnumerable<string> GetProjectCapabilities(ProjectInstance projectInstance)
{
return projectInstance.GetItems(ItemNames.ProjectCapability).Select(item => item.ToString());
}
// Internal for testing
internal static IEnumerable<string> GetTargetFrameworks(ProjectInstance projectInstance)
{
var targetFrameworks = projectInstance.GetPropertyValue(TargetFrameworks);
if (string.IsNullOrEmpty(targetFrameworks))
{
targetFrameworks = projectInstance.GetPropertyValue(TargetFramework);
}
if (string.IsNullOrEmpty(targetFrameworks))
{
targetFrameworks = projectInstance.GetPropertyValue(TargetFrameworkVersion);
}
if (!string.IsNullOrEmpty(targetFrameworks))
{
return targetFrameworks.Split(';')
.Where(tfm => !string.IsNullOrWhiteSpace(tfm))
.Select(tfm => tfm.ToLower());
}
return new List<string>();
}
}
}