Beispiel #1
0
        private static bool RemoveLeaves(SlnFolder folder)
        {
            // Check if we are a leave node
            if (folder.Folders.Count == 0)
            {
                // We want to remove leaves that have only a single project. There is no need to keep that
                // directory as it would be represented by the project file itself. We ignore folders that
                // contain the Separator character as these are folders which have been collapsed already.
                return(folder.Projects.Count == 1 && !folder.Name.Contains(Separator));
            }

            List <SlnFolder> foldersToRemove = new List <SlnFolder>();

            foreach (SlnFolder child in folder.Folders)
            {
                if (RemoveLeaves(child))
                {
                    folder.Projects.AddRange(child.Projects);
                    foldersToRemove.Add(child);
                }
            }

            foreach (SlnFolder folderToRemove in foldersToRemove)
            {
                folder.Folders.Remove(folderToRemove);
            }

            return(false);
        }
Beispiel #2
0
        private IEnumerable <SlnFolder> EnumerateFolders(SlnFolder folder)
        {
            foreach (SlnFolder child in folder.Folders)
            {
                foreach (SlnFolder enumerateFolder in EnumerateFolders(child))
                {
                    yield return(enumerateFolder);
                }
            }

            yield return(folder);
        }
Beispiel #3
0
        private static void CreateHierarchy(SlnHierarchy hierarchy, SlnProject project)
        {
            FileInfo fileInfo = new FileInfo(project.FullPath);

            DirectoryInfo directoryInfo = fileInfo.Directory;

            if (hierarchy._pathToSlnFolderMap.TryGetValue(directoryInfo !.FullName, out SlnFolder childFolder))
            {
                childFolder.Projects.Add(project);

                return;
            }

            childFolder = new SlnFolder(directoryInfo.FullName);

            childFolder.Projects.Add(project);

            hierarchy._pathToSlnFolderMap.Add(directoryInfo.FullName, childFolder);

            directoryInfo = directoryInfo.Parent;

            if (directoryInfo != null)
            {
                while (directoryInfo != null && !string.Equals(directoryInfo.FullName, hierarchy._rootFolder.FullPath, StringComparison.OrdinalIgnoreCase))
                {
                    if (!hierarchy._pathToSlnFolderMap.TryGetValue(directoryInfo.FullName, out SlnFolder folder1))
                    {
                        folder1 = new SlnFolder(directoryInfo.FullName);
                        hierarchy._pathToSlnFolderMap.Add(directoryInfo.FullName, folder1);
                    }

                    childFolder.Parent = folder1;

                    if (!folder1.Folders.Contains(childFolder))
                    {
                        folder1.Folders.Add(childFolder);
                    }

                    directoryInfo = directoryInfo.Parent;

                    childFolder = folder1;
                }

                if (!hierarchy._rootFolder.Folders.Contains(childFolder))
                {
                    hierarchy._rootFolder.Folders.Add(childFolder);
                    childFolder.Parent = hierarchy._rootFolder;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SlnHierarchy" /> class.
        /// </summary>
        /// <param name="projects">The set of projects that should be placed in the hierarchy.</param>
        /// <param name="collapseFolders">An optional value indicating whether or not folders containing a single item should be collapsed into their parent folder.</param>
        public SlnHierarchy(IReadOnlyList <SlnProject> projects, bool collapseFolders = false)
        {
            _rootFolder = GetRootFolder(projects);

            foreach (SlnProject project in projects.Where(i => !i.IsMainProject))
            {
                CreateHierarchy(project);
            }

            if (collapseFolders)
            {
                CollapseFolders(_rootFolder.Folders);
            }
        }
Beispiel #5
0
        private IEnumerable <SlnFolder> EnumerateFolders(SlnFolder folder)
        {
            foreach (SlnFolder child in folder.Folders)
            {
                foreach (SlnFolder enumerateFolder in EnumerateFolders(child).Where(i => !string.IsNullOrWhiteSpace(i.Name)))
                {
                    yield return(enumerateFolder);
                }
            }

            if (!string.IsNullOrWhiteSpace(folder.Name))
            {
                yield return(folder);
            }
        }
Beispiel #6
0
        private void CollapseFolders(IReadOnlyCollection <SlnFolder> folders)
        {
            foreach (SlnFolder folder in folders)
            {
                CollapseFolders(folder.Folders);
            }

            foreach (SlnFolder folderWithSingleChild in folders.Where(i => i.Folders.Count == 1))
            {
                SlnFolder child = folderWithSingleChild.Folders.First();

                folderWithSingleChild.Name = $"{folderWithSingleChild.Name}-{child.Name}";
                folderWithSingleChild.Projects.AddRange(child.Projects);
                folderWithSingleChild.Folders.Clear();
            }
        }
Beispiel #7
0
        /// <summary>
        /// Creates a <see cref="SlnHierarchy" /> based on the directory structure of the specified projects.
        /// </summary>
        /// <param name="projects">The set of projects that should be placed in the hierarchy.</param>
        /// <param name="collapseFolders">An optional value indicating whether or not folders containing a single item should be collapsed into their parent folder.</param>
        /// <returns>A <see cref="SlnHierarchy" /> based on the directory structure of the specified projects.</returns>
        public static SlnHierarchy CreateFromProjectDirectories(IReadOnlyList <SlnProject> projects, bool collapseFolders = false)
        {
            SlnFolder rootFolder = GetRootFolder(projects);

            SlnHierarchy hierarchy = new SlnHierarchy(rootFolder);

            foreach (SlnProject project in projects.Where(i => !i.IsMainProject))
            {
                CreateHierarchy(hierarchy, project);
            }

            if (collapseFolders)
            {
                CollapseFolders(rootFolder.Folders);
                RemoveLeaves(rootFolder);
            }

            return(hierarchy);
        }
Beispiel #8
0
        /// <summary>
        /// Creates a hierarchy based on solution folders declared by projects.
        /// </summary>
        /// <param name="projects">A <see cref="IReadOnlyList{T}" /> of projects.</param>
        /// <returns>A <see cref="SlnHierarchy" /> object containing solution folders and projects.</returns>
        public static SlnHierarchy CreateFromProjectSolutionFolder(IReadOnlyList <SlnProject> projects)
        {
            SlnHierarchy hierarchy = new SlnHierarchy(new SlnFolder(string.Empty));

            foreach (SlnProject project in projects.Where(i => !string.IsNullOrWhiteSpace(i.SolutionFolder)))
            {
                if (!hierarchy._pathToSlnFolderMap.TryGetValue(project.SolutionFolder, out SlnFolder folder))
                {
                    folder = new SlnFolder(project.SolutionFolder)
                    {
                        Parent = hierarchy._rootFolder,
                    };

                    hierarchy._pathToSlnFolderMap.Add(project.SolutionFolder, folder);

                    hierarchy._rootFolder.Folders.Add(folder);
                }

                folder.Projects.Add(project);
            }

            return(hierarchy);
        }
Beispiel #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SlnHierarchy" /> class.
 /// </summary>
 private SlnHierarchy(SlnFolder rootFolder)
 {
     _rootFolder = rootFolder ?? throw new ArgumentNullException(nameof(rootFolder));
 }