Add/remove solution items based on directory structure (#5197)
* WIP support solution folders for dotnet add and remove * Add/remove solution folders based on directory hierarchy * Fix tests * Disable the solution building tests * Address PR comments * Fix a build break due to a new tool version used in the build * Create SlnProjectExtensions and SlnProjectCollectionExtensions per PR comments
This commit is contained in:
parent
05b448944c
commit
d0151a6111
23 changed files with 757 additions and 46 deletions
|
@ -6,6 +6,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;
|
||||
|
||||
|
@ -51,6 +52,8 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
|
|||
|
||||
RemoveEmptyConfigurationSections(slnFile);
|
||||
|
||||
RemoveEmptySolutionFolders(slnFile);
|
||||
|
||||
if (slnChanged)
|
||||
{
|
||||
slnFile.Write();
|
||||
|
@ -82,6 +85,15 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
|
|||
{
|
||||
slnFile.ProjectConfigurationsSection.Remove(buildConfigsToRemove);
|
||||
}
|
||||
|
||||
var nestedProjectsSection = slnFile.Sections.GetSection(
|
||||
"NestedProjects",
|
||||
SlnSectionType.PreProcess);
|
||||
if (nestedProjectsSection != null && nestedProjectsSection.Properties.ContainsKey(slnProject.Id))
|
||||
{
|
||||
nestedProjectsSection.Properties.Remove(slnProject.Id);
|
||||
}
|
||||
|
||||
slnFile.Projects.Remove(slnProject);
|
||||
Reporter.Output.WriteLine(
|
||||
string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, slnProject.FilePath));
|
||||
|
@ -110,5 +122,35 @@ namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveEmptySolutionFolders(SlnFile slnFile)
|
||||
{
|
||||
var referencedSolutionFolders = slnFile.Projects.GetReferencedSolutionFolders();
|
||||
|
||||
var solutionFolderProjects = slnFile.Projects
|
||||
.Where(p => p.TypeGuid == ProjectTypeGuids.SolutionFolderGuid)
|
||||
.ToList();
|
||||
|
||||
if (solutionFolderProjects.Any())
|
||||
{
|
||||
var nestedProjectsSection = slnFile.Sections.GetSection(
|
||||
"NestedProjects",
|
||||
SlnSectionType.PreProcess);
|
||||
|
||||
foreach (var solutionFolderProject in solutionFolderProjects)
|
||||
{
|
||||
if (!referencedSolutionFolders.Contains(solutionFolderProject.Name))
|
||||
{
|
||||
slnFile.Projects.Remove(solutionFolderProject);
|
||||
nestedProjectsSection.Properties.Remove(solutionFolderProject.Id);
|
||||
}
|
||||
}
|
||||
|
||||
if (nestedProjectsSection.IsEmpty)
|
||||
{
|
||||
slnFile.Sections.Remove(nestedProjectsSection);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue