dotnet-installer/src/dotnet/commands/dotnet-remove/dotnet-remove-p2p/Program.cs

70 lines
2.6 KiB
C#
Raw Normal View History

// 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;
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;
namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
{
2016-12-15 13:40:46 -08:00
public class RemoveProjectToProjectReference : IRemoveSubCommand
{
2016-12-15 13:40:46 -08:00
private CommandOption _frameworkOption;
private MsbuildProject _msbuildProj;
2016-12-15 13:40:46 -08:00
internal RemoveProjectToProjectReference(string fileOrDirectory, CommandOption frameworkOption)
{
_msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), fileOrDirectory);
_frameworkOption = frameworkOption;
}
2016-12-15 13:40:46 -08:00
public void Remove(IList<string> references)
{
if (references.Count == 0)
{
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToRemove);
}
2016-12-15 13:40:46 -08:00
int numberOfRemovedReferences = _msbuildProj.RemoveProjectToProjectReferences(
_frameworkOption.Value(),
references);
2016-12-15 13:40:46 -08:00
if (numberOfRemovedReferences != 0)
{
_msbuildProj.ProjectRootElement.Save();
}
}
}
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-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-15 13:40:46 -08:00
protected override IRemoveSubCommand CreateIRemoveSubCommand(string fileOrDirectory)
{
return new RemoveProjectToProjectReference(fileOrDirectory, _frameworkOption);
}
2016-12-15 13:40:46 -08:00
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
{
var removeSubCommand = new RemoveProjectToProjectReferenceCommand();
return removeSubCommand.Create(parentApp);
}
}
}