From 0861f3643d374fe615496229e74a522b72ce0fb5 Mon Sep 17 00:00:00 2001 From: James Hughes Date: Tue, 13 Jun 2023 22:52:07 +0100 Subject: [PATCH 1/2] Created demo project --- .../DemoApiDefinition.fluid.yml | 0 src/DemoProject/DemoProject.csproj | 18 ++++++++++++++++++ src/DemoProject/Program.cs | 4 ++++ .../Parsers/FluidApiDefinitionParserTests.cs | 7 ------- src/SuperFluid.Tests/SuperFluid.Tests.csproj | 7 ------- src/SuperFluid.sln | 6 ++++++ .../Internal/Model/FluidApiMethod.cs | 6 +++--- src/SuperFluid/SuperFluid.csproj | 2 +- 8 files changed, 32 insertions(+), 18 deletions(-) rename src/{SuperFluid.Tests => DemoProject}/DemoApiDefinition.fluid.yml (100%) create mode 100644 src/DemoProject/DemoProject.csproj create mode 100644 src/DemoProject/Program.cs diff --git a/src/SuperFluid.Tests/DemoApiDefinition.fluid.yml b/src/DemoProject/DemoApiDefinition.fluid.yml similarity index 100% rename from src/SuperFluid.Tests/DemoApiDefinition.fluid.yml rename to src/DemoProject/DemoApiDefinition.fluid.yml diff --git a/src/DemoProject/DemoProject.csproj b/src/DemoProject/DemoProject.csproj new file mode 100644 index 0000000..72fb391 --- /dev/null +++ b/src/DemoProject/DemoProject.csproj @@ -0,0 +1,18 @@ + + + + Exe + net7.0 + enable + enable + + + + + + + + + + + diff --git a/src/DemoProject/Program.cs b/src/DemoProject/Program.cs new file mode 100644 index 0000000..63a95e4 --- /dev/null +++ b/src/DemoProject/Program.cs @@ -0,0 +1,4 @@ +using SuperFluid.Tests.Cars; + +Console.WriteLine("Hello, World!"); + diff --git a/src/SuperFluid.Tests/Parsers/FluidApiDefinitionParserTests.cs b/src/SuperFluid.Tests/Parsers/FluidApiDefinitionParserTests.cs index c3727a6..9942ee6 100644 --- a/src/SuperFluid.Tests/Parsers/FluidApiDefinitionParserTests.cs +++ b/src/SuperFluid.Tests/Parsers/FluidApiDefinitionParserTests.cs @@ -87,13 +87,6 @@ public class FluidApiDefinitionParserTests CanTransitionTo = new() }; - private readonly FluidApiMethodDefinition _build = new() - { - Name = "Build", - ReturnType = "string", - CanTransitionTo = new() - }; - [Fact] public void CanDeserializeSimpleCase() { diff --git a/src/SuperFluid.Tests/SuperFluid.Tests.csproj b/src/SuperFluid.Tests/SuperFluid.Tests.csproj index 71497ed..9fd9d11 100644 --- a/src/SuperFluid.Tests/SuperFluid.Tests.csproj +++ b/src/SuperFluid.Tests/SuperFluid.Tests.csproj @@ -7,7 +7,6 @@ false default Library - true @@ -31,10 +30,4 @@ - - - - - - diff --git a/src/SuperFluid.sln b/src/SuperFluid.sln index 26f2556..58691e4 100644 --- a/src/SuperFluid.sln +++ b/src/SuperFluid.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuperFluid", "SuperFluid\Su EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SuperFluid.Tests", "SuperFluid.Tests\SuperFluid.Tests.csproj", "{6C2B21F8-301C-4062-9C12-608DAF4C0E16}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DemoProject", "DemoProject\DemoProject.csproj", "{FA0C7E98-6910-4226-A706-B9B0A0D529A2}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -22,5 +24,9 @@ Global {F49EE144-4D5A-4C5A-93ED-1BE64AFF5D7D}.Debug|Any CPU.Build.0 = Debug|Any CPU {F49EE144-4D5A-4C5A-93ED-1BE64AFF5D7D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F49EE144-4D5A-4C5A-93ED-1BE64AFF5D7D}.Release|Any CPU.Build.0 = Release|Any CPU + {FA0C7E98-6910-4226-A706-B9B0A0D529A2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FA0C7E98-6910-4226-A706-B9B0A0D529A2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FA0C7E98-6910-4226-A706-B9B0A0D529A2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FA0C7E98-6910-4226-A706-B9B0A0D529A2}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/SuperFluid/Internal/Model/FluidApiMethod.cs b/src/SuperFluid/Internal/Model/FluidApiMethod.cs index b8b0234..db516d1 100644 --- a/src/SuperFluid/Internal/Model/FluidApiMethod.cs +++ b/src/SuperFluid/Internal/Model/FluidApiMethod.cs @@ -12,8 +12,8 @@ public FluidApiMethod(string name, string? returnType, IEnumerable CanTransitionTo { get; init; } = new(); } diff --git a/src/SuperFluid/SuperFluid.csproj b/src/SuperFluid/SuperFluid.csproj index 13b439a..cedb4b8 100644 --- a/src/SuperFluid/SuperFluid.csproj +++ b/src/SuperFluid/SuperFluid.csproj @@ -6,7 +6,7 @@ enable default true - 0.0.1 + 0.0.2 SuperFluid James Hughes An incremental source generator for fluent APIs with grammar From 9fd6ef1e6af0daa673fb40553d34079708eb6982 Mon Sep 17 00:00:00 2001 From: James Hughes Date: Tue, 13 Jun 2023 23:16:13 +0100 Subject: [PATCH 2/2] Added demo project --- src/DemoProject/Program.cs | 60 ++++++++++++++++++- .../Services/FluidGeneratorService.cs | 15 +++++ src/SuperFluid/SuperFluid.csproj | 2 +- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/DemoProject/Program.cs b/src/DemoProject/Program.cs index 63a95e4..a73d17d 100644 --- a/src/DemoProject/Program.cs +++ b/src/DemoProject/Program.cs @@ -1,4 +1,62 @@ using SuperFluid.Tests.Cars; -Console.WriteLine("Hello, World!"); +string builtString = Test.Initialize() + .Unlock() + .Enter() + .Start() + .Stop() + .Exit() + .Lock() + .Unlock() + .Enter() + .Start() + .Build(); +Console.WriteLine(builtString); + +public class Test : ICarActor +{ + + private readonly List _calls = new(); + + public ICanUnlock Lock() + { + _calls.Add("Lock!"); + return this; + } + + public ICanStartOrExit Enter() + { + _calls.Add("Enter!"); + return this; + } + + + public ICanLockOrEnter Unlock() + { + _calls.Add("Unlock!"); + return this; + } + + public ICanStopOrBuild Start() + { + _calls.Add("Start!"); + return this; + } + + public ICanLockOrEnter Exit() + { + _calls.Add("Exit!"); + return this; + } + + public ICanStartOrExit Stop() + { + _calls.Add("Stop!"); + return this; + } + + public string Build() => string.Join('\n', _calls); + + public static ICanUnlock Initialize() => new Test(); +} diff --git a/src/SuperFluid/Internal/Services/FluidGeneratorService.cs b/src/SuperFluid/Internal/Services/FluidGeneratorService.cs index 5cc229d..3988b60 100644 --- a/src/SuperFluid/Internal/Services/FluidGeneratorService.cs +++ b/src/SuperFluid/Internal/Services/FluidGeneratorService.cs @@ -23,10 +23,25 @@ public Dictionary Generate(string rawYml) FluidApiModel model = _definitionParser.Parse(definition); Dictionary newSourceFiles = model.States.ToDictionary(s => $"{s.Name}.fluid.g.cs", s => GenerateStateSource(s, model)); + + newSourceFiles.Add($"{model.Name}.fluid.g.cs", GenerateCompoundInterface(model)); return newSourceFiles; } + private string GenerateCompoundInterface(FluidApiModel model) + { + string source = $$""" + namespace {{model.Namespace}}; + + public interface {{model.Name}}: {{string.Join(',', model.States.Select(s => s.Name))}} + { + public static abstract {{model.InitializerMethodReturnState.Name}} {{model.InitialMethod.Name}}(); + } + """; + return source; + } + private string GenerateStateSource(FluidApiState fluidApiState, FluidApiModel model) { IEnumerable methodDeclarations = fluidApiState.MethodTransitions.Select(kvp diff --git a/src/SuperFluid/SuperFluid.csproj b/src/SuperFluid/SuperFluid.csproj index cedb4b8..13b439a 100644 --- a/src/SuperFluid/SuperFluid.csproj +++ b/src/SuperFluid/SuperFluid.csproj @@ -6,7 +6,7 @@ enable default true - 0.0.2 + 0.0.1 SuperFluid James Hughes An incremental source generator for fluent APIs with grammar