Created
March 16, 2021 04:32
-
-
Save mattleibow/bcdde0e4d1ff203d75155a90d2d09f53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NuGet.Common; | |
using NuGet.Packaging; | |
using NuGet.Packaging.Core; | |
using NuGet.Protocol; | |
using NuGet.Protocol.Core.Types; | |
using System; | |
using System.Collections.Concurrent; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ConsoleApp1 | |
{ | |
public class Program | |
{ | |
public static async Task Main() | |
{ | |
if (!Directory.Exists("downloads")) | |
Directory.CreateDirectory("downloads"); | |
var source = Repository.Factory.GetCoreV3("https://aka.ms/skiasharp-eap/index.json"); | |
var cache = new SourceCacheContext(); | |
var logger = NullLogger.Instance; | |
var listResource = await source.GetResourceAsync<PackageSearchResource>(); | |
var downloadResource = await source.GetResourceAsync<DownloadResource>(); | |
var packages = (await listResource.SearchAsync("", new SearchFilter(true, SearchFilterType.IsLatestVersion), 0, 100, logger, default)).ToArray(); | |
var tasks = new ConcurrentBag<Task>(); | |
var progress = 0; | |
foreach (var package in packages) | |
{ | |
if (package.Identity.Id.StartsWith("_")) | |
continue; | |
var versions = (await package.GetVersionsAsync()).ToArray(); | |
for (int verIdx = 0; verIdx < versions.Length; verIdx++) | |
{ | |
var version = versions[verIdx]; | |
Console.WriteLine($"START {package.Identity.Id}: {version.Version}"); | |
var outputPath = $"downloads\\{package.Identity.Id}.{version.Version}.nupkg"; | |
if (File.Exists(outputPath)) | |
{ | |
using var reader = new PackageArchiveReader(outputPath); | |
try | |
{ | |
await reader.ValidatePackageEntriesAsync(default); | |
continue; | |
} | |
catch | |
{ | |
} | |
} | |
var d = Task.Run(async () => | |
{ | |
using var result = await downloadResource.GetDownloadResourceResultAsync( | |
new PackageIdentity(package.Identity.Id, version.Version), | |
new PackageDownloadContext(cache, "externals", true), | |
null, | |
logger, | |
default); | |
using var file = File.Create(outputPath); | |
await result.PackageStream.CopyToAsync(file); | |
var percent = Interlocked.Increment(ref progress) / (double)tasks.Count; | |
Console.WriteLine($"END {package.Identity.Id}: {version.Version} ({percent:P})"); | |
}); | |
tasks.Add(d); | |
} | |
} | |
await Task.WhenAll(tasks); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment