From dfb5b08f26573799a7254b64e022759ed4acf102 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Wed, 21 Dec 2022 19:14:48 +0100 Subject: feat: latest, not working --- code/lib/AsyncEndpoint.cs | 40 ++++++ code/lib/I2R.Endpoints.csproj | 30 +++++ code/lib/SyncEndpoint.cs | 34 +++++ code/source-generator/EndpointFinder.cs | 17 +++ code/source-generator/EndpointGenerator.cs | 146 +++++++++++++++++++++ .../I2R.Endpoints.Generator.csproj | 18 +++ 6 files changed, 285 insertions(+) create mode 100644 code/lib/AsyncEndpoint.cs create mode 100644 code/lib/I2R.Endpoints.csproj create mode 100644 code/lib/SyncEndpoint.cs create mode 100644 code/source-generator/EndpointFinder.cs create mode 100644 code/source-generator/EndpointGenerator.cs create mode 100644 code/source-generator/I2R.Endpoints.Generator.csproj (limited to 'code') diff --git a/code/lib/AsyncEndpoint.cs b/code/lib/AsyncEndpoint.cs new file mode 100644 index 0000000..505032c --- /dev/null +++ b/code/lib/AsyncEndpoint.cs @@ -0,0 +1,40 @@ +namespace I2R.Endpoints; + +public static partial class AsyncEndpoint +{ + public static class Req + { + public abstract class Res + { + public abstract Task HandleAsync( + TRequest request, + CancellationToken cancellationToken = default + ); + } + + public abstract class NoRes + { + public abstract Task HandleAsync( + TRequest request, + CancellationToken cancellationToken = default + ); + } + } + + public static class NoReq + { + public abstract class Res + { + public abstract Task HandleAsync( + CancellationToken cancellationToken = default + ); + } + + public abstract class NoRes + { + public abstract Task HandleAsync( + CancellationToken cancellationToken = default + ); + } + } +} \ No newline at end of file diff --git a/code/lib/I2R.Endpoints.csproj b/code/lib/I2R.Endpoints.csproj new file mode 100644 index 0000000..90ecd90 --- /dev/null +++ b/code/lib/I2R.Endpoints.csproj @@ -0,0 +1,30 @@ + + + + net7.0 + enable + https://git.ivar.systems/dotnet-endpoints/tree/COPYING + https://git.ivar.systems/dotnet-endpoints + git + 1.1.0 + I2R.Endpoints + Ivar Løvlie + A library that enables single file endpoints (or whatever). + Ivar Løvlie + + Initial realease. + + true + + + + + + + + + + + diff --git a/code/lib/SyncEndpoint.cs b/code/lib/SyncEndpoint.cs new file mode 100644 index 0000000..f30923a --- /dev/null +++ b/code/lib/SyncEndpoint.cs @@ -0,0 +1,34 @@ +namespace I2R.Endpoints; + +public static partial class SyncEndpoint +{ + public static class Req + { + public abstract class Res + { + public abstract TResponse Handle( + TRequest request + ); + } + + public abstract class NoRes + { + public abstract void Handle( + TRequest request + ); + } + } + + public static class NoReq + { + public abstract class Res + { + public abstract TResponse Handle(); + } + + public abstract class NoRes + { + public abstract void Handle(); + } + } +} \ No newline at end of file diff --git a/code/source-generator/EndpointFinder.cs b/code/source-generator/EndpointFinder.cs new file mode 100644 index 0000000..b30451e --- /dev/null +++ b/code/source-generator/EndpointFinder.cs @@ -0,0 +1,17 @@ +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace I2R.Endpoints.Generator; + +public class EndpointFinder : ISyntaxReceiver +{ + public HashSet Endpoints { get; } = new(); + + public void OnVisitSyntaxNode(SyntaxNode syntaxNode) { + if (syntaxNode is not ClassDeclarationSyntax endpoint) return; + if ((endpoint.BaseList?.Types.Any(c => EndpointGenerator.IsSyncEndpoint(c.ToString())) ?? false) + || (endpoint.BaseList?.Types.Any(c => EndpointGenerator.IsAyncEndpoint(c.ToString())) ?? false)) { + Endpoints.Add(endpoint); + } + } +} diff --git a/code/source-generator/EndpointGenerator.cs b/code/source-generator/EndpointGenerator.cs new file mode 100644 index 0000000..989a1c5 --- /dev/null +++ b/code/source-generator/EndpointGenerator.cs @@ -0,0 +1,146 @@ +using System.Text.RegularExpressions; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +namespace I2R.Endpoints.Generator; + +[Generator] +public class EndpointGenerator : ISourceGenerator +{ + private const string SourceGenereatedComment = "// Generated by I2R.Endpoints, probably smart to leave it be"; + + public void Initialize(GeneratorInitializationContext context) { + context.RegisterForSyntaxNotifications(() => new EndpointFinder()); + } + + public void Execute(GeneratorExecutionContext context) { + var endpointClasses = ((EndpointFinder) context.SyntaxReceiver)?.Endpoints; + if (endpointClasses == null) { + context.ReportDiagnostic(CreateDebugDiagnostic("no endpoints were found")); + return; + } + + foreach (var info in endpointClasses.Select(GetEndpointInfoFromClass)) { + context.AddSource(info.GeneratedFileName, info.IsSynchronous ? GetSyncSource(info) : GetAsyncSource(info)); + } + } + + private static Diagnostic CreateDebugDiagnostic(string message) { + var descriptor = new DiagnosticDescriptor("debug", "Debug", message, "debug", DiagnosticSeverity.Warning, true); + return Diagnostic.Create(descriptor, null, ""); + } + + private class EndpointInfo + { + public string BaseEndpointClassName { get; set; } + public bool IsSynchronous { get; set; } + public string GeneratedFileName { get; set; } + } + + public static bool IsSyncEndpoint(string name) { + return name.StartsWith("SyncEndpoint"); + } + + public static bool IsAyncEndpoint(string name) { + return name.StartsWith("AsyncEndpoint"); + } + + private static EndpointInfo GetEndpointInfoFromClass(ClassDeclarationSyntax classDeclarationSyntax) { + var baseType = classDeclarationSyntax.BaseList?.Types.FirstOrDefault(); + if (baseType == default) { + return default; + } + + var fullTypeName = baseType.Type.ToString(); + var className = Regex.Match(fullTypeName, "(!?<).+?(?=>)").Value.Replace("<", ""); + return new EndpointInfo() { + BaseEndpointClassName = className, + IsSynchronous = IsSyncEndpoint(baseType.ToString()), + GeneratedFileName = className + ".endpoints.g.cs" + }; + } + + private static string GetSyncSource(EndpointInfo info) { + return $@" +{SourceGenereatedComment} +namespace {typeof(EndpointGenerator).Namespace}; +public static partial class SyncEndpoint +{{ + public static class Req + {{ + public abstract class Res : {info.BaseEndpointClassName} + {{ + public abstract TResponse Handle( + TRequest request + ); + }} + + public abstract class NoRes : {info.BaseEndpointClassName} + {{ + public abstract void Handle( + TRequest request + ); + }} + }} + + public static class NoReq + {{ + public abstract class Res : {info.BaseEndpointClassName} + {{ + public abstract TResponse Handle(); + }} + + public abstract class NoRes : {info.BaseEndpointClassName} + {{ + public abstract void Handle(); + }} + }} +}} +"; + } + + private static string GetAsyncSource(EndpointInfo info) { + return $@" +{SourceGenereatedComment} +namespace {typeof(EndpointGenerator).Namespace}; +public static partial class AsyncEndpoint +{{ + public static class Req + {{ + public abstract class Res : {info.BaseEndpointClassName} + {{ + public abstract Task HandleAsync( + TRequest request, + CancellationToken cancellationToken = default + ); + }} + + public abstract class NoRes : {info.BaseEndpointClassName} + {{ + public abstract Task HandleAsync( + TRequest request, + CancellationToken cancellationToken = default + ); + }} + }} + + public static class NoReq + {{ + public abstract class Res : {info.BaseEndpointClassName} + {{ + public abstract Task HandleAsync( + CancellationToken cancellationToken = default + ); + }} + + public abstract class NoRes : {info.BaseEndpointClassName} + {{ + public abstract Task HandleAsync( + CancellationToken cancellationToken = default + ); + }} + }} +}} +"; + } +} diff --git a/code/source-generator/I2R.Endpoints.Generator.csproj b/code/source-generator/I2R.Endpoints.Generator.csproj new file mode 100644 index 0000000..f67f7d3 --- /dev/null +++ b/code/source-generator/I2R.Endpoints.Generator.csproj @@ -0,0 +1,18 @@ + + + netstandard2.0 + 10 + true + true + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + -- cgit v1.3