Skip to content
This repository was archived by the owner on Feb 27, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion AutoT4/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ namespace BennorMcCarthy.AutoT4
{
public static class DTEExtensions
{
private const string SolutionFolder = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this already exist as a constant somewhere? It feels like it would.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but it would require EnvDTE80. Since I did not want to add visual studio version requirement, I have put it in a constant. Then it will work with all visual studio versions.


public static IEnumerable<Project> GetProjectsWithinBuildScope(this DTE dte, vsBuildScope scope)
{
IEnumerable<Project> projects = null;

switch (scope)
{
case vsBuildScope.vsBuildScopeSolution:
projects = dte.Solution.Projects.OfType<Project>();
projects = GetProjectsInSolution(dte.Solution).Where(x => x.ProjectItems != null);
break;
case vsBuildScope.vsBuildScopeProject:
projects = ((object[])dte.ActiveSolutionProjects).OfType<Project>();
Expand All @@ -24,6 +26,50 @@ public static IEnumerable<Project> GetProjectsWithinBuildScope(this DTE dte, vsB

return projects ?? Enumerable.Empty<Project>();
}

private static IEnumerable<Project> GetProjectsInSolution(Solution solution)
{
foreach (Project project in solution.Projects.OfType<Project>())
{
if (project.Kind == SolutionFolder)
{
foreach (Project folderProject in GetProjectsInSolutionFolder(project).Where(x => x.Kind != SolutionFolder))
{
yield return folderProject;
}
}
else
{
yield return project;
}
}
}

private static IEnumerable<Project> GetProjectsInSolutionFolder(Project solutionFolderProject)
{
if (solutionFolderProject.ProjectItems != null)
{
foreach (ProjectItem projectItem in solutionFolderProject.ProjectItems)
{
Project subProject = projectItem.SubProject as Project;

if (subProject != null)
{
if (subProject.Kind == SolutionFolder)
{
foreach (Project folderProject in GetProjectsInSolutionFolder(subProject).Where(x => x.Kind != SolutionFolder))
{
yield return folderProject;
}
}
else
{
yield return subProject;
}
}
}
}
}
}

public static class ProjectItemExtensions
Expand Down