Browse Source

nfc: Clean up multiple enumerations of IEnumerable<> in WriteSolutionFile().

(This is potentially expensive and the method is public, just a minor code smell.)
pull/3502/head
Peter Crabtree 4 months ago
parent
commit
d0e62a980f
  1. 30
      ICSharpCode.Decompiler/Solution/SolutionCreator.cs

30
ICSharpCode.Decompiler/Solution/SolutionCreator.cs

@ -29,7 +29,7 @@ namespace ICSharpCode.Decompiler.Solution
/// </summary> /// </summary>
public static class SolutionCreator public static class SolutionCreator
{ {
private static readonly XNamespace ProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); static readonly XNamespace ProjectFileNamespace = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003");
/// <summary> /// <summary>
/// Writes a solution file to the specified <paramref name="targetFile"/>. /// Writes a solution file to the specified <paramref name="targetFile"/>.
@ -52,20 +52,22 @@ namespace ICSharpCode.Decompiler.Solution
throw new ArgumentNullException(nameof(projects)); throw new ArgumentNullException(nameof(projects));
} }
if (!projects.Any()) var projectList = projects.ToList();
if (!projectList.Any())
{ {
throw new InvalidOperationException("At least one project is expected."); throw new InvalidOperationException("At least one project is expected.");
} }
using (var writer = new StreamWriter(targetFile)) using (var writer = new StreamWriter(targetFile))
{ {
WriteSolutionFile(writer, projects, targetFile); WriteSolutionFile(writer, projectList, targetFile);
} }
FixProjectReferences(projects); FixProjectReferences(projectList);
} }
private static void WriteSolutionFile(TextWriter writer, IEnumerable<ProjectItem> projects, string solutionFilePath) static void WriteSolutionFile(TextWriter writer, List<ProjectItem> projects, string solutionFilePath)
{ {
WriteHeader(writer); WriteHeader(writer);
WriteProjects(writer, projects, solutionFilePath); WriteProjects(writer, projects, solutionFilePath);
@ -90,7 +92,7 @@ namespace ICSharpCode.Decompiler.Solution
writer.WriteLine("MinimumVisualStudioVersion = 10.0.40219.1"); writer.WriteLine("MinimumVisualStudioVersion = 10.0.40219.1");
} }
private static void WriteProjects(TextWriter writer, IEnumerable<ProjectItem> projects, string solutionFilePath) static void WriteProjects(TextWriter writer, List<ProjectItem> projects, string solutionFilePath)
{ {
foreach (var project in projects) foreach (var project in projects)
{ {
@ -103,7 +105,7 @@ namespace ICSharpCode.Decompiler.Solution
} }
} }
private static IEnumerable<string> WriteSolutionConfigurations(TextWriter writer, IEnumerable<ProjectItem> projects) static List<string> WriteSolutionConfigurations(TextWriter writer, List<ProjectItem> projects)
{ {
var platforms = projects.GroupBy(p => p.PlatformName).Select(g => g.Key).ToList(); var platforms = projects.GroupBy(p => p.PlatformName).Select(g => g.Key).ToList();
@ -125,10 +127,10 @@ namespace ICSharpCode.Decompiler.Solution
return platforms; return platforms;
} }
private static void WriteProjectConfigurations( static void WriteProjectConfigurations(
TextWriter writer, TextWriter writer,
IEnumerable<ProjectItem> projects, List<ProjectItem> projects,
IEnumerable<string> solutionPlatforms) List<string> solutionPlatforms)
{ {
writer.WriteLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution"); writer.WriteLine("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution");
@ -152,9 +154,11 @@ namespace ICSharpCode.Decompiler.Solution
writer.WriteLine("\tEndGlobalSection"); writer.WriteLine("\tEndGlobalSection");
} }
private static void FixProjectReferences(IEnumerable<ProjectItem> projects) static void FixProjectReferences(List<ProjectItem> projects)
{ {
var projectsMap = projects.ToDictionary(p => p.ProjectName, p => p); var projectsMap = projects.ToDictionary(
p => p.ProjectName,
p => p);
foreach (var project in projects) foreach (var project in projects)
{ {
@ -192,7 +196,7 @@ namespace ICSharpCode.Decompiler.Solution
} }
} }
private static string GetRelativePath(string fromFilePath, string toFilePath) static string GetRelativePath(string fromFilePath, string toFilePath)
{ {
Uri fromUri = new Uri(fromFilePath); Uri fromUri = new Uri(fromFilePath);
Uri toUri = new Uri(toFilePath); Uri toUri = new Uri(toFilePath);

Loading…
Cancel
Save