Skip to content

Commit

Permalink
Merge pull request #4 from deveel/sample-console-app
Browse files Browse the repository at this point in the history
Sample Console Applications
  • Loading branch information
tsutomi authored Mar 6, 2024
2 parents 47f4d20 + ad56e54 commit e6543b9
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: dotnet restore

- name: Build
run: dotnet build -c Release --no-restore
run: dotnet build -c Release --no-restore --version-suffix ${{ github.run_number }}

- name: Test
run: dotnet test -c Release --no-build --verbosity normal
Expand All @@ -39,10 +39,10 @@ jobs:
- uses: actions/checkout@v4

- name: Pack Libraries
run: dotnet pack -c Release --include-symbols --include-source -o ./nupkgs
run: dotnet pack -c Release --include-symbols --include-source -o ./nupkgs --version-suffix ${{ github.run_number }}

- name: Publish to GitHub Packages
run: dotnet nuget push ./nupkgs/*.nupkg --source "github" --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate
run: dotnet nuget push ./nupkgs/*.nupkg --source "https://nuget.pkg.github.com/deveel/index.json" --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate

clean:
needs: publish
Expand Down
20 changes: 19 additions & 1 deletion Deveel.Pipelines.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deveel.Pipelines", "src\Deveel.Pipelines\Deveel.Pipelines.csproj", "{218467E4-0DE1-4AA9-946F-476020046D73}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Deveel.Pipelines.XUnit", "test\Deveel.Pipelines.XUnit\Deveel.Pipelines.XUnit.csproj", "{96A87C21-65F0-499A-91BD-F37466694820}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Deveel.Pipelines.XUnit", "test\Deveel.Pipelines.XUnit\Deveel.Pipelines.XUnit.csproj", "{96A87C21-65F0-499A-91BD-F37466694820}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleConsoleApp", "samples\SampleConsoleApp\SampleConsoleApp.csproj", "{BB20DAB5-58C5-4E3A-BD86-A2DA266A3FBE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{725D0B32-DC0B-484C-AB72-D1A0F9022966}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleServiceApp", "samples\SampleServiceApp\SampleServiceApp.csproj", "{17930ACA-F304-4E62-81BC-B71D23AAB38B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -21,10 +27,22 @@ Global
{96A87C21-65F0-499A-91BD-F37466694820}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96A87C21-65F0-499A-91BD-F37466694820}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96A87C21-65F0-499A-91BD-F37466694820}.Release|Any CPU.Build.0 = Release|Any CPU
{BB20DAB5-58C5-4E3A-BD86-A2DA266A3FBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB20DAB5-58C5-4E3A-BD86-A2DA266A3FBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB20DAB5-58C5-4E3A-BD86-A2DA266A3FBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB20DAB5-58C5-4E3A-BD86-A2DA266A3FBE}.Release|Any CPU.Build.0 = Release|Any CPU
{17930ACA-F304-4E62-81BC-B71D23AAB38B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17930ACA-F304-4E62-81BC-B71D23AAB38B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17930ACA-F304-4E62-81BC-B71D23AAB38B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17930ACA-F304-4E62-81BC-B71D23AAB38B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{BB20DAB5-58C5-4E3A-BD86-A2DA266A3FBE} = {725D0B32-DC0B-484C-AB72-D1A0F9022966}
{17930ACA-F304-4E62-81BC-B71D23AAB38B} = {725D0B32-DC0B-484C-AB72-D1A0F9022966}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7761C400-BDFA-45CE-9FD0-232680FB6340}
EndGlobalSection
Expand Down
78 changes: 78 additions & 0 deletions samples/SampleConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Deveel.Pipelines;

var pipeline = new MathBuilder()
.Use<AddHandler>()
.Use<MultiplyHandler>()
.Use<DivideHandler>()
.Use<SubtractHandler>()
.Build();

int? number;
Console.WriteLine("Please provide me a number: ");
while (true) {
if (int.TryParse(Console.ReadLine(), out var value)) {
number = value;
break;
} else {
Console.WriteLine("Invalid number, please try again: ");
}
}

var context = new MathContext {
Number = number.Value
};

await pipeline.ExecuteAsync(context);

Console.WriteLine($"The result is: {context.Number}");


class MathBuilder : PipelineBuilder<MathContext> {
public MathBuilder Use<THandler>() where THandler : class {
AddStep<THandler>();
return this;
}

public Pipeline<MathContext> Build() {
return BuildPipeline(new PipelineBuildContext());
}
}

class MathContext : PipelineExecutionContext {
public int Number { get; set; }
}

abstract class MathHandler {
protected abstract int Execute(int number, int value);

public Task HandleAsync(MathContext context) {
Console.Out.WriteLine($"Executing {GetType().Name} with {context.Number}");

context.Number = Execute(context.Number, context.Number);
return Task.CompletedTask;
}
}

class AddHandler : MathHandler {
protected override int Execute(int number, int value) {
return number + value;
}
}

class MultiplyHandler : MathHandler {
protected override int Execute(int number, int value) {
return number * value;
}
}

class SubtractHandler : MathHandler {
protected override int Execute(int number, int value) {
return number - value;
}
}

class DivideHandler : MathHandler {
protected override int Execute(int number, int value) {
return number / value;
}
}
14 changes: 14 additions & 0 deletions samples/SampleConsoleApp/SampleConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Deveel.Pipelines\Deveel.Pipelines.csproj" />
</ItemGroup>

</Project>
80 changes: 80 additions & 0 deletions samples/SampleServiceApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Deveel.Pipelines;

using Humanizer;

using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection()
.AddSingleton<INameNornalizer, DefaultNameNormalizer>()
.AddMyPipeline(p => p
.Use<AskName>()
.Use<NormalizeName>()
.Use<SayHello>())
.BuildServiceProvider();

var pipeline = services.GetRequiredService<Pipeline<MyContext>>();

await pipeline.ExecuteAsync();


class MyPipelineBuilder : PipelineBuilder<MyContext> {
public MyPipelineBuilder Use<THandler>() where THandler : class {
AddStep<THandler>();
return this;
}

public Pipeline<MyContext> Build(IServiceProvider services)
=> BuildPipeline(new PipelineBuildContext(services));
}

class MyContext : PipelineExecutionContext {
public string? Name { get; set; }
}

class AskName {
public Task HandleAsync(MyContext context, ExecutionDelegate<MyContext> next) {
Console.WriteLine("What's your name?");
context.Name = Console.ReadLine();
// TODO: invoke next -- currently it stops the pipeline after next
return Task.CompletedTask;
}
}

class SayHello {
public void Handle(MyContext context) {
Console.WriteLine($"Hello, {context.Name}");
}
}

class NormalizeName {
private readonly INameNornalizer nameNornalizer;

public NormalizeName(INameNornalizer nameNornalizer) {
this.nameNornalizer = nameNornalizer;
}

public async Task HandleAsync(MyContext context, ExecutionDelegate<MyContext> next) {
context.Name = await nameNornalizer.NormalizeNameAsync(context.Name);
}
}

static class ServiceCollectionExtensions {
public static IServiceCollection AddMyPipeline(this IServiceCollection services, Action<MyPipelineBuilder> configure) {
services.AddSingleton<Pipeline<MyContext>>(provider => {
var builder = new MyPipelineBuilder();
configure(builder);
return builder.Build(provider);
});

return services;
}
}

interface INameNornalizer {
Task<string> NormalizeNameAsync(string? name);
}

class DefaultNameNormalizer : INameNornalizer {
public Task<string> NormalizeNameAsync(string? name)
=> Task.FromResult(name.Pascalize());
}
19 changes: 19 additions & 0 deletions samples/SampleServiceApp/SampleServiceApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Deveel.Pipelines\Deveel.Pipelines.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/Deveel.Pipelines/Deveel.Pipelines.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>0.1.0</VersionPrefix>
<PackageTags>pipelines;pipeline;dotnet;handler;pipes</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/deveel/deveel.pipeline</RepositoryUrl>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.4" />
Expand Down

0 comments on commit e6543b9

Please sign in to comment.