Skip to content

Commit

Permalink
feat: Added ModelGenerator template.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 6, 2023
1 parent eef7188 commit 29d40a6
Show file tree
Hide file tree
Showing 31 changed files with 145 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers;
using OpenApiGenerator.Models;

namespace OpenApiGenerator;

internal static class OptionsExtensions
internal static class Extensions
{
public static IncrementalValueProvider<Settings> DetectSettings(
this IncrementalGeneratorInitializationContext context)
Expand Down Expand Up @@ -35,4 +37,17 @@ public static IncrementalValueProvider<Settings> DetectSettings(

));
}

public static OpenApiDocument GetOpenApiDocument(
this AdditionalText text,
CancellationToken cancellationToken = default)
{
var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty;
var openApiDocument = new OpenApiStringReader().Read(yaml, out _);

openApiDocument.Components ??= new OpenApiComponents();
openApiDocument.Components.Schemas ??= new Dictionary<string, OpenApiSchema>();

return openApiDocument;
}
}
63 changes: 44 additions & 19 deletions src/libs/OpenApiGenerator/Generators/ModelGenerator.cs
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
}
8 changes: 6 additions & 2 deletions src/libs/OpenApiGenerator/Generators/NSwagGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using H.Generators.Extensions;
using System.Collections.Immutable;
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using OpenApiGenerator.Models;

Expand Down Expand Up @@ -31,10 +32,13 @@ private static EquatableArray<FileWithName> GetSourceCode(
CancellationToken cancellationToken = default)
{
var (text, settings) = tuple;
if (!settings.UseNSwag)
{
return ImmutableArray<FileWithName>.Empty;
}

return Sources.GenerateUsingNSwag(text, settings, cancellationToken);
}


#endregion
}
2 changes: 2 additions & 0 deletions src/libs/OpenApiGenerator/Models/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
namespace OpenApiGenerator.Models;

internal readonly record struct Model(
string Name,
string Namespace,
ModelStyle Style,
EquatableArray<Property> Properties);
37 changes: 3 additions & 34 deletions src/libs/OpenApiGenerator/Sources/Sources.Models.cs
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 "//";
}
}
9 changes: 3 additions & 6 deletions src/libs/OpenApiGenerator/Sources/Sources.NSwag.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.Immutable;
using H.Generators.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.OpenApi.Readers;
using NJsonSchema.CodeGeneration.CSharp;
using NSwag.CodeGeneration.CSharp;
using NSwag.CodeGeneration.OperationNameGenerators;
Expand All @@ -19,11 +18,10 @@ public static EquatableArray<FileWithName> GenerateUsingNSwag(
CancellationToken cancellationToken = default)
{
var yaml = text.GetText(cancellationToken)?.ToString() ?? string.Empty;
var openApiDocument = text.GetOpenApiDocument(cancellationToken);
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 schemas = openApiDocument.Components.Schemas;
var allAdditionalExcludedTypeNames = schemas.Keys
.Select(schemaKey => GetAdditionalExcludedTypeNames(schemaKey, schemas))
.SelectMany(x => x)
Expand All @@ -38,14 +36,13 @@ public static EquatableArray<FileWithName> GenerateUsingNSwag(
.ToArray();

return new FileWithName(
Name: $"{prefix}.Models.{schemaKey}.cs",
Name: $"{settings.Namespace}.Models.{schemaKey}.cs",
Text: Generate(openApi, settings, excludedTypeNames));
}));

return files.ToImmutableArray();
}


private static string[] GetAdditionalExcludedTypeNames(
string key,
IDictionary<string, OpenApiSchema> schemas)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.AbuseResponse.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.AsnResponse.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.Carrier.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.Company.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.DomainsResponse.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.Error.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.FullResponse.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.Prefix.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.Prefix6.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.PrivacyResponse.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//HintName: G.Models.RangesResponse.g.cs
//
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.AbuseResponse.cs
//HintName: G.Models.AbuseResponse.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.AsnResponse.cs
//HintName: G.Models.AsnResponse.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.Carrier.cs
//HintName: G.Models.Carrier.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.Company.cs
//HintName: G.Models.Company.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.DomainsResponse.cs
//HintName: G.Models.DomainsResponse.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.Error.cs
//HintName: G.Models.Error.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: openapi.yaml.Models.FullResponse.cs
//HintName: G.Models.FullResponse.cs
//----------------------
// <auto-generated>
// Generated using the NSwag toolchain v13.20.0.0 (NJsonSchema v10.9.0.0 (Newtonsoft.Json v13.0.0.0)) (http://NSwag.org)
Expand All @@ -18,7 +18,7 @@
#pragma warning disable 8603 // Disable "CS8603 Possible null reference return"
#pragma warning disable 8604 // Disable "CS8604 Possible null reference argument for parameter"

namespace OpenApiGenerator
namespace G
{
using System = global::System;

Expand Down
Loading

0 comments on commit 29d40a6

Please sign in to comment.