Skip to content

Commit 1f5c08d

Browse files
author
Nigel Mukandi
committed
Fix CI
1 parent 2f6ef6a commit 1f5c08d

12 files changed

Lines changed: 463 additions & 73 deletions

File tree

.github/workflows/main.yaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
name: main
22

3+
run-name: "${{ github.event.head_commit.message }} — SHA: ${{ github.sha }}"
4+
35
on:
46
push:
57
branches:
68
- main
79
workflow_dispatch:
810

11+
env:
12+
DOTNET_VERSION: '9.0'
13+
914
jobs:
10-
test:
15+
build:
1116
runs-on: ubuntu-latest
1217
steps:
1318
- name: checkout repository
@@ -16,7 +21,7 @@ jobs:
1621
- name: setup dotnet environment
1722
uses: actions/setup-dotnet@d4c94342e560b34958eacfc5d055d21461ed1c5d
1823
with:
19-
dotnet-version: '9.0'
24+
dotnet-version: ${{ env.DOTNET_VERSION }}
2025

2126
- run: dotnet --info
2227

@@ -32,7 +37,8 @@ jobs:
3237
publish-artifacts:
3338
runs-on: ubuntu-latest
3439
name: publish artifacts
35-
needs: test
40+
needs: build
41+
environment: production
3642
steps:
3743
- name: checkout code
3844
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
@@ -42,7 +48,7 @@ jobs:
4248
- name: setup dotnet
4349
uses: actions/setup-dotnet@d4c94342e560b34958eacfc5d055d21461ed1c5d
4450
with:
45-
dotnet-version: '9.0'
51+
dotnet-version: ${{ env.DOTNET_VERSION }}
4652

4753
- name: dotnet info
4854
run: dotnet --info

.github/workflows/pull-request.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
name: pull request
2+
3+
run-name: "PR #${{ github.event.pull_request.number }} — ${{ github.event.pull_request.title }}"
4+
25
on:
36
pull_request:
47
branches:
58
- main
69

710
jobs:
8-
test:
11+
build:
912
runs-on: ubuntu-latest
1013
steps:
1114
- name: checkout repository
1215
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
13-
16+
with:
17+
fetch-depth: '1'
18+
1419
- name: setup dotnet
1520
uses: actions/setup-dotnet@d4c94342e560b34958eacfc5d055d21461ed1c5d
1621
with:

.github/workflows/release.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ jobs:
2828
run: dotnet build --configuration Release
2929

3030
- name: Extract version from tag
31-
id: get_version
32-
run: |
31+
id: get_version
32+
run: |
3333
# Remove the "refs/tags/" prefix from GITHUB_REF
3434
TAG=${GITHUB_REF#refs/tags/}
3535
# Remove leading 'v' if tag looks like "v1.2.3"

FiniteStateMachine/FiniteStateMachineBuilder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public sealed class FiniteStateMachineBuilder<TState, TTrigger>
1010
where TState : notnull
1111
{
1212
private readonly TState _initialState;
13-
private readonly Dictionary<TState, Dictionary<TTrigger, TState>> _transitions = new();
13+
private readonly Dictionary<TState, Dictionary<TTrigger, TransitionOption<TTrigger, TState>>> _transitions = new();
1414

1515
/// <summary>
1616
/// Initializes a new instance of the <see cref="FiniteStateMachineBuilder{TState, TTrigger}"/> class.
@@ -52,7 +52,7 @@ private Dictionary<RuleKey<TState, TTrigger>, Rule<TState, TTrigger>> GetTransit
5252
var rules = new Dictionary<RuleKey<TState, TTrigger>, Rule<TState, TTrigger>>();
5353
foreach (var (fromState, transitions) in _transitions)
5454
{
55-
foreach (var (trigger, targetState) in transitions)
55+
foreach (var (trigger, transitionOption) in transitions)
5656
{
5757
var ruleKey = new RuleKey<TState, TTrigger>
5858
{
@@ -62,8 +62,10 @@ private Dictionary<RuleKey<TState, TTrigger>, Rule<TState, TTrigger>> GetTransit
6262
var rule = new Rule<TState, TTrigger>
6363
{
6464
From = fromState,
65-
To = targetState,
66-
Trigger = trigger
65+
To = transitionOption.TargetState,
66+
Trigger = trigger,
67+
EntryActions = transitionOption.EntryActions,
68+
ExitActions = transitionOption.ExitActions,
6769
};
6870
rules.Add(ruleKey, rule);
6971
}

FiniteStateMachine/Rule.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ public sealed record Rule<TState, TTrigger>
66
public required TState From { get; init; }
77
public required TState To { get; init; }
88
public required TTrigger Trigger { get; init; }
9+
10+
public required List<Action> EntryActions { get; init; }
11+
12+
public required List<Action> ExitActions { get; init; }
913
}

FiniteStateMachine/StateConfiguration.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ namespace FiniteStateMachine;
88
public sealed class StateConfiguration<TState, TTrigger> where TTrigger : Enum
99
{
1010
private readonly TState _state;
11+
12+
private readonly List<Action> _entryActions;
13+
private readonly List<Action> _exitActions;
14+
15+
internal IReadOnlyList<Action> EntryActions => _entryActions;
16+
internal IReadOnlyList<Action> ExitActions => _exitActions;
1117

1218
/// <summary>
1319
/// Gets the transitions defined for the current state.
1420
/// </summary>
15-
internal Dictionary<TTrigger, TState> Transitions { get; } = new();
21+
internal Dictionary<TTrigger, TransitionOption<TTrigger, TState>> Transitions { get; } = new();
1622

1723
/// <summary>
1824
/// Initializes a new instance of the <see cref="StateConfiguration{TState, TTrigger}"/> class.
@@ -21,6 +27,14 @@ public sealed class StateConfiguration<TState, TTrigger> where TTrigger : Enum
2127
internal StateConfiguration(TState state)
2228
{
2329
_state = state;
30+
_entryActions = new List<Action>();
31+
_exitActions = new List<Action>();
32+
}
33+
34+
internal void ClearActions()
35+
{
36+
_entryActions.Clear();
37+
_exitActions.Clear();
2438
}
2539

2640
/// <summary>
@@ -30,6 +44,19 @@ internal StateConfiguration(TState state)
3044
/// <returns>A configuration object to setup the transition.</returns>
3145
public TransitionConfiguration<TState, TTrigger> On(TTrigger trigger)
3246
{
47+
3348
return new TransitionConfiguration<TState, TTrigger>(this, trigger);
3449
}
50+
51+
public StateConfiguration<TState, TTrigger> OnEnter(Action action)
52+
{
53+
_entryActions.Add(action);
54+
return this;
55+
}
56+
57+
public StateConfiguration<TState, TTrigger> OnExit(Action action)
58+
{
59+
_exitActions.Add(action);
60+
return this;
61+
}
3562
}

FiniteStateMachine/StateMachine.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public bool Trigger(TTrigger trigger)
5757
Trigger = trigger
5858
};
5959
if (!_rules.TryGetValue(key, out var rule)) return false;
60+
61+
rule.ExitActions.ForEach(action => action());
62+
6063
CurrentState = rule.To;
64+
65+
rule.EntryActions.ForEach(action => action());
66+
6167
return true;
6268
}
6369
}

FiniteStateMachine/TransitionConfiguration.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ internal TransitionConfiguration(StateConfiguration<TState, TTrigger> stateConfi
1313

1414
public StateConfiguration<TState, TTrigger> GoTo(TState target)
1515
{
16-
_stateConfiguration.Transitions[_trigger] = target;
16+
_stateConfiguration.Transitions[_trigger] = new TransitionOption<TTrigger, TState>
17+
{
18+
Trigger = _trigger,
19+
TargetState = target,
20+
EntryActions = _stateConfiguration.EntryActions.ToList(),
21+
ExitActions = _stateConfiguration.ExitActions.ToList()
22+
};
23+
24+
_stateConfiguration.ClearActions();
25+
1726
return _stateConfiguration;
1827
}
1928
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FiniteStateMachine;
2+
3+
internal sealed record TransitionOption<TTrigger, TState>
4+
{
5+
public required TTrigger Trigger { get; init; }
6+
public required TState TargetState { get; init; }
7+
public required List<Action> EntryActions { get; init; }
8+
public required List<Action> ExitActions { get; init; }
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace FiniteStateMachineTest;
22

3-
internal enum EnumState
3+
internal enum State
44
{
55
Playing,
66
Paused,

0 commit comments

Comments
 (0)