Fixing on PR comments

This commit is contained in:
Ankit Mishra 2016-12-22 15:29:29 -08:00
parent 2738ea8a5a
commit ac988a4ac9
3 changed files with 52 additions and 51 deletions

View file

@ -1,6 +1,10 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // 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. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Build.Construction; using Microsoft.Build.Construction;
using Microsoft.Build.Evaluation; using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions; using Microsoft.Build.Exceptions;
@ -8,10 +12,6 @@ using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Tools.Common; using Microsoft.DotNet.Tools.Common;
using Microsoft.DotNet.Tools.ProjectExtensions; using Microsoft.DotNet.Tools.ProjectExtensions;
using NuGet.Frameworks; using NuGet.Frameworks;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Microsoft.DotNet.Tools namespace Microsoft.DotNet.Tools
{ {
@ -61,6 +61,19 @@ namespace Microsoft.DotNet.Tools
} }
public static MsbuildProject FromDirectory(ProjectCollection projects, string projectDirectory) public static MsbuildProject FromDirectory(ProjectCollection projects, string projectDirectory)
{
FileInfo projectFile = GetProjectFileFromDirectory(projectDirectory);
var project = TryOpenProject(projects, projectFile.FullName);
if (project == null)
{
throw new GracefulException(CommonLocalizableStrings.FoundInvalidProject, projectFile.FullName);
}
return new MsbuildProject(projects, project);
}
public static FileInfo GetProjectFileFromDirectory(string projectDirectory)
{ {
DirectoryInfo dir; DirectoryInfo dir;
try try
@ -90,22 +103,7 @@ namespace Microsoft.DotNet.Tools
throw new GracefulException(CommonLocalizableStrings.MoreThanOneProjectInDirectory, projectDirectory); throw new GracefulException(CommonLocalizableStrings.MoreThanOneProjectInDirectory, projectDirectory);
} }
FileInfo projectFile = files.First(); return files.First();
if (!projectFile.Exists)
{
throw new GracefulException(
CommonLocalizableStrings.CouldNotFindAnyProjectInDirectory,
projectDirectory);
}
var project = TryOpenProject(projects, projectFile.FullName);
if (project == null)
{
throw new GracefulException(CommonLocalizableStrings.FoundInvalidProject, projectFile.FullName);
}
return new MsbuildProject(projects, project);
} }
public int AddProjectToProjectReferences(string framework, IEnumerable<string> refs) public int AddProjectToProjectReferences(string framework, IEnumerable<string> refs)
@ -120,7 +118,7 @@ namespace Microsoft.DotNet.Tools
if (ProjectRootElement.HasExistingItemWithCondition(framework, @ref)) if (ProjectRootElement.HasExistingItemWithCondition(framework, @ref))
{ {
Reporter.Output.WriteLine(string.Format( Reporter.Output.WriteLine(string.Format(
CommonLocalizableStrings.ProjectAlreadyHasAreference, CommonLocalizableStrings.ProjectAlreadyHasAreference,
@ref)); @ref));
continue; continue;
} }
@ -270,4 +268,4 @@ namespace Microsoft.DotNet.Tools
} }
} }
} }
} }

View file

@ -11,6 +11,8 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
public const string AppHelpText = "Package references to add"; public const string AppHelpText = "Package references to add";
public const string SpecifyExactlyOnePackageReference = "Please specify one package reference to add.";
public const string CmdFrameworkDescription = "Add reference only when targetting a specific framework"; public const string CmdFrameworkDescription = "Add reference only when targetting a specific framework";
public const string CmdNoRestoreDescription = "Add reference without performing restore preview and compatibility check."; public const string CmdNoRestoreDescription = "Add reference without performing restore preview and compatibility check.";
@ -30,6 +32,5 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
public const string CmdSource = "SOURCE"; public const string CmdSource = "SOURCE";
public const string CmdPackageDirectory = "PACKAGE_DIRECTORY"; public const string CmdPackageDirectory = "PACKAGE_DIRECTORY";
} }
} }

View file

@ -28,7 +28,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
public static DotNetSubCommandBase Create() public static DotNetSubCommandBase Create()
{ {
var command = new AddPackageReferenceCommand() var command = new AddPackageReferenceCommand
{ {
Name = "package", Name = "package",
FullName = LocalizableStrings.AppFullName, FullName = LocalizableStrings.AppFullName,
@ -69,29 +69,38 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
public override int Run(string fileOrDirectory) public override int Run(string fileOrDirectory)
{ {
var projects = new ProjectCollection(); if (RemainingArguments.Count != 1)
var msbuildProj = MsbuildProject.FromFileOrDirectory(projects, fileOrDirectory);
if (RemainingArguments.Count == 0)
{ {
throw new GracefulException(CommonLocalizableStrings.SpecifyAtLeastOneReferenceToAdd); throw new GracefulException(LocalizableStrings.SpecifyExactlyOnePackageReference);
}
var projectFilePath = string.Empty;
if (!File.Exists(fileOrDirectory))
{
projectFilePath = MsbuildProject.GetProjectFileFromDirectory(fileOrDirectory).FullName;
}
else
{
projectFilePath = fileOrDirectory;
} }
var tempDgFilePath = string.Empty; var tempDgFilePath = string.Empty;
if(!_noRestoreOption.HasValue())
if (!_noRestoreOption.HasValue())
{ {
// Create a Dependency Graph file for the project // Create a Dependency Graph file for the project
tempDgFilePath = CreateTemporaryFile(".dg"); tempDgFilePath = Path.GetTempFileName();
GetProjectDependencyGraph(msbuildProj.ProjectRootElement.FullPath, tempDgFilePath); GetProjectDependencyGraph(projectFilePath, tempDgFilePath);
} }
var result = NuGetCommand.Run(TransformArgs(tempDgFilePath, msbuildProj.ProjectRootElement.FullPath)); var result = NuGetCommand.Run(TransformArgs(RemainingArguments.First(), tempDgFilePath, projectFilePath));
DisposeTemporaryFile(tempDgFilePath); DisposeTemporaryFile(tempDgFilePath);
return result; return result;
} }
private void GetProjectDependencyGraph(string projectFilePath, private void GetProjectDependencyGraph(string projectFilePath, string dgFilePath)
string dgFilePath)
{ {
var args = new List<string>(); var args = new List<string>();
@ -102,7 +111,7 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
args.Add("/t:GenerateRestoreGraphFile"); args.Add("/t:GenerateRestoreGraphFile");
// Pass Dependency Graph file output path // Pass Dependency Graph file output path
args.Add(string.Format("/p:RestoreGraphOutputPath={0}{1}{2}", '"', dgFilePath, '"')); args.Add($"/p:RestoreGraphOutputPath=\"{dgFilePath}\"");
var result = new MSBuildForwardingApp(args).Execute(); var result = new MSBuildForwardingApp(args).Execute();
@ -112,14 +121,6 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
} }
} }
private string CreateTemporaryFile(string extension)
{
var tempDirectory = Path.GetTempPath();
var tempFile = Path.Combine(tempDirectory, Guid.NewGuid().ToString() + extension);
File.Create(tempFile).Dispose();
return tempFile;
}
private void DisposeTemporaryFile(string filePath) private void DisposeTemporaryFile(string filePath)
{ {
if (File.Exists(filePath)) if (File.Exists(filePath))
@ -128,37 +129,38 @@ namespace Microsoft.DotNet.Tools.Add.PackageReference
} }
} }
private string[] TransformArgs(string tempDgFilePath, string projectFilePath) private string[] TransformArgs(string packageId, string tempDgFilePath, string projectFilePath)
{ {
var args = new List<string>(){ var args = new List<string>(){
"package", "package",
"add", "add",
"--package", "--package",
"Newtonsoft.Json", packageId,
"--project", "--project",
projectFilePath projectFilePath
}; };
if(_versionOption.HasValue())
if (_versionOption.HasValue())
{ {
args.Add("--version"); args.Add("--version");
args.Add(_versionOption.Value()); args.Add(_versionOption.Value());
} }
if(_sourceOption.HasValue()) if (_sourceOption.HasValue())
{ {
args.Add("--source"); args.Add("--source");
args.Add(_sourceOption.Value()); args.Add(_sourceOption.Value());
} }
if(_frameworkOption.HasValue()) if (_frameworkOption.HasValue())
{ {
args.Add("--framework"); args.Add("--framework");
args.Add(_frameworkOption.Value()); args.Add(_frameworkOption.Value());
} }
if(_packageDirectoryOption.HasValue()) if (_packageDirectoryOption.HasValue())
{ {
args.Add("--package-directory"); args.Add("--package-directory");
args.Add(_packageDirectoryOption.Value()); args.Add(_packageDirectoryOption.Value());
} }
if(_noRestoreOption.HasValue()) if (_noRestoreOption.HasValue())
{ {
args.Add("--no-restore"); args.Add("--no-restore");
} }