-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added initial endpoints generation.
- Loading branch information
Showing
13 changed files
with
175 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections.Immutable; | ||
using H.Generators.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using OpenApiGenerator.Models; | ||
|
||
namespace OpenApiGenerator.Generators; | ||
|
||
[Generator] | ||
public class ClientGenerator : IIncrementalGenerator | ||
{ | ||
#region Constants | ||
|
||
private const string Id = "OACG"; | ||
|
||
#endregion | ||
|
||
#region Methods | ||
|
||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
var settings = context.DetectSettings(); | ||
|
||
context.AdditionalTextsProvider | ||
.Where(static text => text.Path.EndsWith(".yaml", StringComparison.InvariantCultureIgnoreCase)) | ||
.Combine(settings) | ||
.SelectAndReportExceptions(PrepareData, context, Id) | ||
.SelectMany(static (x, _) => x) | ||
.SelectAndReportExceptions(GetSourceCode, context, Id) | ||
.AddSource(context); | ||
} | ||
|
||
private static EquatableArray<EndPoint> PrepareData( | ||
(AdditionalText text, Settings settings) tuple, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var (text, settings) = tuple; | ||
if (settings.UseNSwag) | ||
{ | ||
return ImmutableArray<EndPoint>.Empty; | ||
} | ||
|
||
var openApiDocument = text.GetOpenApiDocument(cancellationToken); | ||
|
||
var includedOperationIds = new HashSet<string>(settings.IncludeOperationIds); | ||
|
||
return openApiDocument.Paths.SelectMany(path => | ||
path.Value.Operations | ||
.Where(x => | ||
//includedOperationIds.Count == 0 || | ||
includedOperationIds.Contains(x.Value.OperationId) || | ||
includedOperationIds.Contains(x.Value.OperationId.ToPropertyName())) | ||
.Select(operation => new EndPoint( | ||
Id: operation.Value.OperationId, | ||
Namespace: settings.Namespace, | ||
ClassName: settings.ClassName))) | ||
.ToImmutableArray(); | ||
} | ||
|
||
private static FileWithName GetSourceCode( | ||
EndPoint endPoint, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return new FileWithName( | ||
Name: $"{endPoint.FileNameWithoutExtension}.g.cs", | ||
Text: Sources.GenerateEndPoint(endPoint, cancellationToken: cancellationToken)); | ||
} | ||
|
||
#endregion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using H.Generators.Extensions; | ||
|
||
namespace OpenApiGenerator.Models; | ||
|
||
internal readonly record struct EndPoint( | ||
string Id, | ||
string Namespace, | ||
string ClassName | ||
) | ||
{ | ||
public string MethodName => Id.ToPropertyName(); | ||
|
||
public string FileNameWithoutExtension => $"{Namespace}.{ClassName}.EndPoints.{MethodName}"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using H.Generators.Extensions; | ||
using OpenApiGenerator.Models; | ||
|
||
namespace OpenApiGenerator; | ||
|
||
internal static partial class Sources | ||
{ | ||
public static string GenerateEndPoint( | ||
EndPoint endPoint, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return $@" | ||
#nullable enable | ||
namespace {endPoint.Namespace} | ||
{{ | ||
public partial class {endPoint.ClassName} | ||
{{ | ||
public void {endPoint.MethodName}() | ||
{{ | ||
}} | ||
}} | ||
}}".RemoveBlankLinesWhereOnlyWhitespaces(); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/tests/OpenApiGenerator.SnapshotTests/CustomAdditionalText.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...penApiGenerator.SnapshotTests/Snapshots/OpenAi/_#G.Api.EndPoints.ListModels.g.verified.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
//HintName: G.Api.EndPoints.ListModels.g.cs | ||
|
||
#nullable enable | ||
|
||
namespace G | ||
{ | ||
public partial class Api | ||
{ | ||
public void ListModels() | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters