/
PackageIndexerWithCustomBinding.cs
243 lines (206 loc) · 12.1 KB
/
PackageIndexerWithCustomBinding.cs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Blob;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using NuGet.Packaging;
using NuGetTypeSearch.Approach3.Indexing.Models;
using NuGetTypeSearch.Bindings;
using NuGetTypeSearch.Bindings.Catalog;
using NuGetTypeSearch.Bindings.Search;
namespace NuGetTypeSearch.Approach3.Indexing
{
public static class PackageIndexerWithCustomBinding
{
private static readonly HttpClient HttpClient = SharedHttpClient.Instance;
private static readonly JsonSerializer JsonSerializer = JsonSerializer.CreateDefault(
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
[Disable]
[FunctionName("Approach3-02-PackageIndexerWithCustomBinding")]
public static async Task RunAsync(
[QueueTrigger(Constants.IndexingQueue, Connection = Constants.IndexingQueueConnection)]
PackageOperation packageOperation,
[AzureSearchIndex(
SearchServiceName = Constants.SearchServiceName,
SearchServiceKey = Constants.SearchServiceKey,
IndexName = Constants.SearchServiceIndexName,
IndexDocumentType = typeof(PackageDocument),
IndexAction = IndexAction.MergeOrUpload,
CreateOrUpdateIndex = true)]
IAsyncCollector<PackageDocument> documentAddCollector,
[AzureSearchIndex(
SearchServiceName = Constants.SearchServiceName,
SearchServiceKey = Constants.SearchServiceKey,
IndexName = Constants.SearchServiceIndexName,
IndexDocumentType = typeof(PackageDocument),
IndexAction = IndexAction.Delete,
CreateOrUpdateIndex = true)]
IAsyncCollector<PackageDocument> documentDeleteCollector,
[Blob("index/{Id}.{VersionNormalized}.json", FileAccess.ReadWrite, Connection = Constants.IndexConnection)]
CloudBlockBlob packageBlob,
ILogger log)
{
// Build actions
if (packageOperation.IsAdd())
{
await RunAddPackageAsync(packageOperation, packageBlob, log, Constants.DevCommitToSearchIndex
? documentAddCollector
: DummyAsyncCollector.For<PackageDocument>());
}
else if (packageOperation.IsDelete())
{
await RunDeletePackageAsync(packageOperation, packageBlob, Constants.DevCommitToSearchIndex
? documentDeleteCollector
: DummyAsyncCollector.For<PackageDocument>());
}
}
private static async Task RunAddPackageAsync(PackageOperation packageOperation, CloudBlockBlob packageBlob, ILogger log, IAsyncCollector<PackageDocument> documentAddCollector)
{
var packagesToIndex = new List<PackageDocument>();
log.LogInformation("Downloading package {packageId}@{packageVersionNormalized} for indexing...",
packageOperation.Id, packageOperation.Version);
using (var packageInputStream = await HttpClient.GetStreamAsync(packageOperation.PackageUrl))
using (var packageInputSeekableStream = TemporaryFileStream.Create())
{
await packageInputStream.CopyToAsync(packageInputSeekableStream);
packageInputSeekableStream.Position = 0;
log.LogInformation("Finished downloading package {packageId}@{packageVersionNormalized} for indexing...",
packageOperation.Id, packageOperation.Version);
using (var nugetPackage = new PackageArchiveReader(packageInputSeekableStream))
{
log.LogInformation("Analyzing package {packageId}@{packageVersionNormalized}...",
packageOperation.Id, packageOperation.Version);
// Get some metadata
var nuspecReader = nugetPackage.NuspecReader;
var packageIdentity = nuspecReader.GetIdentity();
var packageSummary = nuspecReader.GetDescription();
if (string.IsNullOrEmpty(packageSummary))
{
packageSummary = nuspecReader.GetSummary();
}
var packageToIndex = new PackageDocument(
packageIdentity.Id,
packageIdentity.Version.ToNormalizedString(),
packageIdentity.Version.OriginalVersion,
nuspecReader.GetTitle(),
packageSummary,
nuspecReader.GetAuthors(),
nuspecReader.GetTags(),
nuspecReader.GetIconUrl(),
nuspecReader.GetLicenseUrl(),
nuspecReader.GetProjectUrl(),
packageOperation.Published,
AuxiliaryNuGetData.GetDownloadCount(packageIdentity.Id),
packageOperation.IsListed,
packageIdentity.Version.IsPrerelease);
var targetFrameworks = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var typeNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var frameworkSpecificGroups = nugetPackage.GetReferenceItems();
foreach (var frameworkSpecificGroup in frameworkSpecificGroups)
{
// Get some metadata
var targetFramework = frameworkSpecificGroup.TargetFramework.GetShortFolderName();
targetFrameworks.Add(targetFramework);
log.LogInformation(
"Collecting information for {packageId}@{packageVersionNormalized} and framework {targetFramework}...",
packageOperation.Id, packageOperation.Version, targetFramework);
// Collect assembly data
foreach (var item in frameworkSpecificGroup.Items)
{
var entry = nugetPackage.GetEntry(item);
var entryName = item;
log.LogInformation(
"Collecting assembly information from {entryName} for {packageId}@{packageVersionNormalized} and framework {targetFramework}...",
entryName, packageOperation.Id, packageOperation.Version, targetFramework);
using (var assemblyStream = entry.Open())
using (var assemblySeekableStream = TemporaryFileStream.Create())
{
await assemblyStream.CopyToAsync(assemblySeekableStream);
assemblySeekableStream.Position = 0;
using (var portableExecutableReader = new PEReader(assemblySeekableStream))
{
var metadataReader = portableExecutableReader.GetMetadataReader();
foreach (var typeDefinition in metadataReader.TypeDefinitions.Select(metadataReader
.GetTypeDefinition))
{
if (!typeDefinition.Attributes.HasFlag(TypeAttributes.Public)) continue;
var typeNamespace = metadataReader.GetString(typeDefinition.Namespace);
var typeName = metadataReader.GetString(typeDefinition.Name);
if (typeName.StartsWith("<") || typeName.StartsWith("__Static") ||
typeName.Contains("c__DisplayClass")) continue;
log.LogDebug(
"{packageId}@{packageVersionNormalized}, framework {targetFramework}, entry {entryName}: adding {namespace}.{type}",
packageOperation.Id, packageOperation.Version, targetFramework, entryName, typeNamespace, typeName);
typeNames.Add($"{typeNamespace}.{typeName}");
}
}
}
log.LogInformation(
"Finished collecting assembly information from {entryName} for {packageId}@{packageVersionNormalized} and framework {targetFramework}.",
entryName, packageOperation.Id, packageOperation.Version, targetFramework);
}
log.LogInformation(
"Finished collecting information for {packageId}@{packageVersionNormalized} and framework {targetFramework}.",
packageOperation.Id, packageOperation.Version, targetFramework);
}
packageToIndex.TargetFrameworks = targetFrameworks.ToHashSet();
packageToIndex.TypeNames = typeNames.ToHashSet();
log.LogInformation("Finished analyzing package {packageId}@{packageVersionNormalized}.",
packageOperation.Id, packageOperation.Version);
// Add to index blob
try
{
log.LogInformation("Start storing index blob for package {packageId}@{packageVersionNormalized}...",
packageOperation.Id, packageOperation.Version);
await packageBlob.DeleteIfExistsAsync();
using (var jsonStream = await packageBlob.OpenWriteAsync())
using (var jsonWriter = new StreamWriter(jsonStream))
{
JsonSerializer.Serialize(jsonWriter, packagesToIndex);
}
log.LogInformation("Finished storing index blob for package {packageId}@{packageVersionNormalized}.",
packageOperation.Id, packageOperation.Version);
}
catch (Exception ex)
{
log.LogError(ex, "Error storing index blob for package {packageId}@{packageVersionNormalized}.",
packageOperation.Id, packageOperation.Version);
}
// Add to index
try
{
log.LogInformation("Start storing index document for package {packageId}@{packageVersionNormalized}...",
packageOperation.Id, packageOperation.Version);
await documentAddCollector.AddAsync(packageToIndex);
log.LogInformation("Finished storing index document for package {packageId}@{packageVersionNormalized}.",
packageOperation.Id, packageOperation.Version);
}
catch (Exception ex)
{
log.LogError(ex, "Error storing index document for package {packageId}@{packageVersionNormalized}.",
packageOperation.Id, packageOperation.Version);
}
}
}
}
private static async Task RunDeletePackageAsync(PackageOperation packageOperation, CloudBlockBlob packageBlob, IAsyncCollector<PackageDocument> documentDeleteCollector)
{
// Delete from index blob
await packageBlob.DeleteIfExistsAsync();
// Add delete from index actions
await documentDeleteCollector.AddAsync(new PackageDocument(packageOperation.Id, packageOperation.Version));
}
}
}