Add package sub command
This commit is contained in:
parent
abd496a6f1
commit
3a5b89c242
2 changed files with 60 additions and 18 deletions
|
@ -18,5 +18,9 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
public const string CmdSourceDescription = "Use specific NuGet package sources to use during the restore.";
|
public const string CmdSourceDescription = "Use specific NuGet package sources to use during the restore.";
|
||||||
|
|
||||||
public const string CmdPackageDirectoryDescription = "Restore the packages to this Directory .";
|
public const string CmdPackageDirectoryDescription = "Restore the packages to this Directory .";
|
||||||
|
|
||||||
|
public const string CmdVersionDescription = "Version for the package to be added.";
|
||||||
|
|
||||||
|
public const string CmdDGFileException = "Unable to Create Dependency graph file for project '{0}'. Cannot add package reference.";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,16 +13,18 @@ using Microsoft.DotNet.Cli.CommandLine;
|
||||||
using Microsoft.DotNet.Cli.Utils;
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
using Microsoft.DotNet.Tools.Common;
|
using Microsoft.DotNet.Tools.Common;
|
||||||
using Microsoft.DotNet.Tools.MSBuild;
|
using Microsoft.DotNet.Tools.MSBuild;
|
||||||
|
using Microsoft.DotNet.Tools.NuGet;
|
||||||
using NuGet.Frameworks;
|
using NuGet.Frameworks;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Tools.Add.PackageReference
|
namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
{
|
{
|
||||||
internal class AddPackageReferenceCommand : DotNetSubCommandBase
|
internal class AddPackageReferenceCommand : DotNetSubCommandBase
|
||||||
{
|
{
|
||||||
|
private CommandOption _versionOption;
|
||||||
private CommandOption _frameworkOption;
|
private CommandOption _frameworkOption;
|
||||||
private CommandOption _noRestore;
|
private CommandOption _noRestoreOption;
|
||||||
private CommandOption _source;
|
private CommandOption _sourceOption;
|
||||||
private CommandOption _packageDirectory;
|
private CommandOption _packageDirectoryOption;
|
||||||
|
|
||||||
public static DotNetSubCommandBase Create()
|
public static DotNetSubCommandBase Create()
|
||||||
{
|
{
|
||||||
|
@ -37,22 +39,27 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
|
|
||||||
command.HelpOption("-h|--help");
|
command.HelpOption("-h|--help");
|
||||||
|
|
||||||
|
command._versionOption = command.Option(
|
||||||
|
$"-v|--version",
|
||||||
|
LocalizableStrings.CmdVersionDescription,
|
||||||
|
CommandOptionType.SingleValue);
|
||||||
|
|
||||||
command._frameworkOption = command.Option(
|
command._frameworkOption = command.Option(
|
||||||
$"-f|--framework",
|
$"-f|--framework",
|
||||||
LocalizableStrings.CmdFrameworkDescription,
|
LocalizableStrings.CmdFrameworkDescription,
|
||||||
CommandOptionType.SingleValue);
|
CommandOptionType.SingleValue);
|
||||||
|
|
||||||
command._noRestore = command.Option(
|
command._noRestoreOption = command.Option(
|
||||||
$"-n|--no-restore ",
|
$"-n|--no-restore ",
|
||||||
LocalizableStrings.CmdNoRestoreDescription,
|
LocalizableStrings.CmdNoRestoreDescription,
|
||||||
CommandOptionType.NoValue);
|
CommandOptionType.NoValue);
|
||||||
|
|
||||||
command._source = command.Option(
|
command._sourceOption = command.Option(
|
||||||
$"-s|--source ",
|
$"-s|--source ",
|
||||||
LocalizableStrings.CmdSourceDescription,
|
LocalizableStrings.CmdSourceDescription,
|
||||||
CommandOptionType.SingleValue);
|
CommandOptionType.SingleValue);
|
||||||
|
|
||||||
command._packageDirectory = command.Option(
|
command._packageDirectoryOption = command.Option(
|
||||||
$"--package-directory",
|
$"--package-directory",
|
||||||
LocalizableStrings.CmdPackageDirectoryDescription,
|
LocalizableStrings.CmdPackageDirectoryDescription,
|
||||||
CommandOptionType.SingleValue);
|
CommandOptionType.SingleValue);
|
||||||
|
@ -62,10 +69,10 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
|
|
||||||
public override int Run(string fileOrDirectory)
|
public override int Run(string fileOrDirectory)
|
||||||
{
|
{
|
||||||
WaitForDebugger();
|
|
||||||
var projects = new ProjectCollection();
|
var projects = new ProjectCollection();
|
||||||
var msbuildProj = MsbuildProject.FromFileOrDirectory(projects, fileOrDirectory);
|
var msbuildProj = MsbuildProject.FromFileOrDirectory(projects, fileOrDirectory);
|
||||||
|
|
||||||
|
var x = this.Arguments;
|
||||||
if (RemainingArguments.Count == 0)
|
if (RemainingArguments.Count == 0)
|
||||||
{
|
{
|
||||||
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
|
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd);
|
||||||
|
@ -73,10 +80,10 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
|
|
||||||
var tempDgFilePath = CreateTemporaryFile(".dg");
|
var tempDgFilePath = CreateTemporaryFile(".dg");
|
||||||
|
|
||||||
GetProjectDependencyGraph(msbuildProj.ProjectDirectory, tempDgFilePath);
|
GetProjectDependencyGraph(msbuildProj.ProjectRootElement.FullPath, tempDgFilePath);
|
||||||
|
|
||||||
|
var result = NuGetCommand.Run(TransformArgs(tempDgFilePath, msbuildProj.ProjectRootElement.FullPath));
|
||||||
DisposeTemporaryFile(tempDgFilePath);
|
DisposeTemporaryFile(tempDgFilePath);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,6 +92,9 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
{
|
{
|
||||||
var args = new List<string>();
|
var args = new List<string>();
|
||||||
|
|
||||||
|
// Pass the project file path
|
||||||
|
args.Add(projectFilePath);
|
||||||
|
|
||||||
// Pass the task as generate restore dg file
|
// Pass the task as generate restore dg file
|
||||||
args.Add("/t:GenerateRestoreGraphFile");
|
args.Add("/t:GenerateRestoreGraphFile");
|
||||||
|
|
||||||
|
@ -95,7 +105,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
|
|
||||||
if (result != 0)
|
if (result != 0)
|
||||||
{
|
{
|
||||||
throw new GracefulException("Could not generate dg file");
|
throw new GracefulException(string.Format(LocalizableStrings.CmdDGFileException, projectFilePath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,16 +125,44 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void WaitForDebugger()
|
private string[] TransformArgs(string tempDgFilePath, string projectFilePath)
|
||||||
{
|
{
|
||||||
Console.WriteLine("Waiting for debugger to attach.");
|
var args = new List<string>(){
|
||||||
Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}");
|
"package",
|
||||||
|
"add",
|
||||||
while (!Debugger.IsAttached)
|
"--package",
|
||||||
|
"Newtonsoft.Json",
|
||||||
|
"--project",
|
||||||
|
projectFilePath,
|
||||||
|
"--dg-file",
|
||||||
|
tempDgFilePath
|
||||||
|
};
|
||||||
|
if(_versionOption.HasValue())
|
||||||
{
|
{
|
||||||
System.Threading.Thread.Sleep(100);
|
args.Append("--version");
|
||||||
|
args.Append(_versionOption.Value());
|
||||||
}
|
}
|
||||||
Debugger.Break();
|
if(_sourceOption.HasValue())
|
||||||
|
{
|
||||||
|
args.Append("--source");
|
||||||
|
args.Append(_sourceOption.Value());
|
||||||
|
}
|
||||||
|
if(_frameworkOption.HasValue())
|
||||||
|
{
|
||||||
|
args.Append("--framework");
|
||||||
|
args.Append(_frameworkOption.Value());
|
||||||
|
}
|
||||||
|
if(_noRestoreOption.HasValue())
|
||||||
|
{
|
||||||
|
args.Append("--no-restore");
|
||||||
|
}
|
||||||
|
if(_packageDirectoryOption.HasValue())
|
||||||
|
{
|
||||||
|
args.Append("--package-directory");
|
||||||
|
args.Append(_packageDirectoryOption.Value());
|
||||||
|
}
|
||||||
|
|
||||||
|
return args.ToArray();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue