Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nested section parsing #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/FubuCsProjFile.Testing/FubuCsProjFile.Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@
<Content Include="FubuMVC.SlickGrid.sln">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="NestedSolution.sln">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Sample.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
31 changes: 31 additions & 0 deletions src/FubuCsProjFile.Testing/NestedSolution.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "client", "client", "{F1ABE560-BCB9-4E0D-BDC0-5D401B1B1122}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "app", "client\app\app.csproj", "{5878740B-360A-4942-8902-926907209616}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "service", "service\app\app.csproj", "{B38A237B-0BF4-4FD0-941D-E3640A3058EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B38A237B-0BF4-4FD0-941D-E3640A3058EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B38A237B-0BF4-4FD0-941D-E3640A3058EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B38A237B-0BF4-4FD0-941D-E3640A3058EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B38A237B-0BF4-4FD0-941D-E3640A3058EE}.Release|Any CPU.Build.0 = Release|Any CPU
{5878740B-360A-4942-8902-926907209616}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5878740B-360A-4942-8902-926907209616}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5878740B-360A-4942-8902-926907209616}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5878740B-360A-4942-8902-926907209616}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5878740B-360A-4942-8902-926907209616} = {F1ABE560-BCB9-4E0D-BDC0-5D401B1B1122}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions src/FubuCsProjFile.Testing/SolutionTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,15 @@ public void remove_an_existing_project_from_a_solution()
solution = Solution.LoadFrom("FubuMVC.SlickGrid.Temp.sln");
solution.Projects.Count().ShouldEqual(originalProjectcount - 1);
}

[Test]
public void calculate_solution_path()
{
var solution = Solution.LoadFrom(@"NestedSolution.sln");
var project = solution.Projects.First(p => p.ProjectName == "app");
project.SolutionPath.ShouldEqual("client\\app");
project = solution.Projects.First(p => p.ProjectName == "service");
project.SolutionPath.ShouldEqual("service");
}
}
}
39 changes: 39 additions & 0 deletions src/FubuCsProjFile/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private Solution(string filename, string text)

private readonly IList<string> _globals = new List<string>();
private readonly IList<GlobalSection> _sections = new List<GlobalSection>();
private Dictionary<Guid, Guid> _nestedProjects;

/// <summary>
/// Specify the VS.Net version. At this time, the valid options are
Expand Down Expand Up @@ -110,6 +111,44 @@ public IEnumerable<BuildConfiguration> Configurations()
: section.Properties.Select(x => new BuildConfiguration(x));
}

public Dictionary<Guid, Guid> NestedProjects
{
get
{
if (_nestedProjects == null)
InitProjectNesting();

return _nestedProjects;
}
}

private void InitProjectNesting()
{
var nested = Sections.FirstOrDefault(s => s.SectionName == "NestedProjects");
if (nested == null)
{
_nestedProjects = new Dictionary<Guid, Guid>();
return;
}
_nestedProjects = nested.Properties
.Where(p => p != null)
.Select(p => p.Split(new[] { " = " }, StringSplitOptions.RemoveEmptyEntries))
.Where(p => p.Length == 2)
.Select(p =>
{
Guid childGuid;
if (!Guid.TryParse(p[0], out childGuid))
return null;
Guid parentGuild;
if (!Guid.TryParse(p[1], out parentGuild))
return null;
return Tuple.Create(childGuid, parentGuild);
})
.Where(t => t != null)
.GroupBy(t => t.Item1)
.ToDictionary(t => t.Key, t => t.Select(x => x.Item2).First());
}

public class SolutionReader
{
private readonly Solution _parent;
Expand Down
46 changes: 46 additions & 0 deletions src/FubuCsProjFile/SolutionProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static SolutionProject CreateNewAt(string solutionDirectory, string proje
private readonly IList<string> _directives = new List<string>();
private readonly IList<ProjectSection> _projectSections = new List<ProjectSection>();
private readonly Lazy<CsProjFile> _project;
private string _solutionPath;

public SolutionProject(CsProjFile csProjFile, string solutionDirectory)
{
Expand Down Expand Up @@ -72,6 +73,38 @@ private void InitializeFromSolution(CsProjFile projFile, Solution solution)
}
}

private void InitSolutionPath()
{
if (Solution == null)
{
_solutionPath = "";
return;
}
var nested = Solution.Sections.FirstOrDefault(s => s.SectionName == "NestedProjects");
if (nested == null)
{
_solutionPath = "";
return;
}
var childToParent = Solution.NestedProjects;
var solutionProject = Solution.Projects.FirstOrDefault(p => p.ProjectGuid == _projectGuid);
if (solutionProject != null)
_solutionPath = CalcSolutionPath(Solution, _projectGuid, childToParent, solutionProject.ProjectName);
else
_solutionPath = ProjectName;
}

private static string CalcSolutionPath(Solution solution, Guid projGuid, Dictionary<Guid, Guid> childToParent, string path)
{
if (!childToParent.ContainsKey(projGuid))
return path;
var parentGuid = childToParent[projGuid];
var parent = solution.Projects.FirstOrDefault(p => p.ProjectGuid == parentGuid);
if (parent == null)
return path;
return CalcSolutionPath(solution, parent.ProjectGuid, childToParent, parent.ProjectName + "\\" + path);
}

private void InitializeTfsSourceControlSettings(CsProjFile projFile, Solution solution, GlobalSection tfsSourceControl)
{
var projUnique = tfsSourceControl.Properties.FirstOrDefault(item => item.EndsWith(Path.GetFileName(projFile.FileName)));
Expand Down Expand Up @@ -125,6 +158,19 @@ public CsProjFile Project
get { return _project.Value; }
}

/// <summary>
/// Path to project inside solution project tree, not related to file system path
/// </summary>
public string SolutionPath
{
get
{
if (_solutionPath == null)
InitSolutionPath();
return _solutionPath;
}
}

public Solution Solution { get; set; }

public IList<ProjectSection> ProjectSections
Expand Down