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

[dotnet] Modernize code style in the devtools source generator #15067

Merged
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
@@ -1,68 +1,53 @@
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using System.Text.Json.Serialization;

/// <summary>
/// Represents settings around Definition templates.
/// </summary>
public class CodeGenerationDefinitionTemplateSettings
{
public CodeGenerationDefinitionTemplateSettings()
{
//Set Defaults;
DomainTemplate = new CodeGenerationTemplateSettings
{
TemplatePath = "domain.hbs",
OutputPath = "{{domainName}}\\{{className}}Adapter.cs",
};

CommandTemplate = new CodeGenerationTemplateSettings
{
TemplatePath = "command.hbs",
OutputPath = "{{domainName}}\\{{className}}Command.cs",
};

EventTemplate = new CodeGenerationTemplateSettings
{
TemplatePath = "event.hbs",
OutputPath = "{{domainName}}\\{{className}}EventArgs.cs",
};

TypeObjectTemplate = new CodeGenerationTemplateSettings
{
TemplatePath = "type-object.hbs",
OutputPath = "{{domainName}}\\{{className}}.cs",
};

TypeHashTemplate = new CodeGenerationTemplateSettings
{
TemplatePath = "type-hash.hbs",
OutputPath = "{{domainName}}\\{{className}}.cs",
};

TypeEnumTemplate = new CodeGenerationTemplateSettings
{
TemplatePath = "type-enum.hbs",
OutputPath = "{{domainName}}{{separator}}{{className}}.cs",
};
}

[JsonPropertyName("domainTemplate")]
public CodeGenerationTemplateSettings DomainTemplate { get; set; }
public CodeGenerationTemplateSettings DomainTemplate { get; set; } = new CodeGenerationTemplateSettings
RenderMichael marked this conversation as resolved.
Show resolved Hide resolved
{
TemplatePath = "domain.hbs",
OutputPath = "{{domainName}}\\{{className}}Adapter.cs",
};

[JsonPropertyName("commandTemplate")]
public CodeGenerationTemplateSettings CommandTemplate { get; set; }
public CodeGenerationTemplateSettings CommandTemplate { get; set; } = new CodeGenerationTemplateSettings
{
TemplatePath = "command.hbs",
OutputPath = "{{domainName}}\\{{className}}Command.cs",
};

[JsonPropertyName("eventTemplate")]
public CodeGenerationTemplateSettings EventTemplate { get; set; }
public CodeGenerationTemplateSettings EventTemplate { get; set; } = new CodeGenerationTemplateSettings
{
TemplatePath = "event.hbs",
OutputPath = "{{domainName}}\\{{className}}EventArgs.cs",
};

[JsonPropertyName("typeObjectTemplate")]
public CodeGenerationTemplateSettings TypeObjectTemplate { get; set; }
public CodeGenerationTemplateSettings TypeObjectTemplate { get; set; } = new CodeGenerationTemplateSettings
{
TemplatePath = "type-object.hbs",
OutputPath = "{{domainName}}\\{{className}}.cs",
};


[JsonPropertyName("typeHashTemplate")]
public CodeGenerationTemplateSettings TypeHashTemplate { get; set; }
public CodeGenerationTemplateSettings TypeHashTemplate { get; set; } = new CodeGenerationTemplateSettings
{
TemplatePath = "type-hash.hbs",
OutputPath = "{{domainName}}\\{{className}}.cs",
};

[JsonPropertyName("typeEnumTemplate")]
public CodeGenerationTemplateSettings TypeEnumTemplate { get; set; }
public CodeGenerationTemplateSettings TypeEnumTemplate { get; set; } = new CodeGenerationTemplateSettings
{
TemplatePath = "type-enum.hbs",
OutputPath = "{{domainName}}{{separator}}{{className}}.cs",
};
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,36 @@
using System.Text.Json.Serialization;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using System.Text.Json.Serialization;
using System.Collections.Generic;

/// <summary>
/// Settings to be passed to a ICodeGenerator
/// </summary>
public sealed class CodeGenerationSettings
{
public CodeGenerationSettings()
{
//Set defaults
Include = new List<CodeGenerationTemplateSettings>();
IncludeDeprecatedDomains = true;
IncludeExperimentalDomains = true;
RootNamespace = "BaristaLabs.ChromeDevTools";
DefinitionTemplates = new CodeGenerationDefinitionTemplateSettings();
TemplatesPath = "Templates";
UsingStatements = new List<string>()
{
"System"
};
}

/// <summary>
/// Collection of templates that will be parsed and output in the target folder.
/// </summary>
[JsonPropertyName("include")]
public ICollection<CodeGenerationTemplateSettings> Include { get; set; }
public ICollection<CodeGenerationTemplateSettings> Include { get; set; } = new List<CodeGenerationTemplateSettings>();

/// <summary>
/// Indicates whether or not domains marked as depreciated will be generated. (Default: true)
/// </summary>
[JsonPropertyName("includeDeprecatedDomains")]
public bool IncludeDeprecatedDomains { get; set; }
public bool IncludeDeprecatedDomains { get; set; } = true;

/// <summary>
/// Indicates whether or not domains marked as depreciated will be generated. (Default: true)
/// </summary>
[JsonPropertyName("includeExperimentalDomains")]
public bool IncludeExperimentalDomains { get; set; }
public bool IncludeExperimentalDomains { get; set; } = true;

/// <summary>
/// Gets or sets the root namespace of generated classes.
/// </summary>
[JsonPropertyName("rootNamespace")]
public string RootNamespace { get; set; }
public string RootNamespace { get; set; } = "BaristaLabs.ChromeDevTools";

/// <summary>
/// Gets the version number of the runtime.
Expand All @@ -54,15 +39,18 @@ public CodeGenerationSettings()
public string RuntimeVersion { get; set; }

[JsonPropertyName("definitionTemplates")]
public CodeGenerationDefinitionTemplateSettings DefinitionTemplates { get; set; }
public CodeGenerationDefinitionTemplateSettings DefinitionTemplates { get; set; } = new CodeGenerationDefinitionTemplateSettings();

[JsonPropertyName("templatesPath")]
public string TemplatesPath { get; set; }
public string TemplatesPath { get; set; } = "Templates";

/// <summary>
/// The using statements that will be included on each generated file.
/// </summary>
[JsonPropertyName("usingStatements")]
public ICollection<string> UsingStatements { get; set; }
public ICollection<string> UsingStatements { get; set; } = new List<string>()
{
"System"
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json.Serialization;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using System.Text.Json.Serialization;

/// <summary>
/// Defines settings around templates
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

/// <summary>
/// Represents a base implementation of a code generator.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class CodeGeneratorBase<T> : ICodeGenerator<T>
where T : IDefinition
{
private readonly IServiceProvider m_serviceProvider;
private readonly Lazy<CodeGenerationSettings> m_settings;
private readonly Lazy<TemplatesManager> m_templatesManager;

/// <summary>
/// Gets the service provider associated with the generator.
/// </summary>
public IServiceProvider ServiceProvider => m_serviceProvider;
public IServiceProvider ServiceProvider { get; }

/// <summary>
/// Gets the code generation settings associated with the generator.
Expand All @@ -33,9 +32,9 @@ public abstract class CodeGeneratorBase<T> : ICodeGenerator<T>

protected CodeGeneratorBase(IServiceProvider serviceProvider)
{
m_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
m_settings = new Lazy<CodeGenerationSettings>(() => m_serviceProvider.GetRequiredService<CodeGenerationSettings>());
m_templatesManager = new Lazy<TemplatesManager>(() => m_serviceProvider.GetRequiredService<TemplatesManager>());
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
m_settings = new Lazy<CodeGenerationSettings>(() => ServiceProvider.GetRequiredService<CodeGenerationSettings>());
m_templatesManager = new Lazy<TemplatesManager>(() => ServiceProvider.GetRequiredService<TemplatesManager>());
}

public abstract IDictionary<string, string> GenerateCode(T item, CodeGeneratorContext context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System.Collections.Generic;

/// <summary>
/// Represents the current context of the code generator.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Humanizer;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using Humanizer;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

/// <summary>
/// Generates code for Command Definitions
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Humanizer;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using Humanizer;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Generates code for Domain Definitions
/// </summary>
Expand All @@ -22,27 +21,30 @@ public override IDictionary<string, string> GenerateCode(DomainDefinition domain
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

var typeGenerator = ServiceProvider.GetRequiredService<ICodeGenerator<TypeDefinition>>();
foreach (var type in domainDefinition.Types)
foreach (TypeDefinition type in domainDefinition.Types)
{
typeGenerator.GenerateCode(type, context)
.ToList()
.ForEach(x => result.Add(x.Key, x.Value));
foreach (KeyValuePair<string, string> x in typeGenerator.GenerateCode(type, context))
{
result.Add(x.Key, x.Value);
}
}

var eventGenerator = ServiceProvider.GetRequiredService<ICodeGenerator<EventDefinition>>();
foreach (var @event in domainDefinition.Events)
foreach (EventDefinition @event in domainDefinition.Events)
{
eventGenerator.GenerateCode(@event, context)
.ToList()
.ForEach(x => result.Add(x.Key, x.Value));
foreach (KeyValuePair<string, string> x in eventGenerator.GenerateCode(@event, context))
{
result.Add(x.Key, x.Value);
}
}

var commandGenerator = ServiceProvider.GetRequiredService<ICodeGenerator<CommandDefinition>>();
foreach (var command in domainDefinition.Commands)
foreach (CommandDefinition command in domainDefinition.Commands)
{
commandGenerator.GenerateCode(command, context)
.ToList()
.ForEach(x => result.Add(x.Key, x.Value));
foreach (KeyValuePair<string, string> x in commandGenerator.GenerateCode(command, context))
{
result.Add(x.Key, x.Value);
}
}

if (string.IsNullOrWhiteSpace(Settings.DefinitionTemplates.DomainTemplate.TemplatePath))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Humanizer;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using Humanizer;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Collections.Generic;

/// <summary>
/// Generates code for Event Definitions
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System.Collections.Generic;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System.Collections.Generic;

/// <summary>
/// Represents a code generator that generates code files for a specific IDefinition type.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;

namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
{
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
using System;
using System.Runtime.InteropServices.ComTypes;

/// <summary>
/// Contains extensions for IServiceProvider.
/// </summary>
Expand All @@ -26,7 +25,7 @@ public static IServiceCollection AddCodeGenerationServices(this IServiceCollecti
return serviceCollection
.AddSingleton(settings)
.AddSingleton<TemplatesManager>()
.AddSingleton<ICodeGenerator<ProtocolDefinition>>((sp) => new ProtocolGenerator(sp))
.AddSingleton<ICodeGenerator<ProtocolDefinition.ProtocolDefinition>>((sp) => new ProtocolGenerator(sp))
.AddSingleton<ICodeGenerator<DomainDefinition>>((sp) => new DomainGenerator(sp))
.AddSingleton<ICodeGenerator<TypeDefinition>>((sp) => new TypeGenerator(sp))
.AddSingleton<ICodeGenerator<CommandDefinition>>((sp) => new CommandGenerator(sp))
Expand Down
Loading
Loading