2016-11-29 09:44:39 -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-08 14:56:31 -08:00
|
|
|
using Microsoft.Build.Evaluation;
|
2016-11-29 09:44:39 -08:00
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using System.Collections.Generic;
|
2016-12-15 13:40:46 -08:00
|
|
|
using Microsoft.DotNet.Tools.Remove;
|
2016-11-29 09:44:39 -08:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
|
|
|
|
{
|
2016-12-15 13:40:46 -08:00
|
|
|
public class RemoveProjectToProjectReference : IRemoveSubCommand
|
2016-11-29 09:44:39 -08:00
|
|
|
{
|
2016-12-15 13:40:46 -08:00
|
|
|
private CommandOption _frameworkOption;
|
|
|
|
private MsbuildProject _msbuildProj;
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
internal RemoveProjectToProjectReference(string fileOrDirectory, CommandOption frameworkOption)
|
|
|
|
{
|
|
|
|
_msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), fileOrDirectory);
|
|
|
|
_frameworkOption = frameworkOption;
|
|
|
|
}
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
public void Remove(IList<string> references)
|
|
|
|
{
|
|
|
|
if (references.Count == 0)
|
|
|
|
{
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToRemove);
|
|
|
|
}
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
int numberOfRemovedReferences = _msbuildProj.RemoveProjectToProjectReferences(
|
|
|
|
_frameworkOption.Value(),
|
|
|
|
references);
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
if (numberOfRemovedReferences != 0)
|
|
|
|
{
|
|
|
|
_msbuildProj.ProjectRootElement.Save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 14:31:35 -10:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
public class RemoveProjectToProjectReferenceCommand : RemoveSubCommandBase
|
|
|
|
{
|
|
|
|
private CommandOption _frameworkOption;
|
|
|
|
|
|
|
|
protected override string CommandName => "p2p";
|
|
|
|
protected override string LocalizedDisplayName => LocalizableStrings.AppFullName;
|
|
|
|
protected override string LocalizedDescription => LocalizableStrings.AppDescription;
|
|
|
|
protected override string LocalizedHelpText => LocalizableStrings.AppHelpText;
|
2016-12-13 14:31:35 -10:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
internal override void AddCustomOptions(CommandLineApplication app)
|
|
|
|
{
|
|
|
|
_frameworkOption = app.Option(
|
|
|
|
$"-f|--framework <{CommonLocalizableStrings.CmdFramework}>",
|
|
|
|
LocalizableStrings.CmdFrameworkDescription,
|
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
}
|
2016-12-13 14:31:35 -10:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
protected override IRemoveSubCommand CreateIRemoveSubCommand(string fileOrDirectory)
|
|
|
|
{
|
|
|
|
return new RemoveProjectToProjectReference(fileOrDirectory, _frameworkOption);
|
|
|
|
}
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-15 13:40:46 -08:00
|
|
|
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
|
|
|
|
{
|
|
|
|
var removeSubCommand = new RemoveProjectToProjectReferenceCommand();
|
|
|
|
return removeSubCommand.Create(parentApp);
|
2016-11-29 09:44:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|