2016-12-15 13:40:46 -08:00
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
using Microsoft.DotNet.Cli;
|
2016-12-15 13:40:46 -08:00
|
|
|
using Microsoft.DotNet.Cli.Sln.Internal;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
2017-01-06 10:58:23 -10:00
|
|
|
using Microsoft.DotNet.Tools.Sln;
|
2016-12-15 13:40:46 -08:00
|
|
|
using System;
|
2017-01-04 18:32:09 -10:00
|
|
|
using System.Collections.Generic;
|
2016-12-15 13:40:46 -08:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
|
2017-01-06 10:58:23 -10:00
|
|
|
namespace Microsoft.DotNet.Tools.Sln.Remove
|
2016-12-15 13:40:46 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
internal class RemoveProjectFromSolutionCommand : DotNetSubCommandBase
|
2016-12-15 13:40:46 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
public static DotNetSubCommandBase Create()
|
2016-12-15 13:40:46 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
var command = new RemoveProjectFromSolutionCommand()
|
|
|
|
{
|
2017-01-06 10:58:23 -10:00
|
|
|
Name = "remove",
|
|
|
|
FullName = LocalizableStrings.RemoveAppFullName,
|
|
|
|
Description = LocalizableStrings.RemoveSubcommandHelpText,
|
2016-12-16 01:04:09 -08:00
|
|
|
HandleRemainingArguments = true,
|
2017-01-06 10:58:23 -10:00
|
|
|
ArgumentSeparatorHelpText = LocalizableStrings.RemoveSubcommandHelpText,
|
2016-12-16 01:04:09 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
command.HelpOption("-h|--help");
|
|
|
|
|
|
|
|
return command;
|
2016-12-15 13:40:46 -08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
public override int Run(string fileOrDirectory)
|
2016-12-15 13:40:46 -08:00
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
SlnFile slnFile = SlnFileFactory.CreateFromFileOrDirectory(fileOrDirectory);
|
|
|
|
|
|
|
|
if (RemainingArguments.Count == 0)
|
2016-12-15 13:40:46 -08:00
|
|
|
{
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove);
|
|
|
|
}
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
var relativeProjectPaths = RemainingArguments.Select((p) =>
|
2016-12-15 13:40:46 -08:00
|
|
|
PathUtility.GetRelativePath(
|
2016-12-16 01:04:09 -08:00
|
|
|
PathUtility.EnsureTrailingSlash(slnFile.BaseDirectory),
|
2016-12-15 13:40:46 -08:00
|
|
|
Path.GetFullPath(p))).ToList();
|
|
|
|
|
|
|
|
bool slnChanged = false;
|
|
|
|
foreach (var path in relativeProjectPaths)
|
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
slnChanged |= RemoveProject(slnFile, path);
|
2016-12-15 13:40:46 -08:00
|
|
|
}
|
|
|
|
|
2017-01-03 07:18:45 -10:00
|
|
|
RemoveEmptyConfigurationSections(slnFile);
|
|
|
|
|
2017-01-04 18:32:09 -10:00
|
|
|
RemoveEmptySolutionFolders(slnFile);
|
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
if (slnChanged)
|
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
slnFile.Write();
|
2016-12-15 13:40:46 -08:00
|
|
|
}
|
2016-12-16 01:04:09 -08:00
|
|
|
|
|
|
|
return 0;
|
2016-12-15 13:40:46 -08:00
|
|
|
}
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
private bool RemoveProject(SlnFile slnFile, string projectPath)
|
2016-12-15 13:40:46 -08:00
|
|
|
{
|
2016-12-20 13:04:01 -10:00
|
|
|
var projectPathNormalized = PathUtility.GetPathWithDirectorySeparator(projectPath);
|
2016-12-15 13:40:46 -08:00
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
var projectsToRemove = slnFile.Projects.Where((p) =>
|
2016-12-15 13:40:46 -08:00
|
|
|
string.Equals(p.FilePath, projectPathNormalized, StringComparison.OrdinalIgnoreCase)).ToList();
|
|
|
|
|
|
|
|
bool projectRemoved = false;
|
|
|
|
if (projectsToRemove.Count == 0)
|
|
|
|
{
|
|
|
|
Reporter.Output.WriteLine(string.Format(
|
|
|
|
CommonLocalizableStrings.ProjectReferenceCouldNotBeFound,
|
|
|
|
projectPath));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (var slnProject in projectsToRemove)
|
|
|
|
{
|
2017-01-03 07:18:45 -10:00
|
|
|
var buildConfigsToRemove = slnFile.ProjectConfigurationsSection.GetPropertySet(slnProject.Id);
|
|
|
|
if (buildConfigsToRemove != null)
|
|
|
|
{
|
|
|
|
slnFile.ProjectConfigurationsSection.Remove(buildConfigsToRemove);
|
|
|
|
}
|
2017-01-04 18:32:09 -10:00
|
|
|
|
|
|
|
var nestedProjectsSection = slnFile.Sections.GetSection(
|
|
|
|
"NestedProjects",
|
|
|
|
SlnSectionType.PreProcess);
|
|
|
|
if (nestedProjectsSection != null && nestedProjectsSection.Properties.ContainsKey(slnProject.Id))
|
|
|
|
{
|
|
|
|
nestedProjectsSection.Properties.Remove(slnProject.Id);
|
|
|
|
}
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
slnFile.Projects.Remove(slnProject);
|
2016-12-15 13:40:46 -08:00
|
|
|
Reporter.Output.WriteLine(
|
|
|
|
string.Format(CommonLocalizableStrings.ProjectReferenceRemoved, slnProject.FilePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
projectRemoved = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectRemoved;
|
|
|
|
}
|
2017-01-03 07:18:45 -10:00
|
|
|
|
|
|
|
private void RemoveEmptyConfigurationSections(SlnFile slnFile)
|
|
|
|
{
|
|
|
|
if (slnFile.Projects.Count == 0)
|
|
|
|
{
|
|
|
|
var solutionConfigs = slnFile.Sections.GetSection("SolutionConfigurationPlatforms");
|
|
|
|
if (solutionConfigs != null)
|
|
|
|
{
|
|
|
|
slnFile.Sections.Remove(solutionConfigs);
|
|
|
|
}
|
|
|
|
|
|
|
|
var projectConfigs = slnFile.Sections.GetSection("ProjectConfigurationPlatforms");
|
|
|
|
if (projectConfigs != null)
|
|
|
|
{
|
|
|
|
slnFile.Sections.Remove(projectConfigs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-04 18:32:09 -10:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 13:40:46 -08:00
|
|
|
}
|
|
|
|
}
|