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.
|
|
|
|
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
|
|
|
|
{
|
|
|
|
public class RemoveProjectToProjectReferenceCommand
|
|
|
|
{
|
|
|
|
public static int Run(string[] args)
|
|
|
|
{
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
|
|
CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false)
|
|
|
|
{
|
|
|
|
Name = "dotnet remove p2p",
|
|
|
|
FullName = ".NET Remove Project to Project (p2p) reference Command",
|
|
|
|
Description = "Command to remove project to project (p2p) reference",
|
|
|
|
AllowArgumentSeparator = true,
|
|
|
|
ArgumentSeparatorHelpText = "Project to project references to remove"
|
|
|
|
};
|
|
|
|
|
|
|
|
app.HelpOption("-h|--help");
|
|
|
|
|
|
|
|
CommandArgument projectArgument = app.Argument(
|
|
|
|
"<PROJECT>",
|
|
|
|
"The project file to modify. If a project file is not specified," +
|
|
|
|
" it searches the current working directory for an MSBuild file that has" +
|
|
|
|
" a file extension that ends in `proj` and uses that file.");
|
|
|
|
|
|
|
|
CommandOption frameworkOption = app.Option(
|
|
|
|
"-f|--framework <FRAMEWORK>",
|
|
|
|
"Remove reference only when targetting a specific framework",
|
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
|
|
|
app.OnExecute(() => {
|
|
|
|
if (string.IsNullOrEmpty(projectArgument.Value))
|
|
|
|
{
|
|
|
|
throw new GracefulException(LocalizableStrings.RequiredArgumentNotPassed, "<Project>");
|
|
|
|
}
|
|
|
|
|
2016-11-29 10:23:04 -08:00
|
|
|
var msbuildProj = MsbuildProject.FromFileOrDirectory(projectArgument.Value);
|
2016-11-29 09:44:39 -08:00
|
|
|
|
|
|
|
if (app.RemainingArguments.Count == 0)
|
|
|
|
{
|
|
|
|
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToRemove);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<string> references = app.RemainingArguments;
|
|
|
|
|
2016-11-30 13:43:43 -08:00
|
|
|
int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences(
|
2016-11-29 09:44:39 -08:00
|
|
|
frameworkOption.Value(),
|
|
|
|
references);
|
|
|
|
|
|
|
|
if (numberOfRemovedReferences != 0)
|
|
|
|
{
|
2016-11-29 10:23:04 -08:00
|
|
|
msbuildProj.Project.Save();
|
2016-11-29 09:44:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return app.Execute(args);
|
|
|
|
}
|
|
|
|
catch (GracefulException e)
|
|
|
|
{
|
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
|
|
|
app.ShowHelp();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|