This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPackageAssetToCsvDriver.cs
More file actions
198 lines (169 loc) · 9.3 KB
/
PackageAssetToCsvDriver.cs
File metadata and controls
198 lines (169 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Knapcode.MiniZip;
using NuGet.Client;
using NuGet.ContentModel;
using NuGet.Frameworks;
using NuGet.RuntimeModel;
namespace NuGet.Insights.Worker.PackageAssetToCsv
{
public class PackageAssetToCsvDriver : ICatalogLeafToCsvDriver<PackageAsset>, ICsvResultStorage<PackageAsset>
{
private readonly CatalogClient _catalogClient;
private readonly PackageFileService _packageFileService;
private readonly IOptions<NuGetInsightsWorkerSettings> _options;
private readonly ILogger<PackageAssetToCsvDriver> _logger;
private static readonly IReadOnlyDictionary<string, PatternSetType> NameToPatternSetType = Enum
.GetValues(typeof(PatternSetType))
.Cast<PatternSetType>()
.ToDictionary(x => x.ToString(), x => x);
public PackageAssetToCsvDriver(
CatalogClient catalogClient,
PackageFileService packageFileService,
IOptions<NuGetInsightsWorkerSettings> options,
ILogger<PackageAssetToCsvDriver> logger)
{
_catalogClient = catalogClient;
_packageFileService = packageFileService;
_options = options;
_logger = logger;
}
public string ResultContainerName => _options.Value.PackageAssetContainerName;
public bool SingleMessagePerId => false;
public async Task InitializeAsync()
{
await _packageFileService.InitializeAsync();
}
public Task DestroyAsync()
{
return Task.CompletedTask;
}
public async Task<DriverResult<IReadOnlyList<PackageAsset>>> ProcessLeafAsync(CatalogLeafScan leafScan)
{
var records = await ProcessLeafInternalAsync(leafScan);
return DriverResult.Success(records);
}
private async Task<IReadOnlyList<PackageAsset>> ProcessLeafInternalAsync(CatalogLeafScan leafScan)
{
var scanId = Guid.NewGuid();
var scanTimestamp = DateTimeOffset.UtcNow;
if (leafScan.LeafType == CatalogLeafType.PackageDelete)
{
var leaf = (PackageDeleteCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
return [new PackageAsset(scanId, scanTimestamp, leaf)];
}
else
{
var leaf = (PackageDetailsCatalogLeaf)await _catalogClient.GetCatalogLeafAsync(leafScan.LeafType, leafScan.Url);
var zipDirectory = await _packageFileService.GetZipDirectoryAsync(leafScan.ToPackageIdentityCommit());
if (zipDirectory == null)
{
// Ignore packages where the .nupkg is missing. A subsequent scan will produce a deleted asset record.
return [];
}
var files = zipDirectory
.Entries
.Select(x => x.GetName())
.ToList();
return GetAssets(scanId, scanTimestamp, leaf, files);
}
}
private List<PackageAsset> GetAssets(Guid scanId, DateTimeOffset scanTimestamp, PackageDetailsCatalogLeaf leaf, IReadOnlyList<string> files)
{
var contentItemCollection = new ContentItemCollection();
contentItemCollection.Load(files);
var assets = new List<PackageAsset>();
foreach (var pair in GetPatternSets())
{
var groups = new List<ContentItemGroup>();
try
{
// We must enumerate the item groups here to force the exception to be thrown, if any.
contentItemCollection.PopulateItemGroups(pair.Value, groups);
}
catch (ArgumentException ex) when (IsInvalidDueToHyphenInProfile(ex))
{
return GetErrorResult(scanId, scanTimestamp, leaf, ex, "Package {Id} {Version} contains a portable framework with a hyphen in the profile.");
}
foreach (var group in groups)
{
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.AnyValue, out var anyValue);
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.CodeLanguage, out var codeLanguage);
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.Locale, out var locale);
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.ManagedAssembly, out var managedAssembly);
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.MSBuild, out var msbuild);
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.RuntimeIdentifier, out var runtimeIdentifier);
group.Properties.TryGetValue(ManagedCodeConventions.PropertyNames.SatelliteAssembly, out var satelliteAssembly);
string targetFrameworkMoniker;
try
{
targetFrameworkMoniker = ((NuGetFramework)group.Properties[ManagedCodeConventions.PropertyNames.TargetFrameworkMoniker]).GetShortFolderName();
}
catch (FrameworkException ex) when (IsInvalidDueMissingPortableProfile(ex))
{
return GetErrorResult(scanId, scanTimestamp, leaf, ex, "Package {Id} {Version} contains a portable framework missing a profile.");
}
var parsedFramework = NuGetFramework.Parse(targetFrameworkMoniker);
var roundTripTargetFrameworkMoniker = parsedFramework.GetShortFolderName();
foreach (var item in group.Items)
{
assets.Add(new PackageAsset(scanId, scanTimestamp, leaf, PackageAssetResultType.AvailableAssets)
{
PatternSet = NameToPatternSetType[pair.Key],
PropertyAnyValue = (string)anyValue,
PropertyCodeLanguage = (string)codeLanguage,
PropertyTargetFrameworkMoniker = targetFrameworkMoniker,
PropertyManagedAssembly = (string)managedAssembly,
PropertyMSBuild = (string)msbuild,
PropertyRuntimeIdentifier = (string)runtimeIdentifier,
PropertySatelliteAssembly = (string)satelliteAssembly,
Path = item.Path,
FileName = Path.GetFileName(item.Path),
FileExtension = Path.GetExtension(item.Path),
TopLevelFolder = PathUtility.GetTopLevelFolder(item.Path),
RoundTripTargetFrameworkMoniker = roundTripTargetFrameworkMoniker,
FrameworkName = parsedFramework.Framework,
FrameworkVersion = parsedFramework.Version?.ToString(),
FrameworkProfile = parsedFramework.Profile,
PlatformName = parsedFramework.Platform,
PlatformVersion = parsedFramework.PlatformVersion?.ToString(),
});
}
}
}
if (assets.Count == 0)
{
return [new PackageAsset(scanId, scanTimestamp, leaf, PackageAssetResultType.NoAssets)];
}
return assets;
}
internal static Dictionary<string, PatternSet> GetPatternSets()
{
var runtimeGraph = new RuntimeGraph();
var conventions = new ManagedCodeConventions(runtimeGraph);
return conventions
.Patterns
.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty)
.Where(x => x.Name != nameof(ManagedCodeConventions.Patterns.AnyTargettedFile)) // This pattern is unused.
.ToDictionary(x => x.Name, x => (PatternSet)x.GetGetMethod().Invoke(conventions.Patterns, null));
}
private List<PackageAsset> GetErrorResult(Guid scanId, DateTimeOffset scanTimestamp, PackageDetailsCatalogLeaf leaf, Exception ex, string message)
{
_logger.LogWarning(ex, message, leaf.PackageId, leaf.PackageVersion);
return [new PackageAsset(scanId, scanTimestamp, leaf, PackageAssetResultType.Error)];
}
private static bool IsInvalidDueMissingPortableProfile(FrameworkException ex)
{
return
ex.Message.StartsWith("Invalid portable frameworks for '", StringComparison.Ordinal)
&& ex.Message.EndsWith("'. A portable framework must have at least one framework in the profile.", StringComparison.Ordinal);
}
public static bool IsInvalidDueToHyphenInProfile(ArgumentException ex)
{
return
ex.Message.StartsWith("Invalid portable frameworks '", StringComparison.Ordinal)
&& ex.Message.EndsWith("'. A hyphen may not be in any of the portable framework names.", StringComparison.Ordinal);
}
}
}