Skip to content

Commit

Permalink
Implement parsing of ProjectConfigurationPlatforms
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
wgnf committed Sep 19, 2021
1 parent 6de131b commit 7f9565a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/SlnParser.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ public void Should_Be_Able_To_Parse_SlnParser_Solution_Correctly()
.Should()
.Be(ProjectType.CSharpClassLibrary);

solution
.AllProjects
.ElementAt(0)
.As<SolutionProject>()
.ConfigurationPlatforms
.Should()
.Contain(config => config.Name.Equals("Debug|Any CPU.ActiveCfg"));

// 2. Project - Solution Folder
solution
.AllProjects
Expand Down Expand Up @@ -263,6 +271,14 @@ public void Should_Be_Able_To_Parse_SlnParser_Solution_Correctly()
.Type
.Should()
.Be(ProjectType.CSharpClassLibrary);

solution
.AllProjects
.ElementAt(2)
.As<SolutionProject>()
.ConfigurationPlatforms
.Should()
.Contain(config => config.Name.Equals("Debug|x86.Build.0"));
}

[Fact]
Expand Down
21 changes: 20 additions & 1 deletion src/SlnParser/Contracts/SolutionProject.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;

namespace SlnParser.Contracts
{
Expand All @@ -8,6 +11,9 @@ namespace SlnParser.Contracts
/// </summary>
public class SolutionProject : IProject
{
private readonly ICollection<ConfigurationPlatform> _configurationPlatforms =
new Collection<ConfigurationPlatform>();

/// <summary>
/// Creates a new instance of <see cref="SolutionProject"/>
/// </summary>
Expand All @@ -28,7 +34,7 @@ public SolutionProject(
TypeGuid = typeGuid;
Type = type;
File = fileInfo;
}
}

/// <inheritdoc/>
public Guid Id { get; }
Expand All @@ -46,5 +52,18 @@ public SolutionProject(
/// The File of the Project
/// </summary>
public FileInfo File { get; }

/// <summary>
/// The <see cref="ConfigurationPlatform"/>s configured for this solution
/// </summary>
public IReadOnlyCollection<ConfigurationPlatform> ConfigurationPlatforms =>
_configurationPlatforms.ToList().AsReadOnly();

internal void AddConfigurationPlatform(ConfigurationPlatform configurationPlatform)
{
if (configurationPlatform == null) throw new ArgumentNullException(nameof(configurationPlatform));

_configurationPlatforms.Add(configurationPlatform);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using SlnParser.Contracts;
using SlnParser.Contracts.Exceptions;
using SlnParser.Contracts.Helper;
using System.Collections.Generic;
using System.Linq;

namespace SlnParser.Helper
{
internal sealed class EnrichSolutionWithProjectConfigurationPlatforms : IEnrichSolution
{
private readonly IParseSolutionConfigurationPlatform _parseSolutionConfigurationPlatform;

public EnrichSolutionWithProjectConfigurationPlatforms()
{
_parseSolutionConfigurationPlatform = new SolutionConfigurationPlatformParser();
}

public void Enrich(Solution solution, IEnumerable<string> fileContents)
{
var projectConfigurations = _parseSolutionConfigurationPlatform.Parse(
fileContents,
"GlobalSection(ProjectConfiguration");
MapConfigurationPlatformsToProjects(solution, projectConfigurations);
}

private static void MapConfigurationPlatformsToProjects(
Solution solution,
IEnumerable<ProjectConfigurationPlatform> projectConfigurations)
{
foreach (var configuration in projectConfigurations)
MapConfigurationPlatformToProject(solution, configuration);
}

private static void MapConfigurationPlatformToProject(
Solution solution,
ProjectConfigurationPlatform configuration)
{
if (!configuration.ProjectId.HasValue)
throw new UnexpectedSolutionStructureException(
"Expected to find a project-id " +
$"for the Project-Platform-Configuration '{configuration.ConfigurationPlatform.Name}'");

var project = solution
.AllProjects
.FirstOrDefault(project => project.Id == configuration.ProjectId.Value);

if (project == null)
throw new UnexpectedSolutionStructureException(
"Expected to find a project with the id " +
$"'{configuration.ProjectId.Value}' for the Project-Platform-Configuration " +
$"'{configuration.ConfigurationPlatform.Name}'");

if (!(project is SolutionProject solutionProject))
throw new UnexpectedSolutionStructureException(
"Expected to find a Solution-Project with the id " +
$"'{configuration.ProjectId.Value}' for the Project-Platform-Configuration " +
$"'{configuration.ConfigurationPlatform.Name}' but found " +
$" project of type '{project.GetType().Name}' instead");

solutionProject.AddConfigurationPlatform(configuration.ConfigurationPlatform);
}
}
}
7 changes: 6 additions & 1 deletion src/SlnParser/SolutionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public SolutionParser()
_solutionEnrichers = new List<IEnrichSolution>
{
new EnrichSolutionWithProjects(),
new EnrichSolutionWithSolutionConfigurationPlatforms()
new EnrichSolutionWithSolutionConfigurationPlatforms(),
/*
* NOTE: It's important that this happens _after_ the 'EnrichSolutionWithProjects',
* because we need the parsed projects before we can map the configurations to them
*/
new EnrichSolutionWithProjectConfigurationPlatforms()
};
}

Expand Down

0 comments on commit 7f9565a

Please sign in to comment.