-
Notifications
You must be signed in to change notification settings - Fork 395
/
Copy pathUpToDateCheckBuildEventNotifier.cs
188 lines (152 loc) · 8.29 KB
/
UpToDateCheckBuildEventNotifier.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
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
using Microsoft.VisualStudio.ProjectSystem.UpToDate;
using Microsoft.VisualStudio.ProjectSystem.VS.Build;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
namespace Microsoft.VisualStudio.ProjectSystem.VS.UpToDate
{
/// <summary>
/// Listens for build events and notifies the fast up-to-date check of them
/// via <see cref="IProjectBuildEventListener"/>.
/// </summary>
[Export(ExportContractNames.Scopes.UnconfiguredProject, typeof(IProjectDynamicLoadComponent))]
[AppliesTo(BuildUpToDateCheck.AppliesToExpression)]
internal sealed class UpToDateCheckBuildEventNotifier : OnceInitializedOnceDisposedAsync, IVsUpdateSolutionEvents2, IProjectDynamicLoadComponent
{
private readonly IProjectService _projectService;
private readonly ISolutionBuildManager _solutionBuildManager;
private readonly ISolutionBuildEventListener _solutionBuildEventListener;
private readonly IProjectThreadingService _threadingService;
private readonly IProjectFaultHandlerService _faultHandlerService;
private IAsyncDisposable? _solutionBuildEventsSubscription;
[ImportingConstructor]
public UpToDateCheckBuildEventNotifier(
JoinableTaskContext joinableTaskContext,
IProjectService projectService,
IProjectThreadingService threadingService,
IProjectFaultHandlerService faultHandlerService,
ISolutionBuildManager solutionBuildManager,
ISolutionBuildEventListener solutionBuildEventListener)
: base(new(joinableTaskContext))
{
_projectService = projectService;
_threadingService = threadingService;
_faultHandlerService = faultHandlerService;
_solutionBuildManager = solutionBuildManager;
_solutionBuildEventListener = solutionBuildEventListener;
}
public Task LoadAsync() => InitializeAsync();
public Task UnloadAsync() => DisposeAsync();
protected override async Task InitializeCoreAsync(CancellationToken cancellationToken)
{
_solutionBuildEventsSubscription = await _solutionBuildManager.SubscribeSolutionEventsAsync(this);
}
protected override Task DisposeCoreAsync(bool initialized)
{
if (initialized)
{
if (_solutionBuildEventsSubscription is not null)
{
return _solutionBuildEventsSubscription.DisposeAsync().AsTask();
}
}
return Task.CompletedTask;
}
/// <summary>
/// Called right before a project configuration starts building. Called even if the project is up-to-date.
/// </summary>
int IVsUpdateSolutionEvents2.UpdateProjectCfg_Begin(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, ref int pfCancel)
{
if (IsBuild(dwAction, out bool isRebuild))
{
IEnumerable<IProjectBuildEventListener>? listeners = FindActiveConfiguredProviders(pHierProj, out _);
// Notify the solution build listener that a project build is starting.
// Note there's no equivalent for build completion, as the fast up-to-date check handles
// that for the projects it tracks. We don't need to know when other project types complete.
_solutionBuildEventListener.NotifyProjectBuildStarting(isRebuild);
if (listeners is not null)
{
var buildStartedTimeUtc = DateTime.UtcNow;
foreach (IProjectBuildEventListener listener in listeners)
{
listener.NotifyBuildStarting(buildStartedTimeUtc);
}
}
}
return HResult.OK;
}
/// <summary>
/// Called right after a project configuration is finished building. Called even if the project is up-to-date.
/// </summary>
int IVsUpdateSolutionEvents2.UpdateProjectCfg_Done(IVsHierarchy pHierProj, IVsCfg pCfgProj, IVsCfg pCfgSln, uint dwAction, int fSuccess, int fCancel)
{
if (fCancel == 0 && IsBuild(dwAction, out bool isRebuild))
{
IEnumerable<IProjectBuildEventListener>? listeners = FindActiveConfiguredProviders(pHierProj, out UnconfiguredProject? unconfiguredProject);
if (listeners is not null)
{
JoinableTask task = _threadingService.JoinableTaskFactory.RunAsync(async () =>
{
// Do this work off the main thread
await TaskScheduler.Default;
foreach (IProjectBuildEventListener listener in listeners)
{
await listener.NotifyBuildCompletedAsync(wasSuccessful: fSuccess != 0, isRebuild);
}
});
_faultHandlerService.Forget(task.Task, unconfiguredProject);
}
}
return HResult.OK;
}
/// <summary>
/// Returns <see langword="true"/> if <paramref name="options"/> indicates either a build or rebuild.
/// </summary>
private static bool IsBuild(uint options, out bool isRebuild)
{
const VSSOLNBUILDUPDATEFLAGS anyBuildFlags = VSSOLNBUILDUPDATEFLAGS.SBF_OPERATION_BUILD;
const VSSOLNBUILDUPDATEFLAGS rebuildFlags = VSSOLNBUILDUPDATEFLAGS.SBF_OPERATION_BUILD | VSSOLNBUILDUPDATEFLAGS.SBF_OPERATION_FORCE_UPDATE;
var operation = (VSSOLNBUILDUPDATEFLAGS)options;
isRebuild = (operation & rebuildFlags) == rebuildFlags;
return (operation & anyBuildFlags) == anyBuildFlags;
}
private IEnumerable<IProjectBuildEventListener>? FindActiveConfiguredProviders(IVsHierarchy vsHierarchy, out UnconfiguredProject? unconfiguredProject)
{
unconfiguredProject = _projectService.GetUnconfiguredProject(vsHierarchy, appliesToExpression: BuildUpToDateCheck.AppliesToExpression);
if (unconfiguredProject is not null)
{
IActiveConfiguredProjectProvider activeConfiguredProjectProvider = unconfiguredProject.Services.ExportProvider.GetExportedValue<IActiveConfiguredProjectProvider>();
ConfiguredProject? configuredProject = activeConfiguredProjectProvider.ActiveConfiguredProject;
if (configuredProject is not null)
{
return configuredProject.Services.ExportProvider.GetExportedValues<IProjectBuildEventListener>();
}
}
return null;
}
int IVsUpdateSolutionEvents.UpdateSolution_Begin(ref int pfCancelUpdate)
{
_solutionBuildEventListener.NotifySolutionBuildStarting();
return HResult.OK;
}
int IVsUpdateSolutionEvents.UpdateSolution_Done(int fSucceeded, int fModified, int fCancelCommand)
{
_solutionBuildEventListener.NotifySolutionBuildCompleted(cancelled: false);
return HResult.OK;
}
int IVsUpdateSolutionEvents.UpdateSolution_Cancel()
{
_solutionBuildEventListener.NotifySolutionBuildCompleted(cancelled: true);
return HResult.OK;
}
#region IVsUpdateSolutionEvents stubs
int IVsUpdateSolutionEvents.OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy) => HResult.OK;
int IVsUpdateSolutionEvents.UpdateSolution_StartUpdate(ref int pfCancelUpdate) => HResult.OK;
int IVsUpdateSolutionEvents2.OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy) => HResult.OK;
int IVsUpdateSolutionEvents2.UpdateSolution_Begin(ref int pfCancelUpdate) => HResult.OK;
int IVsUpdateSolutionEvents2.UpdateSolution_Done(int fSucceeded, int fModified, int fCancelCommand) => HResult.OK;
int IVsUpdateSolutionEvents2.UpdateSolution_StartUpdate(ref int pfCancelUpdate) => HResult.OK;
int IVsUpdateSolutionEvents2.UpdateSolution_Cancel() => HResult.OK;
#endregion
}
}