|
| 1 | +using EnvDTE; |
| 2 | +using EnvDTE80; |
| 3 | + |
| 4 | +namespace CodingWithCalvin.ProjectRenamifier.Services |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Service for managing solution folder operations. |
| 8 | + /// </summary> |
| 9 | + internal static class SolutionFolderService |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Gets the solution folder that contains the specified project, if any. |
| 13 | + /// </summary> |
| 14 | + /// <param name="project">The project to find the parent folder for.</param> |
| 15 | + /// <returns>The parent solution folder project, or null if the project is at the solution root.</returns> |
| 16 | + public static Project GetParentSolutionFolder(Project project) |
| 17 | + { |
| 18 | + ThreadHelper.ThrowIfNotOnUIThread(); |
| 19 | + |
| 20 | + if (project?.ParentProjectItem?.ContainingProject != null) |
| 21 | + { |
| 22 | + var parent = project.ParentProjectItem.ContainingProject; |
| 23 | + |
| 24 | + // Verify it's actually a solution folder |
| 25 | + if (parent.Kind == EnvDTE.Constants.vsProjectKindSolutionItems) |
| 26 | + { |
| 27 | + return parent; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Adds a project to the solution, placing it in the specified solution folder if provided. |
| 36 | + /// </summary> |
| 37 | + /// <param name="solution">The solution to add the project to.</param> |
| 38 | + /// <param name="projectFilePath">The full path to the project file.</param> |
| 39 | + /// <param name="parentSolutionFolder">The solution folder to add the project to, or null for solution root.</param> |
| 40 | + /// <returns>The added project.</returns> |
| 41 | + public static Project AddProjectToSolution(Solution solution, string projectFilePath, Project parentSolutionFolder) |
| 42 | + { |
| 43 | + ThreadHelper.ThrowIfNotOnUIThread(); |
| 44 | + |
| 45 | + if (parentSolutionFolder != null) |
| 46 | + { |
| 47 | + // Add to the solution folder |
| 48 | + var solutionFolder = (SolutionFolder)parentSolutionFolder.Object; |
| 49 | + return solutionFolder.AddFromFile(projectFilePath); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + // Add to solution root |
| 54 | + return solution.AddFromFile(projectFilePath); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments