Skip to content

Commit

Permalink
add hosting package
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed May 31, 2021
1 parent aed38d1 commit e2d9d7f
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 73 deletions.
24 changes: 23 additions & 1 deletion Modulight.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Delights.Modules.Hello", "s
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modulight.Modules.CommandLine", "src\Modulight.Modules.CommandLine\Modulight.Modules.CommandLine.csproj", "{08D91884-F32A-4838-99C7-9E4F8FB605E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.CommandLine", "test\Test.CommandLine\Test.CommandLine.csproj", "{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.CommandLine", "test\Test.CommandLine\Test.CommandLine.csproj", "{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modulight.Modules.Hosting", "src\Modulight.Modules.Hosting\Modulight.Modules.Hosting.csproj", "{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A4DA65C6-7D97-4C3D-85F0-BB5152691461}"
ProjectSection(SolutionItems) = preProject
.dockerignore = .dockerignore
.gitignore = .gitignore
LICENSE = LICENSE
README.md = README.md
EndProjectSection
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
Expand Down Expand Up @@ -266,6 +276,18 @@ Global
{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}.Release|x64.Build.0 = Release|Any CPU
{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}.Release|x86.ActiveCfg = Release|Any CPU
{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}.Release|x86.Build.0 = Release|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x64.ActiveCfg = Debug|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x64.Build.0 = Debug|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x86.ActiveCfg = Debug|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x86.Build.0 = Debug|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|Any CPU.Build.0 = Release|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x64.ActiveCfg = Release|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x64.Build.0 = Release|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x86.ActiveCfg = Release|Any CPU
{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@

### Use modules

See [README](./src/Modulight.Modules.Core/README.md) for details.
See [here](./src/Modulight.Modules.Core/README.md) for details.

### Addition steps

#### Use Command line modules

See [README](./src/Modulight.Modules.CommandLine/README.md) for details.
See [here](./src/Modulight.Modules.CommandLine/README.md) for details.

#### Use Razor component modules

See [README](./src/Modulight.Modules.Client.RazorComponents/README.md) for details.
See [here](./src/Modulight.Modules.Client.RazorComponents/README.md) for details.

#### Use Blazor UI hosting template

See [README](./src/Modulight.UI.Blazor/README.md) for details.
See [here](./src/Modulight.UI.Blazor/README.md) for details.

#### Use ASP.NET modules

See [README](./src/Modulight.Modules.Server.AspNet/README.md) for details.
See [here](./src/Modulight.Modules.Server.AspNet/README.md) for details.

#### Use GraphQL modules

See [README](./src/Modulight.Modules.Server.GraphQL/README.md) for details.
See [here](./src/Modulight.Modules.Server.GraphQL/README.md) for details.

## Example codes

Expand Down
1 change: 1 addition & 0 deletions build/DeployPackageTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public override void Finally(BuildContext context)
readonly List<string> ModulightPackages = new List<string>
{
"Modulight.Modules.Core",
"Modulight.Modules.Hosting",
"Modulight.Modules.CommandLine",
"Modulight.Modules.Client.RazorComponents",
"Modulight.Modules.Server.AspNet",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Hosting;
using System;

namespace Modulight.Modules.CommandLine
{
/// <summary>
///
/// </summary>
public class CommandLineModuleBuilderOptions
{
/// <summary>
/// Default to true.
/// </summary>
public bool SuppressStatusMessages { get; set; } = true;
}
}
10 changes: 8 additions & 2 deletions src/Modulight.Modules.CommandLine/CommandLineModuleExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ public static class CommandLineModuleExtensions
/// Use building plugin for commandline modules.
/// </summary>
/// <param name="modules"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IModuleHostBuilder UseCommandLineModules(this IModuleHostBuilder modules)
public static IModuleHostBuilder UseCommandLineModules(this IModuleHostBuilder modules, Action<CommandLineModuleBuilderOptions>? configureOptions = null)
{
return modules.ConfigureBuilderServices(services =>
{
services.TryAddTransient<ICommandLineModuleManifestBuilder, DefaultCommandLineModuleManifestBuilder>();
var optionsBuilder = services.AddOptions<CommandLineModuleBuilderOptions>();
if (configureOptions is not null)
{
optionsBuilder.Configure(configureOptions);
}
}).UsePlugin<CommandLineModulePlugin>().AddModule<CommandLineCoreModule>();
}

Expand Down Expand Up @@ -98,7 +104,7 @@ public static ICommandLineModuleManifestBuilder WithDefaultsFromModuleType(this

var cmdAttrs = type.GetCustomAttributes<CommandFromAttribute>(true);

foreach(var attr in cmdAttrs)
foreach (var attr in cmdAttrs)
{
builder.AddCommand(attr.Type);
}
Expand Down

This file was deleted.

29 changes: 28 additions & 1 deletion src/Modulight.Modules.CommandLine/CommandLineModulePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Modulight.Modules.CommandLine;
using Modulight.Modules.Hosting;
using System;
Expand All @@ -11,10 +13,35 @@ namespace Modulight.Modules.CommandLine
{
internal sealed class CommandLineModulePlugin : ModuleHostBuilderPlugin
{
public CommandLineModulePlugin(IServiceProvider builderServices) => BuilderServices = builderServices;
public CommandLineModulePlugin(IServiceProvider builderServices, IOptions<CommandLineModuleBuilderOptions> options)
{
BuilderServices = builderServices;
Options = options.Value;
}

IServiceProvider BuilderServices { get; }

public CommandLineModuleBuilderOptions Options { get; }

public override void AfterBuild(ModuleDefinition[] modules, IServiceCollection services)
{
services.AddHostedService<CommandLineWorker>();

if (Options.SuppressStatusMessages is true)
{
services.AddLogging(builder =>
{
builder.AddFilter("Modulight.Modules.Hosting", LogLevel.Warning);
});
}

// From HostBuilder.UseConsoleLifetime
services.AddSingleton<IHostLifetime, ConsoleLifetime>();
services.Configure<ConsoleLifetimeOptions>(o => o.SuppressStatusMessages = Options.SuppressStatusMessages);

base.AfterBuild(modules, services);
}

public override void AfterModule(ModuleDefinition module, IServiceCollection services)
{
if (module.Type.IsModule<ICommandLineModule>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@

<ItemGroup>
<ProjectReference Include="..\Modulight.Modules.Core\Modulight.Modules.Core.csproj" />
<ProjectReference Include="..\Modulight.Modules.Hosting\Modulight.Modules.Hosting.csproj" />
</ItemGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions src/Modulight.Modules.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

For general modules:

```cs
```csharp
services.AddModules(builder => {
builder.AddModule<FooModule>();
});
```

2. Configure the module initilizing & shutdown.

```cs
```csharp
var host = services.GetModuleHost();
await host.Initialize();

Expand All @@ -36,8 +36,8 @@ await using var _ = await services.UseModuleHost();

Or use extension methods for hosting:

```cs
// ASP.NET hosting. (provided by package Modulight.Modules.Server.AspNet)
```csharp
// Generic hosting. (provided by package Modulight.Modules.Hosting)
// in Program: Task Main(string[] args)
await CreateHostBuilder(args).Build().RunAsyncWithModules();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
using Modulight.Modules.Server.AspNet;
using System;
using System.Threading.Tasks;

namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// Extension methods for Asp.NET hosting.
/// Extension methods for module hosting.
/// </summary>
public static class AspNetServerModuleHostingExtensions
public static class ModuleHostingExtensions
{
/// <summary>
/// Run with modules
Expand Down
35 changes: 35 additions & 0 deletions src/Modulight.Modules.Hosting/Modulight.Modules.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Nullable>enable</Nullable>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MPL-2.0</PackageLicenseExpression>
<Authors>StardustDL</Authors>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Description>Hosting for Modulight modular framework.</Description>
<PackageProjectUrl>https://github.com/StardustDL/modulight</PackageProjectUrl>
<RepositoryUrl>https://github.com/StardustDL/modulight</RepositoryUrl>
<RepositoryType>Git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>modular;hosting</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Modulight.Modules.Core\Modulight.Modules.Core.csproj" />
</ItemGroup>

<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions src/Modulight.Modules.Hosting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Modulight.Modules.Hosting

[![](https://buildstats.info/nuget/Modulight.Modules.Hosting)](https://www.nuget.org/packages/Modulight.Modules.Hosting/)

```csharp
// Generic hosting. (provided by package Modulight.Modules.Hosting)
// in Program: Task Main(string[] args)
await CreateHostBuilder(args).Build().RunAsyncWithModules();
```
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Modulight.Modules.Server.AspNet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![](https://buildstats.info/nuget/Modulight.Modules.Server.AspNet)](https://www.nuget.org/packages/Modulight.Modules.Server.AspNet/)

```cs
```csharp
// in Startup: void ConfigureServices(ISeviceCollection services)
services.AddModules(builder => {
Expand Down
2 changes: 2 additions & 0 deletions src/Modulight.Modules.Test/Modulight.Modules.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

<ItemGroup>
<ProjectReference Include="..\Modulight.Modules.Client.RazorComponents\Modulight.Modules.Client.RazorComponents.csproj" />
<ProjectReference Include="..\Modulight.Modules.CommandLine\Modulight.Modules.CommandLine.csproj" />
<ProjectReference Include="..\Modulight.Modules.Core\Modulight.Modules.Core.csproj" />
<ProjectReference Include="..\Modulight.Modules.Hosting\Modulight.Modules.Hosting.csproj" />
<ProjectReference Include="..\Modulight.Modules.Server.AspNet\Modulight.Modules.Server.AspNet.csproj" />
<ProjectReference Include="..\Modulight.Modules.Server.GraphQL\Modulight.Modules.Server.GraphQL.csproj" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/Modulight.UI.Blazor.Hosting/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Modulight.UI.Blazor.Hosting

[Modulight.UI.Blazor.Hosting ![](https://buildstats.info/nuget/Modulight.UI.Blazor.Hosting?includePreReleases=true)](https://www.nuget.org/packages/Modulight.UI.Blazor.Hosting/)

```cs
// void ConfigureServices(IServiceCollection services)
Expand Down
2 changes: 1 addition & 1 deletion test/Test.CommandLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Test.CommandLine
{
class Program
{
public static async Task Main(string[] args) => await CreateHostBuilder(args).RunConsoleAsyncWithModules(true);
public static async Task Main(string[] args) => await CreateHostBuilder(args).Build().RunAsyncWithModules();

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
Expand Down

0 comments on commit e2d9d7f

Please sign in to comment.