Exemple #1
0
        /// <summary>
        /// Processes the solution file to extract the collection of solution folders and their IDs.
        /// </summary>
        /// <param name="slnFile">The solution file to examine.</param>
        /// <returns>A dictionary of all of the solution folders present in the solution mapped to their IDs.</returns>
        /// <example>"SomeFolder\SomeSubFolder" => "{SOME-GUID}"</example>
        public static IDictionary <string, string> GetSolutionFolderPaths(this SlnFile slnFile)
        {
            var nestedProjects = slnFile.GetSolutionFolderSection();

            if (nestedProjects == null)
            {
                return(new Dictionary <string, string>());
            }

            return(slnFile.GetSolutionFolderPaths(nestedProjects.Properties));
        }
        public static void AddSolutionFolders(this SlnFile slnFile, SlnProject slnProject)
        {
            if (slnProject == null)
            {
                throw new ArgumentException();
            }

            var solutionFolders = slnProject.GetSolutionFoldersFromProject();

            if (solutionFolders.Any())
            {
                var nestedProjectsSection = slnFile.Sections.GetOrCreateSection(
                    "NestedProjects",
                    SlnSectionType.PreProcess);

                var pathToGuidMap = slnFile.GetSolutionFolderPaths(nestedProjectsSection.Properties);

                string parentDirGuid           = null;
                var    solutionFolderHierarchy = string.Empty;
                foreach (var dir in solutionFolders)
                {
                    solutionFolderHierarchy = Path.Combine(solutionFolderHierarchy, dir);
                    if (pathToGuidMap.ContainsKey(solutionFolderHierarchy))
                    {
                        parentDirGuid = pathToGuidMap[solutionFolderHierarchy];
                    }
                    else
                    {
                        var solutionFolder = new SlnProject
                        {
                            Id       = Guid.NewGuid().ToString("B").ToUpper(),
                            TypeGuid = ProjectTypeGuids.SolutionFolderGuid,
                            Name     = dir,
                            FilePath = dir
                        };

                        slnFile.Projects.Add(solutionFolder);

                        if (parentDirGuid != null)
                        {
                            nestedProjectsSection.Properties[solutionFolder.Id] = parentDirGuid;
                        }
                        parentDirGuid = solutionFolder.Id;
                    }
                }

                nestedProjectsSection.Properties[slnProject.Id] = parentDirGuid;
            }
        }