Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
shkarface committed Apr 28, 2024
2 parents 8ec528f + 86c2f3e commit 599ffc3
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-sonarscanner": {
"version": "5.6.0",
"version": "5.7.2",
"commands": [
"dotnet-sonarscanner"
]
Expand All @@ -15,4 +15,4 @@
]
}
}
}
}
2 changes: 1 addition & 1 deletion .github/workflows/deploy-nuget.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ jobs:
run: |-
dotnet nuget push "packages/*.nupkg" \
--skip-duplicate \
--api-key ${{ secrets.NUGET__API_KEY }} \
--api-key ${{ secrets.NUGET_API_KEY }} \
--source https://api.nuget.org/v3/index.json
23 changes: 23 additions & 0 deletions .github/workflows/sonarqube.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Run SonarQube Analysis

on:
push:
branches:
- main

paths-ignore:
- "**.md"
- ".vscode/**"

concurrency:
group: sonarqube-analysis

jobs:
test:
uses: ./.github/workflows/tests-base.yaml
with:
sonarqube: true
sonarqube_host: ${{ vars.SONARQUBE_HOST }}
secrets:
SONARQUBE_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
15 changes: 8 additions & 7 deletions .github/workflows/tests-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Run Tests

on:
push:
branches-ignore:
- main

paths-ignore:
- "**.md"

Expand All @@ -15,9 +18,11 @@ on:
required: false
default: false

secrets:
SONARQUBE_HOST:
sonarqube_host:
type: string
required: false

secrets:
SONARQUBE_TOKEN:
required: false

Expand All @@ -28,9 +33,6 @@ jobs:
runs-on: ubuntu-latest

env:
PROJECT_KEY: ditkrg_DIT.Workflower_AYF14rjSb80e2b0bns3t
SONARQUBE_HOST: ${{ secrets.SONARQUBE_HOST }}
SONARQUBE_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
ASPNETCORE_ENVIRONMENT: Testing

steps:
Expand All @@ -42,7 +44,6 @@ jobs:
if: ${{ !inputs.sonarqube }}

- name: Run tests
if: ${{ !inputs.sonarqube }}
run: dotnet test

###############################
Expand All @@ -69,7 +70,7 @@ jobs:
run: |
dotnet tool run dotnet-sonarscanner begin -k:"$PROJECT_KEY" \
-d:sonar.login="$SONARQUBE_TOKEN" \
-d:sonar.host.url="$SONARQUBE_HOST" \
-d:sonar.host.url="${{ inputs.sonarqube_host }}" \
-d:sonar.cs.vscoveragexml.reportsPaths=coverage.xml
dotnet build --no-incremental
Expand Down
3 changes: 3 additions & 0 deletions src/DIT.Workflower.DependencyInjection/Common.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace DIT.Workflower.DependencyInjection;

public record DIContextWrapper<TContext>(TContext Context, IServiceProvider ServiceProvider);
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,33 @@ namespace DIT.Workflower.DependencyInjection.Extensions;
public static class IServiceCollectionExtensions
{

public static ITransitionStart<TState, TCommand, TContext> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, in int version = 1)
public static ITransitionStart<TState, TCommand, DIContextWrapper<TContext>> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, in int version = 1)
where TState : struct
where TCommand : struct
{
var id = WorkflowDefinitionWrapper<TState, TCommand, TContext>.GetDefaultId();
return AddWorkflowDefinition<TState, TCommand, TContext>(services, id, version);
}

public static ITransitionStart<TState, TCommand, TContext> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, in string id)
public static ITransitionStart<TState, TCommand, DIContextWrapper<TContext>> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, in string id)
where TState : struct
where TCommand : struct
{
return AddWorkflowDefinition<TState, TCommand, TContext>(services, id, version: 1);
}

public static ITransitionStart<TState, TCommand, TContext> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, string id, int version)
public static ITransitionStart<TState, TCommand, DIContextWrapper<TContext>> AddWorkflowDefinition<TState, TCommand, TContext>(this IServiceCollection services, string id, int version)
where TState : struct
where TCommand : struct
{
var builder = WorkflowDefinitionBuilder<TState, TCommand, TContext>.Create();
var builder = WorkflowDefinitionBuilder<TState, TCommand, DIContextWrapper<TContext>>.Create();

services.TryAddSingleton<IWorkflowFactory<TState, TCommand, TContext>, DefaultWorkflowFactory<TState, TCommand, TContext>>();
services.TryAddSingleton<IWorkflowFactory<TState, TCommand, DIContextWrapper<TContext>>, DefaultWorkflowFactory<TState, TCommand, DIContextWrapper<TContext>>>();

services.AddSingleton<IWorkflow<TState, TCommand, TContext>, WorkflowDefinitionWrapper<TState, TCommand, TContext>>(sp =>
services.AddSingleton<IWorkflow<TState, TCommand, DIContextWrapper<TContext>>, WorkflowDefinitionWrapper<TState, TCommand, DIContextWrapper<TContext>>>(sp =>
{
var definition = (WorkflowDefinitionBuilder<TState, TCommand, TContext>)builder;
var wrapper = new WorkflowDefinitionWrapper<TState, TCommand, TContext>(definition, id, version);
var definition = (WorkflowDefinitionBuilder<TState, TCommand, DIContextWrapper<TContext>>)builder;
var wrapper = new WorkflowDefinitionWrapper<TState, TCommand, DIContextWrapper<TContext>>(definition, id, version);
return wrapper;
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace DIT.Workflower.DependencyInjection.Extensions;

public static class IServiceProviderExtensions
{
public static IWorkflowFactory<TState, TCommand, DIContextWrapper<TContext>> GetRequiredWorkflowFactory<TState, TCommand, TContext>(this IServiceProvider sp)
where TState : struct
where TCommand : struct
{
return sp.GetRequiredService<IWorkflowFactory<TState, TCommand, DIContextWrapper<TContext>>>();
}

public static IWorkflowFactory<TState, TCommand, DIContextWrapper<TContext>>? GetWorkflowFactory<TState, TCommand, TContext>(this IServiceProvider sp)
where TState : struct
where TCommand : struct
{
return sp.GetService<IWorkflowFactory<TState, TCommand, DIContextWrapper<TContext>>>();
}

public static IWorkflow<TState, TCommand, DIContextWrapper<TContext>> CreateWorkflow<TState, TCommand, TContext>(this IServiceProvider sp, int version = 1)
where TState : struct
where TCommand : struct
{
var factory = GetRequiredWorkflowFactory<TState, TCommand, TContext>(sp);
return factory.CreateWorkflow(version);
}

public static IWorkflow<TState, TCommand, DIContextWrapper<TContext>> CreateWorkflow<TState, TCommand, TContext>(this IServiceProvider sp, string id, int version = 1)
where TState : struct
where TCommand : struct
{
var factory = GetRequiredWorkflowFactory<TState, TCommand, TContext>(sp);
return factory.CreateWorkflow(id, version);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ public WorkflowDefinitionWrapper(WorkflowDefinitionBuilder<TState, TCommand, TCo

public static string GetDefaultId()
{
return $"{typeof(TState).Name}_{typeof(TCommand).Name}_{typeof(TContext).Name}";
var ctxType = typeof(TContext);
var ctxName = ctxType.Name;

if (ctxType.IsGenericType)
ctxName = string.Join("+", typeof(TContext).GetGenericArguments().Select(x => x.Name));

return $"{typeof(TState).Name}_{typeof(TCommand).Name}_{ctxName}";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public void Test()

var sp = sc.BuildServiceProvider();

var workflowFactory = sp.GetRequiredService<IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>>();

var workflowFactory = sp.GetRequiredWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>();

var v1 = workflowFactory.CreateWorkflow(id);
var v2 = workflowFactory.CreateWorkflow(id, version: 2);
Expand All @@ -62,17 +61,20 @@ public void Test()
public void IdGenerationTest()
{
var sc = new ServiceCollection();
const string expectedId = "PhoneState_PhoneCommand_PhoneCall";

sc.AddWorkflowDefinition<PhoneState, PhoneCommand, PhoneCall>(version: 1)
.From(PhoneState.Idle)
.On(PhoneCommand.IncomingCall)
.To(PhoneState.Ringing);

var sp = sc.BuildServiceProvider();
var workflowFactory = sp.GetRequiredService<IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>>();
var workflowFactory = sp.GetRequiredWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>();
var workflow = workflowFactory.CreateWorkflow();

Assert.Equal("PhoneState_PhoneCommand_PhoneCall", workflow.Id);
Assert.Equal(expectedId, workflow.Id);
Assert.Equal(expectedId, sp.CreateWorkflow<PhoneState, PhoneCommand, PhoneCall>().Id);
Assert.Equal(expectedId, sp.CreateWorkflow<PhoneState, PhoneCommand, PhoneCall>(expectedId).Id);
}

[Fact]
Expand All @@ -86,7 +88,7 @@ public void UnknownWorkflowReferenceThrows()
.To(PhoneState.Ringing);

var sp = sc.BuildServiceProvider();
var workflowFactory = sp.GetRequiredService<IWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>>();
var workflowFactory = sp.GetRequiredWorkflowFactory<PhoneState, PhoneCommand, PhoneCall>();
Assert.Throws<KeyNotFoundException>(() => workflowFactory.CreateWorkflow("unknown"));
}

Expand Down

0 comments on commit 599ffc3

Please sign in to comment.