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 folders, IReadOnlyDictionary <string, string> configurationMap, IReadOnlyDictionary <string, string> platformMap) { configurationMap = configurationMap == null ? new Dictionary <string, string>() : configurationMap; platformMap = platformMap == null ? new Dictionary <string, string>() : platformMap; var vsver = Version.Parse(_vsVersion); writer.WriteLine(Header, _fileFormatVersion); writer.WriteLine($"# Visual Studio Version {vsver.Major}"); writer.WriteLine($"VisualStudioVersion = {_vsVersion}"); writer.WriteLine($"MinimumVisualStudioVersion = {_minVsVersion}"); var slnFolders = BuildSlnFolderList(); var projectMap = _projects.ToDictionary(x => x.FullPath); foreach (SlnProject project in _projects) { writer.WriteLine($@"Project(""{project.ProjectTypeGuid.ToSolutionString()}"") = ""{project.Name}"", ""{project.FullPath}"", ""{project.ProjectGuid.ToSolutionString()}"""); if (project.DependingProjects != null && project.DependingProjects.Length != 0) { bool isFound = false; foreach (var dep in project.DependingProjects.Where(x => projectMap.ContainsKey(x))) { if (!isFound) { writer.WriteLine("\t\tProjectSection(ProjectDependencies) = postProject"); isFound = true; } writer.WriteLine($"\t\t\t{projectMap[dep].ProjectGuid.ToSolutionString()} = {projectMap[dep].ProjectGuid.ToSolutionString()}"); } if (isFound) { writer.WriteLine("\t\tEndProjectSection"); } } writer.WriteLine("EndProject"); } if (SolutionItems.Count > 0) { foreach (var slnFolder in slnFolders) { writer.WriteLine($@"Project(""{SlnFolder.FolderProjectTypeGuid.ToSolutionString()}"") = ""{slnFolder.Value.Name}"", ""{slnFolder.Value.Name}"", ""{slnFolder.Value.FolderGuid.ToSolutionString()}"" "); writer.WriteLine("\tProjectSection(SolutionItems) = preProject"); foreach (var solutionItem in SolutionItems.Where(x => x.Folder.FolderGuid == slnFolder.Value.FolderGuid)) { writer.WriteLine($"\t\t{solutionItem.FullPath} = {solutionItem.FullPath}"); } writer.WriteLine("\tEndProjectSection"); 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"); var(globalConfigurations, globalPlatforms) = GetSolutionConfigurations(); WriteSolutionConfigurations(writer, globalConfigurations, globalPlatforms); WriteProjectConfigurations(writer, globalConfigurations, globalPlatforms, configurationMap, platformMap); 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"); } { bool isFound = false; foreach (var slnFolder in _solutionFolders) { if (slnFolders.ContainsKey(Path.GetDirectoryName(slnFolder.Key))) { if (!isFound) { writer.WriteLine("\tGlobalSection(NestedProjects) = preSolution"); isFound = true; } writer.WriteLine($"\t\t{slnFolder.Value.FolderGuid.ToSolutionString()} = {slnFolders[Path.GetDirectoryName(slnFolder.Key)].FolderGuid.ToSolutionString()}"); } var proj = _projects.FirstOrDefault(x => x.SlnFolder != null && x.SlnFolder == slnFolder.Key); if (proj != null) { if (!isFound) { writer.WriteLine("\tGlobalSection(NestedProjects) = preSolution"); isFound = true; } writer.WriteLine($"\t\t{proj.ProjectGuid.ToSolutionString()} = {slnFolder.Value.FolderGuid.ToSolutionString()}"); } } if (isFound) { writer.WriteLine("\tEndGlobalSection"); } } writer.WriteLine("EndGlobal"); }