-
-
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 ModelGenerator template.
- Loading branch information
Showing
31 changed files
with
145 additions
and
90 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 |
---|---|---|
@@ -1,38 +1,63 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System.Collections.Immutable; | ||
using H.Generators.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using OpenApiGenerator.Models; | ||
|
||
namespace H.Generators; | ||
namespace OpenApiGenerator; | ||
|
||
[Generator] | ||
public class ModelGenerator : IIncrementalGenerator | ||
{ | ||
#region Constants | ||
|
||
//private const string Id = "MG"; | ||
private const string Id = "OAMG"; | ||
|
||
#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(GetSourceCode, context, Id) | ||
// .AddSource(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<FileWithName> GetSourceCode( | ||
// (AdditionalText text, Settings settings) tuple, | ||
// CancellationToken cancellationToken = default) | ||
// { | ||
// var (text, settings) = tuple; | ||
// | ||
// return Sources.GenerateUsingNSwag(text, settings, cancellationToken); | ||
// } | ||
|
||
private static EquatableArray<Model> PrepareData( | ||
(AdditionalText text, Settings settings) tuple, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var (text, settings) = tuple; | ||
if (settings.UseNSwag) | ||
{ | ||
return ImmutableArray<Model>.Empty; | ||
} | ||
|
||
var openApiDocument = text.GetOpenApiDocument(cancellationToken); | ||
|
||
return openApiDocument.Components.Schemas | ||
.Select(schema => new Model( | ||
Name: schema.Key.ToPropertyName(), | ||
Namespace: settings.Namespace, | ||
Style: settings.ModelStyle, | ||
Properties: Array.Empty<Property>().ToImmutableArray())) | ||
.ToImmutableArray(); | ||
} | ||
|
||
private static FileWithName GetSourceCode( | ||
Model model, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return new FileWithName( | ||
Name: $"{model.Namespace}.Models.{model.Name}.g.cs", | ||
Text: Sources.GenerateModel(model, 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 |
---|---|---|
@@ -1,44 +1,13 @@ | ||
using System.Collections.Immutable; | ||
using H.Generators.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.OpenApi.Readers; | ||
using OpenApiGenerator.Models; | ||
using OpenApiSchema = Microsoft.OpenApi.Models.OpenApiSchema; | ||
using NSwag; | ||
|
||
namespace OpenApiGenerator; | ||
|
||
internal static partial class Sources | ||
{ | ||
public static EquatableArray<FileWithName> GenerateModel( | ||
AdditionalText text, | ||
Settings settings, | ||
public static string GenerateModel( | ||
Model model, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty; | ||
var openApi = Task.Run(() => | ||
OpenApiYamlDocument.FromYamlAsync(yaml, cancellationToken), cancellationToken).Result; | ||
var openApiDocument = new OpenApiStringReader().Read(yaml, out _); | ||
var schemas = openApiDocument.Components?.Schemas ?? new Dictionary<string, OpenApiSchema>(); | ||
var prefix = Path.GetFileName(text.Path); | ||
var allAdditionalExcludedTypeNames = schemas.Keys | ||
.Select(schemaKey => GetAdditionalExcludedTypeNames(schemaKey, schemas)) | ||
.SelectMany(x => x) | ||
.ToArray(); | ||
|
||
var files = new List<FileWithName>(); | ||
files.AddRange(schemas.Keys.Select(schemaKey => | ||
{ | ||
var excludedTypeNames = schemas.Keys | ||
.Where(x => x != schemaKey) | ||
.Concat(allAdditionalExcludedTypeNames.Except(GetAdditionalExcludedTypeNames(schemaKey, schemas))) | ||
.ToArray(); | ||
|
||
return new FileWithName( | ||
Name: $"{prefix}.Models.{schemaKey}.cs", | ||
Text: Generate(openApi, settings, excludedTypeNames)); | ||
})); | ||
|
||
return files.ToImmutableArray(); | ||
return "//"; | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
...enerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AbuseResponse.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,2 @@ | ||
//HintName: G.Models.AbuseResponse.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...iGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.AsnResponse.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,2 @@ | ||
//HintName: G.Models.AsnResponse.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...enApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Carrier.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,2 @@ | ||
//HintName: G.Models.Carrier.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...enApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Company.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,2 @@ | ||
//HintName: G.Models.Company.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...erator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.DomainsResponse.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,2 @@ | ||
//HintName: G.Models.DomainsResponse.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...OpenApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Error.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,2 @@ | ||
//HintName: G.Models.Error.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...Generator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.FullResponse.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,2 @@ | ||
//HintName: G.Models.FullResponse.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...penApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix.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,2 @@ | ||
//HintName: G.Models.Prefix.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...enApiGenerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.Prefix6.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,2 @@ | ||
//HintName: G.Models.Prefix6.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...erator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.PrivacyResponse.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,2 @@ | ||
//HintName: G.Models.PrivacyResponse.g.cs | ||
// |
2 changes: 2 additions & 0 deletions
2
...nerator.SnapshotTests/Snapshots/YamlWithLocalFile/_#G.Models.RangesResponse.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,2 @@ | ||
//HintName: G.Models.RangesResponse.g.cs | ||
// |
1 change: 1 addition & 0 deletions
1
...pshots/YamlWithLocalFileUseNSwag/Tests.YamlWithLocalFileUseNSwag_Diagnostics.verified.txt
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 @@ | ||
[] |
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
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
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
Oops, something went wrong.