Skip to content

Commit da7043e

Browse files
committed
created new serialization model for better yaml support, add regex name cleaning
1 parent 023c55e commit da7043e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1567
-353
lines changed

docs/configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ data:
8686
# Generate class names with prefixed schema name eg. dbo.MyTable = DboMyTable
8787
prefixWithSchemaName: false
8888

89+
# Rename entities and properties with regular expressions
90+
renaming:
91+
# list of regular expressions to clean entity names
92+
entities:
93+
- ^(sp|tbl|udf|vw)_
94+
95+
# list of regular expressions to clean property names
96+
properties:
97+
- ^{Table.Name}(?=Id|Name)
98+
8999
# mapping class file configuration
90100
mapping:
91101
namespace: '{Project.Namespace}.Data.Mapping' # the mapping class namespace

src/EntityFrameworkCore.Generator.Core/GeneratorOptionsSerializer.cs renamed to src/EntityFrameworkCore.Generator.Core/ConfigurationSerializer.cs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.IO;
33

4-
using EntityFrameworkCore.Generator.Options;
4+
using EntityFrameworkCore.Generator.Serialization;
55

66
using Microsoft.Extensions.Logging;
77

@@ -11,17 +11,17 @@
1111
namespace EntityFrameworkCore.Generator;
1212

1313
/// <summary>
14-
/// Serialization and Deserialization for the <see cref="GeneratorOptions"/> class
14+
/// Serialization and Deserialization for the <see cref="Generator"/> class
1515
/// </summary>
16-
public class GeneratorOptionsSerializer : IGeneratorOptionsSerializer
16+
public class ConfigurationSerializer : IConfigurationSerializer
1717
{
18-
private readonly ILogger<GeneratorOptionsSerializer> _logger;
18+
private readonly ILogger<ConfigurationSerializer> _logger;
1919

2020
/// <summary>
21-
/// Initializes a new instance of the <see cref="GeneratorOptionsSerializer"/> class.
21+
/// Initializes a new instance of the <see cref="ConfigurationSerializer"/> class.
2222
/// </summary>
2323
/// <param name="logger">The logger.</param>
24-
public GeneratorOptionsSerializer(ILogger<GeneratorOptionsSerializer> logger)
24+
public ConfigurationSerializer(ILogger<ConfigurationSerializer> logger)
2525
{
2626
_logger = logger;
2727
}
@@ -36,8 +36,8 @@ public GeneratorOptionsSerializer(ILogger<GeneratorOptionsSerializer> logger)
3636
/// </summary>
3737
/// <param name="directory">The directory where the file is located.</param>
3838
/// <param name="file">The name of the options file.</param>
39-
/// <returns>An instance of <see cref="GeneratorOptions"/> if the file exists; otherwise <c>null</c>.</returns>
40-
public GeneratorOptions Load(string directory = null, string file = OptionsFileName)
39+
/// <returns>An instance of <see cref="Generator"/> if the file exists; otherwise <c>null</c>.</returns>
40+
public GeneratorModel Load(string directory = null, string file = OptionsFileName)
4141
{
4242
var path = GetPath(directory, file);
4343
if (!File.Exists(path))
@@ -46,20 +46,30 @@ public GeneratorOptions Load(string directory = null, string file = OptionsFileN
4646
return null;
4747
}
4848

49-
var factory = new GeneratorOptionsFactory();
49+
_logger.LogInformation($"Loading options file: {file}");
50+
using var reader = File.OpenText(path);
51+
52+
return Load(reader);
53+
}
54+
55+
/// <summary>
56+
/// Loads the options using the specified <paramref name="reader" />
57+
/// </summary>
58+
/// <param name="reader">The reader.</param>
59+
/// <returns>
60+
/// An instance of <see cref="Generator" />.
61+
/// </returns>
62+
public GeneratorModel Load(TextReader reader)
63+
{
64+
if (reader == null)
65+
return null;
5066

5167
var deserializer = new DeserializerBuilder()
5268
.WithNamingConvention(CamelCaseNamingConvention.Instance)
53-
.WithObjectFactory(factory)
5469
.Build();
5570

56-
_logger.LogInformation($"Loading options file: {file}");
57-
GeneratorOptions generatorOptions;
58-
using (var streamReader = File.OpenText(path))
59-
generatorOptions = deserializer.Deserialize<GeneratorOptions>(streamReader);
60-
61-
generatorOptions.Variables.ShouldEvaluate = true;
62-
return generatorOptions;
71+
// use Serialization model for better yaml support
72+
return deserializer.Deserialize<GeneratorModel>(reader);
6373
}
6474

6575
/// <summary>
@@ -69,7 +79,7 @@ public GeneratorOptions Load(string directory = null, string file = OptionsFileN
6979
/// <param name="directory">The directory where the file is located.</param>
7080
/// <param name="file">The name of the options file.</param>
7181
/// <returns>The full path of the options file.</returns>
72-
public string Save(GeneratorOptions generatorOptions, string directory = null, string file = OptionsFileName)
82+
public string Save(GeneratorModel generatorOptions, string directory = null, string file = OptionsFileName)
7383
{
7484
if (string.IsNullOrWhiteSpace(directory))
7585
directory = Environment.CurrentDirectory;
@@ -88,17 +98,13 @@ public string Save(GeneratorOptions generatorOptions, string directory = null, s
8898
var path = Path.Combine(directory, file);
8999

90100
var serializer = new SerializerBuilder()
91-
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
101+
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults)
92102
.WithNamingConvention(CamelCaseNamingConvention.Instance)
93103
.Build();
94104

95-
generatorOptions.Variables.ShouldEvaluate = false;
96-
97105
using (var streamWriter = File.CreateText(path))
98106
serializer.Serialize(streamWriter, generatorOptions);
99107

100-
generatorOptions.Variables.ShouldEvaluate = true;
101-
102108
return path;
103109
}
104110

src/EntityFrameworkCore.Generator.Core/GeneratorOptionsFactory.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/EntityFrameworkCore.Generator.Core/IGeneratorOptionsSerializer.cs renamed to src/EntityFrameworkCore.Generator.Core/IConfigurationSerializer.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
1-
using EntityFrameworkCore.Generator.Options;
1+
using System.IO;
22

33
namespace EntityFrameworkCore.Generator;
44

55
/// <summary>
6-
/// <c>interface</c> for serialization and deserialization of <see cref="GeneratorOptions"/> class
6+
/// <c>interface</c> for serialization and deserialization of <see cref="Serialization.GeneratorModel"/> class
77
/// </summary>
8-
public interface IGeneratorOptionsSerializer
8+
public interface IConfigurationSerializer
99
{
1010
/// <summary>
1111
/// Loads the options file using the specified <paramref name="directory"/> and <paramref name="file"/>.
1212
/// </summary>
1313
/// <param name="directory">The directory where the file is located.</param>
1414
/// <param name="file">The name of the options file.</param>
15-
/// <returns>An instance of <see cref="GeneratorOptions"/> if the file exists; otherwise <c>null</c>.</returns>
16-
GeneratorOptions Load(string directory = null, string file = GeneratorOptionsSerializer.OptionsFileName);
15+
/// <returns>An instance of <see cref="Serialization.GeneratorModel"/> if the file exists; otherwise <c>null</c>.</returns>
16+
Serialization.GeneratorModel Load(string directory = null, string file = ConfigurationSerializer.OptionsFileName);
17+
18+
/// <summary>
19+
/// Loads the options using the specified <paramref name="reader" />
20+
/// </summary>
21+
/// <param name="reader">The reader.</param>
22+
/// <returns>
23+
/// An instance of <see cref="Generator" />.
24+
/// </returns>
25+
Serialization.GeneratorModel Load(TextReader reader);
1726

1827
/// <summary>
1928
/// Saves the generator options to the specified <paramref name="directory"/> and <paramref name="file"/>.
@@ -22,13 +31,13 @@ public interface IGeneratorOptionsSerializer
2231
/// <param name="directory">The directory where the file is located.</param>
2332
/// <param name="file">The name of the options file.</param>
2433
/// <returns>The full path of the options file.</returns>
25-
string Save(GeneratorOptions generatorOptions, string directory = null, string file = GeneratorOptionsSerializer.OptionsFileName);
34+
string Save(Serialization.GeneratorModel generatorOptions, string directory = null, string file = ConfigurationSerializer.OptionsFileName);
2635

2736
/// <summary>
2837
/// Determines if the specified options file exists.
2938
/// </summary>
3039
/// <param name="directory">The directory where the file is located.</param>
3140
/// <param name="file">The name of the options file.</param>
3241
/// <returns><c>true</c> if options file exits; otherwise <c>false</c>.</returns>
33-
bool Exists(string directory = null, string file = GeneratorOptionsSerializer.OptionsFileName);
34-
}
42+
bool Exists(string directory = null, string file = ConfigurationSerializer.OptionsFileName);
43+
}

src/EntityFrameworkCore.Generator.Core/ModelGenerator.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,16 @@ private void CreateProperties(Entity entity, IEnumerable<DatabaseColumn> columns
195195
entity.Properties.Add(property);
196196
}
197197

198-
string propertyName = ToPropertyName(entity.EntityClass, column.Name);
198+
string name = ToPropertyName(entity.EntityClass, column.Name);
199+
string propertyName = name;
200+
201+
foreach (var selection in _options.Data.Entity.Renaming.Properties.Where(p => p.Expression.HasValue()))
202+
propertyName = Regex.Replace(propertyName, selection.Expression, string.Empty);
203+
204+
// make sure regex doesn't remove everything
205+
if (propertyName.IsNullOrEmpty())
206+
propertyName = name;
207+
199208
propertyName = _namer.UniqueName(entity.EntityClass, propertyName);
200209

201210
property.PropertyName = propertyName;
@@ -590,7 +599,12 @@ private string EntityName(string name)
590599
else if (tableNaming != TableNaming.Singular && entityNaming == EntityNaming.Singular)
591600
name = name.Singularize(false);
592601

593-
return name;
602+
var rename = name;
603+
foreach (var selection in _options.Data.Entity.Renaming.Entities.Where(p => p.Expression.HasValue()))
604+
rename = Regex.Replace(rename, selection.Expression, string.Empty);
605+
606+
// make sure regex doesn't remove everything
607+
return rename.HasValue() ? rename : name;
594608
}
595609

596610

0 commit comments

Comments
 (0)