-
Notifications
You must be signed in to change notification settings - Fork 690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes to add package command #1086
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,16 @@ | |
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NuGet.Commands; | ||
using NuGet.Common; | ||
using NuGet.Configuration; | ||
using NuGet.Frameworks; | ||
using NuGet.Packaging.Core; | ||
using NuGet.ProjectModel; | ||
using NuGet.Protocol; | ||
using NuGet.Protocol.Core.Types; | ||
using NuGet.Versioning; | ||
|
||
|
@@ -77,8 +72,6 @@ public async Task<int> ExecuteCommand(PackageReferenceArgs packageReferenceArgs, | |
.Where(t => t.Success) | ||
.Select(t => t.Graph.Framework)); | ||
|
||
var x = restorePreviewResult.Result.GetAllUnresolved(); | ||
|
||
if (packageReferenceArgs.Frameworks?.Any() == true) | ||
{ | ||
// If the user has specified frameworks then we intersect that with the compatible frameworks. | ||
|
@@ -98,6 +91,7 @@ public async Task<int> ExecuteCommand(PackageReferenceArgs packageReferenceArgs, | |
packageReferenceArgs.Logger.LogError(string.Format(CultureInfo.CurrentCulture, | ||
Strings.Error_AddPkgIncompatibleWithAllFrameworks, | ||
packageReferenceArgs.PackageDependency.Id, | ||
packageReferenceArgs.Frameworks?.Any() == true ? Strings.AddPkg_UserSpecified : Strings.AddPkg_All, | ||
packageReferenceArgs.ProjectPath)); | ||
|
||
return 1; | ||
|
@@ -141,7 +135,7 @@ public async Task<int> ExecuteCommand(PackageReferenceArgs packageReferenceArgs, | |
return 0; | ||
} | ||
|
||
private async Task<RestoreResultPair> PreviewAddPackageReference(PackageReferenceArgs packageReferenceArgs, | ||
private static async Task<RestoreResultPair> PreviewAddPackageReference(PackageReferenceArgs packageReferenceArgs, | ||
DependencyGraphSpec dgSpec, | ||
PackageSpec originalPackageSpec) | ||
{ | ||
|
@@ -186,7 +180,7 @@ private async Task<RestoreResultPair> PreviewAddPackageReference(PackageReferenc | |
} | ||
} | ||
|
||
private DependencyGraphSpec ReadProjectDependencyGraph(PackageReferenceArgs packageReferenceArgs) | ||
private static DependencyGraphSpec ReadProjectDependencyGraph(PackageReferenceArgs packageReferenceArgs) | ||
{ | ||
DependencyGraphSpec spec = null; | ||
|
||
|
@@ -198,30 +192,60 @@ private DependencyGraphSpec ReadProjectDependencyGraph(PackageReferenceArgs pack | |
return spec; | ||
} | ||
|
||
private void UpdatePackageVersionIfNeeded(RestoreResultPair restorePreviewResult, | ||
private static void UpdatePackageVersionIfNeeded(RestoreResultPair restorePreviewResult, | ||
PackageReferenceArgs packageReferenceArgs) | ||
{ | ||
// If the user did not specify a version then write the exact resolved version | ||
if (packageReferenceArgs.NoVersion) | ||
{ | ||
// Get the flattened restore graph | ||
var flattenedRestoreGraph = restorePreviewResult | ||
.Result | ||
.RestoreGraphs | ||
.First() | ||
.Flattened; | ||
|
||
// Get the package entry and version from the graph | ||
var packageEntry = flattenedRestoreGraph | ||
.Single(p => p.Key.Name.Equals(packageReferenceArgs.PackageDependency.Id, StringComparison.OrdinalIgnoreCase)); | ||
var resolvedVersion = packageEntry | ||
.Key | ||
.Version; | ||
|
||
//Update the packagedependency with the new version | ||
packageReferenceArgs.PackageDependency = new PackageDependency(packageReferenceArgs.PackageDependency.Id, | ||
VersionRange.Parse(resolvedVersion.ToString())); | ||
// Get the package version from the graph | ||
var resolvedVersion = GetPackageVersionFromRestoreResult(restorePreviewResult, packageReferenceArgs); | ||
|
||
if (resolvedVersion != null) | ||
{ | ||
//Update the packagedependency with the new version | ||
packageReferenceArgs.PackageDependency = new PackageDependency(packageReferenceArgs.PackageDependency.Id, | ||
new VersionRange(resolvedVersion)); | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might make the code more clear to just return the version here, it would be easier to handle errors as needed from a higher level. |
||
|
||
private static NuGetVersion GetPackageVersionFromRestoreResult(RestoreResultPair restorePreviewResult, | ||
PackageReferenceArgs packageReferenceArgs) | ||
{ | ||
// Get the restore graphs from the restore result | ||
var restoreGraphs = restorePreviewResult | ||
.Result | ||
.RestoreGraphs; | ||
|
||
if (packageReferenceArgs.Frameworks?.Any() == true) | ||
{ | ||
// If the user specified frameworks then we get the flattened graphs only from the compatible frameworks. | ||
var userSpecifiedFrameworks = new HashSet<NuGetFramework>( | ||
packageReferenceArgs | ||
.Frameworks | ||
.Select(f => NuGetFramework.Parse(f))); | ||
|
||
restoreGraphs = restoreGraphs | ||
.Where(r => userSpecifiedFrameworks.Contains(r.Framework)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the user accidentally specified a framework that doesn't exist in the project? a error message for that scenario might help, looks like it would just go on and possibly fail with an unrelated error message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed here |
||
} | ||
|
||
foreach (var restoreGraph in restoreGraphs) | ||
{ | ||
var matchingPackageEntries = restoreGraph | ||
.Flattened | ||
.Select(p => p) | ||
.Where(p => p.Key.Name.Equals(packageReferenceArgs.PackageDependency.Id, StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (matchingPackageEntries.Any()) | ||
{ | ||
return matchingPackageEntries | ||
.First() | ||
.Key | ||
.Version; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see where null is being handled. If finding the highest version comes back as null it looks like this just keeps going