Skip to content

Commit 68b664e

Browse files
committed
Refs #29 - VS2012 Extension was crashing in ClearPreviousErrors; Nesting outputs using "DependentUpon" property was crashing on non-sdk (legacy) projects.
1 parent d022850 commit 68b664e

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/VisualStudio/Shared/RunTemplate/RunTemplateWrapper.cs

+33-6
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private static IVsOutputWindowPane CreateCustomPane()
7777
string customTitle = "CodegenCS Template";
7878
outWindow.CreatePane(ref codegenCSOutputPane, customTitle, 1, 1);
7979
IVsOutputWindowPane customPane;
80-
outWindow.GetPane(ref codegenCSOutputPane, out customPane);
80+
outWindow.GetPane(ref codegenCSOutputPane, out customPane);
8181
return customPane;
8282
}
8383

@@ -156,7 +156,7 @@ public async Task RunAsync()
156156
void ClearPreviousErrors(ErrorListProvider errorListProvider)
157157
{
158158
// For VS2022 edition (libraries version >=17.x) the tasks (even ErrorTask) all inherit from Microsoft.VisualStudio.Shell.TaskListItem
159-
foreach (var task in errorListProvider.Tasks.OfType<TaskListItem>())
159+
foreach (var task in errorListProvider.Tasks.OfType<TaskListItem>().ToList())
160160
{
161161
if (task.Document == _templateItemPath || !File.Exists(task.Document))
162162
errorListProvider.Tasks.Remove(task);
@@ -350,15 +350,42 @@ async Task AddFilesToSolutionAsync(string outputFolder, ProjectItem parentItem)
350350
outputItem = parentItem.ProjectItems.AddFromFile(fullPath);
351351
await Task.Delay(1); // let UI refresh
352352
}
353-
if (outputItem.Properties.Item("DependentUpon").Value.ToString() != parentItem.Name)
354-
outputItem.Properties.Item("DependentUpon").Value = parentItem.Name; // TODO: check if DependentUpon works for old non-sdk-style. If needed check https://github.com/madskristensen/FileNesting
355-
if (outputItem.Properties.Item("ItemType").Value.ToString() != o.FileType.ToString())
356-
outputItem.Properties.Item("ItemType").Value = o.FileType.ToString();
353+
if (await ContainsPropertyAsync(outputItem, "DependentUpon") && outputItem.Properties.Item("DependentUpon").Value.ToString() != parentItem.Name)
354+
await SetItemPropertyAsync(outputItem, "DependentUpon", parentItem.Name); // for non-sdk projects this property doesn't exist but the item is just nested automatically?!
355+
if (await ContainsPropertyAsync(outputItem, "ItemType") && outputItem.Properties.Item("ItemType").Value.ToString() != o.FileType.ToString())
356+
await SetItemPropertyAsync(outputItem, "ItemType", o.FileType.ToString());
357357
break;
358358
}
359359
}
360360
}
361361

362+
public static async Task<bool> ContainsPropertyAsync(ProjectItem projectItem, string propertyName)
363+
{
364+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
365+
if (projectItem.Properties != null)
366+
{
367+
foreach (Property item in projectItem.Properties)
368+
{
369+
if (item != null && item.Name == propertyName)
370+
return true;
371+
}
372+
}
373+
return false;
374+
}
375+
376+
private static async Task<bool> SetItemPropertyAsync(ProjectItem item, string propertyName, string value)
377+
{
378+
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
379+
if (await ContainsPropertyAsync(item, propertyName))
380+
{
381+
item.Properties.Item(propertyName).Value = value;
382+
return true;
383+
}
384+
385+
return false;
386+
}
387+
388+
362389
void AddError(string errorMessage, TaskCategory category, int line, int column)
363390
{
364391
#if VS2022_OR_NEWER

0 commit comments

Comments
 (0)