Skip to content

Commit

Permalink
Store projectguid as guid type (#21)
Browse files Browse the repository at this point in the history
* Fix solutions with oddly formated project guids
Do not write config/platform types when they are empty.

* fix unit test

* Store project guid in guid type
  • Loading branch information
kingerja authored and jeffkl committed May 3, 2018
1 parent 59743f9 commit 7e06d7d
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/SlnGen.Build.Tasks.UnitTests/SlnFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void LotsOfProjects()
// pick random and shuffled configurations and platforms
var projectConfigurations = configurations.OrderBy(a => Guid.NewGuid()).Take(randomGenerator.Next(1, configurations.Length)).ToList();
var projectPlatforms = platforms.OrderBy(a => Guid.NewGuid()).Take(randomGenerator.Next(1, platforms.Length)).ToList();
projects[i] = new SlnProject(GetTempFileName(), $"Project{i:D6}", Guid.NewGuid().ToSolutionString(), Guid.NewGuid().ToSolutionString(), projectConfigurations, projectPlatforms, isMainProject: i == 0);
projects[i] = new SlnProject(GetTempFileName(), $"Project{i:D6}", Guid.NewGuid(), Guid.NewGuid().ToSolutionString(), projectConfigurations, projectPlatforms, isMainProject: i == 0);
}

ValidateProjectInSolution(projects);
Expand All @@ -41,16 +41,16 @@ public void LotsOfProjects()
[Test]
public void MultipleProjects()
{
SlnProject projectA = new SlnProject(GetTempFileName(), "ProjectA", "C95D800E-F016-4167-8E1B-1D3FF94CE2E2", "88152E7E-47E3-45C8-B5D3-DDB15B2F0435", new[] { "Debug" }, new[] { "x64" }, isMainProject: true);
SlnProject projectB = new SlnProject(GetTempFileName(), "ProjectB", "EAD108BE-AC70-41E6-A8C3-450C545FDC0E", "F38341C3-343F-421A-AE68-94CD9ADCD32F", new[] { "Debug" }, new[] { "x64" }, isMainProject: false);
SlnProject projectA = new SlnProject(GetTempFileName(), "ProjectA", Guid.Parse("C95D800E-F016-4167-8E1B-1D3FF94CE2E2"), "88152E7E-47E3-45C8-B5D3-DDB15B2F0435", new[] { "Debug" }, new[] { "x64" }, isMainProject: true);
SlnProject projectB = new SlnProject(GetTempFileName(), "ProjectB", Guid.Parse("EAD108BE-AC70-41E6-A8C3-450C545FDC0E"), "F38341C3-343F-421A-AE68-94CD9ADCD32F", new[] { "Debug" }, new[] { "x64" }, isMainProject: false);

ValidateProjectInSolution(projectA, projectB);
}

[Test]
public void SingleProject()
{
SlnProject projectA = new SlnProject(GetTempFileName(), "ProjectA", "C95D800E-F016-4167-8E1B-1D3FF94CE2E2", "88152E7E-47E3-45C8-B5D3-DDB15B2F0435", new[] { "Debug" }, new[] { "x64" }, isMainProject: true);
SlnProject projectA = new SlnProject(GetTempFileName(), "ProjectA", Guid.Parse("C95D800E-F016-4167-8E1B-1D3FF94CE2E2"), "88152E7E-47E3-45C8-B5D3-DDB15B2F0435", new[] { "Debug" }, new[] { "x64" }, isMainProject: true);

ValidateProjectInSolution(projectA);
}
Expand All @@ -67,12 +67,12 @@ private void ValidateProjectInSolution(Action<SlnProject, ProjectInSolution> cus

foreach (SlnProject slnProject in projects)
{
solutionFile.ProjectsByGuid.ContainsKey(slnProject.ProjectGuid).ShouldBeTrue();
solutionFile.ProjectsByGuid.ContainsKey(slnProject.ProjectGuid.ToSolutionString()).ShouldBeTrue();

ProjectInSolution projectInSolution = solutionFile.ProjectsByGuid[slnProject.ProjectGuid];
ProjectInSolution projectInSolution = solutionFile.ProjectsByGuid[slnProject.ProjectGuid.ToSolutionString()];

projectInSolution.AbsolutePath.ShouldBe(slnProject.FullPath);
projectInSolution.ProjectGuid.ShouldBe(slnProject.ProjectGuid);
projectInSolution.ProjectGuid.ShouldBe(slnProject.ProjectGuid.ToSolutionString());
projectInSolution.ProjectName.ShouldBe(slnProject.Name);

var configurationPlatforms = from configuration in slnProject.Configurations from platform in slnProject.Platforms select $"{configuration}|{platform}";
Expand Down
4 changes: 2 additions & 2 deletions src/SlnGen.Build.Tasks.UnitTests/SlnProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void GetProjectGuidSdkProject()

actualProject.ProjectGuid.ShouldNotBeNull();

Guid.TryParse(actualProject.ProjectGuid, out _).ShouldBeTrue();
Guid.TryParse(actualProject.ProjectGuid.ToSolutionString(), out _).ShouldBeTrue();
}

[TestCase(true)]
Expand Down Expand Up @@ -148,7 +148,7 @@ private SlnProject CreateAndValidateProject(bool isMainProject = false, string e

if (expectedGuid != null)
{
actualProject.ProjectGuid.ShouldBe(expectedGuid);
actualProject.ProjectGuid.ToSolutionString().ShouldBe(expectedGuid);
}

actualProject.IsMainProject.ShouldBe(isMainProject);
Expand Down
6 changes: 3 additions & 3 deletions src/SlnGen.Build.Tasks/Internal/SlnFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void Save(TextWriter writer)

foreach (SlnProject project in _projects)
{
writer.WriteLine($@"Project(""{project.ProjectTypeGuid}"") = ""{project.Name}"", ""{project.FullPath}"", ""{project.ProjectGuid}""");
writer.WriteLine($@"Project(""{project.ProjectTypeGuid}"") = ""{project.Name}"", ""{project.FullPath}"", ""{project.ProjectGuid.ToSolutionString()}""");
writer.WriteLine("EndProject");
}

Expand Down Expand Up @@ -136,8 +136,8 @@ public void Save(TextWriter writer)
{
if (!string.IsNullOrWhiteSpace(configuration) && !string.IsNullOrWhiteSpace(platform))
{
writer.WriteLine($@" {project.ProjectGuid}.{configuration}|{platform}.ActiveCfg = {configuration}|{platform}");
writer.WriteLine($@" {project.ProjectGuid}.{configuration}|{platform}.Build.0 = {configuration}|{platform}");
writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.ActiveCfg = {configuration}|{platform}");
writer.WriteLine($@" {project.ProjectGuid.ToSolutionString()}.{configuration}|{platform}.Build.0 = {configuration}|{platform}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/SlnGen.Build.Tasks/Internal/SlnHierarchy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void BuildHierarchyBottomUp(SlnProject project, string root)
{
// TODO: Collapse folders with single sub folder. So if foo had just a subfolder bar, collapse it to foo\bar in Visual Studio
string parent = Directory.GetParent(project.FullPath).FullName;
string currentGuid = project.ProjectGuid;
string currentGuid = project.ProjectGuid.ToSolutionString();

while (true)
{
Expand Down
19 changes: 12 additions & 7 deletions src/SlnGen.Build.Tasks/Internal/SlnProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ internal sealed class SlnProject
{".wixproj", "930C7802-8A8C-48F9-8165-68863BCCD9DD"},
};

public SlnProject([NotNull] string fullPath, [NotNull] string name, [NotNull] string projectGuid, [NotNull] string projectTypeGuid, [NotNull] IEnumerable<string> configurations, [NotNull] IEnumerable<string> platforms, bool isMainProject)
public SlnProject([NotNull] string fullPath, [NotNull] string name, [NotNull] Guid projectGuid, [NotNull] string projectTypeGuid, [NotNull] IEnumerable<string> configurations, [NotNull] IEnumerable<string> platforms, bool isMainProject)
{
FullPath = fullPath ?? throw new ArgumentNullException(nameof(fullPath));
Name = name ?? throw new ArgumentNullException(nameof(name));
ProjectGuid = projectGuid ?? throw new ArgumentNullException(nameof(projectGuid));
ProjectGuid = projectGuid;
ProjectTypeGuid = projectTypeGuid ?? throw new ArgumentNullException(nameof(projectTypeGuid));
IsMainProject = isMainProject;
Configurations = configurations;
Expand All @@ -44,7 +44,7 @@ public SlnProject([NotNull] string fullPath, [NotNull] string name, [NotNull] st

public string Name { get; }

public string ProjectGuid { get; }
public Guid ProjectGuid { get; }

public string ProjectTypeGuid { get; }

Expand Down Expand Up @@ -81,10 +81,15 @@ public static SlnProject FromProject([NotNull] Project project, [NotNull] IReadO
IEnumerable<string> configurations = project.GetPossiblePropertyValuesOrDefault("Configuration", "Debug");
IEnumerable<string> platforms = project.GetPossiblePropertyValuesOrDefault("Platform", "AnyCPU");

string projectGuid = isLegacyProjectSystem ? project.GetPropertyValueOrDefault(ProjectGuidPropertyName, Guid.NewGuid().ToSolutionString()) : Guid.NewGuid().ToSolutionString();
projectGuid = $"{{{projectGuid.ToUpper().Replace(" ","").Trim('{','}')}}}";

return new SlnProject(project.FullPath, name, projectGuid, projectTypeGuid, configurations, platforms, isMainProject);
if (!Guid.TryParse(
isLegacyProjectSystem
? project.GetPropertyValueOrDefault(ProjectGuidPropertyName, Guid.NewGuid().ToString())
: Guid.NewGuid().ToString(), out Guid projectGuid))
{
throw new FormatException($"property ProjectGuid has an invalid format in {project.FullPath}");
}
return new SlnProject(project.FullPath, name, projectGuid, projectTypeGuid, configurations, platforms,
isMainProject);
}
}
}

0 comments on commit 7e06d7d

Please sign in to comment.