-
Notifications
You must be signed in to change notification settings - Fork 644
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
Refactored PackageService code for use by Search-by-TFM #9261
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/NuGetGallery.Core/Services/AssetFrameworkService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using NuGet.Client; | ||
using NuGet.ContentModel; | ||
using NuGet.Frameworks; | ||
using NuGet.Packaging.Core; | ||
using NuGet.RuntimeModel; | ||
|
||
namespace NuGetGallery | ||
{ | ||
public static class AssetFrameworkService | ||
{ | ||
/// <summary> | ||
/// This method combines the logic used in restore operations to make a determination about the TFM supported by the package. | ||
/// We have curated a set of compatibility requirements for our needs in NuGet.org. The client logic can be found here: | ||
/// https://github.com/NuGet/NuGet.Client/blob/63255047fe7052cc33b763356ff995d9166f719e/src/NuGet.Core/NuGet.Commands/RestoreCommand/CompatibilityChecker.cs#L252-L294 | ||
/// https://github.com/NuGet/NuGet.Client/blob/63255047fe7052cc33b763356ff995d9166f719e/src/NuGet.Core/NuGet.Commands/RestoreCommand/CompatibilityChecker.cs#L439-L442 | ||
/// ...and our combination of these elements is below. | ||
/// The logic is essentially this: | ||
/// - Determine whether we're looking at a tools package. In this case we will use tools "pattern sets" (collections of file patterns | ||
/// defined in <see cref="ManagedCodeConventions" />) to assess which frameworks are targeted by the package. | ||
/// - If this isn't a tools package, we look for build-time, runtime, content and resource file patterns | ||
/// For added details on the various cases, see unit tests targeting this method. | ||
/// </summary> | ||
public static IEnumerable<NuGetFramework> GetAssetFrameworks(string packageId, IReadOnlyList<PackageType> packageTypes, IList<string> packageFiles) | ||
{ | ||
var supportedTFMs = Enumerable.Empty<NuGetFramework>(); | ||
if (packageFiles != null && packageFiles.Any()) | ||
{ | ||
// Setup content items for analysis | ||
var items = new ContentItemCollection(); | ||
items.Load(packageFiles); | ||
var runtimeGraph = new RuntimeGraph(); | ||
var conventions = new ManagedCodeConventions(runtimeGraph); | ||
|
||
// Let's test for tools packages first--they're a special case | ||
var groups = Enumerable.Empty<ContentItemGroup>(); | ||
if (packageTypes.Count == 1 && (packageTypes[0] == PackageType.DotnetTool || | ||
packageTypes[0] == PackageType.DotnetCliTool)) | ||
{ | ||
// Only a package that is a tool package (and nothing else) will be matched against tools pattern set | ||
groups = items.FindItemGroups(conventions.Patterns.ToolsAssemblies); | ||
} | ||
else | ||
{ | ||
// Gather together a list of pattern sets indicating the kinds of packages we wish to evaluate | ||
var patterns = new[] | ||
{ | ||
conventions.Patterns.CompileRefAssemblies, | ||
conventions.Patterns.CompileLibAssemblies, | ||
conventions.Patterns.RuntimeAssemblies, | ||
conventions.Patterns.ContentFiles, | ||
conventions.Patterns.ResourceAssemblies, | ||
}; | ||
|
||
// Add MSBuild to this list, but we need to ensure we have package assets before they make the cut. | ||
// A series of files in the right places won't matter if there's no {id}.props|targets. | ||
var msbuildPatterns = new[] | ||
{ | ||
conventions.Patterns.MSBuildFiles, | ||
conventions.Patterns.MSBuildMultiTargetingFiles, | ||
}; | ||
|
||
// We'll create a set of "groups" --these are content items which satisfy file pattern sets | ||
var standardGroups = patterns | ||
.SelectMany(p => items.FindItemGroups(p)); | ||
|
||
// Filter out MSBuild assets that don't match the package ID and append to groups we already have | ||
var msbuildGroups = msbuildPatterns | ||
.SelectMany(p => items.FindItemGroups(p)) | ||
.Where(g => HasBuildItemsForPackageId(g.Items, packageId)); | ||
groups = standardGroups.Concat(msbuildGroups); | ||
} | ||
|
||
// Now that we have a collection of groups which have made it through the pattern set filter, let's transform them into TFMs | ||
supportedTFMs = groups | ||
.SelectMany(p => p.Properties) | ||
.Where(pair => pair.Key == ManagedCodeConventions.PropertyNames.TargetFrameworkMoniker) | ||
.Select(pair => pair.Value) | ||
.Cast<NuGetFramework>() | ||
.Distinct(); | ||
} | ||
|
||
return supportedTFMs; | ||
} | ||
|
||
private static bool HasBuildItemsForPackageId(IEnumerable<ContentItem> items, string packageId) | ||
{ | ||
foreach (var item in items) | ||
{ | ||
var fileName = Path.GetFileName(item.Path); | ||
if (fileName == PackagingCoreConstants.EmptyFolder) | ||
{ | ||
return true; | ||
} | ||
|
||
if ($"{packageId}.props".Equals(fileName, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
|
||
if ($"{packageId}.targets".Equals(fileName, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
this needs the .NET Foundation header
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've added this in now