blob: 99bf449163614c4017cb9889ee83b1119a7ecc6e (
plain)
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
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.ProjectSystem.Properties;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TaskStatusCenter;
using Microsoft.VisualStudio.Threading;
using Task = System.Threading.Tasks.Task;
namespace QtVsTools.Core.MsBuild
{
using Core;
using Options;
using VisualStudio;
public partial class MsBuildProject
{
private static PunisherQueue<MsBuildProject> InitQueue => StaticLazy.Get(() =>
InitQueue, () => new PunisherQueue<MsBuildProject>());
private static ITaskHandler2 InitStatus { get; set; }
public UnconfiguredProject UnconfiguredProject { get; private set; }
public EventWaitHandle Initialized { get; }
private static async Task InitDispatcherLoopAsync()
{
if (VsServiceProvider.Instance is not AsyncPackage package)
return;
while (!VsShellUtilities.ShutdownToken.IsCancellationRequested) {
while (InitQueue.IsEmpty)
await Task.Delay(100, VsShellUtilities.ShutdownToken);
if (InitQueue.TryDequeue(out var tracker)) {
if (InitStatus == null) {
await package.JoinableTaskFactory.SwitchToMainThreadAsync();
tracker.BeginInitStatus();
await TaskScheduler.Default;
} else {
await package.JoinableTaskFactory.SwitchToMainThreadAsync();
tracker.UpdateInitStatus(0);
await TaskScheduler.Default;
}
await tracker.InitializeAsync();
}
if (InitStatus == null)
continue;
var cancellationRequested = InitStatus.UserCancellation.IsCancellationRequested;
if (!InitQueue.IsEmpty && !cancellationRequested)
continue;
if (cancellationRequested)
InitQueue.Clear();
EndInitStatus();
}
}
private async Task InitializeAsync()
{
var p = 0;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
UpdateInitStatus(p += 10);
UpdateInitStatus(p += 10);
if (VcProject is not IVsBrowseObjectContext context) {
if (VcProject.Object is not EnvDTE.Project project)
return;
context = project.Object as IVsBrowseObjectContext;
}
if (context == null)
return;
UpdateInitStatus(p += 10);
UnconfiguredProject = context.UnconfiguredProject;
if (UnconfiguredProject?.ProjectService.Services == null)
return;
await TaskScheduler.Default;
UpdateInitStatus(p += 10);
var service = UnconfiguredProject.Services
.ProjectConfigurationsService;
if (service == null)
return;
var configs = await service.GetKnownProjectConfigurationsAsync();
UpdateInitStatus(p += 10);
Initialized.Set();
var n = configs.Count;
var d = (100 - p) / (n * 2);
foreach (var config in configs) {
var configProject = await UnconfiguredProject.LoadConfiguredProjectAsync(config);
UpdateInitStatus(p += d);
configProject.ProjectUnloading += OnProjectUnloadingAsync;
if (QtOptionsPage.BuildDebugInformation) {
Messages.Print($"{DateTime.Now:HH:mm:ss.FFF} "
+ $"QtProjectTracker({Thread.CurrentThread.ManagedThreadId}): "
+ $"Started tracking [{config.Name}] {VcProjectPath}");
}
UpdateInitStatus(p += d);
}
}
private async Task OnProjectUnloadingAsync(object sender, EventArgs args)
{
if (sender is ConfiguredProject project) {
if (QtOptionsPage.BuildDebugInformation) {
Messages.Print($"{DateTime.Now:HH:mm:ss.FFF} QtProjectTracker: "
+ $"Stopped tracking [{project.ProjectConfiguration.Name}] "
+ $"{project.UnconfiguredProject.FullPath}");
}
lock (CriticalSection) {
project.ProjectUnloading -= OnProjectUnloadingAsync;
Instances.TryRemove(project.UnconfiguredProject.FullPath, out _);
}
await Task.Yield();
}
}
private void BeginInitStatus()
{
lock (StaticCriticalSection) {
if (InitStatus != null)
return;
try {
InitStatus = StatusCenter.PreRegister(
new TaskHandlerOptions
{
Title = "Qt VS Tools: Setting up project tracking..."
},
new TaskProgressData
{
ProgressText = $"{VcProjectPath} ({InitQueue.Count} projects remaining)",
CanBeCanceled = true,
PercentComplete = 0
})
as ITaskHandler2;
} catch (Exception exception) {
exception.Log();
}
InitStatus?.RegisterTask(new Task(() => throw new InvalidOperationException()));
}
}
private void UpdateInitStatus(int percentComplete)
{
lock (StaticCriticalSection) {
if (InitStatus == null)
return;
try {
InitStatus.Progress.Report(new TaskProgressData
{
ProgressText = $"{Path.GetFileNameWithoutExtension(VcProjectPath)} "
+ $"({InitQueue.Count} project(s) remaining)",
CanBeCanceled = true,
PercentComplete = percentComplete
});
} catch (Exception exception) {
exception.Log();
}
}
}
private static void EndInitStatus()
{
lock (StaticCriticalSection) {
if (InitStatus == null)
return;
InitStatus.Dismiss();
InitStatus = null;
}
}
}
}
|