-
Notifications
You must be signed in to change notification settings - Fork 751
/
Copy pathProfilesObserver.cs
348 lines (286 loc) · 11.6 KB
/
ProfilesObserver.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.ProjectSystem.Properties;
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.Shell;
using System.Collections.Immutable;
using System.Threading.Tasks.Dataflow;
using EnvDTE;
using Microsoft.VisualStudio.LanguageServer.Client;
using Uno.UI.RemoteControl.VS.Helpers;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.RpcContracts.Logging;
using Microsoft.VisualStudio.ProjectSystem.Debug;
using System.Reflection;
using System.Management.Instrumentation;
using Microsoft.VisualStudio.RpcContracts.Build;
using EnvDTE80;
namespace Uno.UI.RemoteControl.VS.DebuggerHelper;
#pragma warning disable VSTHRD010 // Invoke single-threaded types on Main thread
internal class ProfilesObserver : IDisposable
{
private readonly AsyncPackage _asyncPackage;
private readonly Action<string> _debugLog;
private readonly DTE _dte;
private readonly Func<string?, string, Task> _onDebugFrameworkChanged;
private readonly Func<string?, string, Task> _onDebugProfileChanged;
private record FrameworkServices(object? ActiveDebugFrameworkServices, MethodInfo? SetActiveFrameworkMethod, MethodInfo? GetProjectFrameworksAsyncMethod);
private FrameworkServices? _projectFrameworkServices;
private string? _currentActiveDebugProfile;
private string? _currentActiveDebugFramework;
private IDisposable? _projectRuleSubscriptionLink;
private UnconfiguredProject? _unconfiguredProject;
// Keep the handlers below in order to avoid collection
// and allow DTE to call them.
_dispSolutionEvents_ProjectAddedEventHandler? _projectAdded;
_dispSolutionEvents_ProjectRemovedEventHandler? _projectRemoved;
_dispSolutionEvents_ProjectRenamedEventHandler? _projectRenamed;
_dispCommandEvents_AfterExecuteEventHandler? _afterExecute;
public string? CurrentActiveDebugProfile
=> _currentActiveDebugProfile;
public string? CurrentActiveDebugFramework
=> _currentActiveDebugFramework;
public ProfilesObserver(
AsyncPackage asyncPackage
, EnvDTE.DTE dte
, Func<string?, string, Task> onDebugFrameworkChanged
, Func<string?, string, Task> onDebugProfileChanged
, Action<string> debugLog)
{
_asyncPackage = asyncPackage;
_debugLog = debugLog;
_dte = dte;
_onDebugFrameworkChanged = onDebugFrameworkChanged;
_onDebugProfileChanged = onDebugProfileChanged;
ObserveSolutionEvents();
}
object[]? _existingStartupProjects = [];
private void TryUpdateSolution()
{
if (_dte.Solution.SolutionBuild.StartupProjects is object[] newStartupProjects)
{
if (!newStartupProjects.SequenceEqual(_existingStartupProjects))
{
// log all projects
_existingStartupProjects = newStartupProjects;
}
if (_unconfiguredProject is null)
{
_ = ObserveProfilesAsync();
}
}
}
public async Task ObserveProfilesAsync()
{
try
{
_debugLog("Starting observing profile");
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if ((await _dte.GetStartupProjectsAsync()) is { } startupProjects)
{
if (startupProjects.FirstOrDefault() is Project dteProject
&& (await GetUnconfiguredProjectAsync(dteProject)) is { } unconfiguredProject)
{
_debugLog($"Observing {unconfiguredProject.FullPath}");
_unconfiguredProject = unconfiguredProject;
_unconfiguredProject.ProjectUnloading += OnUnconfiguredProject_ProjectUnloadingAsync;
var configuredProject = unconfiguredProject.Services.ActiveConfiguredProjectProvider?.ActiveConfiguredProject;
var projectSubscriptionService = configuredProject?.Services.ActiveConfiguredProjectSubscription;
if (projectSubscriptionService is not null)
{
var projectChangesBlock = DataflowBlockSlim.CreateActionBlock(
CaptureAndApplyExecutionContext<IProjectVersionedValue<Tuple<IProjectSubscriptionUpdate, IProjectCapabilitiesSnapshot>>>(ProjectRuleBlock_ChangedAsync));
var evaluationLinkOptions = new StandardRuleDataflowLinkOptions
{
RuleNames = ImmutableHashSet.Create("ProjectDebugger"),
PropagateCompletion = true
};
var projectBlock = projectSubscriptionService.ProjectRuleSource.SourceBlock.SyncLinkOptions(evaluationLinkOptions, true);
var unconfiguredProjectBlock = ProjectDataSources.SyncLinkOptions(unconfiguredProject.Capabilities.SourceBlock);
_projectRuleSubscriptionLink = ProjectDataSources.SyncLinkTo(
projectBlock,
unconfiguredProjectBlock,
projectChangesBlock,
new() { PropagateCompletion = true });
}
}
}
}
catch (Exception ex)
{
_debugLog($"Failed to observe {ex}");
}
}
private void ObserveSolutionEvents()
{
_projectAdded = (s) =>
{
_debugLog($"_projectAdded: {s}");
TryUpdateSolution();
};
_projectRemoved = (s) =>
{
_debugLog($"_projectRemoved: {s}");
TryUpdateSolution();
};
_projectRenamed = (s, v) =>
{
_debugLog($"_projectRenamed: {s}");
TryUpdateSolution();
};
_afterExecute = (s, c, o, m) =>
{
_debugLog($"_afterExecute: {s} {c} {o} {m}");
TryUpdateSolution();
};
_debugLog("Observing solution");
_dte.Events.SolutionEvents.ProjectAdded += _projectAdded;
_dte.Events.SolutionEvents.ProjectRemoved += _projectRemoved;
_dte.Events.SolutionEvents.ProjectRenamed += _projectRenamed;
_dte.Events.CommandEvents.AfterExecute += _afterExecute;
}
private async Task OnUnconfiguredProject_ProjectUnloadingAsync(object? sender, EventArgs args)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
_debugLog($"unconfiguredProject was unloaded");
_currentActiveDebugFramework = null;
_currentActiveDebugProfile = null;
// Force a refresh of reflection calls
_projectFrameworkServices = null;
_projectRuleSubscriptionLink?.Dispose();
_projectRuleSubscriptionLink = null;
if (_unconfiguredProject is not null)
{
_unconfiguredProject.ProjectUnloading -= OnUnconfiguredProject_ProjectUnloadingAsync;
_unconfiguredProject = null;
}
}
private static Func<TInput, Task> CaptureAndApplyExecutionContext<TInput>(Func<TInput, Task> function)
{
var context = ExecutionContext.Capture();
return (TInput input) =>
{
var currentSynchronizationContext = SynchronizationContext.Current;
using var executionContext = context.CreateCopy();
Task? result = null;
ExecutionContext.Run(executionContext, delegate
{
SynchronizationContext.SetSynchronizationContext(currentSynchronizationContext);
result = function(input);
}, null);
return result!;
};
}
private async Task ProjectRuleBlock_ChangedAsync(IProjectVersionedValue<Tuple<IProjectSubscriptionUpdate, IProjectCapabilitiesSnapshot>> projectSnapshot)
{
try
{
if (projectSnapshot.Value.Item1.CurrentState.TryGetValue("ProjectDebugger", out var ruleSnapshot))
{
ruleSnapshot.Properties.TryGetValue("ActiveDebugProfile", out var activeDebugProfile);
ruleSnapshot.Properties.TryGetValue("ActiveDebugFramework", out var activeDebugFramework);
if (!string.IsNullOrEmpty(activeDebugProfile) && activeDebugProfile != _currentActiveDebugProfile)
{
var previousProfile = _currentActiveDebugProfile;
_currentActiveDebugProfile = activeDebugProfile;
await _onDebugProfileChanged(previousProfile, _currentActiveDebugProfile);
}
if (!string.IsNullOrEmpty(activeDebugFramework) && activeDebugFramework != _currentActiveDebugFramework)
{
var previousDebugFramework = _currentActiveDebugFramework;
_currentActiveDebugFramework = activeDebugFramework;
await _onDebugFrameworkChanged(previousDebugFramework, _currentActiveDebugFramework);
}
}
}
catch (Exception e)
{
_debugLog($"Failed to process changedAsync: {e}");
}
}
public async Task SetActiveTargetFrameworkAsync(string targetFramework)
{
_debugLog($"SetActiveTargetFrameworkAsync({targetFramework})");
EnsureActiveDebugFrameworkServices();
if (_projectFrameworkServices?.SetActiveFrameworkMethod?.Invoke(_projectFrameworkServices.ActiveDebugFrameworkServices, [targetFramework]) is Task t)
{
await t;
}
}
public async Task<List<string>?> GetActiveTargetFrameworksAsync()
{
_debugLog($"GetActiveTargetFrameworksAsync()");
EnsureActiveDebugFrameworkServices();
if (_projectFrameworkServices?.GetProjectFrameworksAsyncMethod?.Invoke(_projectFrameworkServices.ActiveDebugFrameworkServices, []) is Task<List<string>?> listTask)
{
return await listTask;
}
return new();
}
public async Task SetActiveLaunchProfileAsync(string launchProfile)
{
var provider = _unconfiguredProject?.Services.ActiveConfiguredProjectProvider?.ActiveConfiguredProject?.Services.ExportProvider;
if (provider?.GetService<ILaunchSettingsProvider>() is { } launchSettingsProvider)
{
await launchSettingsProvider.SetActiveProfileAsync(launchProfile);
}
}
public async Task<ImmutableList<ILaunchProfile>> GetLaunchProfilesAsync()
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var provider = _unconfiguredProject?.Services.ActiveConfiguredProjectProvider?.ActiveConfiguredProject?.Services.ExportProvider;
if (provider?.GetService<ILaunchSettingsProvider>() is { } launchSettingsProvider)
{
return launchSettingsProvider.CurrentSnapshot.Profiles;
}
return ImmutableList.Create<ILaunchProfile>();
}
public async Task<UnconfiguredProject?> GetUnconfiguredProjectAsync(Project dteProject)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// Get the IVsSolution service
if (await _asyncPackage.GetServiceAsync(typeof(SVsSolution)) is IVsSolution solution)
{
// Convert DTE project to IVsHierarchy
solution.GetProjectOfUniqueName(dteProject.UniqueName, out var hierarchy);
// Get UnconfiguredProject from IVsHierarchy
if (hierarchy is IVsBrowseObjectContext browseContext)
{
return browseContext.UnconfiguredProject;
}
else if (hierarchy.GetProperty((uint)VSConstants.VSITEMID.Root, (int)__VSHPROPID.VSHPROPID_BrowseObject, out object browseObject) >= 0 && browseObject is IVsBrowseObjectContext context)
{
return context.UnconfiguredProject;
}
}
return null;
}
private void EnsureActiveDebugFrameworkServices()
{
var provider = _unconfiguredProject?.Services.ActiveConfiguredProjectProvider?.ActiveConfiguredProject?.Services.ExportProvider;
if (_projectFrameworkServices is null && provider is not null)
{
var type = Type.GetType("Microsoft.VisualStudio.ProjectSystem.Debug.IActiveDebugFrameworkServices, Microsoft.VisualStudio.ProjectSystem.Managed");
if (typeof(MefExtensions).GetMethods().FirstOrDefault(m => m.Name == "GetService") is { } getServiceMethod)
{
var typedMethod = getServiceMethod.MakeGenericMethod(type);
var activeDebugFrameworkServices = typedMethod.Invoke(null, [provider, /*allow default*/false]);
// https://github.com/dotnet/project-system/blob/34eb57b35962367b71c2a1d79f6c486945586e24/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Debug/IActiveDebugFrameworkServices.cs#L20-L21
if (activeDebugFrameworkServices.GetType().GetMethod("SetActiveDebuggingFrameworkPropertyAsync") is { } setActiveFrameworkMethod
// https://github.com/dotnet/project-system/blob/34eb57b35962367b71c2a1d79f6c486945586e24/src/Microsoft.VisualStudio.ProjectSystem.Managed/ProjectSystem/Debug/IActiveDebugFrameworkServices.cs#L20-L21
&& activeDebugFrameworkServices.GetType().GetMethod("GetProjectFrameworksAsync") is { } getProjectFrameworksAsyncMethod)
{
_projectFrameworkServices = new(activeDebugFrameworkServices, setActiveFrameworkMethod, getProjectFrameworksAsyncMethod);
}
}
}
}
public void Dispose()
=> _projectRuleSubscriptionLink?.Dispose();
}