From 97286107d907ebf7f31ae54e6897710afb095e4f Mon Sep 17 00:00:00 2001 From: "maly.lemire" Date: Tue, 14 Jul 2015 14:05:08 -0400 Subject: [PATCH] Added support for solution folders. - Updated DTEExtensions in ExtensionMethods. --- AutoT4/ExtensionMethods.cs | 48 +++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/AutoT4/ExtensionMethods.cs b/AutoT4/ExtensionMethods.cs index 650bc34..471a4c9 100644 --- a/AutoT4/ExtensionMethods.cs +++ b/AutoT4/ExtensionMethods.cs @@ -8,6 +8,8 @@ namespace BennorMcCarthy.AutoT4 { public static class DTEExtensions { + private const string SolutionFolder = "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}"; + public static IEnumerable GetProjectsWithinBuildScope(this DTE dte, vsBuildScope scope) { IEnumerable projects = null; @@ -15,7 +17,7 @@ public static IEnumerable GetProjectsWithinBuildScope(this DTE dte, vsB switch (scope) { case vsBuildScope.vsBuildScopeSolution: - projects = dte.Solution.Projects.OfType(); + projects = GetProjectsInSolution(dte.Solution).Where(x => x.ProjectItems != null); break; case vsBuildScope.vsBuildScopeProject: projects = ((object[])dte.ActiveSolutionProjects).OfType(); @@ -24,6 +26,50 @@ public static IEnumerable GetProjectsWithinBuildScope(this DTE dte, vsB return projects ?? Enumerable.Empty(); } + + private static IEnumerable GetProjectsInSolution(Solution solution) + { + foreach (Project project in solution.Projects.OfType()) + { + 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 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