Compare commits
No commits in common. "28b9c4c79eabfd1994037cf37d4f6c163388b585" and "7c2501b76529aed324a0f496f8a5445cd9817ae3" have entirely different histories.
28b9c4c79e
...
7c2501b765
@ -5,6 +5,26 @@ VisualStudioVersion = 17.3.32901.215
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Host", "src\Connected.Host\Connected.Host.csproj", "{A2A01D43-0CEE-4E49-8EC1-F16CA92F3C30}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependencies", "Dependencies", "{D875F828-05A0-4A52-9A36-6F5B0370171D}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Data", "..\Connected.Framework\src\Connected.Data\Connected.Data.csproj", "{67532F27-3F13-404D-AD65-34317719059D}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Types", "..\Connected.Common.Types\src\Connected.Common.Types\Connected.Common.Types.csproj", "{216E4481-92B6-4E52-8877-3C6D547803ED}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Rest", "..\Connected.Framework\src\Connected.Rest\Connected.Rest.csproj", "{AD931DF4-9695-4D9A-8E08-8A5041175755}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Entities", "..\Connected.Framework\src\Connected.Entities\Connected.Entities.csproj", "{DB941857-07B7-4CD0-B43F-32EC51B5F195}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Expressions", "..\Connected.Framework\src\Connected.Expressions\Connected.Expressions.csproj", "{6FCD7B5E-A42C-433A-8C2E-5BA412736F99}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Interop", "..\Connected.Framework\src\Connected.Interop\Connected.Interop.csproj", "{84D0EAE0-724F-421A-BDA2-5DEC2D9A254C}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.ServiceModel.Client.Data", "..\Connected.Framework.ServiceModel.Client\src\Connected.ServiceModel.Client.Data\Connected.ServiceModel.Client.Data.csproj", "{EBBECB6F-C996-41C4-B05A-39309A03839E}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connected.Instance", "..\Connected.Framework\src\Connected.Instance\Connected.Instance.csproj", "{5312A990-8797-44E2-9BA9-858D51DE3825}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Notes", "..\Connected.Common\src\Connected.Common.Notes\Connected.Common.Notes.csproj", "{28F99091-B4CB-43BF-A6DF-4D47621420D0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -15,10 +35,57 @@ Global
|
||||
{A2A01D43-0CEE-4E49-8EC1-F16CA92F3C30}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A2A01D43-0CEE-4E49-8EC1-F16CA92F3C30}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A2A01D43-0CEE-4E49-8EC1-F16CA92F3C30}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{67532F27-3F13-404D-AD65-34317719059D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{67532F27-3F13-404D-AD65-34317719059D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{67532F27-3F13-404D-AD65-34317719059D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{67532F27-3F13-404D-AD65-34317719059D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{216E4481-92B6-4E52-8877-3C6D547803ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{216E4481-92B6-4E52-8877-3C6D547803ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{216E4481-92B6-4E52-8877-3C6D547803ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{216E4481-92B6-4E52-8877-3C6D547803ED}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{AD931DF4-9695-4D9A-8E08-8A5041175755}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{AD931DF4-9695-4D9A-8E08-8A5041175755}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{AD931DF4-9695-4D9A-8E08-8A5041175755}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{AD931DF4-9695-4D9A-8E08-8A5041175755}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{DB941857-07B7-4CD0-B43F-32EC51B5F195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{DB941857-07B7-4CD0-B43F-32EC51B5F195}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{DB941857-07B7-4CD0-B43F-32EC51B5F195}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{DB941857-07B7-4CD0-B43F-32EC51B5F195}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6FCD7B5E-A42C-433A-8C2E-5BA412736F99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6FCD7B5E-A42C-433A-8C2E-5BA412736F99}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6FCD7B5E-A42C-433A-8C2E-5BA412736F99}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6FCD7B5E-A42C-433A-8C2E-5BA412736F99}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{84D0EAE0-724F-421A-BDA2-5DEC2D9A254C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{84D0EAE0-724F-421A-BDA2-5DEC2D9A254C}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{84D0EAE0-724F-421A-BDA2-5DEC2D9A254C}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{84D0EAE0-724F-421A-BDA2-5DEC2D9A254C}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{EBBECB6F-C996-41C4-B05A-39309A03839E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{EBBECB6F-C996-41C4-B05A-39309A03839E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{EBBECB6F-C996-41C4-B05A-39309A03839E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{EBBECB6F-C996-41C4-B05A-39309A03839E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5312A990-8797-44E2-9BA9-858D51DE3825}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5312A990-8797-44E2-9BA9-858D51DE3825}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5312A990-8797-44E2-9BA9-858D51DE3825}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5312A990-8797-44E2-9BA9-858D51DE3825}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{28F99091-B4CB-43BF-A6DF-4D47621420D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{28F99091-B4CB-43BF-A6DF-4D47621420D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{28F99091-B4CB-43BF-A6DF-4D47621420D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{28F99091-B4CB-43BF-A6DF-4D47621420D0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{67532F27-3F13-404D-AD65-34317719059D} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{216E4481-92B6-4E52-8877-3C6D547803ED} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{AD931DF4-9695-4D9A-8E08-8A5041175755} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{DB941857-07B7-4CD0-B43F-32EC51B5F195} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{6FCD7B5E-A42C-433A-8C2E-5BA412736F99} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{84D0EAE0-724F-421A-BDA2-5DEC2D9A254C} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{EBBECB6F-C996-41C4-B05A-39309A03839E} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{5312A990-8797-44E2-9BA9-858D51DE3825} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
{28F99091-B4CB-43BF-A6DF-4D47621420D0} = {D875F828-05A0-4A52-9A36-6F5B0370171D}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {8E8D9F6F-68E1-466C-8D83-5ECB3AA3FC76}
|
||||
EndGlobalSection
|
||||
|
@ -1,14 +1,49 @@
|
||||
namespace Connected.Host.Configuration;
|
||||
|
||||
internal class SysConfiguration
|
||||
namespace Connected.Host.Configuration
|
||||
{
|
||||
private const string DefaultStart = "Connected.Instance.Start, Connected.Instance";
|
||||
internal static class SysConfiguration
|
||||
{
|
||||
private const string DefaultStart = "Connected.Instance.Start, Connected.Instance";
|
||||
|
||||
private string _start = DefaultStart;
|
||||
private static List<MicroServiceDescriptor> _microServices;
|
||||
private static List<string> _locations;
|
||||
private static string _start;
|
||||
public static List<MicroServiceDescriptor> MicroServices => _microServices;
|
||||
public static List<string> Locations => _locations;
|
||||
public static string Start => _start;
|
||||
public static void Build()
|
||||
{
|
||||
var cb = new ConfigurationBuilder();
|
||||
var appPath = AppContext.BaseDirectory;
|
||||
var segments = new Uri(appPath).Segments;
|
||||
|
||||
public List<MicroServiceDescriptor> MicroServices { get; set; } = new();
|
||||
segments = segments.Select(e => e.Replace("%20", " ")).ToArray();
|
||||
|
||||
public List<string> Repositories { get; set; } = new();
|
||||
for (var i = 1; i <= segments.Length; i++)
|
||||
{
|
||||
var pathBase = Path.Combine(segments[0..i]);
|
||||
var filePath = Path.Combine(pathBase, "sys.json");
|
||||
|
||||
public string Start { get => _start ?? DefaultStart; set => _start = value; }
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
cb.AddJsonFile(filePath, true, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cb.AddEnvironmentVariables();
|
||||
|
||||
var config = cb.Build();
|
||||
|
||||
_microServices = new List<MicroServiceDescriptor>();
|
||||
_locations = new List<string>();
|
||||
|
||||
config.Bind("microServices", _microServices);
|
||||
config.Bind("locations", _locations);
|
||||
|
||||
_start = config["start"];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_start))
|
||||
_start = DefaultStart;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
|
||||
<PackageReference Include="NuGet.PackageManagement" Version="6.4.0" />
|
||||
<PackageReference Include="NuGet.Protocol" Version="6.4.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.SqlServer.Management.SqlParser" Version="160.22515.0" />
|
||||
|
@ -1,32 +0,0 @@
|
||||
using System.Reflection;
|
||||
using Connected.Host.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Connected.Host;
|
||||
|
||||
internal sealed class ConnectedPlatformService : BackgroundService
|
||||
{
|
||||
private SysConfiguration Config { get; }
|
||||
private IConfiguration GlobalConfig { get; }
|
||||
public ConnectedPlatformService(IOptions<SysConfiguration> config, IConfiguration globalConfig)
|
||||
{
|
||||
Config = config.Value;
|
||||
GlobalConfig = globalConfig;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await DependencyLoader.LoadPackages(Config.MicroServices, Config.Repositories);
|
||||
|
||||
if (Type.GetType(Config.Start) is not Type startupType)
|
||||
throw new Exception($"Cannot resolve startup type. ({Config.Start}).");
|
||||
|
||||
if (startupType.GetMethod("ConfigureAsync", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) is not MethodInfo configureMethod)
|
||||
throw new Exception($"ConfigureAsync does not exists in the startup type {startupType}. Startup must have static 'async Task ConfigureAsync(params string[] args)' method.");
|
||||
|
||||
if (configureMethod.Invoke(null, new object[] { GlobalConfig }) is not Task task)
|
||||
throw new Exception($"ConfigureAsync of type {startupType} must return Task.");
|
||||
|
||||
await task;
|
||||
}
|
||||
}
|
@ -1,343 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,42 +1,67 @@
|
||||
using System.Reflection;
|
||||
using Connected.Host.Configuration;
|
||||
|
||||
namespace Connected.Host
|
||||
{
|
||||
public static partial class Program
|
||||
public static class Program
|
||||
{
|
||||
public static async Task Main(params string[] args)
|
||||
public static void Main(params string[] args)
|
||||
{
|
||||
using IHost host = Microsoft.Extensions.Hosting.Host
|
||||
.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration((_, cfg) =>
|
||||
{
|
||||
var appPath = AppContext.BaseDirectory;
|
||||
var segments = new Uri(appPath).Segments;
|
||||
SysConfiguration.Build();
|
||||
|
||||
segments = segments.Select(e => e.Replace("%20", " ")).ToArray();
|
||||
|
||||
for (var i = 1; i <= segments.Length; i++)
|
||||
{
|
||||
var pathBase = Path.Combine(segments[0..i]);
|
||||
var filePath = Path.Combine(pathBase, "sys.json");
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
cfg.AddJsonFile(filePath, true, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cfg.AddEnvironmentVariables();
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services.Configure<SysConfiguration>(context.Configuration);
|
||||
services.AddHostedService<ConnectedPlatformService>();
|
||||
})
|
||||
.Build();
|
||||
|
||||
await host.RunAsync();
|
||||
Start(ParseArguments(args));
|
||||
}
|
||||
|
||||
private static void Start(Dictionary<string, string> args)
|
||||
{
|
||||
foreach (var microService in SysConfiguration.MicroServices)
|
||||
LoadMicroServiceAssembly(microService);
|
||||
|
||||
if (Type.GetType(SysConfiguration.Start) is not Type startupType)
|
||||
throw new Exception($"Cannot resolve startup type. ({SysConfiguration.Start}).");
|
||||
|
||||
if (startupType.GetMethod("ConfigureAsync", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) is not MethodInfo configureMethod)
|
||||
throw new Exception($"ConfigureAsync does not exists in the startup type {startupType}. Startup must have static 'async Task ConfigureAsync(params string[] args)' method.");
|
||||
|
||||
if (configureMethod.Invoke(null, new object[] { args }) is not Task task)
|
||||
throw new Exception($"ConfigureAsync of type {startupType} must return Task.");
|
||||
|
||||
task.ConfigureAwait(false);
|
||||
|
||||
task.GetType().GetProperty("Result")?.GetValue(task);
|
||||
}
|
||||
|
||||
private static Assembly? LoadMicroServiceAssembly(MicroServiceDescriptor descriptor)
|
||||
{
|
||||
var assemblyFile = descriptor.Name.EndsWith(".dll") ? descriptor.Name : $"{descriptor.Name}.dll";
|
||||
|
||||
foreach (var location in SysConfiguration.Locations)
|
||||
{
|
||||
var assemblyPath = Path.Combine(location, assemblyFile);
|
||||
|
||||
if (File.Exists(assemblyPath))
|
||||
return Assembly.LoadFrom(assemblyPath);
|
||||
}
|
||||
|
||||
return Assembly.Load(AssemblyName.GetAssemblyName(assemblyFile));
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> ParseArguments(params string[] args)
|
||||
{
|
||||
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
foreach (var i in args)
|
||||
{
|
||||
var tokens = i.Split('=', 2);
|
||||
|
||||
if (tokens.Length == 1)
|
||||
result.Add(tokens[0].Trim(), string.Empty);
|
||||
else
|
||||
result.Add(tokens[0].Trim(), tokens[1].Trim());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,29 +1,153 @@
|
||||
{
|
||||
"repositories": [
|
||||
"c:/nuget/packages",
|
||||
"https://api.nuget.org/v3/index.json"
|
||||
],
|
||||
"microServices": [
|
||||
{
|
||||
"name": "Connected.Common.Types",
|
||||
"version": "1.0.*-*"
|
||||
"name": "Connected",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common.Notes",
|
||||
"version": "1.0.*-*"
|
||||
"name": "Connected.Caching",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.ServiceModel.Client.Data",
|
||||
"version": "1.0.*-*"
|
||||
"name": "Connected.Collections",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.ServiceModel.Client",
|
||||
"version": "1.0.*-*"
|
||||
"name": "Connected.Configuration",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Data",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Entities",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Globalization",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Hosting",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Interop",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Middleware",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Net",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Notifications",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Rest",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Runtime",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Security",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.ServiceModel",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Services",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Threading",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Validation",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Instance",
|
||||
"version": "1.0.*-*"
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common.Model",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common.Types",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common.Types.Model",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Logistics.Documents",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Logistics.Processes.Receive",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Contacts.Types",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Logistics.Types",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Logistics.Stock",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common.Notes",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.Common.Notes.Model",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.ServiceModel.Client.Data",
|
||||
"version": "1.0.0.0"
|
||||
},
|
||||
{
|
||||
"name": "Connected.ServiceModel.Client",
|
||||
"version": "1.0.0.0"
|
||||
}
|
||||
],
|
||||
"locations": [
|
||||
"D:\\Connected\\Connected.Framework\\Connected.Instance\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Framework\\Connected.Entities\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Framework\\Connected.Rest\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Framework.ServiceModel\\Connected.ServiceModel\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Common\\Connected.Common\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Common.Types\\Connected.Common.Types\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Common\\Connected.Common.Notes\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Logistics\\Connected.Logistics.Processes.Receive\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Logistics\\Connected.Logistics.Documents\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Logistics\\Connected.Logistics.Types\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Logistics\\Connected.Logistics.Stock\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Customers\\Connected.Contacts.Types\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Framework.ServiceModel.Client\\Connected.ServiceModel.Client\\bin\\Debug\\net7.0",
|
||||
"D:\\Connected\\Connected.Framework.ServiceModel.Client\\Connected.ServiceModel.Client.Data\\bin\\Debug\\net7.0"
|
||||
],
|
||||
"start": "Connected.Instance.Start, Connected.Instance"
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user