Example #1
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;
                }
            }
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
0
        /// <summary>
        /// Saves the Visual Studio solution to a file.
        /// </summary>
        /// <param name="rootPath">A root path for the solution to make other paths relative to.</param>
        /// <param name="writer">The <see cref="TextWriter" /> to save the solution file to.</param>
        /// <param name="useFolders">Specifies if folders should be created.</param>
        /// <param name="collapseFolders">An optional value indicating whether or not folders containing a single item should be collapsed into their parent folder.</param>
        internal void Save(string rootPath, TextWriter writer, bool useFolders, bool collapseFolders = false)
        {
            writer.WriteLine(Header, _fileFormatVersion);

            if (SolutionItems.Count > 0)
            {
                writer.WriteLine($@"Project(""{SlnFolder.FolderProjectTypeGuid}"") = ""Solution Items"", ""Solution Items"", ""{Guid.NewGuid().ToSolutionString()}"" ");
                writer.WriteLine("	ProjectSection(SolutionItems) = preProject");
                foreach (string solutionItem in SolutionItems.Select(i => i.ToRelativePath(rootPath)))
                {
                    writer.WriteLine($"		{solutionItem} = {solutionItem}");
                }

                writer.WriteLine("	EndProjectSection");
                writer.WriteLine("EndProject");
            }

            foreach (SlnProject project in _projects.OrderBy(i => i.FullPath))
            {
                writer.WriteLine($@"Project(""{project.ProjectTypeGuid.ToSolutionString()}"") = ""{project.Name}"", ""{project.FullPath.ToRelativePath(rootPath)}"", ""{project.ProjectGuid.ToSolutionString()}""");
                writer.WriteLine("EndProject");
            }

            SlnHierarchy hierarchy = null;

            if (useFolders && _projects.Any(i => !i.IsMainProject))
            {
                hierarchy = new SlnHierarchy(_projects, collapseFolders);

                foreach (SlnFolder folder in hierarchy.Folders)
                {
                    writer.WriteLine($@"Project(""{folder.ProjectTypeGuid}"") = ""{folder.Name}"", ""{folder.FullPath.ToRelativePath(rootPath)}"", ""{folder.FolderGuid.ToSolutionString()}""");
                    writer.WriteLine("EndProject");
                }
            }

            writer.WriteLine("Global");

            if (useFolders && _projects.Count > 1 && hierarchy != null)
            {
                writer.WriteLine(@"	GlobalSection(NestedProjects) = preSolution");

                foreach (SlnFolder folder in hierarchy.Folders.Where(i => i.Parent != null))
                {
                    foreach (SlnProject project in folder.Projects)
                    {
                        writer.WriteLine($@"		{project.ProjectGuid.ToSolutionString()} = {folder.FolderGuid.ToSolutionString()}");
                    }

                    writer.WriteLine($@"		{folder.FolderGuid.ToSolutionString()} = {folder.Parent.FolderGuid.ToSolutionString()}");
                }

                writer.WriteLine("	EndGlobalSection");
            }

            writer.WriteLine("	GlobalSection(SolutionConfigurationPlatforms) = preSolution");

            HashSet <string> solutionPlatforms = Platforms != null && Platforms.Any()
                ? new HashSet <string>(GetValidSolutionPlatforms(Platforms), StringComparer.OrdinalIgnoreCase)
                : new HashSet <string>(GetValidSolutionPlatforms(_projects.SelectMany(i => i.Platforms)), StringComparer.OrdinalIgnoreCase);

            HashSet <string> solutionConfigurations = Configurations != null && Configurations.Any()
                ? new HashSet <string>(Configurations, StringComparer.OrdinalIgnoreCase)
                : new HashSet <string>(_projects.SelectMany(i => i.Configurations).Where(i => !i.IsNullOrWhiteSpace()), StringComparer.OrdinalIgnoreCase);

            foreach (string configuration in solutionConfigurations)
            {
                foreach (string platform in solutionPlatforms)
                {
                    if (!string.IsNullOrWhiteSpace(configuration) && !string.IsNullOrWhiteSpace(platform))
                    {
                        writer.WriteLine($"		{configuration}|{platform} = {configuration}|{platform}");
                    }
                }
            }

            writer.WriteLine("	EndGlobalSection");

            writer.WriteLine("	GlobalSection(ProjectConfigurationPlatforms) = postSolution");

            foreach (SlnProject project in _projects)
            {
                string projectGuid = project.ProjectGuid.ToSolutionString();

                foreach (string configuration in solutionConfigurations)
                {
                    bool foundConfiguration = TryGetProjectSolutionConfiguration(configuration, project, out string projectSolutionConfiguration);

                    foreach (string platform in solutionPlatforms)
                    {
                        bool foundPlatform = TryGetProjectSolutionPlatform(platform, project, out string projectSolutionPlatform, out string projectBuildPlatform);

                        writer.WriteLine($@"		{projectGuid}.{configuration}|{platform}.ActiveCfg = {projectSolutionConfiguration}|{projectSolutionPlatform}");

                        if (foundPlatform && foundConfiguration)
                        {
                            writer.WriteLine($@"		{projectGuid}.{configuration}|{platform}.Build.0 = {projectSolutionConfiguration}|{projectBuildPlatform}");
                        }

                        if (project.IsDeployable)
                        {
                            writer.WriteLine($@"		{projectGuid}.{configuration}|{platform}.Deploy.0 = {projectSolutionConfiguration}|{projectSolutionPlatform}");
                        }
                    }
                }
            }

            writer.WriteLine("	EndGlobalSection");

            writer.WriteLine("	GlobalSection(SolutionProperties) = preSolution");
            writer.WriteLine("		HideSolutionNode = FALSE");
            writer.WriteLine("	EndGlobalSection");

            writer.WriteLine("	GlobalSection(ExtensibilityGlobals) = postSolution");
            writer.WriteLine($"		SolutionGuid = {SolutionGuid.ToSolutionString()}");
            writer.WriteLine("	EndGlobalSection");

            writer.WriteLine("EndGlobal");
        }