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; } } }
/// <summary> /// Adds the specified projects to the solution file. /// </summary> /// <param name="projects">An <see cref="IEnumerable{T}" /> of projects to add.</param> /// <param name="customProjectTypeGuids">An <see cref="IReadOnlyDictionary{TKey,TValue}" /> containing any custom project type GUIDs to use.</param> /// <param name="mainProjectFullPath">Optional full path to the main project.</param> public void AddProjects(IEnumerable <Project> projects, IReadOnlyDictionary <string, Guid> customProjectTypeGuids, string mainProjectFullPath = null) { _projects.AddRange( projects .Distinct(new EqualityComparer <Project>((x, y) => string.Equals(x.FullPath, y.FullPath, StringComparison.OrdinalIgnoreCase), i => i.FullPath.GetHashCode())) .Select(i => { SlnProject project = SlnProject.FromProject(i, customProjectTypeGuids, string.Equals(i.FullPath, mainProjectFullPath, StringComparison.OrdinalIgnoreCase)); if (project != null && ExistingProjectGuids != null && ExistingProjectGuids.TryGetValue(project.FullPath, out Guid projectGuid)) { project.ProjectGuid = projectGuid; } return(project); }) .Where(i => i != null)); }
/// <summary> /// Generates a solution file. /// </summary> /// <param name="arguments">The current <see cref="ProgramArguments" />.</param> /// <param name="projects">A <see cref="IEnumerable{String}" /> containing the entry projects.</param> /// <param name="logger">A <see cref="ISlnGenLogger" /> to use for logging.</param> /// <returns>A <see cref="Tuple{String, Int32, Int32, Guid}" /> with the full path to the solution file, the count of custom project type GUIDs used, the count of solution items, and the solution GUID.</returns> public static (string solutionFileFullPath, int customProjectTypeGuidCount, int solutionItemCount, Guid solutionGuid) GenerateSolutionFile(ProgramArguments arguments, IEnumerable <Project> projects, ISlnGenLogger logger) { List <Project> projectList = projects.ToList(); Project firstProject = projectList.First(); IReadOnlyDictionary <string, Guid> customProjectTypeGuids = SlnProject.GetCustomProjectTypeGuids(firstProject); IReadOnlyCollection <string> solutionItems = SlnProject.GetSolutionItems(firstProject, logger).ToList(); string solutionFileFullPath = arguments.SolutionFileFullPath?.LastOrDefault(); if (solutionFileFullPath.IsNullOrWhiteSpace()) { string solutionDirectoryFullPath = arguments.SolutionDirectoryFullPath?.LastOrDefault(); if (solutionDirectoryFullPath.IsNullOrWhiteSpace()) { solutionDirectoryFullPath = firstProject.DirectoryPath; } string solutionFileName = Path.ChangeExtension(Path.GetFileName(firstProject.FullPath), "sln"); solutionFileFullPath = Path.Combine(solutionDirectoryFullPath !, solutionFileName); } logger.LogMessageHigh($"Generating Visual Studio solution \"{solutionFileFullPath}\" ..."); if (customProjectTypeGuids.Count > 0) { logger.LogMessageLow("Custom Project Type GUIDs:"); foreach (KeyValuePair <string, Guid> item in customProjectTypeGuids) { logger.LogMessageLow(" {0} = {1}", item.Key, item.Value); } } SlnFile solution = new SlnFile { Platforms = arguments.GetPlatforms(), Configurations = arguments.GetConfigurations(), }; if (SlnFile.TryParseExistingSolution(solutionFileFullPath, out Guid solutionGuid, out IReadOnlyDictionary <string, Guid> projectGuidsByPath)) { logger.LogMessageNormal("Updating existing solution file and reusing Visual Studio cache"); solution.SolutionGuid = solutionGuid; solution.ExistingProjectGuids = projectGuidsByPath; arguments.LoadProjectsInVisualStudio = new[] { bool.TrueString }; } solution.AddProjects(projectList, customProjectTypeGuids, arguments.IgnoreMainProject ? null : firstProject.FullPath); solution.AddSolutionItems(solutionItems); solution.Save(solutionFileFullPath, arguments.EnableFolders(), arguments.EnableCollapseFolders()); return(solutionFileFullPath, customProjectTypeGuids.Count, solutionItems.Count, solution.SolutionGuid); }
private bool TryGetProjectSolutionPlatform(string solutionPlatform, SlnProject project, out string projectSolutionPlatform, out string projectBuildPlatform) { projectSolutionPlatform = null; projectBuildPlatform = null; bool containsWin32 = false; bool containsX64 = false; bool containsAmd64 = false; bool containsX86 = false; bool containsAnyCPU = false; foreach (string projectPlatform in project.Platforms) { if (string.Equals(projectPlatform, solutionPlatform, StringComparison.OrdinalIgnoreCase) || string.Equals(projectPlatform.ToSolutionPlatform(), solutionPlatform, StringComparison.OrdinalIgnoreCase)) { projectSolutionPlatform = solutionPlatform; projectBuildPlatform = solutionPlatform; return(true); } switch (projectPlatform.ToLowerInvariant()) { case "anycpu": case "any cpu": containsAnyCPU = true; break; case "x64": containsX64 = true; break; case "x86": containsX86 = true; break; case "amd64": containsAmd64 = true; break; case "win32": containsWin32 = true; break; } } if (string.Equals(solutionPlatform, "Any CPU", StringComparison.OrdinalIgnoreCase)) { if (containsX64) { projectSolutionPlatform = projectBuildPlatform = "x64"; return(true); } if (containsX86) { projectSolutionPlatform = projectBuildPlatform = "x86"; return(true); } if (containsAmd64) { projectSolutionPlatform = projectBuildPlatform = "amd64"; return(true); } if (containsWin32) { projectSolutionPlatform = projectBuildPlatform = "Win32"; return(true); } } if (string.Equals(solutionPlatform, "x86", StringComparison.OrdinalIgnoreCase)) { if (containsWin32) { projectSolutionPlatform = projectBuildPlatform = "Win32"; return(true); } if (containsAnyCPU) { projectSolutionPlatform = projectBuildPlatform = "Any CPU"; return(true); } } if (string.Equals(solutionPlatform, "x64", StringComparison.OrdinalIgnoreCase)) { if (containsAmd64) { projectSolutionPlatform = projectBuildPlatform = "amd64"; return(true); } if (containsAnyCPU) { projectSolutionPlatform = projectBuildPlatform = "Any CPU"; return(true); } } projectSolutionPlatform = project.Platforms.First().ToSolutionPlatform(); return(false); }
private bool TryGetProjectSolutionConfiguration(string solutionConfiguration, SlnProject project, out string projectSolutionConfiguration) { foreach (string projectConfiguration in project.Configurations) { if (string.Equals(projectConfiguration, solutionConfiguration, StringComparison.OrdinalIgnoreCase)) { projectSolutionConfiguration = solutionConfiguration; return(true); } } projectSolutionConfiguration = project.Configurations.First(); return(false); }