-
Notifications
You must be signed in to change notification settings - Fork 884
/
NugetCommon.cs
456 lines (393 loc) · 21 KB
/
NugetCommon.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright © 2017 - 2021 Chocolatey Software, Inc
// Copyright © 2011 - 2017 RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace chocolatey.infrastructure.app.nuget
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using adapters;
using Alphaleonis.Win32.Filesystem;
using Chocolatey.NuGet.Frameworks;
using infrastructure.configuration;
using configuration;
using domain;
using filesystem;
using logging;
using NuGet;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Credentials;
using NuGet.PackageManagement;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.ProjectManagement;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using results;
using Console = adapters.Console;
using Environment = adapters.Environment;
using System.Collections.Concurrent;
public sealed class NugetCommon
{
private static readonly ConcurrentDictionary<string, SourceRepository> _repositories = new ConcurrentDictionary<string, SourceRepository>();
[Obsolete("This member is unused and should probably be removed.")]
private static Lazy<IConsole> _console = new Lazy<IConsole>(() => new Console());
#pragma warning disable IDE1006
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("This member is unused and should probably be removed.")]
public static void initialize_with(Lazy<IConsole> console)
{
_console = console;
}
#pragma warning restore IDE1006
[Obsolete("This member is unused and should probably be removed.")]
private static IConsole Console
{
get { return _console.Value; }
}
[Obsolete("This overload is obsolete and will be removed in a future version.")]
public static ChocolateyPackagePathResolver GetPathResolver(ChocolateyConfiguration configuration, IFileSystem nugetPackagesFileSystem)
=> GetPathResolver(nugetPackagesFileSystem);
public static ChocolateyPackagePathResolver GetPathResolver(IFileSystem nugetPackagesFileSystem)
{
return new ChocolateyPackagePathResolver(ApplicationParameters.PackagesLocation, nugetPackagesFileSystem);
}
public static void ClearRepositoriesCache()
{
_repositories.Clear();
}
public static SourceRepository GetLocalRepository()
{
var nugetSource = new PackageSource(ApplicationParameters.PackagesLocation);
return Repository.Factory.GetCoreV3(nugetSource);
}
public static IEnumerable<SourceRepository> GetRemoteRepositories(ChocolateyConfiguration configuration, ILogger nugetLogger, IFileSystem filesystem)
{
// Set user agent for all NuGet library calls. Should not affect any HTTP calls that Chocolatey itself would make.
UserAgent.SetUserAgentString(new UserAgentStringBuilder("{0}/{1} via NuGet Client".FormatWith(ApplicationParameters.UserAgent, configuration.Information.ChocolateyProductVersion)));
// ensure credentials can be grabbed from configuration
SetHttpHandlerCredentialService(configuration);
if (!string.IsNullOrWhiteSpace(configuration.Proxy.Location))
{
"chocolatey".Log().Debug("Using proxy server '{0}'.".FormatWith(configuration.Proxy.Location));
var proxy = new System.Net.WebProxy(configuration.Proxy.Location, true);
if (!String.IsNullOrWhiteSpace(configuration.Proxy.User) && !String.IsNullOrWhiteSpace(configuration.Proxy.EncryptedPassword))
{
proxy.Credentials = new NetworkCredential(configuration.Proxy.User, NugetEncryptionUtility.DecryptString(configuration.Proxy.EncryptedPassword));
}
if (!string.IsNullOrWhiteSpace(configuration.Proxy.BypassList))
{
"chocolatey".Log().Debug("Proxy has a bypass list of '{0}'.".FormatWith(configuration.Proxy.BypassList.EscapeCurlyBraces()));
proxy.BypassList = configuration.Proxy.BypassList.Replace("*",".*").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
proxy.BypassProxyOnLocal = configuration.Proxy.BypassOnLocal;
ProxyCache.Instance.Override(proxy);
}
else
{
// We need to override the proxy so that we don't use the NuGet configuration.
// We must however also be able to use the system proxy.
// Our changes to ProxyCache test for a null overridden proxy and get the system proxy if it's null.
ProxyCache.Instance.Override(proxy: null);
}
IEnumerable<string> sources = configuration.Sources.ToStringSafe().Split(new[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries);
IList<SourceRepository> repositories = new List<SourceRepository>();
var updatedSources = new StringBuilder();
foreach (var sourceValue in sources.OrEmpty())
{
var source = sourceValue;
var bypassProxy = false;
var sourceClientCertificates = new List<X509Certificate>();
if (!string.IsNullOrWhiteSpace(configuration.SourceCommand.Certificate))
{
"chocolatey".Log().Debug("Using passed in certificate for source {0}".FormatWith(source));
sourceClientCertificates.Add(new X509Certificate2(configuration.SourceCommand.Certificate, configuration.SourceCommand.CertificatePassword));
}
if (configuration.MachineSources.Any(m => m.Name.IsEqualTo(source) || m.Key.IsEqualTo(source)))
{
try
{
var machineSource = configuration.MachineSources.FirstOrDefault(m => m.Key.IsEqualTo(source));
if (machineSource == null)
{
machineSource = configuration.MachineSources.FirstOrDefault(m => m.Name.IsEqualTo(source));
"chocolatey".Log().Debug("Switching source name {0} to actual source value '{1}'.".FormatWith(sourceValue, machineSource.Key.ToStringSafe()));
source = machineSource.Key;
}
if (machineSource != null)
{
bypassProxy = machineSource.BypassProxy;
if (bypassProxy) "chocolatey".Log().Debug("Source '{0}' is configured to bypass proxies.".FormatWith(source));
if (!string.IsNullOrWhiteSpace(machineSource.Certificate))
{
"chocolatey".Log().Debug("Using configured certificate for source {0}".FormatWith(source));
sourceClientCertificates.Add(new X509Certificate2(machineSource.Certificate, NugetEncryptionUtility.DecryptString(machineSource.EncryptedCertificatePassword)));
}
}
}
catch (Exception ex)
{
"chocolatey".Log().Warn("Attempted to use a source name {0} to get default source but failed:{1} {2}".FormatWith(sourceValue, System.Environment.NewLine, ex.Message));
}
}
if (_repositories.ContainsKey(source))
{
repositories.Add(_repositories[source]);
}
else
{
var nugetSource = new PackageSource(source);
// If not parsed as a http(s) or local source, let's try resolving the path
// Since NuGet.Client is not able to parse all relative paths
// Conversion to absolute paths is handled by clients, not by the libraries as per
// https://github.com/NuGet/NuGet.Client/pull/3783
if (nugetSource.TrySourceAsUri is null)
{
string fullsource;
try
{
fullsource = filesystem.GetFullPath(source);
}
catch
{
// If an invalid source was passed in, we don't care here, pass it along
fullsource = source;
}
nugetSource = new PackageSource(fullsource);
if (!nugetSource.IsLocal)
{
throw new ApplicationException("Source '{0}' is unable to be parsed".FormatWith(source));
}
"chocolatey".Log().Debug("Updating Source path from {0} to {1}".FormatWith(source, fullsource));
updatedSources.AppendFormat("{0};", fullsource);
}
else
{
updatedSources.AppendFormat("{0};", source);
}
nugetSource.ClientCertificates = sourceClientCertificates;
var repo = Repository.Factory.GetCoreV3(nugetSource);
if (nugetSource.IsHttp || nugetSource.IsHttps)
{
#pragma warning disable RS0030 // Do not used banned APIs
var httpSourceResource = repo.GetResource<HttpSourceResource>();
#pragma warning restore RS0030 // Do not used banned APIs
if (httpSourceResource != null)
{
httpSourceResource.HttpSource.HttpCacheDirectory = ApplicationParameters.HttpCacheLocation;
}
}
_repositories.TryAdd(source, repo);
repositories.Add(repo);
}
}
if (updatedSources.Length != 0)
{
configuration.Sources = updatedSources.Remove(updatedSources.Length - 1, 1).ToStringSafe();
}
return repositories;
}
[Obsolete("This overload is deprecated and will be removed in v3.")]
public static IReadOnlyList<NuGetEndpointResources> GetRepositoryResources(ChocolateyConfiguration configuration, ILogger nugetLogger, IFileSystem filesystem)
{
return GetRepositoryResources(configuration, nugetLogger, filesystem, new ChocolateySourceCacheContext(configuration));
}
public static IReadOnlyList<NuGetEndpointResources> GetRepositoryResources(ChocolateyConfiguration configuration, ILogger nugetLogger, IFileSystem filesystem, ChocolateySourceCacheContext cacheContext)
{
IEnumerable<SourceRepository> remoteRepositories = GetRemoteRepositories(configuration, nugetLogger, filesystem);
return GetRepositoryResources(remoteRepositories, cacheContext);
}
[Obsolete("This overload is deprecated and will be removed in v3.")]
public static IReadOnlyList<NuGetEndpointResources> GetRepositoryResources(IEnumerable<SourceRepository> packageRepositories)
{
return GetRepositoryResources(packageRepositories, cacheContext: null);
}
public static IReadOnlyList<NuGetEndpointResources> GetRepositoryResources(IEnumerable<SourceRepository> packageRepositories, ChocolateySourceCacheContext cacheContext)
{
return NuGetEndpointResources.GetResourcesBySource(packageRepositories, cacheContext).ToList();
}
public static void SetHttpHandlerCredentialService(ChocolateyConfiguration configuration)
{
HttpHandlerResourceV3.CredentialService = new Lazy<ICredentialService>(
() => new CredentialService(
new AsyncLazy<IEnumerable<ICredentialProvider>>(
() => GetCredentialProvidersAsync(configuration)), false, true));
}
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously.
// We don't care about this method being synchronous because this is just used to pass in the credential provider to Lazy<ICredentialService>
private static async Task<IEnumerable<ICredentialProvider>> GetCredentialProvidersAsync(ChocolateyConfiguration configuration)
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
{
return new List<ICredentialProvider>() { new ChocolateyNugetCredentialProvider(configuration) };
}
public static void GetLocalPackageDependencies(PackageIdentity package,
NuGetFramework framework,
IEnumerable<PackageResult> allLocalPackages,
ISet<SourcePackageDependencyInfo> dependencyInfos
)
{
if (dependencyInfos.Contains(package)) return;
var metadata = allLocalPackages
.FirstOrDefault(p => p.PackageMetadata.Id.Equals(package.Id, StringComparison.OrdinalIgnoreCase) && p.PackageMetadata.Version.Equals(package.Version))
.PackageMetadata;
var group = NuGetFrameworkUtility.GetNearest<PackageDependencyGroup>(metadata.DependencyGroups, framework);
var dependencies = group?.Packages ?? Enumerable.Empty<PackageDependency>();
var result = new SourcePackageDependencyInfo(
package,
dependencies,
true,
null,
null,
null);
dependencyInfos.Add(result);
foreach (var dependency in dependencies)
{
GetLocalPackageDependencies(dependency.Id, dependency.VersionRange, framework, allLocalPackages, dependencyInfos);
}
}
public static void GetLocalPackageDependencies(string packageId,
VersionRange versionRange,
NuGetFramework framework,
IEnumerable<PackageResult> allLocalPackages,
ISet<SourcePackageDependencyInfo> dependencyInfos
)
{
var versionsMetadata = allLocalPackages
.Where(p => p.PackageMetadata.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase) && versionRange.Satisfies(p.PackageMetadata.Version))
.Select(p => p.PackageMetadata);
foreach (var metadata in versionsMetadata)
{
var group = NuGetFrameworkUtility.GetNearest<PackageDependencyGroup>(metadata.DependencyGroups, framework);
var dependencies = group?.Packages ?? Enumerable.Empty<PackageDependency>();
var result = new SourcePackageDependencyInfo(
metadata.Id,
metadata.Version,
dependencies,
true,
null,
null,
null);
if (dependencyInfos.Contains(result)) return;
dependencyInfos.Add(result);
foreach (var dependency in dependencies)
{
GetLocalPackageDependencies(dependency.Id, dependency.VersionRange, framework, allLocalPackages, dependencyInfos);
}
}
}
public static async Task GetPackageDependencies(PackageIdentity package,
NuGetFramework framework,
SourceCacheContext cacheContext,
ILogger logger,
IEnumerable<NuGetEndpointResources> resources,
ISet<SourcePackageDependencyInfo> availablePackages,
ISet<PackageDependency> dependencyCache,
ChocolateyConfiguration configuration)
{
if (availablePackages.Contains(package)) return;
var dependencyInfoResources = resources.DependencyInfoResources();
foreach (var dependencyInfoResource in dependencyInfoResources)
{
SourcePackageDependencyInfo dependencyInfo = null;
try
{
dependencyInfo = await dependencyInfoResource.ResolvePackage(
package, framework, cacheContext, logger, CancellationToken.None);
}
catch (AggregateException ex) when (!(ex.InnerException is null))
{
"chocolatey".Log().Warn(ex.InnerException.Message);
}
catch (Exception ex)
{
"chocolatey".Log().Warn(ex.InnerException.Message);
}
if (dependencyInfo == null) continue;
availablePackages.Add(dependencyInfo);
foreach (var dependency in dependencyInfo.Dependencies)
{
if (dependencyCache.Contains(dependency)) continue;
dependencyCache.Add(dependency);
await GetPackageDependencies(
dependency.Id, framework, cacheContext, logger, resources, availablePackages, dependencyCache, configuration);
}
}
}
public static async Task GetPackageDependencies(string packageId,
NuGetFramework framework,
SourceCacheContext cacheContext,
ILogger logger,
IEnumerable<NuGetEndpointResources> resources,
ISet<SourcePackageDependencyInfo> availablePackages,
ISet<PackageDependency> dependencyCache,
ChocolateyConfiguration configuration)
{
//if (availablePackages.Contains(packageID)) return;
var dependencyInfoResources = resources.DependencyInfoResources();
foreach (var dependencyInfoResource in dependencyInfoResources)
{
IEnumerable<SourcePackageDependencyInfo> dependencyInfos = Array.Empty<SourcePackageDependencyInfo>();
try
{
dependencyInfos = await dependencyInfoResource.ResolvePackages(
packageId, configuration.Prerelease, framework, cacheContext, logger, CancellationToken.None);
}
catch (AggregateException ex) when (!(ex.InnerException is null))
{
"chocolatey".Log().Warn(ex.InnerException.Message);
}
catch (Exception ex)
{
"chocolatey".Log().Warn(ex.InnerException.Message);
}
if (!dependencyInfos.Any()) continue;
availablePackages.AddRange(dependencyInfos);
foreach (var dependency in dependencyInfos.SelectMany(p => p.Dependencies))
{
if (dependencyCache.Contains(dependency)) continue;
dependencyCache.Add(dependency);
// Recursion is fun, kids
await GetPackageDependencies(
dependency.Id, framework, cacheContext, logger, resources, availablePackages, dependencyCache, configuration);
}
}
}
public static async Task GetPackageParents(string packageId,
ISet<SourcePackageDependencyInfo> parentPackages,
IEnumerable<SourcePackageDependencyInfo> locallyInstalledPackages)
{
foreach (var package in locallyInstalledPackages.Where(p => !parentPackages.Contains(p)))
{
if (parentPackages.Contains(package)) continue;
if (package.Dependencies.Any(p => p.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase)))
{
parentPackages.Add(package);
await GetPackageParents(package.Id, parentPackages, locallyInstalledPackages);
}
}
}
}
}