Compare commits

...

20 Commits

Author SHA1 Message Date
Matija Koželj 4d6d9d5cd7 Fix dependency loader to properly handle relative local repositories
2 years ago
Matija Koželj a0a9418bd0 Fix dependency loader to resolve highest compatible package versions
2 years ago
Matija Koželj 5370ccfd2d Update nuget.config to include local nuget repo
2 years ago
Matija Koželj 2e24e07e40 Add nuget local auto-publish to Connected.Host.csproj
2 years ago
Matija Koželj 3616eb62ea Set package dependency resolution to max version instead of min version
2 years ago
Matija Koželj 3dc20a9325 Add check for application shutdown when waiting for debugger
2 years ago
Matija Koželj 9d22093165 Add support for tool paths
2 years ago
Matija Koželj 4d5c93f094 Change reference paths to application base instead of executingassembly
2 years ago
Matija Koželj 3ff30ea1fd Add command line parameters
2 years ago
Matija Koželj 3e01ffbe51 Add automatic versioning to Connected.Host
2 years ago
Matija Koželj 28b9c4c79e Clean up sys.json
2 years ago
Matija Koželj 9ec22923d5 Remove unused dependencies
2 years ago
Matija Koželj 37f1ac1321 Add references to packages used in the host .exe
2 years ago
Matija Koželj c67b11eec3 Rewrite host to use standard hosting patterns
2 years ago
Matija Koželj c5c6ac4a72 Strip SysConfiguration to fit with the standard model
2 years ago
Matija Koželj 35b4388b3c Move base initialization into separate class
2 years ago
Matija Koželj be3bc5c365 Add depedency loader for NuGet package loading
2 years ago
Tom Pipinic 7c2501b765 moved to src folder
2 years ago
Tom Pipinic 51884e6ff6 Refactoring namespaces
2 years ago
Matija Koželj 5b1cadc553 Initial commit
2 years ago

@ -0,0 +1,226 @@
# Remove the line below if you want to inherit .editorconfig settings from higher directories
root = true
# C# files
[*.cs]
#### Core EditorConfig Options ####
# Indentation and spacing
indent_size = 4
indent_style = tab
tab_width = 4
# New line preferences
end_of_line = crlf
insert_final_newline = false
#### .NET Coding Conventions ####
# Organize usings
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = true
file_header_template = unset
# this. and Me. preferences
dotnet_style_qualification_for_event = false
dotnet_style_qualification_for_field = false
dotnet_style_qualification_for_method = false
dotnet_style_qualification_for_property = false
# Language keywords vs BCL types preferences
dotnet_style_predefined_type_for_locals_parameters_members = true
dotnet_style_predefined_type_for_member_access = true
# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
dotnet_style_parentheses_in_other_operators = never_if_unnecessary
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity
# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members
# Expression-level preferences
dotnet_style_coalesce_expression = true
dotnet_style_collection_initializer = true
dotnet_style_explicit_tuple_names = true
dotnet_style_namespace_match_folder = true
dotnet_style_null_propagation = true
dotnet_style_object_initializer = true
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_prefer_auto_properties = true
dotnet_style_prefer_compound_assignment = true
dotnet_style_prefer_conditional_expression_over_assignment = true
dotnet_style_prefer_conditional_expression_over_return = true
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed
dotnet_style_prefer_inferred_anonymous_type_member_names = true
dotnet_style_prefer_inferred_tuple_names = true
dotnet_style_prefer_is_null_check_over_reference_equality_method = true
dotnet_style_prefer_simplified_boolean_expressions = true
dotnet_style_prefer_simplified_interpolation = true
# Field preferences
dotnet_style_readonly_field = true
# Parameter preferences
dotnet_code_quality_unused_parameters = all
# Suppression preferences
dotnet_remove_unnecessary_suppression_exclusions = none
# New line preferences
dotnet_style_allow_multiple_blank_lines_experimental = false
dotnet_style_allow_statement_immediately_after_block_experimental = false
#### C# Coding Conventions ####
# var preferences
csharp_style_var_elsewhere = true
csharp_style_var_for_built_in_types = true
csharp_style_var_when_type_is_apparent = true
# Expression-bodied members
csharp_style_expression_bodied_accessors = true
csharp_style_expression_bodied_constructors = false
csharp_style_expression_bodied_indexers = true
csharp_style_expression_bodied_lambdas = true
csharp_style_expression_bodied_local_functions = false
csharp_style_expression_bodied_methods = false
csharp_style_expression_bodied_operators = false
csharp_style_expression_bodied_properties = true
# Pattern matching preferences
csharp_style_pattern_matching_over_as_with_null_check = true
csharp_style_pattern_matching_over_is_with_cast_check = true
csharp_style_prefer_extended_property_pattern = true
csharp_style_prefer_not_pattern = true
csharp_style_prefer_pattern_matching = true
csharp_style_prefer_switch_expression = true
# Null-checking preferences
csharp_style_conditional_delegate_call = true
# Modifier preferences
csharp_prefer_static_local_function = true
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async
csharp_style_prefer_readonly_struct = true
# Code-block preferences
csharp_prefer_braces = when_multiline
csharp_prefer_simple_using_statement = true
csharp_style_namespace_declarations = file_scoped
csharp_style_prefer_method_group_conversion = true
csharp_style_prefer_top_level_statements = true
# Expression-level preferences
csharp_prefer_simple_default_expression = true
csharp_style_deconstructed_variable_declaration = true
csharp_style_implicit_object_creation_when_type_is_apparent = true
csharp_style_inlined_variable_declaration = true
csharp_style_prefer_index_operator = true
csharp_style_prefer_local_over_anonymous_function = true
csharp_style_prefer_null_check_over_type_check = true
csharp_style_prefer_range_operator = true
csharp_style_prefer_tuple_swap = true
csharp_style_prefer_utf8_string_literals = true
csharp_style_throw_expression = true
csharp_style_unused_value_assignment_preference = discard_variable
csharp_style_unused_value_expression_statement_preference = discard_variable
# 'using' directive preferences
csharp_using_directive_placement = outside_namespace
# New line preferences
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false
csharp_style_allow_embedded_statements_on_same_line_experimental = false
#### C# Formatting Rules ####
# New line preferences
csharp_new_line_before_catch = true
csharp_new_line_before_else = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_open_brace = all
csharp_new_line_between_query_expression_clauses = true
# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = true
csharp_indent_labels = one_less_than_current
csharp_indent_switch_labels = true
# Space preferences
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false
# Wrapping preferences
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false
#### Naming styles ####
# Naming rules
dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
# Symbol specifications
dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =
dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =
dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =
# Naming styles
dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
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
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A2A01D43-0CEE-4E49-8EC1-F16CA92F3C30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8E8D9F6F-68E1-466C-8D83-5ECB3AA3FC76}
EndGlobalSection
EndGlobal

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="%PACKAGEHOME%/External" />
</config>
<packageRestore>
<add key="enabled" value="True" />
<add key="automatic" value="True" />
</packageRestore>
<packageSources>
<add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
<add key="LocalNugetServer" value="%LOCAL_NUGET_SERVER%" />
<add key="Local file repository" value="%LOCAL_NUGET%" />
</packageSources>
<disabledPackageSources />
</configuration>

@ -0,0 +1,8 @@
namespace Connected.Host.Configuration
{
internal class MicroServiceDescriptor
{
public string Name { get; set; }
public string Version { get; set; }
}
}

@ -0,0 +1,14 @@
namespace Connected.Host.Configuration;
internal class SysConfiguration
{
private const string DefaultStart = "Connected.Instance.Start, Connected.Instance";
private string _start = DefaultStart;
public List<MicroServiceDescriptor> MicroServices { get; set; } = new();
public List<string> Repositories { get; set; } = new();
public string Start { get => _start ?? DefaultStart; set => _start = value; }
}

@ -0,0 +1,61 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PackAsTool>true</PackAsTool>
<IsPackable>true</IsPackable>
<ToolCommandName>connected</ToolCommandName>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>$(AssemblyName)</Title>
<Authors>Tom PIT ltd</Authors>
<Copyright>2022 Tom PIT ltd</Copyright>
<PackageProjectUrl>https://git.tompit.com/Connected/Info</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>connected;platform;</PackageTags>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageOutputPath>$(OutputPath)</PackageOutputPath>
</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" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Nerdbank.GitVersioning"
Version="3.5.119"
PrivateAssets="all"
Condition="!Exists('packages.config')" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<Content Update="sys.json">
<Pack>false</Pack>
</Content>
</ItemGroup>
<Target Name="CopyPackages" AfterTargets="Pack">
<Copy SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).nupkg" DestinationFolder="$([System.Environment]::GetEnvironmentVariable('LOCAL_NUGET'))" />
<Copy SourceFiles="$(OutputPath)..\$(PackageId).$(PackageVersion).snupkg" DestinationFolder="$([System.Environment]::GetEnvironmentVariable('LOCAL_NUGET'))" />
</Target>
<Target Name="UploadLocalPackages" AfterTargets="CopyPackages" >
<Exec Command="dotnet nuget push -s LocalNugetServer $(OutputPath)..\$(PackageId).$(PackageVersion).nupkg --api-key $([System.Environment]::GetEnvironmentVariable('LOCAL_NUGET_SERVER_TOKEN'))" IgnoreExitCode="true"></Exec>
<Exec Command="dotnet nuget push -s LocalNugetServer $(OutputPath)..\$(PackageId).$(PackageVersion).snupkg --api-key $([System.Environment]::GetEnvironmentVariable('LOCAL_NUGET_SERVER_TOKEN'))" IgnoreExitCode="true"></Exec>
</Target>
</Project>

@ -0,0 +1,37 @@
using System.Diagnostics;
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)
{
if (GlobalConfig.GetValue<bool>("waitForDebugger"))
while (!Debugger.IsAttached && !stoppingToken.IsCancellationRequested)
await Task.Delay(500);
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;
}
}

@ -0,0 +1,369 @@
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.GetFullPath(".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)
{
var packageSource = new PackageSource(source);
if (Uri.TryCreate(source, UriKind.RelativeOrAbsolute, out var sourceUri))
{
if (!sourceUri.IsAbsoluteUri)
packageSource = new PackageSource(Path.GetFullPath(source));
}
//if (packageSource.IsLocal || packageSource.TrySourceAsUri is null)
//{
// /*
// * Suspected local, verify
// */
// var local = Directory.Exists(source);
// if (local)
// {
// /*
// * If it's a cache, add all folders to sources
// */
// foreach (var directorySource in GetPackageSources(Directory.GetDirectories(source)))
// yield return directorySource;
// }
//}
yield return packageSource;
}
}
/// <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(
await GetPackageIdentity(new MicroServiceDescriptor { Name = dependency.Id, Version = dependency.VersionRange.ToString() }, cacheContext, logger, repositories, cancelToken),
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);
}
}

@ -0,0 +1,48 @@
using System.Reflection;
using Connected.Host.Configuration;
namespace Connected.Host
{
public static partial class Program
{
public static async Task Main(params string[] args)
{
using IHost host = Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((_, cfg) =>
{
cfg.AddCommandLine(args);
var appPath = AppContext.BaseDirectory;
var segments = new Uri(appPath).Segments;
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;
}
}
if(File.Exists(Path.GetFullPath("./sys.json")))
cfg.AddJsonFile("./sys.json", true, false);
cfg.AddEnvironmentVariables();
})
.ConfigureServices((context, services) =>
{
services.Configure<SysConfiguration>(context.Configuration);
services.AddHostedService<ConnectedPlatformService>();
})
.Build();
await host.RunAsync();
}
}
}

@ -0,0 +1,39 @@
{
"profiles": {
"TomPIT.Connected": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:5063"
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Synchronize Entities": {
"commandName": "Project",
"commandLineArgs": "entitySynchronization=rebuild"
},
"Synchronize Entity": {
"commandName": "Project",
"commandLineArgs": "entitySynchronization=type:Logistics.Documents/Logistics.Documents.Receive.ReceivePlannedItem"
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:14733",
"sslPort": 44321
}
}
}

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

@ -0,0 +1,30 @@
{
"repositories": [
"c:/nuget/packages",
"https://api.nuget.org/v3/index.json"
],
"microServices": [
{
"name": "Connected.Common.Types",
"version": "1.0.*-*"
},
{
"name": "Connected.Common.Notes",
"version": "1.0.*-*"
},
{
"name": "Connected.ServiceModel.Client.Data",
"version": "1.0.*-*"
},
{
"name": "Connected.ServiceModel.Client",
"version": "1.0.*-*"
},
{
"name": "Connected.Instance",
"version": "1.0.*-*"
}
],
"start": "Connected.Instance.Start, Connected.Instance",
"waitForDebugger": true
}

@ -0,0 +1,14 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0-prerelease",
"semVer1NumericIdentifierPadding": 4,
"nugetPackageVersion": {
"semVer": 2, // optional. Set to either 1 or 2 to control how the NuGet package version string is generated. Default is 1.
"precision": "build" // optional. Use when you want to use a more or less precise package version than the default major.minor.build.
},
"cloudBuild": {
"buildNumber": {
"enabled": true
}
}
}
Loading…
Cancel
Save