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;
|
2016-12-13 14:31:35 -10:00
|
|
|
using Microsoft.DotNet.Tools.Common;
|
2016-11-29 09:44:39 -08:00
|
|
|
using System.Collections.Generic;
|
2016-12-13 14:31:35 -10:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2016-11-29 09:44:39 -08:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Remove.ProjectToProjectReference
|
|
|
|
{
|
|
|
|
public class RemoveProjectToProjectReferenceCommand
|
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
internal static CommandLineApplication CreateApplication(CommandLineApplication parentApp)
|
2016-11-29 09:44:39 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
CommandLineApplication app = parentApp.Command("p2p", throwOnUnexpectedArg: false);
|
|
|
|
app.FullName = LocalizableStrings.AppFullName;
|
|
|
|
app.Description = LocalizableStrings.AppDescription;
|
|
|
|
app.HandleRemainingArguments = true;
|
|
|
|
app.ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText;
|
2016-11-29 09:44:39 -08:00
|
|
|
|
|
|
|
app.HelpOption("-h|--help");
|
|
|
|
|
|
|
|
CommandOption frameworkOption = app.Option(
|
2016-12-13 14:31:35 -10:00
|
|
|
$"-f|--framework <{CommonLocalizableStrings.CmdFramework}>",
|
2016-12-05 02:57:30 -08:00
|
|
|
LocalizableStrings.CmdFrameworkDescription,
|
2016-11-29 09:44:39 -08:00
|
|
|
CommandOptionType.SingleValue);
|
|
|
|
|
|
|
|
app.OnExecute(() => {
|
2016-12-13 14:31:35 -10:00
|
|
|
try
|
2016-11-29 09:44:39 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
if (!parentApp.Arguments.Any())
|
|
|
|
{
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, Constants.ProjectOrSolutionArgumentName);
|
|
|
|
}
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
var projectOrDirectory = parentApp.Arguments.First().Value;
|
|
|
|
if (string.IsNullOrEmpty(projectOrDirectory))
|
|
|
|
{
|
|
|
|
projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
|
|
|
|
}
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), projectOrDirectory);
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
if (app.RemainingArguments.Count == 0)
|
|
|
|
{
|
|
|
|
throw new GracefulException(LocalizableStrings.SpecifyAtLeastOneReferenceToRemove);
|
|
|
|
}
|
2016-11-29 09:44:39 -08:00
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
List<string> references = app.RemainingArguments;
|
|
|
|
|
|
|
|
int numberOfRemovedReferences = msbuildProj.RemoveProjectToProjectReferences(
|
|
|
|
frameworkOption.Value(),
|
|
|
|
references);
|
|
|
|
|
|
|
|
if (numberOfRemovedReferences != 0)
|
|
|
|
{
|
|
|
|
msbuildProj.ProjectRootElement.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
catch (GracefulException e)
|
2016-11-29 09:44:39 -08:00
|
|
|
{
|
2016-12-13 14:31:35 -10:00
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
|
|
|
app.ShowHelp();
|
|
|
|
return 1;
|
2016-11-29 09:44:39 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-13 14:31:35 -10:00
|
|
|
return app;
|
2016-11-29 09:44:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|