Skip to content

Commit

Permalink
Merge pull request #41 from hadashiA/ku/dotnet
Browse files Browse the repository at this point in the history
Add VitalRouter.Extensions.Hosting
  • Loading branch information
hadashiA authored Feb 6, 2024
2 parents 788bde9 + 15efe76 commit d518721
Show file tree
Hide file tree
Showing 306 changed files with 625 additions and 140 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ jobs:
with:
dotnet-version: |
8.0.x
- run: ls -l
- run: ls -l ./VitalRouter
- run: dotnet build -c Debug
- run: dotnet test -c Debug --no-build

Expand All @@ -36,7 +34,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ViralRouter/Library
path: src/ViralRouter.Unity/Library
key: Library-VitalRouter #-${{ matrix.unityVersion }}-${{ matrix.feature }}
restore-keys: |
Library-ViralRouter-
Expand All @@ -47,6 +45,6 @@ jobs:
UNITY_PASSWORD: ${{ secrets.UNITY_PASSWORD }}
UNITY_LICENSE: ${{ secrets[matrix.license] }}
with:
projectPath: ./VitalRouter.Unity
projectPath: ./src/VitalRouter.Unity
githubToken: ${{ secrets.GITHUB_TOKEN }}

4 changes: 2 additions & 2 deletions .github/workflows/update-version-number.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ jobs:

- name: Update package.json ${{ steps.configure.outputs.git-tag }}
run: |
ruby -i -pe 'gsub(/"version"\s*:\s*"([\d\.]+)"/, %q{"version": "${{ steps.configure.outputs.git-tag }}"})' VitalRouter.Unity/Assets/VitalRouter/package.json
ruby -i -pe 'gsub(/"version"\s*:\s*"([\d\.]+)"/, %q{"version": "${{ steps.configure.outputs.git-tag }}"})' src/VitalRouter.Unity/Assets/VitalRouter/package.json
- name: Update Directory.Build.props ${{ steps.configure.outputs.git-tag }}
run: |
ruby -i -pe 'gsub(%r{(<PackageVersion>)[\d\.]+(</PackageVersion>)}, %q{\1${{ steps.configure.outputs.git-tag }}\2})' Directory.Build.props
- name: Update README.md ${{ steps.configure.outputs.git-tag }}
run: |
ruby -i -pe 'gsub(%r{(https://github.com/hadashiA/VitalRouter.git\?path=/VitalRouter.Unity/Assets/VitalRouter#)[\d\.]+}, %q{\1${{ steps.configure.outputs.git-tag }}})' README.md
ruby -i -pe 'gsub(%r{(https://github.com/hadashiA/VitalRouter.git\?path=/VitalRouter.Unity/Assets/VitalRouter#)[\d\.]+}, %q{\1${{ steps.configure.outputs.git-tag }}})' README.md
- name: Check update on git
id: check_update
Expand Down
64 changes: 31 additions & 33 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -324,41 +324,39 @@ MigrationBackup/
# Unity

### Unity ###
VitalRouter.Unity/[Ll]ibrary/
VitalRouter.Unity/[Tt]emp/
VitalRouter.Unity/[Oo]bj/
VitalRouter.Unity/[Bb]uild/
VitalRouter.Unity/[Bb]uilds/
VitalRouter.Unity/Assets/AssetStoreTools*
VitalRouter.Unity/*.csproj
VitalRouter.Unity/*.sln
VitalRouter.Unity/UserSettings/*

VitalRouter.Benchmark/[Ll]ibrary/
VitalRouter.Benchmark/[Tt]emp/
VitalRouter.Benchmark/[Oo]bj/
VitalRouter.Benchmark/[Bb]uild/
VitalRouter.Benchmark/[Bb]uilds/
VitalRouter.Benchmark/Assets/AssetStoreTools*
src/VitalRouter.Unity/[Ll]ibrary/
src/VitalRouter.Unity/[Tt]emp/
src/VitalRouter.Unity/[Oo]bj/
src/VitalRouter.Unity/[Bb]uild/
src/VitalRouter.Unity/[Bb]uilds/
src/VitalRouter.Unity/Assets/AssetStoreTools*
src/VitalRouter.Unity/*.csproj
src/VitalRouter.Unity/*.sln
src/VitalRouter.Unity/UserSettings/*

# tests/VitalRouter.Benchmark/[Ll]ibrary/
# tests/VitalRouter.Benchmark/[Tt]emp/
# tests/VitalRouter.Benchmark/[Oo]bj/
# tests/VitalRouter.Benchmark/[Bb]uild/
# tests/VitalRouter.Benchmark/[Bb]uilds/
# tests/VitalRouter.Benchmark/Assets/AssetStoreTools*

# Autogenerated VS/MD/Consulo solution and project files
VitalRouter/bin/*
VitalRouter/obj/*

VitalRouter.Benchmark/ExportedObj/
VitalRouter.Benchmark/.consulo/
VitalRouter.Benchmark/*.csproj
VitalRouter.Benchmark/*.unityproj
VitalRouter.Benchmark/*.sln
VitalRouter.Benchmark/*.suo
VitalRouter.Benchmark/*.tmp
VitalRouter.Benchmark/*.user
VitalRouter.Benchmark/*.userprefs
VitalRouter.Benchmark/*.pidb
VitalRouter.Benchmark/*.booproj
VitalRouter.Benchmark/*.svd
VitalRouter.Benchmark/bin/*
VitalRouter.Benchmark/obj/*

# VitalRouter.Benchmark/ExportedObj/
# VitalRouter.Benchmark/.consulo/
# VitalRouter.Benchmark/*.csproj
# VitalRouter.Benchmark/*.unityproj
# VitalRouter.Benchmark/*.sln
# VitalRouter.Benchmark/*.suo
# VitalRouter.Benchmark/*.tmp
# VitalRouter.Benchmark/*.user
# VitalRouter.Benchmark/*.userprefs
# VitalRouter.Benchmark/*.pidb
# VitalRouter.Benchmark/*.booproj
# VitalRouter.Benchmark/*.svd
# VitalRouter.Benchmark/bin/*
# VitalRouter.Benchmark/obj/*

# Rider
**/.idea/*
Expand Down
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Description>The zero-allocation in-memory message passing library. The very thin layer that encourages decrative routings.</Description>
</PropertyGroup>
</Project>
Binary file not shown.
2 changes: 0 additions & 2 deletions VitalRouter.Unity/ProjectSettings/ProjectVersion.txt

This file was deleted.

25 changes: 0 additions & 25 deletions VitalRouter.Unity/VitalRouter.Unity.sln

This file was deleted.

53 changes: 38 additions & 15 deletions VitalRouter.sln
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter.SourceGenerator", "VitalRouter.SourceGenerator\VitalRouter.SourceGenerator.csproj", "{1E429509-F841-48F1-A111-5D0CD35707CC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter", "src\VitalRouter\VitalRouter.csproj", "{5EB8B2E1-8C01-4D6F-96B3-F4DC0E8BE4C6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter", "VitalRouter\VitalRouter.csproj", "{DD9A5ED8-8B7E-4F41-BDB6-67294C5752D9}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter.SourceGenerator", "src\VitalRouter.SourceGenerator\VitalRouter.SourceGenerator.csproj", "{40D96280-07A3-408E-AB76-642FEE5E0EAE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter.Tests", "VitalRouter.Tests\VitalRouter.Tests.csproj", "{A7FF360B-BC95-4EDD-8905-6B39592840B3}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter.Extensions.Hosting", "src\VitalRouter.Extensions.Hosting\VitalRouter.Extensions.Hosting.csproj", "{4ECC08A7-9829-47DB-AA97-88CEE6743838}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter.Tests", "tests\VitalRouter.Tests\VitalRouter.Tests.csproj", "{163E0808-D858-4C31-9C37-A0B50B912608}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VitalRouter.Extensions.Hosting.Tests", "tests\VitalRouter.Extensions.Hosting.Tests\VitalRouter.Extensions.Hosting.Tests.csproj", "{E6C26E17-4F85-48C8-B5C9-1DC1549C362D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{40063808-A412-46A8-A4E2-E77256444895}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{CBCBF5DB-09AA-4FAD-A9D1-C148885BB189}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1E429509-F841-48F1-A111-5D0CD35707CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E429509-F841-48F1-A111-5D0CD35707CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E429509-F841-48F1-A111-5D0CD35707CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E429509-F841-48F1-A111-5D0CD35707CC}.Release|Any CPU.Build.0 = Release|Any CPU
{DD9A5ED8-8B7E-4F41-BDB6-67294C5752D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD9A5ED8-8B7E-4F41-BDB6-67294C5752D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD9A5ED8-8B7E-4F41-BDB6-67294C5752D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD9A5ED8-8B7E-4F41-BDB6-67294C5752D9}.Release|Any CPU.Build.0 = Release|Any CPU
{A7FF360B-BC95-4EDD-8905-6B39592840B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A7FF360B-BC95-4EDD-8905-6B39592840B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A7FF360B-BC95-4EDD-8905-6B39592840B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A7FF360B-BC95-4EDD-8905-6B39592840B3}.Release|Any CPU.Build.0 = Release|Any CPU
{5EB8B2E1-8C01-4D6F-96B3-F4DC0E8BE4C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5EB8B2E1-8C01-4D6F-96B3-F4DC0E8BE4C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5EB8B2E1-8C01-4D6F-96B3-F4DC0E8BE4C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5EB8B2E1-8C01-4D6F-96B3-F4DC0E8BE4C6}.Release|Any CPU.Build.0 = Release|Any CPU
{40D96280-07A3-408E-AB76-642FEE5E0EAE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40D96280-07A3-408E-AB76-642FEE5E0EAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40D96280-07A3-408E-AB76-642FEE5E0EAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40D96280-07A3-408E-AB76-642FEE5E0EAE}.Release|Any CPU.Build.0 = Release|Any CPU
{4ECC08A7-9829-47DB-AA97-88CEE6743838}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4ECC08A7-9829-47DB-AA97-88CEE6743838}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4ECC08A7-9829-47DB-AA97-88CEE6743838}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4ECC08A7-9829-47DB-AA97-88CEE6743838}.Release|Any CPU.Build.0 = Release|Any CPU
{163E0808-D858-4C31-9C37-A0B50B912608}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{163E0808-D858-4C31-9C37-A0B50B912608}.Debug|Any CPU.Build.0 = Debug|Any CPU
{163E0808-D858-4C31-9C37-A0B50B912608}.Release|Any CPU.ActiveCfg = Release|Any CPU
{163E0808-D858-4C31-9C37-A0B50B912608}.Release|Any CPU.Build.0 = Release|Any CPU
{E6C26E17-4F85-48C8-B5C9-1DC1549C362D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6C26E17-4F85-48C8-B5C9-1DC1549C362D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6C26E17-4F85-48C8-B5C9-1DC1549C362D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6C26E17-4F85-48C8-B5C9-1DC1549C362D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{5EB8B2E1-8C01-4D6F-96B3-F4DC0E8BE4C6} = {40063808-A412-46A8-A4E2-E77256444895}
{4ECC08A7-9829-47DB-AA97-88CEE6743838} = {40063808-A412-46A8-A4E2-E77256444895}
{40D96280-07A3-408E-AB76-642FEE5E0EAE} = {40063808-A412-46A8-A4E2-E77256444895}
{E6C26E17-4F85-48C8-B5C9-1DC1549C362D} = {CBCBF5DB-09AA-4FAD-A9D1-C148885BB189}
{163E0808-D858-4C31-9C37-A0B50B912608} = {CBCBF5DB-09AA-4FAD-A9D1-C148885BB189}
EndGlobalSection
EndGlobal
18 changes: 0 additions & 18 deletions VitalRouter/VitalRouter.csproj

This file was deleted.

130 changes: 130 additions & 0 deletions src/VitalRouter.Extensions.Hosting/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using VitalRouter.Internal;

namespace VitalRouter;

public class VitalRouterOptions
{
public InterceptorStackBuilder Filters { get; } = new();
public CommandOrdering Ordering { get; set; }

internal readonly List<MapRoutesInfo> MapRoutesInfos = [];
internal readonly List<VitalRouterOptions> Subsequents = [];

public VitalRouterOptions MapAll(Assembly assembly)
{
var types = assembly.GetTypes().Where(x => x.GetCustomAttribute<RoutesAttribute>() != null);
foreach (var type in types)
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(type));
}
return this;
}

public VitalRouterOptions Map<T>()
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return this;
}

public VitalRouterOptions Map<T>(T instance) where T : class
{
MapRoutesInfos.Add(MapRoutesInfo.Analyze(typeof(T)));
return this;
}

public VitalRouterOptions Sequential()
{
Filters.Add<SequentialOrdering>();
return this;
}

public VitalRouterOptions FanOut(Action<VitalRouterOptions> configure)
{
var subsequent = new VitalRouterOptions();
configure(subsequent);
Subsequents.Add(subsequent);
return this;
}
}

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddVitalRouter(this IServiceCollection services)
{
return services.AddVitalRouter(static _ => { });
}

public static IServiceCollection AddVitalRouter(this IServiceCollection services, Action<VitalRouterOptions> configure)
{
var options = new VitalRouterOptions();
configure(options);

var router = new Router();
services.AddSingleton(router);
var routers = new List<(Router, VitalRouterOptions)>();
services.AddVitalRouterRecursive(router, options, routers);
services.AddHostedService(container => new VitalRouterHostedService(container, routers));

return services;
}

static IServiceCollection AddVitalRouterInterceptors(this IServiceCollection services, VitalRouterOptions options)
{
switch (options.Ordering)
{
case CommandOrdering.Sequential:
options.Filters.Add<SequentialOrdering>();
break;
}

foreach (var interceptorType in options.Filters.Types)
{
services.TryAddSingleton(interceptorType);
}

foreach (var info in options.MapRoutesInfos)
{
for (var paramIndex = 1; paramIndex < info.ParameterInfos.Length; paramIndex++)
{
var interceptorType = info.ParameterInfos[paramIndex].ParameterType;
services.TryAddSingleton(interceptorType);
}
}

return services;
}

static void AddVitalRouterRecursive(
this IServiceCollection services,
Router routerInstance,
VitalRouterOptions options,
ICollection<(Router, VitalRouterOptions)> routers)
{
services.AddVitalRouterInterceptors(options);

foreach (var info in options.MapRoutesInfos)
{
services.TryAddSingleton(info.Type);
}

if (options.Subsequents.Count > 0)
{
var fanOut = new FanOutInterceptor();
foreach (var subsequentOptions in options.Subsequents)
{
var subsequentRouter = new Router();
services.AddVitalRouterRecursive(subsequentRouter, subsequentOptions, routers);
fanOut.Add(subsequentRouter);
}
routerInstance.Filter(fanOut);
}

routers.Add((routerInstance, options));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<LangVersion>12</LangVersion>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VitalRouter\VitalRouter.csproj" />
<ProjectReference Include="..\VitalRouter.SourceGenerator\VitalRouter.SourceGenerator.csproj">
<OutputItemType>Analyzer</OutputItemType>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
</ItemGroup>

</Project>
Loading

0 comments on commit d518721

Please sign in to comment.