diff --git a/src/dotnet/commands/dotnet-remove/Program.cs b/src/dotnet/commands/dotnet-remove/Program.cs index 48706a88a..a6fc65c53 100644 --- a/src/dotnet/commands/dotnet-remove/Program.cs +++ b/src/dotnet/commands/dotnet-remove/Program.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using Microsoft.DotNet.Cli; +using Microsoft.DotNet.Tools.Remove.PackageReference; using Microsoft.DotNet.Tools.Remove.ProjectFromSolution; using Microsoft.DotNet.Tools.Remove.ProjectToProjectReference; @@ -18,6 +19,7 @@ namespace Microsoft.DotNet.Tools.Remove { RemoveProjectFromSolutionCommand.Create, RemoveProjectToProjectReferenceCommand.Create, + RemovePackageReferenceCommand.Create }; public static int Run(string[] args) diff --git a/src/dotnet/commands/dotnet-remove/dotnet-remove-package/LocalizableStrings.cs b/src/dotnet/commands/dotnet-remove/dotnet-remove-package/LocalizableStrings.cs new file mode 100644 index 000000000..bde80c6ea --- /dev/null +++ b/src/dotnet/commands/dotnet-remove/dotnet-remove-package/LocalizableStrings.cs @@ -0,0 +1,17 @@ +// 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. + +namespace Microsoft.DotNet.Tools.Remove.PackageReference +{ + internal class LocalizableStrings + { + public const string AppFullName = ".NET Remove Package reference Command."; + + public const string AppDescription = "Command to remove package reference."; + + public const string AppHelpText = "Package reference to remove."; + + public const string SpecifyExactlyOnePackageReference = "Please specify only one package reference to remove."; + + } +} \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-remove/dotnet-remove-package/Program.cs b/src/dotnet/commands/dotnet-remove/dotnet-remove-package/Program.cs new file mode 100644 index 000000000..b63b058e2 --- /dev/null +++ b/src/dotnet/commands/dotnet-remove/dotnet-remove-package/Program.cs @@ -0,0 +1,76 @@ +// 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.Build.Evaluation; +using Microsoft.DotNet.Cli; +using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.Tools.Common; +using Microsoft.DotNet.Tools.MSBuild; +using Microsoft.DotNet.Tools.NuGet; +using NuGet.Frameworks; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; + +namespace Microsoft.DotNet.Tools.Remove.PackageReference +{ + internal class RemovePackageReferenceCommand : DotNetSubCommandBase + { + + public static DotNetSubCommandBase Create() + { + var command = new RemovePackageReferenceCommand + { + Name = "package", + FullName = LocalizableStrings.AppFullName, + Description = LocalizableStrings.AppDescription, + HandleRemainingArguments = true, + ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText, + }; + + command.HelpOption("-h|--help"); + + return command; + } + + public override int Run(string fileOrDirectory) + { + if (RemainingArguments.Count != 1) + { + throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference); + } + + var projectFilePath = string.Empty; + + if (!File.Exists(fileOrDirectory)) + { + projectFilePath = MsbuildProject.GetProjectFileFromDirectory(fileOrDirectory).FullName; + } + else + { + projectFilePath = fileOrDirectory; + } + + var packageToRemove = RemainingArguments.First(); + var result = NuGetCommand.Run(TransformArgs(packageToRemove, projectFilePath)); + + return result; + } + + private string[] TransformArgs(string packageId, string projectFilePath) + { + return new string[]{ + "package", + "remove", + "--package", + packageId, + "--project", + projectFilePath + }; + } + } +} \ No newline at end of file