Repository navigation
Expand file tree
/
Copy pathWizardImplementation.cs
More file actions
467 lines (405 loc) · 17.9 KB
/
Copy pathWizardImplementation.cs
File metadata and controls
467 lines (405 loc) · 17.9 KB
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License
using System;
using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Threading.Tasks;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Imaging;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TemplateWizard;
using Microsoft.VisualStudio.Threading;
using NuGet.VisualStudio;
// Although the strings are the same in the wizard for both extensions,
// they are included with both their respective VSPackages.
// Strings for both extensions can be found in {PathToWindowsAppSDK}\dev\VSIX\Extension\Cs\Common\VSPackage.resx
// Wizard strings are numbers 1044 and above.
#if CSHARP_EXTENSION
using Resources = WindowsAppSDK.Cs.Extension.Dev17.VSPackage;
#elif CPP_EXTENSION
using Resources = WindowsAppSDK.Cpp.Extension.Dev17.VSPackage;
#endif
namespace WindowsAppSDK.TemplateUtilities
{
public enum ErrorMessageFormat
{
MessageBox,
InfoBar
}
public partial class NuGetPackageInstaller : IWizard
{
internal static Guid SolutionVCProjectGuid = new Guid("8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942");
private Project _project;
private IComponentModel _componentModel;
private IEnumerable<string> _nuGetPackages;
private IVsNuGetProjectUpdateEvents _nugetProjectUpdateEvents;
private IVsThreadedWaitDialog2 _waitDialog;
private Dictionary<string, Exception> _failedPackageExceptions = new Dictionary<string, Exception>();
// Replaceable in unit tests to avoid blocking MessageBox popups.
internal static Func<string, string, MessageBoxButtons, MessageBoxIcon, DialogResult> ShowMessageBox = MessageBox.Show;
public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
{
ThreadHelper.ThrowIfNotOnUIThread();
_componentModel = ServiceProvider.GlobalProvider.GetService(typeof(SComponentModel)) as IComponentModel;
if (_componentModel == null)
{
System.Diagnostics.Debug.WriteLine("Warning: Could not obtain IComponentModel service.");
}
_waitDialog = ServiceProvider.GlobalProvider.GetService(typeof(SVsThreadedWaitDialog)) as IVsThreadedWaitDialog2;
if (_waitDialog == null)
{
System.Diagnostics.Debug.WriteLine("Warning: Could not obtain IVsThreadedWaitDialog2 service.");
}
if (_componentModel != null)
{
_nugetProjectUpdateEvents = _componentModel.GetService<IVsNuGetProjectUpdateEvents>();
if (_nugetProjectUpdateEvents != null)
{
_nugetProjectUpdateEvents.SolutionRestoreFinished += OnSolutionRestoreFinished;
}
}
// Assuming package list is passed via a custom parameter in the .vstemplate file
if (replacementsDictionary.TryGetValue("$NuGetPackages$", out string packages))
{
_nuGetPackages = packages.Split(';').Where(p => !string.IsNullOrEmpty(p));
}
}
public void ProjectFinishedGenerating(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
_project = project;
Guid _projectGuid = GetProjectGuid(project);
if (_projectGuid.Equals(SolutionVCProjectGuid))
{
ThreadHelper.JoinableTaskFactory.Run(async () =>
{
await InstallNuGetPackagesAsync();
});
}
}
private async Task InstallNuGetPackagesAsync()
{
await ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
int canceled = 0; // Initialize as not canceled
// Start the package installation task but do not await it here
var installationTask = StartInstallationAsync();
// Start the threaded wait dialog
if (_waitDialog != null)
{
_waitDialog.StartWaitDialog(null, Resources._1044, null, null, Resources._1045, 0, false, true);
}
// Now await the installation task to complete
await installationTask;
// Once the installation is complete, end the wait dialog
if (_waitDialog != null)
{
_waitDialog.EndWaitDialog(out canceled);
}
// If _waitDialog is null, canceled remains 0 (not canceled)
// Check if the process was canceled before proceeding
if (canceled == 0) // If not canceled, finalize the process
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
SaveAllProjects();
}
});
}
private async Task StartInstallationAsync()
{
if (_componentModel == null)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
LogError("ComponentModel service is not available.");
return;
}
IVsPackageInstaller installer = _componentModel.GetService<IVsPackageInstaller>();
if (installer == null)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
LogError("Could not obtain IVsPackageInstaller service.");
return;
}
if (_project == null)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
LogError("Project reference is null. Likely a ProjectGroup vstemplate.");
return;
}
// Process each package installation
foreach (var packageId in _nuGetPackages)
{
try
{
await Task.Run(() => installer.InstallPackage(null, _project, packageId, "", false));
}
catch (Exception ex)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
LogError($"Failed to install NuGet package: {packageId}. Error: {ex.Message}");
_failedPackageExceptions[packageId] = ex;
}
}
if (_failedPackageExceptions.Count > 0)
{
// Build error message in the requested format
var errorMessage = CreateErrorMessage(ErrorMessageFormat.InfoBar);
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
LogError(errorMessage);
_ = DisplayInfoBarAsync(errorMessage);
}
}
public void BeforeOpeningFile(ProjectItem _)
{
}
public void ProjectItemFinishedGenerating(ProjectItem _)
{
}
public void RunFinished()
{
ThreadHelper.ThrowIfNotOnUIThread();
Guid _projectGuid = GetProjectGuid(_project);
if (_projectGuid.Equals(SolutionVCProjectGuid))
{
if (_failedPackageExceptions.Count > 0)
{
var errorMessage = CreateErrorMessage(ErrorMessageFormat.MessageBox);
LogError(errorMessage);
var result = ShowMessageBox(
errorMessage,
Resources._1046,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
// Show the Output window with the detailed errors
ShowOutputWindow(CreateDetailedErrorMessage());
return;
}
return;
}
}
private void ShowOutputWindow(string errorMessage)
{
ThreadHelper.ThrowIfNotOnUIThread();
OutputWindowHelper.ShowMessageInOutputWindow(errorMessage);
}
private void SaveAllProjects()
{
ThreadHelper.ThrowIfNotOnUIThread("SaveAllProjects must be called on the UI thread.");
var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
if (dte != null && dte.Solution != null && dte.Solution.Projects != null)
{
foreach (Project project in dte.Solution.Projects)
{
if (project != null)
{
project.Save();
}
}
}
}
private void OnSolutionRestoreFinished(IReadOnlyList<string> projects)
{
ThreadHelper.JoinableTaskFactory.Run(async delegate
{
// Accessing _project requires the main thread
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (_project == null)
{
return;
}
// Normally, either InstallNuGetPackagesAsync is called from ProjectFinishedGenerating
// for VC++ projects (C++ templates) or from here for C# and wapproj projects. In the
// C++ wapproj template, InstallNuGetPackagesAsync() was called twice for the vcxproj,
// once from each location. This caused the NuGet install API to throw an
// InvalidOperationException because the package was already installed.
// This check prevents the double installation attempt.
Guid _projectGuid = GetProjectGuid(_project);
if (_projectGuid.Equals(SolutionVCProjectGuid))
{
if (_failedPackageExceptions.Count > 0)
{
var errorMessage = CreateErrorMessage(ErrorMessageFormat.InfoBar);
LogError(errorMessage);
_ = DisplayInfoBarAsync(errorMessage);
return;
}
return;
}
else
{
// Debouncing prevents multiple rapid executions of 'InstallNuGetPackageAsync'
// during solution restore.
if (_nugetProjectUpdateEvents == null)
{
return;
}
_nugetProjectUpdateEvents.SolutionRestoreFinished -= OnSolutionRestoreFinished;
var joinableTaskFactory = new JoinableTaskFactory(ThreadHelper.JoinableTaskContext);
_ = joinableTaskFactory.RunAsync(InstallNuGetPackagesAsync);
}
});
}
private string CreateErrorMessage(ErrorMessageFormat format)
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
var packageNames = string.Join(", ", _failedPackageExceptions.Keys);
var separator = format == ErrorMessageFormat.MessageBox ? "\n\n" : " ";
var projectName = _project?.Name ?? "Unknown Project";
var errorMessage = format == ErrorMessageFormat.InfoBar ?
string.Format(Resources._1047, projectName, packageNames)
: string.Format(Resources._1048, projectName, packageNames);
return errorMessage;
}
catch (MissingManifestResourceException ex)
{
ShowLocalizationErrorDialog(ex);
// Return a fallback message
var packageNames = string.Join(", ", _failedPackageExceptions.Keys);
return $"Unable to add package references to project: {packageNames}";
}
}
private string CreateDetailedErrorMessage()
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
var errorLines = new System.Text.StringBuilder();
var projectName = _project?.Name ?? "Unknown Project";
errorLines.AppendLine(string.Format(Resources._1051, projectName));
foreach (var package in _failedPackageExceptions)
{
errorLines.AppendLine($"{package.Key} - {package.Value.GetType().FullName}: {package.Value.Message}");
}
errorLines.AppendLine();
errorLines.Append(Resources._1052);
return errorLines.ToString();
}
catch (MissingManifestResourceException ex)
{
ShowLocalizationErrorDialog(ex);
// Return a fallback message
var errorLines = new System.Text.StringBuilder();
var projectName = _project?.Name ?? "Unknown Project";
errorLines.AppendLine($"Missing package references for project: {projectName}");
foreach (var package in _failedPackageExceptions)
{
errorLines.AppendLine($"{package.Key} - {package.Value.GetType().FullName}: {package.Value.Message}");
}
errorLines.AppendLine();
errorLines.Append("Please add the package references manually using NuGet Package Manager.");
return errorLines.ToString();
}
}
private Guid GetProjectGuid(Project project)
{
ThreadHelper.ThrowIfNotOnUIThread();
Guid projectGuid;
if (project != null && Guid.TryParse(project.Kind, out projectGuid))
{
return projectGuid;
}
return Guid.Empty;
}
private void LogError(string message)
{
ThreadHelper.ThrowIfNotOnUIThread();
ThreadHelper.JoinableTaskFactory.Run(async delegate
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
IVsActivityLog log = ServiceProvider.GlobalProvider.GetService(typeof(SVsActivityLog)) as IVsActivityLog;
if (log != null)
{
int hr = log.LogEntry((uint)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, ToString(), message);
}
});
}
private void ShowLocalizationErrorDialog(MissingManifestResourceException ex)
{
ThreadHelper.ThrowIfNotOnUIThread();
var errorMessage = $"A localization error occurred while loading the template wizard:\n\n{ex.Message}\n\n" +
"The template may not have been installed correctly. Please reinstall the Windows App SDK extension.";
MessageBox.Show(
errorMessage,
"Localization Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
// Also log to activity log
LogError($"Localization error: {ex.Message}");
// Show in output window
ShowOutputWindow($"Localization Error:\n{ex.Message}\n{ex.StackTrace}");
}
private async Task DisplayInfoBarAsync(string errorMessage)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
var infoBar = CreateNuGetInfoBar(errorMessage);
var infoBarUi = CreateInfoBarUI(infoBar);
// Write detailed error message to output window
var detailedErrorMessage = CreateDetailedErrorMessage();
ShowOutputWindow(detailedErrorMessage);
if (infoBarUi == null)
{
LogError("Could not create InfoBar UI element. Logged error message to output window.");
return;
}
infoBarUi.Advise(new NuGetInfoBarUIEvents(detailedErrorMessage), out uint _);
IVsShell shell = ServiceProvider.GlobalProvider.GetService(typeof(SVsShell)) as IVsShell;
if (shell == null)
{
LogError("Could not obtain IVsShell service");
return;
}
// Get the main window's InfoBar host using VSSPROPID_MainWindowInfoBarHost
object infoBarHostObj;
int hr = shell.GetProperty((int)__VSSPROPID7.VSSPROPID_MainWindowInfoBarHost, out infoBarHostObj);
if (!(infoBarHostObj is IVsInfoBarHost infoBarHost))
{
LogError("Could not obtain IVsInfoBarHost service");
return;
}
infoBarHost.AddInfoBar(infoBarUi);
}
private IVsInfoBarUIElement CreateInfoBarUI(IVsInfoBar infoBar)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (infoBar == null)
{
LogError("InfoBar model is null.");
return null;
}
IVsInfoBarUIFactory infoBarUIFactory = ServiceProvider.GlobalProvider.GetService(typeof(SVsInfoBarUIFactory)) as IVsInfoBarUIFactory;
if (infoBarUIFactory == null)
{
LogError("Could not obtain IVsInfoBarUIFactory service");
return null;
}
return infoBarUIFactory.CreateInfoBar(infoBar);
}
private IVsInfoBar CreateNuGetInfoBar(string message)
{
var infoBar = new InfoBarModel(
textSpans: new[]
{
new InfoBarTextSpan(message)
},
actionItems: new InfoBarActionItem[]
{
new InfoBarHyperlink(Resources._1049, "ManageNuGetPackages"),
new InfoBarHyperlink(Resources._1050, "SeeErrorDetails")
},
image: KnownMonikers.NuGetNoColorError,
isCloseButtonVisible: true);
return infoBar;
}
public bool ShouldAddProjectItem(string _)
{
return true;
}
}
}