public static SlnHierarchy FromProjects(IReadOnlyList <SlnProject> projects) { SlnHierarchy hierarchy = new SlnHierarchy(); List <string> projectDirectoryPaths = projects.Where(i => !i.IsMainProject).Select(i => Directory.GetParent(i.FullPath).FullName).ToList(); if (projectDirectoryPaths.Count > 1) { string commonPrefix = GetCommonDirectoryPath(projectDirectoryPaths); foreach (SlnProject project in projects) { if (project.IsMainProject) { continue; } hierarchy.BuildHierarchyBottomUp(project, commonPrefix.TrimEnd(Path.DirectorySeparatorChar)); } } return(hierarchy); }
public void Save(TextWriter writer, bool useFolders) { writer.WriteLine(Header, _fileFormatVersion); if (SolutionItems.Count > 0) { writer.WriteLine($@"Project(""{SlnFolder.FolderProjectTypeGuid.ToSolutionString()}"") = "".Solution Items"", ""Solution Items"", ""{Guid.NewGuid().ToSolutionString()}"" "); writer.WriteLine(" ProjectSection(SolutionItems) = preProject"); foreach (string solutionItem in SolutionItems) { writer.WriteLine($" {solutionItem} = {solutionItem}"); } writer.WriteLine(" EndProjectSection"); writer.WriteLine("EndProject"); } foreach (SlnProject project in _projects) { writer.WriteLine($@"Project(""{project.ProjectTypeGuid.ToSolutionString()}"") = ""{project.Name}"", ""{project.FullPath}"", ""{project.ProjectGuid.ToSolutionString()}"""); writer.WriteLine("EndProject"); } SlnHierarchy hierarchy = null; if (useFolders && _projects.Any(i => !i.IsMainProject)) { hierarchy = new SlnHierarchy(_projects); foreach (SlnFolder folder in hierarchy.Folders) { writer.WriteLine($@"Project(""{folder.ProjectTypeGuid.ToSolutionString()}"") = ""{folder.Name}"", ""{folder.FullPath}"", ""{folder.FolderGuid.ToSolutionString()}"""); writer.WriteLine("EndProject"); } } writer.WriteLine("Global"); if (useFolders && _projects.Count > 1) { 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> allPlatforms = new HashSet <string>(_projects.SelectMany(i => i.Platforms).OrderBy(i => i), StringComparer.OrdinalIgnoreCase); HashSet <string> allConfigurations = new HashSet <string>(_projects.SelectMany(i => i.Configurations), StringComparer.OrdinalIgnoreCase); foreach (string configuration in allConfigurations) { foreach (string platform in allPlatforms.Where(i => !string.Equals(i, "Win32", StringComparison.OrdinalIgnoreCase))) { if (!string.IsNullOrWhiteSpace(configuration) && !string.IsNullOrWhiteSpace(platform)) { writer.WriteLine($" {configuration}|{platform} = {configuration}|{platform}"); } } } writer.WriteLine(" EndGlobalSection"); writer.WriteLine(" GlobalSection(ProjectConfigurationPlatforms) = preSolution"); foreach (SlnProject project in _projects) { foreach (string configuration in allConfigurations) { foreach (string platform in allPlatforms) { if (!string.IsNullOrWhiteSpace(configuration) && !string.IsNullOrWhiteSpace(platform)) { if (project.Configurations.Contains(configuration) && project.Platforms.Contains(platform)) { writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.ActiveCfg = {configuration}|{platform}"); writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.Build.0 = {configuration}|{platform}"); if (project.IsDeployable) { writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.Deploy.0 = {configuration}|{platform}"); } } } } } } writer.WriteLine(" EndGlobalSection"); writer.WriteLine("EndGlobal"); }
public void Save(TextWriter writer) { writer.WriteLine(Header, _fileFormatVersion); foreach (SlnProject project in _projects) { writer.WriteLine($@"Project(""{project.ProjectTypeGuid}"") = ""{project.Name}"", ""{project.FullPath}"", ""{project.ProjectGuid}"""); writer.WriteLine("EndProject"); } if (SolutionItems.Count > 0) { writer.WriteLine($@"Project(""{SlnFolder.ProjectTypeGuid}"") = ""Solution Items"", ""Solution Items"", ""{Guid.NewGuid().ToSolutionString()}"" "); writer.WriteLine(" ProjectSection(SolutionItems) = preProject"); foreach (string solutionItem in SolutionItems) { writer.WriteLine($" {solutionItem} = {solutionItem}"); } writer.WriteLine(" EndProjectSection"); writer.WriteLine("EndProject"); } SlnHierarchy hierarchy = SlnHierarchy.FromProjects(_projects); if (hierarchy.Folders.Count > 0) { foreach (SlnFolder folder in hierarchy.Folders) { writer.WriteLine($@"Project(""{folder.TypeGuid}"") = ""{folder.Name}"", ""{folder.FullPath}"", ""{folder.Guid}"""); writer.WriteLine("EndProject"); } } writer.WriteLine("Global"); writer.WriteLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution"); foreach (string configuration in _configurations) { foreach (string platform in _platforms) { writer.WriteLine($" {configuration}|{platform} = {configuration}|{platform}"); } } writer.WriteLine(" EndGlobalSection"); writer.WriteLine(" GlobalSection(ProjectConfigurationPlatforms) = preSolution"); foreach (SlnProject project in _projects) { foreach (string configuration in _configurations) { foreach (string platform in _platforms) { writer.WriteLine($@" {project.ProjectGuid}.{configuration}|{platform}.ActiveCfg = {configuration}|{platform}"); writer.WriteLine($@" {project.ProjectGuid}.{configuration}|{platform}.Build.0 = {configuration}|{platform}"); } } } writer.WriteLine(" EndGlobalSection"); if (_projects.Count > 1) { writer.WriteLine(@" GlobalSection(NestedProjects) = preSolution"); foreach (KeyValuePair <string, string> nestedProject in hierarchy.Hierarchy) { writer.WriteLine($@" {nestedProject.Key} = {nestedProject.Value}"); } writer.WriteLine(" EndGlobalSection"); } writer.WriteLine("EndGlobal"); }
public void Save(TextWriter writer, bool folders) { writer.WriteLine(Header, _fileFormatVersion); foreach (SlnProject project in _projects) { writer.WriteLine($@"Project(""{project.ProjectTypeGuid.ToSolutionString()}"") = ""{project.Name}"", ""{project.FullPath}"", ""{project.ProjectGuid.ToSolutionString()}"""); writer.WriteLine("EndProject"); } if (SolutionItems.Count > 0) { writer.WriteLine($@"Project(""{SlnFolder.FolderProjectTypeGuid.ToSolutionString()}"") = ""Solution Items"", ""Solution Items"", ""{Guid.NewGuid().ToSolutionString()}"" "); writer.WriteLine(" ProjectSection(SolutionItems) = preProject"); foreach (string solutionItem in SolutionItems) { writer.WriteLine($" {solutionItem} = {solutionItem}"); } writer.WriteLine(" EndProjectSection"); writer.WriteLine("EndProject"); } SlnHierarchy hierarchy = null; if (folders) { hierarchy = SlnHierarchy.FromProjects(_projects); if (hierarchy.Folders.Count > 0) { foreach (SlnFolder folder in hierarchy.Folders) { writer.WriteLine($@"Project(""{folder.ProjectTypeGuid.ToSolutionString()}"") = ""{folder.Name}"", ""{folder.FullPath}"", ""{folder.FolderGuid.ToSolutionString()}"""); writer.WriteLine("EndProject"); } } } writer.WriteLine("Global"); writer.WriteLine(" GlobalSection(SolutionConfigurationPlatforms) = preSolution"); IEnumerable <string> globalConfigurations = new HashSet <string>(_projects.SelectMany(p => p.Configurations)).Distinct(); IEnumerable <string> globalPlatforms = new HashSet <string>(_projects.SelectMany(p => p.Platforms)).Distinct().ToList(); foreach (string configuration in globalConfigurations) { foreach (string platform in globalPlatforms) { if (!string.IsNullOrWhiteSpace(configuration) && !string.IsNullOrWhiteSpace(platform)) { writer.WriteLine($" {configuration}|{platform} = {configuration}|{platform}"); } } } writer.WriteLine(" EndGlobalSection"); writer.WriteLine(" GlobalSection(ProjectConfigurationPlatforms) = preSolution"); foreach (SlnProject project in _projects) { foreach (string configuration in project.Configurations) { foreach (string platform in project.Platforms) { if (!string.IsNullOrWhiteSpace(configuration) && !string.IsNullOrWhiteSpace(platform)) { writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.ActiveCfg = {configuration}|{platform}"); writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.Build.0 = {configuration}|{platform}"); } } } } writer.WriteLine(" EndGlobalSection"); if (folders && _projects.Count > 1) { writer.WriteLine(@" GlobalSection(NestedProjects) = preSolution"); foreach (KeyValuePair <Guid, Guid> nestedProject in hierarchy.Hierarchy) { writer.WriteLine($@" {nestedProject.Key.ToSolutionString()} = {nestedProject.Value.ToSolutionString()}"); } writer.WriteLine(" EndGlobalSection"); } writer.WriteLine("EndGlobal"); }