Skip to content

Instantly share code, notes, and snippets.

@bjorkstromm
Last active March 4, 2024 23:36
Show Gist options
  • Save bjorkstromm/ad5776c36559410f45d5dcd0181a5c64 to your computer and use it in GitHub Desktop.
Save bjorkstromm/ad5776c36559410f45d5dcd0181a5c64 to your computer and use it in GitHub Desktop.
using NuGet.Common;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Packaging.Signing;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Resolver;
using NuGet.Versioning;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp5
{
class Program
{
static async Task Main(string[] args)
{
var packageId = "cake.nuget";
var packageVersion = NuGetVersion.Parse("0.30.0");
var nuGetFramework = NuGetFramework.ParseFolder("net46");
var settings = Settings.LoadDefaultSettings(root: null);
var sourceRepositoryProvider = new SourceRepositoryProvider(settings, Repository.Provider.GetCoreV3());
using (var cacheContext = new SourceCacheContext())
{
var repositories = sourceRepositoryProvider.GetRepositories();
var availablePackages = new HashSet<SourcePackageDependencyInfo>(PackageIdentityComparer.Default);
await GetPackageDependencies(
new PackageIdentity(packageId, packageVersion),
nuGetFramework, cacheContext, NullLogger.Instance, repositories, availablePackages);
var resolverContext = new PackageResolverContext(
DependencyBehavior.Lowest,
new[] { packageId },
Enumerable.Empty<string>(),
Enumerable.Empty<PackageReference>(),
Enumerable.Empty<PackageIdentity>(),
availablePackages,
sourceRepositoryProvider.GetRepositories().Select(s => s.PackageSource),
NullLogger.Instance);
var resolver = new PackageResolver();
var packagesToInstall = resolver.Resolve(resolverContext, CancellationToken.None)
.Select(p => availablePackages.Single(x => PackageIdentityComparer.Default.Equals(x, p)));
var packagePathResolver = new PackagePathResolver(Path.GetFullPath("packages"));
var packageExtractionContext = new PackageExtractionContext(
PackageSaveMode.Defaultv3,
XmlDocFileSaveMode.None,
NullLogger.Instance,
new PackageSignatureVerifier(
SignatureVerificationProviderFactory.GetSignatureVerificationProviders()),
SignedPackageVerifierSettings.GetDefault());
var frameworkReducer = new FrameworkReducer();
foreach (var packageToInstall in packagesToInstall)
{
PackageReaderBase packageReader;
var installedPath = packagePathResolver.GetInstalledPath(packageToInstall);
if (installedPath == null)
{
var downloadResource = await packageToInstall.Source.GetResourceAsync<DownloadResource>(CancellationToken.None);
var downloadResult = await downloadResource.GetDownloadResourceResultAsync(
packageToInstall,
new PackageDownloadContext(cacheContext),
SettingsUtility.GetGlobalPackagesFolder(settings),
NullLogger.Instance, CancellationToken.None);
await PackageExtractor.ExtractPackageAsync(
downloadResult.PackageSource,
downloadResult.PackageStream,
packagePathResolver,
packageExtractionContext,
CancellationToken.None);
packageReader = downloadResult.PackageReader;
}
else
{
packageReader = new PackageFolderReader(installedPath);
}
var libItems = packageReader.GetLibItems();
var nearest = frameworkReducer.GetNearest(nuGetFramework, libItems.Select(x => x.TargetFramework));
Console.WriteLine(string.Join("\n", libItems
.Where(x => x.TargetFramework.Equals(nearest))
.SelectMany(x => x.Items)));
var frameworkItems = packageReader.GetFrameworkItems();
nearest = frameworkReducer.GetNearest(nuGetFramework, frameworkItems.Select(x => x.TargetFramework));
Console.WriteLine(string.Join("\n", frameworkItems
.Where(x => x.TargetFramework.Equals(nearest))
.SelectMany(x => x.Items)));
}
}
async Task GetPackageDependencies(PackageIdentity package,
NuGetFramework framework,
SourceCacheContext cacheContext,
ILogger logger,
IEnumerable<SourceRepository> repositories,
ISet<SourcePackageDependencyInfo> availablePackages)
{
if (availablePackages.Contains(package)) return;
foreach (var sourceRepository in repositories)
{
var dependencyInfoResource = await sourceRepository.GetResourceAsync<DependencyInfoResource>();
var dependencyInfo = await dependencyInfoResource.ResolvePackage(
package, framework, cacheContext, logger, CancellationToken.None);
if (dependencyInfo == null) continue;
availablePackages.Add(dependencyInfo);
foreach (var dependency in dependencyInfo.Dependencies)
{
await GetPackageDependencies(
new PackageIdentity(dependency.Id, dependency.VersionRange.MinVersion),
framework, cacheContext, logger, repositories, availablePackages);
}
}
}
}
}
}
@jasells
Copy link

jasells commented Mar 4, 2024

I had to update/change this line replacing PackageSignatureVerifier to ClientPolicyContext:

var cliCtx = ClientPolicyContext.GetClientPolicy(NullSettings.Instance, NullLogger.Instance);
var packageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv3,
                                            XmlDocFileSaveMode.None,
                                            cliCtx,
                                            NullLogger.Instance);

I presume the version of the packages I am using (6.9.1) is newer than when you wrote this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment