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;
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Remove.ProjectFromSolution
|
|
|
|
{
|
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()
|
|
|
|
{
|
|
|
|
Name = "project",
|
|
|
|
FullName = LocalizableStrings.AppFullName,
|
|
|
|
Description = LocalizableStrings.AppDescription,
|
|
|
|
HandleRemainingArguments = true,
|
|
|
|
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
var projectPathNormalized = PathUtility.GetPathWithBackSlashes(projectPath);
|
|
|
|
|
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)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|