Add depedency loader for NuGet package loading

develop
Matija Koželj 2 years ago
parent 7c2501b765
commit be3bc5c365

@ -0,0 +1,343 @@
using System.Reflection;
using System.Runtime.Versioning;
using Connected.Host.Configuration;
using Microsoft.Extensions.DependencyModel;
using NuGet.Configuration;
using NuGet.Frameworks;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Packaging.Signing;
using NuGet.Protocol.Core.Types;
using NuGet.Resolver;
using NuGet.Versioning;
namespace Connected.Host;
public class DependencyLoader
{
/// <summary>
/// Loads packages from the specified repositories into memory.
/// </summary>
/// <param name="packages">A list of package Id and version descriptors to load.</param>
/// <param name="repositoryLocations">A list of NuGet repositories to check.</param>
/// <param name="cancellationToken"></param>
/// <exception cref="InvalidOperationException">Thrown when a package identity could not be resolved.</exception>
private async Task Load(IEnumerable<MicroServiceDescriptor> packages, IEnumerable<string> repositoryLocations, CancellationToken? cancellationToken = null)
{
cancellationToken ??= CancellationToken.None;
//TODO: Wire up actual logger
var logger = new NuGet.Common.NullLogger();
/*
* Define a source provider with specified sources.
*/
var packageSources = GetPackageSources(repositoryLocations);
/*
* Define a package source provider with empty settings with the specified package sources.
*/
var packageSourceProvider = new PackageSourceProvider(Settings.LoadMachineWideSettings(Settings.DefaultSettingsFileName), packageSources);
/*
* Define a repository provider with our package source and the latest API capabilities.
*/
var repositoryProvider = new SourceRepositoryProvider(packageSourceProvider, Repository.Provider.GetCoreV3());
/*
* Setup contexts for dependency resolution and package downloading.
* First, the disposable cache
*/
using var sourceCacheContext = new SourceCacheContext();
/*
* Next, the compatible framework versions.
*/
var version = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(TargetFrameworkAttribute), false)
.SingleOrDefault() as TargetFrameworkAttribute;
var targetFramework = NuGetFramework.ParseFrameworkName(version.FrameworkName, DefaultFrameworkNameProvider.Instance);
/*
* Complete list of all touched packages, to be filled out by installer and downloader.
*/
var allPackages = new HashSet<SourcePackageDependencyInfo>();
/*
* Default dependency context.
*/
var dependencyContext = DependencyContext.Default;
/*
* Extract list of repositories.
*/
var repositories = repositoryProvider.GetRepositories();
/*
* Get all package identities and packages.
*/
foreach (var dependency in packages)
{
var packageIdentity = await GetPackageIdentity(dependency, sourceCacheContext, logger, repositories, cancellationToken.Value);
if (packageIdentity is null)
throw new InvalidOperationException($"Cannot find package {dependency.Name}.");
await GetPackageDependencies(packageIdentity, sourceCacheContext, targetFramework, logger, repositories, dependencyContext, allPackages, cancellationToken.Value);
}
var packagesToInstall = GetPackagesToInstall(repositoryProvider, logger, packages, allPackages);
/*
* Install packages in standard location.
*/
var packageDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, ".packages");
var nugetSettings = Settings.LoadDefaultSettings(packageDirectory);
await InstallPackages(sourceCacheContext, logger, packagesToInstall, packageDirectory, nugetSettings, cancellationToken.Value);
}
/// <summary>
/// Installs the list of packages and loads them into memory.
/// </summary>
/// <param name="sourceCacheContext">The shared context for resolved packages.</param>
/// <param name="logger">A standard NuGet logger.</param>
/// <param name="packagesToInstall">A list of packages to install.</param>
/// <param name="rootPackagesDirectory">The root directory into which the packages are downloaded and extracted.</param>
/// <param name="nugetSettings">A set of NuGet settings, containing local repos and download behavior settings.</param>
/// <param name="cancellationToken"></param>
private async Task InstallPackages(SourceCacheContext sourceCacheContext, NuGet.Common.ILogger logger,
IEnumerable<SourcePackageDependencyInfo> packagesToInstall, string rootPackagesDirectory,
ISettings nugetSettings, CancellationToken cancellationToken)
{
var packagePathResolver = new PackagePathResolver(rootPackagesDirectory, true);
var packageExtractionContext = new PackageExtractionContext(
PackageSaveMode.Defaultv3,
XmlDocFileSaveMode.Skip,
ClientPolicyContext.GetClientPolicy(nugetSettings, logger),
logger);
foreach (var package in packagesToInstall)
{
var downloadResource = await package.Source.GetResourceAsync<DownloadResource>(cancellationToken);
/*
* Download package (from online, local or shared cache).
*/
var downloadResult = await downloadResource.GetDownloadResourceResultAsync(
package,
new PackageDownloadContext(sourceCacheContext),
SettingsUtility.GetGlobalPackagesFolder(nugetSettings),
logger,
cancellationToken);
/*
* Extract package.
*/
var extractedFiles = await PackageExtractor.ExtractPackageAsync(
downloadResult.PackageSource,
downloadResult.PackageStream,
packagePathResolver,
packageExtractionContext,
cancellationToken);
/*
* Install applicable dlls.
*/
foreach (var assembly in extractedFiles.Where(e => e.EndsWith(".dll")))
{
try
{
Assembly.LoadFrom(assembly);
}
catch (Exception e)
{
logger.LogWarning($"Could not load assembly. {e}");
}
}
}
}
/// <summary>
/// Generates a list of package sources from a list of repository urls.
/// </summary>
/// <param name="sources">A list of URIs of specified sources.</param>
/// <returns>A list of package sources for the specified NuGet repository URIs.</returns>
private IEnumerable<PackageSource> GetPackageSources(IEnumerable<string> sources)
{
foreach (var source in sources)
yield return new PackageSource(source);
}
/// <summary>
/// Returns a list of missing packages to satisfy all dependencies.
/// </summary>
/// <param name="sourceRepositoryProvider">A source repository provider for all resources to load packages from.</param>
/// <param name="logger">A standard NuGet logger.</param>
/// <param name="packages">A list of packages for which to resolve all dependencies.</param>
/// <param name="availablePackages">A list of available packages to resolve from.</param>
/// <returns></returns>
private IEnumerable<SourcePackageDependencyInfo> GetPackagesToInstall(SourceRepositoryProvider sourceRepositoryProvider, NuGet.Common.ILogger logger, IEnumerable<MicroServiceDescriptor> packages, HashSet<SourcePackageDependencyInfo> availablePackages)
{
/*
* Create resolver to resolve missing packages
*/
var resolverContext = new PackageResolverContext(
DependencyBehavior.Lowest,
packages.Select(x => x.Name),
Enumerable.Empty<string>(),
Enumerable.Empty<PackageReference>(),
Enumerable.Empty<PackageIdentity>(),
availablePackages,
sourceRepositoryProvider.GetRepositories().Select(s => s.PackageSource),
logger);
var packagesToInstall = new PackageResolver()
.Resolve(resolverContext, CancellationToken.None)
.Select(p => availablePackages.Single(x => PackageIdentityComparer.Default.Equals(x, p)));
return packagesToInstall;
}
/// <summary>
/// Resolves a package identity from the supplied Id and version.
/// </summary>
/// <param name="microserviceDescriptor">The version and id of the required package.</param>
/// <param name="cache">The shared cache in which resolved packages are stored.</param>
/// <param name="nugetLogger">A standard NuGet logger.</param>
/// <param name="repositories">A list of repositories to check when resolving the package version.</param>
/// <param name="cancelToken"></param>
/// <returns>Returns the resolved package identity or null if it could not be resolved.</returns>
/// <exception cref="InvalidOperationException">Thrown when the version range could not be parsed.</exception>
private async Task<PackageIdentity?> GetPackageIdentity(MicroServiceDescriptor microserviceDescriptor, SourceCacheContext cache, NuGet.Common.ILogger nugetLogger, IEnumerable<SourceRepository> repositories, CancellationToken cancelToken)
{
/*
* Go through repositories in order.
*/
foreach (var sourceRepository in repositories)
{
/*
* Get resource, available versions and attempt to resolve the appropriate version. If none is available, take the last one.
*/
var findPackageResource = await sourceRepository.GetResourceAsync<FindPackageByIdResource>();
var availableVersions = await findPackageResource.GetAllVersionsAsync(microserviceDescriptor.Name, cache, nugetLogger, cancelToken);
NuGetVersion selected;
if (microserviceDescriptor.Version is not null)
{
if (!VersionRange.TryParse(microserviceDescriptor.Version, out var range))
throw new InvalidOperationException("Invalid version range provided.");
var bestVersion = range.FindBestMatch(availableVersions);
selected = bestVersion;
}
else
selected = availableVersions.LastOrDefault();
if (selected is not null)
return new PackageIdentity(microserviceDescriptor.Name, selected);
}
return null;
}
/// <summary>
/// Searches the package dependency graph for the chain of all packages to install.
/// </summary>
private async Task GetPackageDependencies(PackageIdentity package,
SourceCacheContext cacheContext,
NuGetFramework framework,
NuGet.Common.ILogger logger,
IEnumerable<SourceRepository> repositories,
DependencyContext hostDependencies,
ISet<SourcePackageDependencyInfo> availablePackages,
CancellationToken cancelToken)
{
/*
* Already present, skip.
*/
if (availablePackages.Contains(package))
return;
foreach (var sourceRepository in repositories)
{
/*
* Get dependency info.
*/
var dependencyInfoResource = await sourceRepository.GetResourceAsync<DependencyInfoResource>();
var dependencyInfo = await dependencyInfoResource.ResolvePackage(
package,
framework,
cacheContext,
logger,
cancelToken);
if (dependencyInfo is null)
continue;
/*
* Only return packages not provided by the host.
*/
var sourceDependencies = new SourcePackageDependencyInfo(
dependencyInfo.Id,
dependencyInfo.Version,
dependencyInfo.Dependencies.Where(dependency => !DependencySuppliedByHost(hostDependencies, dependency)),
dependencyInfo.Listed,
dependencyInfo.Source);
availablePackages.Add(sourceDependencies);
/*
* Get the dependency's packages.
*/
foreach (var dependency in sourceDependencies.Dependencies)
{
await GetPackageDependencies(
new PackageIdentity(dependency.Id, dependency.VersionRange.MinVersion),
cacheContext,
framework,
logger,
repositories,
hostDependencies,
availablePackages,
cancelToken);
}
break;
}
}
/// <summary>
/// Checks if a package dependency is already provided by the host.
/// </summary>
/// <param name="hostDependencies">The context packages.</param>
/// <param name="dependency">The dependency to check for.</param>
/// <returns>True if the host provides the dependency, false otherwise.</returns>
private bool DependencySuppliedByHost(DependencyContext hostDependencies, PackageDependency dependency)
{
/*
* Check runtimes for package with same id.
*/
var runtimeLib = hostDependencies.RuntimeLibraries.FirstOrDefault(r => r.Name == dependency.Id);
if (runtimeLib is null)
return false;
/*
* Check for version compatibility.
*/
var parsedLibVersion = NuGetVersion.Parse(runtimeLib.Version);
if (parsedLibVersion.IsPrerelease)
/*
* Latest and greatest. Always accept due to system compatiblity reasons.
*/
return true;
/*
* Check for version compatibility.
*/
return dependency.VersionRange.Satisfies(parsedLibVersion);
}
/// <summary>
/// Loads packages from the specified repositories into memory.
/// </summary>
/// <param name="packages">A list of package Id and version descriptors to load.</param>
/// <param name="repositories">A list of NuGet repositories to check.</param>
internal static async Task LoadPackages(IEnumerable<MicroServiceDescriptor> packages, IEnumerable<string> repositories)
{
await new DependencyLoader().Load(packages, repositories);
}
}
Loading…
Cancel
Save