Skip to content

Commit f33ad97

Browse files
[dotnet] Modernize code style in the devtools source generator (#15067)
* [dotnet] Modernize code style in the devtools source generator
1 parent 02f3ca7 commit f33ad97

25 files changed

+190
-262
lines changed
Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,53 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
24
{
3-
using System.Text.Json.Serialization;
4-
55
/// <summary>
66
/// Represents settings around Definition templates.
77
/// </summary>
88
public class CodeGenerationDefinitionTemplateSettings
99
{
10-
public CodeGenerationDefinitionTemplateSettings()
11-
{
12-
//Set Defaults;
13-
DomainTemplate = new CodeGenerationTemplateSettings
14-
{
15-
TemplatePath = "domain.hbs",
16-
OutputPath = "{{domainName}}\\{{className}}Adapter.cs",
17-
};
18-
19-
CommandTemplate = new CodeGenerationTemplateSettings
20-
{
21-
TemplatePath = "command.hbs",
22-
OutputPath = "{{domainName}}\\{{className}}Command.cs",
23-
};
24-
25-
EventTemplate = new CodeGenerationTemplateSettings
26-
{
27-
TemplatePath = "event.hbs",
28-
OutputPath = "{{domainName}}\\{{className}}EventArgs.cs",
29-
};
30-
31-
TypeObjectTemplate = new CodeGenerationTemplateSettings
32-
{
33-
TemplatePath = "type-object.hbs",
34-
OutputPath = "{{domainName}}\\{{className}}.cs",
35-
};
36-
37-
TypeHashTemplate = new CodeGenerationTemplateSettings
38-
{
39-
TemplatePath = "type-hash.hbs",
40-
OutputPath = "{{domainName}}\\{{className}}.cs",
41-
};
42-
43-
TypeEnumTemplate = new CodeGenerationTemplateSettings
44-
{
45-
TemplatePath = "type-enum.hbs",
46-
OutputPath = "{{domainName}}{{separator}}{{className}}.cs",
47-
};
48-
}
49-
5010
[JsonPropertyName("domainTemplate")]
51-
public CodeGenerationTemplateSettings DomainTemplate { get; set; }
11+
public CodeGenerationTemplateSettings DomainTemplate { get; set; } = new CodeGenerationTemplateSettings
12+
{
13+
TemplatePath = "domain.hbs",
14+
OutputPath = "{{domainName}}\\{{className}}Adapter.cs",
15+
};
5216

5317
[JsonPropertyName("commandTemplate")]
54-
public CodeGenerationTemplateSettings CommandTemplate { get; set; }
18+
public CodeGenerationTemplateSettings CommandTemplate { get; set; } = new CodeGenerationTemplateSettings
19+
{
20+
TemplatePath = "command.hbs",
21+
OutputPath = "{{domainName}}\\{{className}}Command.cs",
22+
};
5523

5624
[JsonPropertyName("eventTemplate")]
57-
public CodeGenerationTemplateSettings EventTemplate { get; set; }
25+
public CodeGenerationTemplateSettings EventTemplate { get; set; } = new CodeGenerationTemplateSettings
26+
{
27+
TemplatePath = "event.hbs",
28+
OutputPath = "{{domainName}}\\{{className}}EventArgs.cs",
29+
};
5830

5931
[JsonPropertyName("typeObjectTemplate")]
60-
public CodeGenerationTemplateSettings TypeObjectTemplate { get; set; }
32+
public CodeGenerationTemplateSettings TypeObjectTemplate { get; set; } = new CodeGenerationTemplateSettings
33+
{
34+
TemplatePath = "type-object.hbs",
35+
OutputPath = "{{domainName}}\\{{className}}.cs",
36+
};
37+
6138

6239
[JsonPropertyName("typeHashTemplate")]
63-
public CodeGenerationTemplateSettings TypeHashTemplate { get; set; }
40+
public CodeGenerationTemplateSettings TypeHashTemplate { get; set; } = new CodeGenerationTemplateSettings
41+
{
42+
TemplatePath = "type-hash.hbs",
43+
OutputPath = "{{domainName}}\\{{className}}.cs",
44+
};
6445

6546
[JsonPropertyName("typeEnumTemplate")]
66-
public CodeGenerationTemplateSettings TypeEnumTemplate { get; set; }
47+
public CodeGenerationTemplateSettings TypeEnumTemplate { get; set; } = new CodeGenerationTemplateSettings
48+
{
49+
TemplatePath = "type-enum.hbs",
50+
OutputPath = "{{domainName}}{{separator}}{{className}}.cs",
51+
};
6752
}
6853
}
Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,36 @@
1+
using System.Text.Json.Serialization;
2+
using System.Collections.Generic;
3+
14
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
25
{
3-
using System.Text.Json.Serialization;
4-
using System.Collections.Generic;
5-
66
/// <summary>
77
/// Settings to be passed to a ICodeGenerator
88
/// </summary>
99
public sealed class CodeGenerationSettings
1010
{
11-
public CodeGenerationSettings()
12-
{
13-
//Set defaults
14-
Include = new List<CodeGenerationTemplateSettings>();
15-
IncludeDeprecatedDomains = true;
16-
IncludeExperimentalDomains = true;
17-
RootNamespace = "BaristaLabs.ChromeDevTools";
18-
DefinitionTemplates = new CodeGenerationDefinitionTemplateSettings();
19-
TemplatesPath = "Templates";
20-
UsingStatements = new List<string>()
21-
{
22-
"System"
23-
};
24-
}
25-
2611
/// <summary>
2712
/// Collection of templates that will be parsed and output in the target folder.
2813
/// </summary>
2914
[JsonPropertyName("include")]
30-
public ICollection<CodeGenerationTemplateSettings> Include { get; set; }
15+
public ICollection<CodeGenerationTemplateSettings> Include { get; set; } = new List<CodeGenerationTemplateSettings>();
3116

3217
/// <summary>
3318
/// Indicates whether or not domains marked as depreciated will be generated. (Default: true)
3419
/// </summary>
3520
[JsonPropertyName("includeDeprecatedDomains")]
36-
public bool IncludeDeprecatedDomains { get; set; }
21+
public bool IncludeDeprecatedDomains { get; set; } = true;
3722

3823
/// <summary>
3924
/// Indicates whether or not domains marked as depreciated will be generated. (Default: true)
4025
/// </summary>
4126
[JsonPropertyName("includeExperimentalDomains")]
42-
public bool IncludeExperimentalDomains { get; set; }
27+
public bool IncludeExperimentalDomains { get; set; } = true;
4328

4429
/// <summary>
4530
/// Gets or sets the root namespace of generated classes.
4631
/// </summary>
4732
[JsonPropertyName("rootNamespace")]
48-
public string RootNamespace { get; set; }
33+
public string RootNamespace { get; set; } = "BaristaLabs.ChromeDevTools";
4934

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

5641
[JsonPropertyName("definitionTemplates")]
57-
public CodeGenerationDefinitionTemplateSettings DefinitionTemplates { get; set; }
42+
public CodeGenerationDefinitionTemplateSettings DefinitionTemplates { get; set; } = new CodeGenerationDefinitionTemplateSettings();
5843

5944
[JsonPropertyName("templatesPath")]
60-
public string TemplatesPath { get; set; }
45+
public string TemplatesPath { get; set; } = "Templates";
6146

6247
/// <summary>
6348
/// The using statements that will be included on each generated file.
6449
/// </summary>
6550
[JsonPropertyName("usingStatements")]
66-
public ICollection<string> UsingStatements { get; set; }
51+
public ICollection<string> UsingStatements { get; set; } = new List<string>()
52+
{
53+
"System"
54+
};
6755
}
6856
}

third_party/dotnet/devtools/src/generator/CodeGen/CodeGenerationTemplateSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
24
{
3-
using System.Text.Json.Serialization;
4-
55
/// <summary>
66
/// Defines settings around templates
77
/// </summary>

third_party/dotnet/devtools/src/generator/CodeGen/CodeGeneratorBase.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
3+
using System;
4+
using System.Collections.Generic;
5+
16
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
27
{
3-
using Microsoft.Extensions.DependencyInjection;
4-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
5-
using System;
6-
using System.Collections.Generic;
7-
88
/// <summary>
99
/// Represents a base implementation of a code generator.
1010
/// </summary>
1111
/// <typeparam name="T"></typeparam>
1212
public abstract class CodeGeneratorBase<T> : ICodeGenerator<T>
1313
where T : IDefinition
1414
{
15-
private readonly IServiceProvider m_serviceProvider;
1615
private readonly Lazy<CodeGenerationSettings> m_settings;
1716
private readonly Lazy<TemplatesManager> m_templatesManager;
1817

1918
/// <summary>
2019
/// Gets the service provider associated with the generator.
2120
/// </summary>
22-
public IServiceProvider ServiceProvider => m_serviceProvider;
21+
public IServiceProvider ServiceProvider { get; }
2322

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

3433
protected CodeGeneratorBase(IServiceProvider serviceProvider)
3534
{
36-
m_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
37-
m_settings = new Lazy<CodeGenerationSettings>(() => m_serviceProvider.GetRequiredService<CodeGenerationSettings>());
38-
m_templatesManager = new Lazy<TemplatesManager>(() => m_serviceProvider.GetRequiredService<TemplatesManager>());
35+
ServiceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
36+
m_settings = new Lazy<CodeGenerationSettings>(() => ServiceProvider.GetRequiredService<CodeGenerationSettings>());
37+
m_templatesManager = new Lazy<TemplatesManager>(() => ServiceProvider.GetRequiredService<TemplatesManager>());
3938
}
4039

4140
public abstract IDictionary<string, string> GenerateCode(T item, CodeGeneratorContext context);

third_party/dotnet/devtools/src/generator/CodeGen/CodeGeneratorContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
2+
using System.Collections.Generic;
3+
14
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
25
{
3-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
4-
using System.Collections.Generic;
5-
66
/// <summary>
77
/// Represents the current context of the code generator.
88
/// </summary>

third_party/dotnet/devtools/src/generator/CodeGen/CommandGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
using Humanizer;
2+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
3+
using System;
4+
using System.Collections.Generic;
5+
16
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
27
{
3-
using Humanizer;
4-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
5-
using System;
6-
using System.Collections.Generic;
7-
88
/// <summary>
99
/// Generates code for Command Definitions
1010
/// </summary>

third_party/dotnet/devtools/src/generator/CodeGen/DomainGenerator.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
using Humanizer;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
4+
using System;
5+
using System.Collections.Generic;
6+
17
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
28
{
3-
using Humanizer;
4-
using Microsoft.Extensions.DependencyInjection;
5-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
6-
using System;
7-
using System.Collections.Generic;
8-
using System.Linq;
9-
109
/// <summary>
1110
/// Generates code for Domain Definitions
1211
/// </summary>
@@ -22,27 +21,30 @@ public override IDictionary<string, string> GenerateCode(DomainDefinition domain
2221
var result = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
2322

2423
var typeGenerator = ServiceProvider.GetRequiredService<ICodeGenerator<TypeDefinition>>();
25-
foreach (var type in domainDefinition.Types)
24+
foreach (TypeDefinition type in domainDefinition.Types)
2625
{
27-
typeGenerator.GenerateCode(type, context)
28-
.ToList()
29-
.ForEach(x => result.Add(x.Key, x.Value));
26+
foreach (KeyValuePair<string, string> x in typeGenerator.GenerateCode(type, context))
27+
{
28+
result.Add(x.Key, x.Value);
29+
}
3030
}
3131

3232
var eventGenerator = ServiceProvider.GetRequiredService<ICodeGenerator<EventDefinition>>();
33-
foreach (var @event in domainDefinition.Events)
33+
foreach (EventDefinition @event in domainDefinition.Events)
3434
{
35-
eventGenerator.GenerateCode(@event, context)
36-
.ToList()
37-
.ForEach(x => result.Add(x.Key, x.Value));
35+
foreach (KeyValuePair<string, string> x in eventGenerator.GenerateCode(@event, context))
36+
{
37+
result.Add(x.Key, x.Value);
38+
}
3839
}
3940

4041
var commandGenerator = ServiceProvider.GetRequiredService<ICodeGenerator<CommandDefinition>>();
41-
foreach (var command in domainDefinition.Commands)
42+
foreach (CommandDefinition command in domainDefinition.Commands)
4243
{
43-
commandGenerator.GenerateCode(command, context)
44-
.ToList()
45-
.ForEach(x => result.Add(x.Key, x.Value));
44+
foreach (KeyValuePair<string, string> x in commandGenerator.GenerateCode(command, context))
45+
{
46+
result.Add(x.Key, x.Value);
47+
}
4648
}
4749

4850
if (string.IsNullOrWhiteSpace(Settings.DefinitionTemplates.DomainTemplate.TemplatePath))

third_party/dotnet/devtools/src/generator/CodeGen/EventGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
using Humanizer;
2+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
3+
using System;
4+
using System.Collections.Generic;
5+
16
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
27
{
3-
using Humanizer;
4-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
5-
using System;
6-
using System.Collections.Generic;
7-
88
/// <summary>
99
/// Generates code for Event Definitions
1010
/// </summary>

third_party/dotnet/devtools/src/generator/CodeGen/ICodeGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
2+
using System.Collections.Generic;
3+
14
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
25
{
3-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
4-
using System.Collections.Generic;
5-
66
/// <summary>
77
/// Represents a code generator that generates code files for a specific IDefinition type.
88
/// </summary>

third_party/dotnet/devtools/src/generator/CodeGen/IServiceProviderExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
3+
using System;
4+
15
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen
26
{
3-
using Microsoft.Extensions.DependencyInjection;
4-
using OpenQA.Selenium.DevToolsGenerator.ProtocolDefinition;
5-
using System;
6-
using System.Runtime.InteropServices.ComTypes;
7-
87
/// <summary>
98
/// Contains extensions for IServiceProvider.
109
/// </summary>
@@ -26,7 +25,7 @@ public static IServiceCollection AddCodeGenerationServices(this IServiceCollecti
2625
return serviceCollection
2726
.AddSingleton(settings)
2827
.AddSingleton<TemplatesManager>()
29-
.AddSingleton<ICodeGenerator<ProtocolDefinition>>((sp) => new ProtocolGenerator(sp))
28+
.AddSingleton<ICodeGenerator<ProtocolDefinition.ProtocolDefinition>>((sp) => new ProtocolGenerator(sp))
3029
.AddSingleton<ICodeGenerator<DomainDefinition>>((sp) => new DomainGenerator(sp))
3130
.AddSingleton<ICodeGenerator<TypeDefinition>>((sp) => new TypeGenerator(sp))
3231
.AddSingleton<ICodeGenerator<CommandDefinition>>((sp) => new CommandGenerator(sp))

0 commit comments

Comments
 (0)