Add/remove projects also updates build configurations (#5134)

* Add/remove projects also updates build configurations

* Fix the algorithm for adding/removing build configurations

* Address PR comments
This commit is contained in:
Justin Goshi 2017-01-03 07:18:45 -10:00 committed by GitHub
parent 06d33f01ce
commit 19b4bad315
16 changed files with 550 additions and 43 deletions

View file

@ -8,6 +8,7 @@ using Microsoft.DotNet.Cli.Sln.Internal;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@ -103,10 +104,69 @@ namespace Microsoft.DotNet.Tools.Add.ProjectToSolution
FilePath = projectPathNormalized
};
AddDefaultBuildConfigurations(slnFile, slnProject);
slnFile.Projects.Add(slnProject);
Reporter.Output.WriteLine(
string.Format(CommonLocalizableStrings.ProjectAddedToTheSolution, projectPath));
}
}
private void AddDefaultBuildConfigurations(SlnFile slnFile, SlnProject slnProject)
{
var defaultConfigurations = new List<string>()
{
"Debug|Any CPU",
"Debug|x64",
"Debug|x86",
"Release|Any CPU",
"Release|x64",
"Release|x86",
};
// NOTE: The order you create the sections determines the order they are written to the sln
// file. In the case of an empty sln file, in order to make sure the solution configurations
// section comes first we need to add it first. This doesn't affect correctness but does
// stop VS from re-ordering things later on. Since we are keeping the SlnFile class low-level
// it shouldn't care about the VS implementation details. That's why we handle this here.
AddDefaultSolutionConfigurations(defaultConfigurations, slnFile.SolutionConfigurationsSection);
AddDefaultProjectConfigurations(
defaultConfigurations,
slnFile.ProjectConfigurationsSection.GetOrCreatePropertySet(slnProject.Id));
}
private void AddDefaultSolutionConfigurations(
List<string> defaultConfigurations,
SlnPropertySet solutionConfigs)
{
foreach (var config in defaultConfigurations)
{
if (!solutionConfigs.ContainsKey(config))
{
solutionConfigs[config] = config;
}
}
}
private void AddDefaultProjectConfigurations(
List<string> defaultConfigurations,
SlnPropertySet projectConfigs)
{
foreach (var config in defaultConfigurations)
{
var activeCfgKey = $"{config}.ActiveCfg";
if (!projectConfigs.ContainsKey(activeCfgKey))
{
projectConfigs[activeCfgKey] = config;
}
var build0Key = $"{config}.Build.0";
if (!projectConfigs.ContainsKey(build0Key))
{
projectConfigs[build0Key] = config;
}
}
}
}
}