Skip to content

Commit

Permalink
Use MSBuild properties to update solution file name (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
mruxmohan4 authored Nov 27, 2023
1 parent 1e52580 commit 3ba13c2
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 4 deletions.
95 changes: 95 additions & 0 deletions src/Microsoft.VisualStudio.SlnGen.UnitTests/SlnFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,87 @@ public void PathsWorkForAllDirectorySeparatorChars()
actualSolutionText.ShouldBe(solutionText, StringCompareShould.IgnoreLineEndings);
}

[Fact]
public void TestSlnGenProjectNamePropertyForSolutionName()
{
Project[] projects =
{
ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectA"))
.Property("SlnGenProjectName", "RandomComponent")
.Save(),
};

string solutionFilePath = GetSolutionFilePath(projects);
solutionFilePath.ShouldContain("RandomComponent.sln");
}

[Fact]
public void TestDefaultSolutionName()
{
Project[] projects =
{
ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectA")).Save(),
};

string solutionFilePath = GetSolutionFilePath(projects);
solutionFilePath.ShouldContain("ProjectA.sln");
}

[Fact]
public void TestSlnGenFoldersPropertyToEnableFolderCreation()
{
Project projectA = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectA"))
.Property("SlnGenFolders", "true")
.Save();

Project projectB = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectB", directoryPath: "testB"))
.Property("SlnGenFolders", "true")
.Save();

Project projectC = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectC", directoryPath: "testC"))
.Property("SlnGenFolders", "true")
.Save();

string solutionFilePath = GetSolutionFilePath(new Project[] { projectA, projectB, projectC });
string contents = File.ReadAllText(solutionFilePath);
contents.ShouldContain("\"..\\testB\",");
contents.ShouldContain("\"..\\testC\",");
}

[Fact]
public void TestSlnGenFoldersPropertyToDisableFolderCreation()
{
Project projectA = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectA"))
.Property("SlnGenFolders", "false")
.Save();

Project projectB = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectB", directoryPath: "testB"))
.Property("SlnGenFolders", "false")
.Save();

Project projectC = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectC", directoryPath: "testC"))
.Property("SlnGenFolders", "false")
.Save();

string solutionFilePath = GetSolutionFilePath(new Project[] { projectA, projectB, projectC });
string contents = File.ReadAllText(solutionFilePath);
contents.ShouldNotContain("\"..\\testB\",");
contents.ShouldNotContain("\"..\\testC\",");
}

[Fact]
public void TestNoFolderCreation()
{
Project projectA = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectA")).Save();
Project projectB = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectB", directoryPath: "testB")).Save();
Project projectC = ProjectCreator.Templates.SdkCsproj(path: GetTempProjectFile("ProjectC", directoryPath: "testC")).Save();

string solutionFilePath = GetSolutionFilePath(new Project[] { projectA, projectB, projectC });
string contents = File.ReadAllText(solutionFilePath);
contents.ShouldNotContain("\"..\\testB\",");
contents.ShouldNotContain("\"..\\testC\",");
}

[Fact]
public void ProjectConfigurationPlatformOrderingSameAsProjects()
{
Expand Down Expand Up @@ -1206,6 +1287,20 @@ public void SlnProject_IsBuildable_ReflectedAsProjectConfigurationInSolutionIncl
false);
}

private string GetSolutionFilePath(Project[] projects)
{
ProgramArguments programArguments = new ()
{
LaunchVisualStudio = new[] { bool.FalseString },
};

TestLogger testLogger = new ();

(string solutionFileFullPath, _, _, _) = SlnFile.GenerateSolutionFile(programArguments, projects, testLogger);

return solutionFileFullPath;
}

private void ValidateProjectInSolution(Action<SlnProject, ProjectInSolution> customValidator, SlnProject[] projects, bool useFolders)
{
string solutionFilePath = GetTempFileName();
Expand Down
32 changes: 31 additions & 1 deletion src/Microsoft.VisualStudio.SlnGen/ProgramArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,23 @@ public ProgramArguments()
/// <summary>
/// Gets a value indicating whether or not folders should be created in the solution.
/// </summary>
/// <param name="slnGenFoldersPropertyValue">The SlnGenFolders property value if it exists />.</param>
/// <returns>true if folders should be used, otherwise false.</returns>
public bool EnableFolders() => GetBoolean(Folders);
public bool EnableFolders(string slnGenFoldersPropertyValue)
{
bool? enableFolders = TryGetBoolean(Folders);
if (enableFolders != null)
{
return enableFolders.Value; // Use the value when available
}

if (bool.TryParse(slnGenFoldersPropertyValue, out bool result))
{
return result; // Fall back to the SlnGenFolders property value
}

return false;
}

/// <summary>
/// Gets the Configuration values based on what was specified as command-line arguments.
Expand Down Expand Up @@ -517,5 +532,20 @@ private bool GetBoolean(string[] values, bool defaultValue = false)

return defaultValue;
}

private bool? TryGetBoolean(string[] values)
{
if (values == null || values.Length == 0)
{
return null;
}

if (bool.TryParse(values.Last(), out bool result))
{
return result;
}

return null;
}
}
}
10 changes: 7 additions & 3 deletions src/Microsoft.VisualStudio.SlnGen/SlnFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public static (string solutionFileFullPath, int customProjectTypeGuidCount, int
solutionDirectoryFullPath = firstProject.DirectoryPath;
}

string solutionFileName = Path.ChangeExtension(Path.GetFileName(firstProject.FullPath), "sln");
var firstProjectName = firstProject.GetPropertyValueOrDefault(MSBuildPropertyNames.SlnGenProjectName, Path.GetFileName(firstProject.FullPath));
string solutionFileName = Path.ChangeExtension(firstProjectName, "sln");

solutionFileFullPath = Path.Combine(solutionDirectoryFullPath!, solutionFileName);
}
Expand Down Expand Up @@ -197,7 +198,7 @@ public static (string solutionFileFullPath, int customProjectTypeGuidCount, int
}
}

if (SlnFile.TryParseExistingSolution(solutionFileFullPath, out Guid solutionGuid, out IReadOnlyDictionary<string, Guid> projectGuidsByPath))
if (TryParseExistingSolution(solutionFileFullPath, out Guid solutionGuid, out IReadOnlyDictionary<string, Guid> projectGuidsByPath))
{
logger.LogMessageNormal("Updating existing solution file and reusing Visual Studio cache");

Expand All @@ -211,9 +212,12 @@ public static (string solutionFileFullPath, int customProjectTypeGuidCount, int

solution.AddSolutionItems(solutionItems);

string slnGenFoldersPropertyValue = firstProject.GetPropertyValueOrDefault(MSBuildPropertyNames.SlnGenFolders, "false");
var enableFolders = arguments.EnableFolders(slnGenFoldersPropertyValue);

if (!logger.HasLoggedErrors)
{
solution.Save(solutionFileFullPath, arguments.EnableFolders(), logger, arguments.EnableCollapseFolders(), arguments.EnableAlwaysBuild());
solution.Save(solutionFileFullPath, enableFolders, logger, arguments.EnableCollapseFolders(), arguments.EnableAlwaysBuild());
}

return (solutionFileFullPath, customProjectTypeGuids.Count, solutionItems.Count, solution.SolutionGuid);
Expand Down

0 comments on commit 3ba13c2

Please sign in to comment.