dotnet-installer/src/dotnet/commands/dotnet-add/dotnet-add-package/Program.cs

176 lines
5.8 KiB
C#
Raw Normal View History

2016-12-20 14:23:11 -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 System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
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;
2016-12-20 16:25:18 -08:00
using Microsoft.DotNet.Tools.NuGet;
2016-12-20 14:23:11 -08:00
using NuGet.Frameworks;
namespace Microsoft.DotNet.Tools.Add.PackageReference
{
internal class AddPackageReferenceCommand : DotNetSubCommandBase
{
2016-12-20 16:25:18 -08:00
private CommandOption _versionOption;
2016-12-20 14:23:11 -08:00
private CommandOption _frameworkOption;
2016-12-20 16:25:18 -08:00
private CommandOption _noRestoreOption;
private CommandOption _sourceOption;
private CommandOption _packageDirectoryOption;
2016-12-20 14:23:11 -08:00
public static DotNetSubCommandBase Create()
{
2016-12-22 15:29:29 -08:00
var command = new AddPackageReferenceCommand
2016-12-20 14:23:11 -08:00
{
Name = "package",
FullName = LocalizableStrings.AppFullName,
Description = LocalizableStrings.AppDescription,
HandleRemainingArguments = true,
ArgumentSeparatorHelpText = LocalizableStrings.AppHelpText,
};
command.HelpOption("-h|--help");
2016-12-20 16:25:18 -08:00
command._versionOption = command.Option(
2016-12-21 11:44:45 -08:00
$"-v|--version <{LocalizableStrings.CmdVersion}>",
2016-12-20 16:25:18 -08:00
LocalizableStrings.CmdVersionDescription,
CommandOptionType.SingleValue);
2016-12-20 14:23:11 -08:00
command._frameworkOption = command.Option(
2016-12-21 11:44:45 -08:00
$"-f|--framework <{LocalizableStrings.CmdFramework}>",
2016-12-20 14:23:11 -08:00
LocalizableStrings.CmdFrameworkDescription,
CommandOptionType.SingleValue);
2016-12-20 16:25:18 -08:00
command._noRestoreOption = command.Option(
2016-12-20 19:48:52 -08:00
"-n|--no-restore ",
2016-12-20 14:23:11 -08:00
LocalizableStrings.CmdNoRestoreDescription,
CommandOptionType.NoValue);
2016-12-20 16:25:18 -08:00
command._sourceOption = command.Option(
2016-12-21 11:44:45 -08:00
$"-s|--source <{LocalizableStrings.CmdSource}>",
2016-12-20 14:23:11 -08:00
LocalizableStrings.CmdSourceDescription,
CommandOptionType.SingleValue);
2016-12-20 16:25:18 -08:00
command._packageDirectoryOption = command.Option(
2016-12-21 11:44:45 -08:00
$"--package-directory <{LocalizableStrings.CmdPackageDirectory}>",
2016-12-20 14:23:11 -08:00
LocalizableStrings.CmdPackageDirectoryDescription,
CommandOptionType.SingleValue);
return command;
}
public override int Run(string fileOrDirectory)
{
2016-12-22 15:29:29 -08:00
if (RemainingArguments.Count != 1)
{
throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference);
}
var projectFilePath = string.Empty;
2016-12-20 14:23:11 -08:00
2016-12-22 15:29:29 -08:00
if (!File.Exists(fileOrDirectory))
{
projectFilePath = MsbuildProject.GetProjectFileFromDirectory(fileOrDirectory).FullName;
}
else
2016-12-20 14:23:11 -08:00
{
2016-12-22 15:29:29 -08:00
projectFilePath = fileOrDirectory;
2016-12-20 14:23:11 -08:00
}
2016-12-20 19:48:52 -08:00
var tempDgFilePath = string.Empty;
2016-12-22 15:29:29 -08:00
if (!_noRestoreOption.HasValue())
2016-12-20 19:48:52 -08:00
{
// Create a Dependency Graph file for the project
2016-12-22 15:29:29 -08:00
tempDgFilePath = Path.GetTempFileName();
GetProjectDependencyGraph(projectFilePath, tempDgFilePath);
2016-12-20 19:48:52 -08:00
}
2016-12-20 14:23:11 -08:00
2016-12-22 15:29:29 -08:00
var result = NuGetCommand.Run(TransformArgs(RemainingArguments.First(), tempDgFilePath, projectFilePath));
2016-12-20 14:23:11 -08:00
DisposeTemporaryFile(tempDgFilePath);
2016-12-22 15:29:29 -08:00
2016-12-20 19:48:52 -08:00
return result;
2016-12-20 14:23:11 -08:00
}
2016-12-22 15:29:29 -08:00
private void GetProjectDependencyGraph(string projectFilePath, string dgFilePath)
2016-12-20 14:23:11 -08:00
{
var args = new List<string>();
2016-12-20 16:25:18 -08:00
// Pass the project file path
args.Add(projectFilePath);
2016-12-20 19:48:52 -08:00
// Pass the task as generate restore Dependency Graph file
2016-12-20 14:23:11 -08:00
args.Add("/t:GenerateRestoreGraphFile");
2016-12-20 19:48:52 -08:00
// Pass Dependency Graph file output path
2016-12-22 15:29:29 -08:00
args.Add($"/p:RestoreGraphOutputPath=\"{dgFilePath}\"");
2016-12-20 14:23:11 -08:00
var result = new MSBuildForwardingApp(args).Execute();
if (result != 0)
{
2016-12-20 16:25:18 -08:00
throw new GracefulException(string.Format(LocalizableStrings.CmdDGFileException, projectFilePath));
2016-12-20 14:23:11 -08:00
}
}
private void DisposeTemporaryFile(string filePath)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
2016-12-22 15:29:29 -08:00
private string[] TransformArgs(string packageId, string tempDgFilePath, string projectFilePath)
2016-12-20 14:23:11 -08:00
{
2016-12-20 16:25:18 -08:00
var args = new List<string>(){
"package",
"add",
"--package",
2016-12-22 15:29:29 -08:00
packageId,
2016-12-20 16:25:18 -08:00
"--project",
2016-12-20 19:48:52 -08:00
projectFilePath
2016-12-20 16:25:18 -08:00
};
2016-12-22 15:29:29 -08:00
if (_versionOption.HasValue())
2016-12-20 16:25:18 -08:00
{
2016-12-20 19:48:52 -08:00
args.Add("--version");
args.Add(_versionOption.Value());
2016-12-20 16:25:18 -08:00
}
2016-12-22 15:29:29 -08:00
if (_sourceOption.HasValue())
2016-12-20 16:25:18 -08:00
{
2016-12-20 19:48:52 -08:00
args.Add("--source");
args.Add(_sourceOption.Value());
2016-12-20 16:25:18 -08:00
}
2016-12-22 15:29:29 -08:00
if (_frameworkOption.HasValue())
2016-12-20 16:25:18 -08:00
{
2016-12-20 19:48:52 -08:00
args.Add("--framework");
args.Add(_frameworkOption.Value());
}
2016-12-22 15:29:29 -08:00
if (_packageDirectoryOption.HasValue())
2016-12-20 19:48:52 -08:00
{
args.Add("--package-directory");
args.Add(_packageDirectoryOption.Value());
2016-12-20 16:25:18 -08:00
}
2016-12-22 15:29:29 -08:00
if (_noRestoreOption.HasValue())
2016-12-20 16:25:18 -08:00
{
2016-12-20 19:48:52 -08:00
args.Add("--no-restore");
2016-12-20 16:25:18 -08:00
}
2016-12-20 19:48:52 -08:00
else
2016-12-20 14:23:11 -08:00
{
2016-12-20 19:48:52 -08:00
args.Add("--dg-file");
args.Add(tempDgFilePath);
2016-12-20 14:23:11 -08:00
}
2016-12-20 16:25:18 -08:00
return args.ToArray();
2016-12-20 14:23:11 -08:00
}
}
}