Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dark-mode in mvc #200

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ private static void AppendProducesForOk(
}
else
{
sb.AppendLine(
4,
sb.Append(
12,
responseModel.CollectionDataType == "List"
? $".Produces<IEnumerable<{dataType}>>()"
: $".Produces<{responseModel.CollectionDataType}<{dataType}>>()");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
global using Atc.Rest.ApiGenerator.Contracts.Extensions;
global using Atc.Rest.ApiGenerator.Contracts.Models;
global using Atc.Rest.ApiGenerator.Framework.ContentGenerators;
global using Atc.Rest.ApiGenerator.Framework.ContentGenerators.Server;
global using Atc.Rest.ApiGenerator.Framework.ContentGeneratorsParameters.Server;
global using Atc.Rest.ApiGenerator.Framework.Factories.Parameters.Server;
global using Atc.Rest.ApiGenerator.Framework.Factories.Parameters.ServerClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,15 @@ public void ScaffoldWebApplicationExtensions(

public void ScaffoldConfigureSwaggerOptions()
{
var contentGenerator = new ContentGenerators.ContentGeneratorServeConfigureSwaggerOptions(
new ContentGeneratorBaseParameters(Namespace: projectName));
var fullNamespace = $"{projectName}";

var contentGeneratorServerSwaggerDocOptionsParameters = ContentGeneratorServerSwaggerDocOptionsParameterFactory
.Create(
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new ContentGeneratorServeConfigureSwaggerOptions(
contentGeneratorServerSwaggerDocOptionsParameters);

var content = contentGenerator.Generate();

Expand Down Expand Up @@ -222,7 +229,7 @@ public void ScaffoldStartupFile()

public void ScaffoldWebConfig()
{
var contentGenerator = new Framework.ContentGenerators.Server.ContentGeneratorServerWebConfig();
var contentGenerator = new ContentGeneratorServerWebConfig();

var content = contentGenerator.Generate();

Expand All @@ -244,7 +251,7 @@ public void GenerateConfigureSwaggerDocOptions()
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new Framework.ContentGenerators.Server.ContentGeneratorServerSwaggerDocOptions(
var contentGenerator = new ContentGeneratorServerSwaggerDocOptions(
new GeneratedCodeHeaderGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
new GeneratedCodeAttributeGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
contentGeneratorServerSwaggerDocOptionsParameters);
Expand Down
1 change: 1 addition & 0 deletions src/Atc.Rest.ApiGenerator.Framework.Mvc/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
global using Atc.Rest.ApiGenerator.Contracts.Extensions;
global using Atc.Rest.ApiGenerator.Contracts.Models;
global using Atc.Rest.ApiGenerator.Framework.ContentGenerators;
global using Atc.Rest.ApiGenerator.Framework.ContentGenerators.Server;
global using Atc.Rest.ApiGenerator.Framework.ContentGeneratorsParameters.Server;
global using Atc.Rest.ApiGenerator.Framework.Factories.Parameters.Server;
global using Atc.Rest.ApiGenerator.Framework.Factories.Parameters.ServerClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,27 @@ public void ScaffoldWebApplicationExtensions(
=> throw new NotSupportedException($"{nameof(ScaffoldWebApplicationExtensions)} is not supported for MVC");

public void ScaffoldConfigureSwaggerOptions()
=> throw new NotSupportedException($"{nameof(ScaffoldConfigureSwaggerOptions)} is not supported for MVC");
{
var fullNamespace = $"{projectName}";

var contentGeneratorServerSwaggerDocOptionsParameters = ContentGeneratorServerSwaggerDocOptionsParameterFactory
.Create(
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new ContentGeneratorServeConfigureSwaggerOptions(
contentGeneratorServerSwaggerDocOptionsParameters);

var content = contentGenerator.Generate();

var contentWriter = new ContentWriter(logger);
contentWriter.Write(
projectPath,
projectPath.CombineFileInfo("Options", "ConfigureSwaggerOptions.cs"),
ContentWriterArea.Src,
content,
overrideIfExist: false);
}

public void ScaffoldProgramFile(
SwaggerThemeMode swaggerThemeMode)
Expand Down Expand Up @@ -162,7 +182,7 @@ public void ScaffoldStartupFile()

public void ScaffoldWebConfig()
{
var contentGenerator = new Framework.ContentGenerators.Server.ContentGeneratorServerWebConfig();
var contentGenerator = new ContentGeneratorServerWebConfig();

var content = contentGenerator.Generate();

Expand All @@ -184,7 +204,7 @@ public void GenerateConfigureSwaggerDocOptions()
fullNamespace,
openApiDocument.ToSwaggerDocOptionsParameters());

var contentGenerator = new Framework.ContentGenerators.Server.ContentGeneratorServerSwaggerDocOptions(
var contentGenerator = new ContentGeneratorServerSwaggerDocOptions(
new GeneratedCodeHeaderGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
new GeneratedCodeAttributeGenerator(new GeneratedCodeGeneratorParameters(apiGeneratorVersion)),
contentGeneratorServerSwaggerDocOptionsParameters);
Expand All @@ -206,6 +226,7 @@ public void MaintainGlobalUsings(
{
"System.CodeDom.Compiler",
"System.Reflection",
"System.Text",
domainProjectName,
$"{projectName}.Generated",
$"{projectName}.Options",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Atc.Rest.ApiGenerator.Framework.Minimal.ContentGenerators;
namespace Atc.Rest.ApiGenerator.Framework.ContentGenerators.Server;

public class ContentGeneratorServeConfigureSwaggerOptions : IContentGenerator
{
private readonly ContentGeneratorBaseParameters parameters;
private readonly ContentGeneratorServerSwaggerDocOptionsParameters parameters;

public ContentGeneratorServeConfigureSwaggerOptions(
ContentGeneratorBaseParameters parameters)
ContentGeneratorServerSwaggerDocOptionsParameters parameters)
{
this.parameters = parameters;
}
Expand Down Expand Up @@ -50,13 +50,69 @@ public string Generate()
sb.AppendLine(8, "ApiVersionDescription description)");
sb.AppendLine(4, "{");

// TODO: Add fix details to the OpenApiInfo
sb.AppendLine(8, "var text = new StringBuilder(\"An example API to showcase minimal api implementation using the Atc.Rest.MinimalApi Nuget package.\");");
var description = string.Empty;
if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.Description))
{
description = parameters.SwaggerDocOptions.Description!
.Replace("\"", string.Empty, StringComparison.Ordinal)
.Replace("'", string.Empty, StringComparison.Ordinal)
.Trim();
}

sb.AppendLine(8, $"var text = new StringBuilder(\"{description}\");");
sb.AppendLine(8, "var info = new OpenApiInfo");
sb.AppendLine(8, "{");
sb.AppendLine(12, "Title = $\"{environment.ApplicationName} {description.GroupName.ToUpperInvariant()}\",");
sb.AppendLine(12, "Version = description.ApiVersion.ToString(),");
sb.AppendLine(12, "Contact = new OpenApiContact { Name = \"atc-net\", Email = \"[email protected]\" },");
if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactName) ||
!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactEmail) ||
!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactUrl))
{
sb.AppendLine(12, "Contact = new OpenApiContact");
sb.AppendLine(12, "{");

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactName))
{
sb.AppendLine(16, $"Name = \"{parameters.SwaggerDocOptions.ContactName}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactEmail))
{
sb.AppendLine(16, $"Email = \"{parameters.SwaggerDocOptions.ContactEmail}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactUrl))
{
sb.AppendLine(16, $"Url = new Uri(\"{parameters.SwaggerDocOptions.ContactUrl}\"),");
}

sb.AppendLine(12, "},");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.TermsOfService))
{
sb.AppendLine(12, $"TermsOfService = new Uri(\"{parameters.SwaggerDocOptions.TermsOfService}\"),");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseName) ||
!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseUrl))
{
sb.AppendLine(12, "License = new OpenApiLicense");
sb.AppendLine(12, "{");

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseName))
{
sb.AppendLine(16, $"Name = \"{parameters.SwaggerDocOptions.LicenseName}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseUrl))
{
sb.AppendLine(16, $"Url = new Uri(\"{parameters.SwaggerDocOptions.LicenseUrl}\"),");
}

sb.AppendLine(12, "},");
}

sb.AppendLine(8, "};");
sb.AppendLine();
sb.AppendLine(8, "if (description.IsDeprecated)");
Expand All @@ -79,7 +135,7 @@ public string Generate()
sb.AppendLine();
sb.AppendLine(16, "foreach (var link in policy.Links)");
sb.AppendLine(16, "{");
sb.AppendLine(20, "if (link.Type != \"text /html\")");
sb.AppendLine(20, "if (link.Type != \"text/html\")");
sb.AppendLine(20, "{");
sb.AppendLine(24, "continue;");
sb.AppendLine(20, "}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,93 +28,17 @@ public string Generate()
sb.AppendLine("{");
sb.AppendLine(4, "private readonly IApiVersionDescriptionProvider provider;");
sb.AppendLine();
sb.AppendLine(4, "/// <summary>");
sb.AppendLine(4, "/// Initializes a new instance of the <see cref=\"ConfigureSwaggerOptions\"/> class.");
sb.AppendLine(4, "/// </summary>");
sb.AppendLine(4, "/// <param name=\"provider\">The <see cref=\"IApiVersionDescriptionProvider\">provider</see> used to generate Swagger documents.</param>");
sb.AppendLine(4, "public ConfigureSwaggerDocOptions(");
sb.AppendLine(8, "IApiVersionDescriptionProvider provider)");
sb.AppendLine(8, "=> this.provider = provider;");
sb.AppendLine();
sb.AppendLine(4, "public void Configure(");
sb.AppendLine(8, "SwaggerGenOptions options)");
sb.AppendLine(4, "{");
sb.AppendLine(8, "foreach (var version in provider.ApiVersionDescriptions)");
sb.AppendLine(8, "{");
sb.AppendLine(12, "options.SwaggerDoc(");
sb.AppendLine(16, "version.GroupName,");
sb.AppendLine(16, "new OpenApiInfo");
sb.AppendLine(16, "{");

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.Version))
{
sb.AppendLine(20, $"Version = \"{parameters.SwaggerDocOptions.Version}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.Title))
{
sb.AppendLine(20, $"Title = \"{parameters.SwaggerDocOptions.Title}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.Description))
{
sb.AppendLine(
20,
parameters.SwaggerDocOptions.Description.Contains('\n', StringComparison.Ordinal)
? $"Description = @\"{parameters.SwaggerDocOptions.Description}\","
: $"Description = \"{parameters.SwaggerDocOptions.Description}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactName) ||
!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactEmail) ||
!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactUrl))
{
sb.AppendLine(20, "Contact = new OpenApiContact");
sb.AppendLine(20, "{");

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactName))
{
sb.AppendLine(24, $"Name = \"{parameters.SwaggerDocOptions.ContactName}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactEmail))
{
sb.AppendLine(24, $"Email = \"{parameters.SwaggerDocOptions.ContactEmail}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.ContactUrl))
{
sb.AppendLine(24, $"Url = new Uri(\"{parameters.SwaggerDocOptions.ContactUrl}\"),");
}

sb.AppendLine(20, "},");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.TermsOfService))
{
sb.AppendLine(20, $"TermsOfService = new Uri(\"{parameters.SwaggerDocOptions.TermsOfService}\"),");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseName) ||
!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseUrl))
{
sb.AppendLine(20, "License = new OpenApiLicense");
sb.AppendLine(20, "{");

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseName))
{
sb.AppendLine(24, $"Name = \"{parameters.SwaggerDocOptions.LicenseName}\",");
}

if (!string.IsNullOrWhiteSpace(parameters.SwaggerDocOptions.LicenseUrl))
{
sb.AppendLine(24, $"Url = new Uri(\"{parameters.SwaggerDocOptions.LicenseUrl}\"),");
}

sb.AppendLine(20, "},");
}

sb.AppendLine(16, "});");
sb.AppendLine(8, "}");
sb.AppendLine();
sb.AppendLine(8, "options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, \"xml\"));");
sb.AppendLine(4, "}");
sb.AppendLine(8, "=> options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, \"xml\"));");
sb.Append('}');

return sb.ToString();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
var link = document.querySelector("link[rel*='icon']") || document.createElement("link");
var link = document.querySelector("link[rel*='icon']") || document.createElement("link");
link.type = "image/x-icon";
link.rel = "shortcut icon";
link.href = "/favicon.ico";
document.getElementsByTagName("head")[0].appendChild(link);

document.addEventListener('DOMContentLoaded', function () {
setTimeout(function () {
const linkElement = document.querySelector('div.topbar-wrapper > a[rel="noopener noreferrer"].link');
if (linkElement) {
const svgElement = linkElement.querySelector('svg');
if (svgElement) {
linkElement.parentNode.insertBefore(svgElement, linkElement);
}
linkElement.remove();
}
}, 100);
}, false);
2 changes: 2 additions & 0 deletions src/Atc.Rest.ApiGenerator/Generators/ServerHostGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ public async Task<bool> Generate()
projectOptions.ApiOptions.Generator.SwaggerThemeMode);
serverHostGeneratorMvc.ScaffoldStartupFile();
serverHostGeneratorMvc.ScaffoldWebConfig();
serverHostGeneratorMvc.ScaffoldConfigureSwaggerOptions();

serverHostGeneratorMvc.GenerateConfigureSwaggerDocOptions();

serverHostGeneratorMvc.MaintainGlobalUsings(
projectOptions.ApiOptions.Generator.RemoveNamespaceGroupSeparatorInGlobalUsings);
serverHostGeneratorMvc.MaintainWwwResources();

if (serverHostTestGeneratorMvc is not null &&
projectOptions.PathForTestGenerate is not null)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
global using System.CodeDom.Compiler;
global using System.Reflection;
global using System.Text;

global using Asp.Versioning.ApiExplorer;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// This code was auto-generated by ApiGenerator x.x.x.x.
//
// Changes to this file may cause incorrect behavior and will be lost if
Expand All @@ -11,29 +11,15 @@ public class ConfigureSwaggerDocOptions : IConfigureOptions<SwaggerGenOptions>
{
private readonly IApiVersionDescriptionProvider provider;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigureSwaggerOptions"/> class.
/// </summary>
/// <param name="provider">The <see cref="IApiVersionDescriptionProvider">provider</see> used to generate Swagger documents.</param>
public ConfigureSwaggerDocOptions(
IApiVersionDescriptionProvider provider)
=> this.provider = provider;

public void Configure(
SwaggerGenOptions options)
{
foreach (var version in provider.ApiVersionDescriptions)
{
options.SwaggerDoc(
version.GroupName,
new OpenApiInfo
{
Version = "1.0",
Title = "Demo Sample Api",
Description = "Demo Sample Api - SingleFileVersion",
Contact = new OpenApiContact
{
Name = "atc-net A/S",
},
});
}

options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
}
=> options.IncludeXmlComments(Path.ChangeExtension(GetType().Assembly.Location, "xml"));
}
Loading
Loading