-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathNuGetDownloader.cs
More file actions
52 lines (39 loc) · 2.06 KB
/
Copy pathNuGetDownloader.cs
File metadata and controls
52 lines (39 loc) · 2.06 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
using System.IO.Compression;
using Microsoft.CodeAnalysis;
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
namespace TUnit.Core.SourceGenerator.Tests;
public static class NuGetDownloader
{
private static SourceCacheContext CacheContext = new();
private static ILogger Logger = NullLogger.Instance;
private static string OutputPath = Path.Combine(Path.GetTempPath(), "TUnit.Core.SourceGenerator.Tests", "NuGetPackages");
public static async Task<IEnumerable<MetadataReference>> DownloadPackageAsync(string packageId, string version)
{
var extractedPath = Path.Combine(OutputPath, $"{packageId}.{version}");
if (!Directory.Exists(extractedPath))
{
var settings = NuGet.Configuration.Settings.LoadDefaultSettings(null);
var sourceRepositoryProvider = new SourceRepositoryProvider(new PackageSourceProvider(settings), Repository.Provider.GetCoreV3());
var repository = sourceRepositoryProvider.CreateRepository(new PackageSource("https://api.nuget.org/v3/index.json"));
var resource = await repository.GetResourceAsync<FindPackageByIdResource>();
Directory.CreateDirectory(OutputPath);
var packagePath = Path.Combine(OutputPath, $"{packageId}.{version}.nupkg");
using (var packageStream = File.Create(packagePath))
{
await resource.CopyNupkgToStreamAsync(packageId, NuGetVersion.Parse(version), packageStream, CacheContext, Logger, CancellationToken.None);
}
Directory.CreateDirectory(extractedPath);
using (var zip = new ZipArchive(File.OpenRead(packagePath), ZipArchiveMode.Read))
{
zip.ExtractToDirectory(extractedPath);
}
}
var files = Directory.EnumerateFiles(extractedPath, "*.dll", SearchOption.AllDirectories);
return files
.Where(f => f.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
.Select(x => MetadataReference.CreateFromFile(x));
}
}