Skip to content

Commit ee4d01e

Browse files
meziantouCopilot
andcommitted
Replace NuGet.Protocol with HttpClient for package downloads
NuGet.Protocol added ~30 transitive dependencies, causing a ~1 minute restore+build overhead on CI. Use direct HttpClient calls to the NuGet flat container API instead — zero extra dependencies needed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5766608 commit ee4d01e

2 files changed

Lines changed: 7 additions & 22 deletions

File tree

Meziantou.Polyfill.Generator/Meziantou.Polyfill.Generator.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
<ItemGroup>
99
<PackageReference Include="Meziantou.Framework.FullPath" Version="1.1.17" />
1010
<PackageReference Include="Microsoft.CodeAnalysis" Version="5.3.0" />
11-
<PackageReference Include="NuGet.Protocol" Version="6.13.1" />
1211
</ItemGroup>
1312

1413
<ItemGroup>

Meziantou.Polyfill.Generator/Program.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
#pragma warning disable MA0047 // Declare types in namespaces
33
#pragma warning disable MA0048 // File name must match type name
44
using System.IO.Compression;
5+
using System.Net.Http;
56
using System.Reflection;
67
using System.Text.Json;
78
using Meziantou.Polyfill.Generator;
89
using Microsoft.CodeAnalysis.CSharp;
910
using Microsoft.CodeAnalysis;
1011
using Meziantou.Framework;
1112
using System.Text.RegularExpressions;
12-
using NuGet.Common;
13-
using NuGet.Protocol;
14-
using NuGet.Protocol.Core.Types;
15-
using NuGet.Versioning;
1613

1714
// Reference doesn't contains internal types which may be needed
1815
// So, use the runtime types to get all available types and methods
@@ -663,31 +660,20 @@ static async Task EnsurePackagesDownloadedAsync(string nugetPackagesPath, (strin
663660

664661
Console.WriteLine($"Downloading {packagesToDownload.Length} missing reference assembly package(s)...");
665662

666-
using var cacheContext = new SourceCacheContext();
667-
var repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json");
668-
var resource = await repository.GetResourceAsync<FindPackageByIdResource>();
669-
663+
using var httpClient = new HttpClient();
670664
foreach (var (packageId, version) in packagesToDownload)
671665
{
666+
var url = $"https://api.nuget.org/v3-flatcontainer/{packageId}/{version}/{packageId}.{version}.nupkg";
672667
Console.WriteLine($" Downloading {packageId}@{version}...");
673668

674-
using var packageStream = new MemoryStream();
675-
var success = await resource.CopyNupkgToStreamAsync(
676-
packageId,
677-
new NuGetVersion(version),
678-
packageStream,
679-
cacheContext,
680-
NullLogger.Instance,
681-
CancellationToken.None);
669+
using var response = await httpClient.GetAsync(url);
670+
response.EnsureSuccessStatusCode();
682671

683-
if (!success)
684-
throw new InvalidOperationException($"Failed to download NuGet package {packageId}@{version}");
672+
using var packageStream = await response.Content.ReadAsStreamAsync();
673+
using var archive = new ZipArchive(packageStream, ZipArchiveMode.Read);
685674

686675
var packageDir = Path.Combine(nugetPackagesPath, packageId, version);
687-
packageStream.Seek(0, SeekOrigin.Begin);
688-
689676
Directory.CreateDirectory(packageDir);
690-
using var archive = new ZipArchive(packageStream, ZipArchiveMode.Read);
691677
archive.ExtractToDirectory(packageDir);
692678

693679
Console.WriteLine($" Installed {packageId}@{version}");

0 commit comments

Comments
 (0)