|
| 1 | +# FunctionalStateMachine.Diagrams |
| 2 | + |
| 3 | +`FunctionalStateMachine.Diagrams` is a Roslyn source generator that emits Mermaid diagrams from fluent state machine definitions at compile time. It scans methods annotated with `[StateMachineDiagram]` and writes `.md` files into your project. |
| 4 | + |
| 5 | +## How to use |
| 6 | + |
| 7 | +### 1) Reference as an analyzer |
| 8 | + |
| 9 | +When using a project reference, the generator must be referenced as an analyzer: |
| 10 | + |
| 11 | +```xml |
| 12 | +<ItemGroup> |
| 13 | + <ProjectReference Include="..\FunctionalStateMachine.Diagrams\FunctionalStateMachine.Diagrams.csproj" |
| 14 | + OutputItemType="Analyzer" |
| 15 | + ReferenceOutputAssembly="false" /> |
| 16 | +</ItemGroup> |
| 17 | +``` |
| 18 | + |
| 19 | +### 2) Annotate a builder method |
| 20 | + |
| 21 | +```csharp |
| 22 | +using FunctionalStateMachine.Diagrams; |
| 23 | + |
| 24 | +[StateMachineDiagram("diagrams/MyMachine.md")] |
| 25 | +public static StateMachine<MyState, MyTrigger, MyData, MyCommand> Build() |
| 26 | +{ |
| 27 | + return StateMachine<MyState, MyTrigger, MyData, MyCommand>.Create() |
| 28 | + .StartWith(MyState.Initial) |
| 29 | + .For(MyState.Initial) |
| 30 | + .On<MyTrigger.StartTrigger>() |
| 31 | + .TransitionTo(MyState.Running) |
| 32 | + .Build(); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### 3) Build the project |
| 37 | + |
| 38 | +The generator writes the diagram to the path you specify (relative to the project directory). |
| 39 | + |
| 40 | +## Notes on project references |
| 41 | + |
| 42 | +If `FunctionalStateMachine.Diagrams` is added as a normal project reference, the source generator will not run and no diagrams will be emitted. Because Roslyn generators are only loaded via analyzer references, there is no reliable way for the generator to detect an incorrect reference. |
| 43 | + |
| 44 | +If you want the build to complain when the analyzer is missing, you can add a guard target in your project: |
| 45 | + |
| 46 | +```xml |
| 47 | +<Target Name="ValidateDiagramGenerator" BeforeTargets="CoreCompile"> |
| 48 | + <Error Condition="@(Analyzer->AnyHaveMetadataValue('Identity', 'FunctionalStateMachine.Diagrams')) == 'false'" |
| 49 | + Text="FunctionalStateMachine.Diagrams must be referenced as an analyzer. See README for setup." /> |
| 50 | +</Target> |
| 51 | +``` |
0 commit comments