2017-01-04 18:48:14 -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.
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2017-01-04 18:48:14 -08:00
|
|
|
using Microsoft.DotNet.Cli;
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
using Microsoft.DotNet.Tools.NuGet;
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Remove.PackageReference
|
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
internal class RemovePackageReferenceCommand : CommandBase
|
2017-01-04 18:48:14 -08:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
private readonly AppliedOption _appliedCommand;
|
|
|
|
private readonly string _fileOrDirectory;
|
2017-01-04 18:48:14 -08:00
|
|
|
|
2017-03-15 09:27:27 -07:00
|
|
|
public RemovePackageReferenceCommand(
|
|
|
|
AppliedOption appliedCommand,
|
2017-03-19 14:30:43 -07:00
|
|
|
string fileOrDirectory,
|
|
|
|
ParseResult parseResult) : base(parseResult)
|
2017-01-04 18:48:14 -08:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
if (appliedCommand == null)
|
2017-01-04 18:48:14 -08:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
throw new ArgumentNullException(nameof(appliedCommand));
|
|
|
|
}
|
2017-03-15 09:27:27 -07:00
|
|
|
if (fileOrDirectory == null)
|
|
|
|
{
|
|
|
|
throw new ArgumentNullException(nameof(fileOrDirectory));
|
|
|
|
}
|
2017-03-10 16:43:44 -08:00
|
|
|
if (_appliedCommand.Arguments.Count != 1)
|
2017-01-04 18:48:14 -08:00
|
|
|
{
|
|
|
|
throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference);
|
|
|
|
}
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
_appliedCommand = appliedCommand;
|
2017-03-15 09:27:27 -07:00
|
|
|
_fileOrDirectory = fileOrDirectory;
|
2017-03-10 16:43:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int Execute()
|
|
|
|
{
|
2017-01-04 18:48:14 -08:00
|
|
|
var projectFilePath = string.Empty;
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
if (!File.Exists(_fileOrDirectory))
|
2017-01-04 18:48:14 -08:00
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
projectFilePath = MsbuildProject.GetProjectFileFromDirectory(_fileOrDirectory).FullName;
|
2017-01-04 18:48:14 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
projectFilePath = _fileOrDirectory;
|
2017-01-04 18:48:14 -08:00
|
|
|
}
|
|
|
|
|
2017-03-10 16:43:44 -08:00
|
|
|
var packageToRemove = _appliedCommand.Arguments.Single();
|
2017-01-05 13:05:36 -08:00
|
|
|
var result = NuGetCommand.Run(TransformArgs(packageToRemove, projectFilePath));
|
2017-01-04 18:48:14 -08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string[] TransformArgs(string packageId, string projectFilePath)
|
|
|
|
{
|
2017-03-10 16:43:44 -08:00
|
|
|
return new string[]
|
|
|
|
{
|
2017-01-04 18:48:14 -08:00
|
|
|
"package",
|
|
|
|
"remove",
|
|
|
|
"--package",
|
|
|
|
packageId,
|
|
|
|
"--project",
|
|
|
|
projectFilePath
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2017-03-10 16:43:44 -08:00
|
|
|
}
|