3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Diagnostics ;
7
6
using System . Linq ;
8
7
using Microsoft . Build . BackEnd . Logging ;
9
8
using Microsoft . Build . BuildCheck . Acquisition ;
10
- using Microsoft . Build . BuildCheck . Logging ;
11
9
using Microsoft . Build . Experimental . BuildCheck ;
12
10
using Microsoft . Build . Framework ;
11
+ using static Microsoft . Build . BuildCheck . Infrastructure . BuildCheckManagerProvider ;
13
12
14
13
namespace Microsoft . Build . BuildCheck . Infrastructure ;
15
- internal sealed class BuildCheckConnectorLogger ( IBuildAnalysisLoggingContextFactory loggingContextFactory , IBuildCheckManager buildCheckManager )
16
- : ILogger
14
+
15
+ internal sealed class BuildCheckConnectorLogger : ILogger
17
16
{
17
+ private readonly Dictionary < Type , Action < BuildEventArgs > > _eventHandlers ;
18
+ private readonly IBuildCheckManager _buildCheckManager ;
19
+ private readonly IBuildAnalysisLoggingContextFactory _loggingContextFactory ;
20
+
21
+ internal BuildCheckConnectorLogger (
22
+ IBuildAnalysisLoggingContextFactory loggingContextFactory ,
23
+ IBuildCheckManager buildCheckManager )
24
+ {
25
+ _buildCheckManager = buildCheckManager ;
26
+ _loggingContextFactory = loggingContextFactory ;
27
+ _eventHandlers = GetBuildEventHandlers ( ) ;
28
+ }
29
+
18
30
public LoggerVerbosity Verbosity { get ; set ; }
31
+
19
32
public string ? Parameters { get ; set ; }
20
33
21
34
public void Initialize ( IEventSource eventSource )
@@ -29,70 +42,67 @@ public void Initialize(IEventSource eventSource)
29
42
}
30
43
}
31
44
32
- private void EventSource_AnyEventRaised ( object sender , BuildEventArgs e )
45
+ public void Shutdown ( )
33
46
{
34
- if ( e is ProjectEvaluationFinishedEventArgs projectEvaluationFinishedEventArgs )
35
- {
36
- if ( projectEvaluationFinishedEventArgs . ProjectFile ? . EndsWith ( ".metaproj" ) ?? false )
37
- {
38
- return ;
39
- }
40
-
41
- buildCheckManager . ProcessEvaluationFinishedEventArgs (
42
- loggingContextFactory . CreateLoggingContext ( e . BuildEventContext ! ) ,
43
- projectEvaluationFinishedEventArgs ) ;
47
+ }
44
48
45
- buildCheckManager . EndProjectEvaluation ( BuildCheckDataSource . EventArgs , e . BuildEventContext ! ) ;
46
- }
47
- else if ( e is ProjectEvaluationStartedEventArgs projectEvaluationStartedEventArgs )
48
- {
49
- // Skip autogenerated transient projects (as those are not user projects to be analyzed)
50
- if ( projectEvaluationStartedEventArgs . ProjectFile ? . EndsWith ( ".metaproj" ) ?? false )
51
- {
52
- return ;
53
- }
54
-
55
- buildCheckManager . StartProjectEvaluation ( BuildCheckDataSource . EventArgs , e . BuildEventContext ! ,
56
- projectEvaluationStartedEventArgs . ProjectFile ! ) ;
57
- }
58
- else if ( e is ProjectStartedEventArgs projectStartedEvent )
49
+ private void HandleProjectEvaluationFinishedEvent ( ProjectEvaluationFinishedEventArgs eventArgs )
50
+ {
51
+ if ( ! IsMetaProjFile ( eventArgs . ProjectFile ) )
59
52
{
60
- buildCheckManager . StartProjectRequest ( BuildCheckDataSource . EventArgs , e . BuildEventContext ! ) ;
53
+ _buildCheckManager . ProcessEvaluationFinishedEventArgs (
54
+ _loggingContextFactory . CreateLoggingContext ( eventArgs . BuildEventContext ! ) ,
55
+ eventArgs ) ;
56
+
57
+ _buildCheckManager . EndProjectEvaluation ( BuildCheckDataSource . EventArgs , eventArgs . BuildEventContext ! ) ;
61
58
}
62
- else if ( e is ProjectFinishedEventArgs projectFinishedEventArgs )
59
+ }
60
+
61
+ private void HandleProjectEvaluationStartedEvent ( ProjectEvaluationStartedEventArgs eventArgs )
62
+ {
63
+ if ( ! IsMetaProjFile ( eventArgs . ProjectFile ) )
63
64
{
64
- buildCheckManager . EndProjectRequest ( BuildCheckDataSource . EventArgs , e . BuildEventContext ! ) ;
65
+ _buildCheckManager . StartProjectEvaluation ( BuildCheckDataSource . EventArgs , eventArgs . BuildEventContext ! , eventArgs . ProjectFile ! ) ;
65
66
}
66
- else if ( e is BuildCheckEventArgs buildCheckBuildEventArgs )
67
+ }
68
+
69
+ private bool IsMetaProjFile ( string ? projectFile ) => ! string . IsNullOrEmpty ( projectFile ) && projectFile ! . EndsWith ( ".metaproj" , StringComparison . OrdinalIgnoreCase ) ;
70
+
71
+ private void EventSource_AnyEventRaised ( object sender , BuildEventArgs e )
72
+ {
73
+ if ( _eventHandlers . TryGetValue ( e . GetType ( ) , out Action < BuildEventArgs > ? handler ) )
67
74
{
68
- if ( buildCheckBuildEventArgs is BuildCheckTracingEventArgs tracingEventArgs )
69
- {
70
- _stats . Merge ( tracingEventArgs . TracingData , ( span1 , span2 ) => span1 + span2 ) ;
71
- }
72
- else if ( buildCheckBuildEventArgs is BuildCheckAcquisitionEventArgs acquisitionEventArgs )
73
- {
74
- buildCheckManager . ProcessAnalyzerAcquisition ( acquisitionEventArgs . ToAnalyzerAcquisitionData ( ) ) ;
75
- }
75
+ handler ( e ) ;
76
76
}
77
77
}
78
78
79
79
private readonly Dictionary < string , TimeSpan > _stats = new Dictionary < string , TimeSpan > ( ) ;
80
80
81
81
private void EventSource_BuildFinished ( object sender , BuildFinishedEventArgs e )
82
82
{
83
- _stats . Merge ( buildCheckManager . CreateTracingStats ( ) , ( span1 , span2 ) => span1 + span2 ) ;
83
+ _stats . Merge ( _buildCheckManager . CreateTracingStats ( ) , ( span1 , span2 ) => span1 + span2 ) ;
84
84
string msg = string . Join ( Environment . NewLine , _stats . Select ( a => a . Key + ": " + a . Value ) ) ;
85
85
86
- BuildEventContext buildEventContext = e . BuildEventContext ?? new BuildEventContext (
87
- BuildEventContext . InvalidNodeId , BuildEventContext . InvalidTargetId ,
88
- BuildEventContext . InvalidProjectContextId , BuildEventContext . InvalidTaskId ) ;
86
+ BuildEventContext buildEventContext = e . BuildEventContext
87
+ ?? new BuildEventContext (
88
+ BuildEventContext . InvalidNodeId ,
89
+ BuildEventContext . InvalidTargetId ,
90
+ BuildEventContext . InvalidProjectContextId ,
91
+ BuildEventContext . InvalidTaskId ) ;
89
92
90
- LoggingContext loggingContext = loggingContextFactory . CreateLoggingContext ( buildEventContext ) ;
93
+ LoggingContext loggingContext = _loggingContextFactory . CreateLoggingContext ( buildEventContext ) ;
91
94
92
95
// Tracing: https://github.com/dotnet/msbuild/issues/9629
93
96
loggingContext . LogCommentFromText ( MessageImportance . High , msg ) ;
94
97
}
95
98
96
- public void Shutdown ( )
97
- { }
99
+ private Dictionary < Type , Action < BuildEventArgs > > GetBuildEventHandlers ( ) => new ( )
100
+ {
101
+ { typeof ( ProjectEvaluationFinishedEventArgs ) , ( BuildEventArgs e ) => HandleProjectEvaluationFinishedEvent ( ( ProjectEvaluationFinishedEventArgs ) e ) } ,
102
+ { typeof ( ProjectEvaluationStartedEventArgs ) , ( BuildEventArgs e ) => HandleProjectEvaluationStartedEvent ( ( ProjectEvaluationStartedEventArgs ) e ) } ,
103
+ { typeof ( ProjectStartedEventArgs ) , ( BuildEventArgs e ) => _buildCheckManager . StartProjectRequest ( BuildCheckDataSource . EventArgs , e . BuildEventContext ! ) } ,
104
+ { typeof ( ProjectFinishedEventArgs ) , ( BuildEventArgs e ) => _buildCheckManager . EndProjectRequest ( BuildCheckDataSource . EventArgs , e . BuildEventContext ! ) } ,
105
+ { typeof ( BuildCheckTracingEventArgs ) , ( BuildEventArgs e ) => _stats . Merge ( ( ( BuildCheckTracingEventArgs ) e ) . TracingData , ( span1 , span2 ) => span1 + span2 ) } ,
106
+ { typeof ( BuildCheckAcquisitionEventArgs ) , ( BuildEventArgs e ) => _buildCheckManager . ProcessAnalyzerAcquisition ( ( ( BuildCheckAcquisitionEventArgs ) e ) . ToAnalyzerAcquisitionData ( ) , e . BuildEventContext ! ) } ,
107
+ } ;
98
108
}
0 commit comments