/
CliConfigMapper.cs
413 lines (352 loc) · 17.2 KB
/
CliConfigMapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.Json;
using RevEng.Common.Cli.Configuration;
namespace RevEng.Common.Cli
{
public static class CliConfigMapper
{
public static ReverseEngineerCommandOptions ToOptions(this CliConfig config, string connectionString, DatabaseType databaseType, string projectPath, bool isDacpac, string configPath)
{
if (config is null)
{
throw new ArgumentNullException(nameof(config));
}
if (string.IsNullOrEmpty(connectionString))
{
throw new ArgumentNullException(nameof(connectionString));
}
if (string.IsNullOrEmpty(projectPath))
{
throw new ArgumentNullException(nameof(projectPath));
}
if (string.IsNullOrEmpty(configPath))
{
throw new ArgumentNullException(nameof(configPath));
}
var selectedToBeGenerated = config.CodeGeneration.Type.ToUpperInvariant() switch
{
"DBCONTEXT" => 1,
"ENTITIES" => 2,
_ => 0, // "all"
};
var replacements = config.Replacements ?? new Replacements();
var typeMappings = config.TypeMappings ?? new TypeMappings();
var names = config.Names ?? new Names();
var dbContextDefaultName = names.DbContextName
?? GetDbContextNameSuggestion(connectionString, databaseType);
var options = new ReverseEngineerCommandOptions
{
ConnectionString = connectionString,
DatabaseType = databaseType,
ProjectPath = projectPath,
ModelNamespace = names.ModelNamespace,
ContextNamespace = names.DbContextNamespace,
UseFluentApiOnly = !config.CodeGeneration.UseDataAnnotations,
ContextClassName = dbContextDefaultName,
Tables = BuildObjectList(config),
UseDatabaseNames = config.CodeGeneration.UseDatabaseNames,
UseInflector = config.CodeGeneration.UseInflector,
UseT4 = config.CodeGeneration.UseT4,
T4TemplatePath = config.CodeGeneration.T4TemplatePath != null ? PathHelper.GetAbsPath(config.CodeGeneration.T4TemplatePath, projectPath) : null,
IncludeConnectionString = !isDacpac && config.CodeGeneration.EnableOnConfiguring,
SelectedToBeGenerated = selectedToBeGenerated,
Dacpac = isDacpac ? connectionString : null,
CustomReplacers = GetNamingOptions(configPath),
UseLegacyPluralizer = config.CodeGeneration.UseLegacyInflector,
UncountableWords = replacements.UncountableWords?.ToList(),
UseSpatial = typeMappings.UseSpatial,
UseHierarchyId = typeMappings.UseHierarchyId,
UseNodaTime = typeMappings.UseNodaTime,
UseBoolPropertiesWithoutDefaultSql = config.CodeGeneration.RemoveDefaultSqlFromBoolProperties,
UseNoNavigations = config.CodeGeneration.UseNoNavigationsPreview,
RunCleanup = config.CodeGeneration.SoftDeleteObsoleteFiles,
UseManyToManyEntity = config.CodeGeneration.UseManyToManyEntity,
UseMultipleSprocResultSets = config.CodeGeneration.DiscoverMultipleStoredProcedureResultsetsPreview,
UseLegacyResultSetDiscovery = config.CodeGeneration.UseAlternateStoredProcedureResultsetDiscovery,
PreserveCasingWithRegex = replacements.PreserveCasingWithRegex,
UseDateOnlyTimeOnly = typeMappings.UseDateOnlyTimeOnly,
UseNullableReferences = config.CodeGeneration.UseNullableReferenceTypes,
ProjectRootNamespace = names.RootNamespace,
MergeDacpacs = config.CodeGeneration.MergeDacpacs,
UseDecimalDataAnnotation = config.CodeGeneration.UseDecimalDataAnnotation,
UsePrefixNavigationNaming = config.CodeGeneration.UsePrefixNavigationNaming,
// Not supported:
UseHandleBars = false,
SelectedHandlebarsLanguage = 0, // handlebars support, will not support it
OptionsPath = null, // handlebars support, will not support it
// Not implemented:
UseNoDefaultConstructor = false, // not implemented, will consider if asked for
DefaultDacpacSchema = null, // not implemented, will consider if asked for
UseNoObjectFilter = false, // will always add all objects and use exclusions to filter list (for now)
UseAsyncCalls = true, // not implemented
FilterSchemas = false, // not implemented
Schemas = null, // not implemented
};
if (config.FileLayout is null)
{
return options;
}
options.OutputPath = config.FileLayout.OutputPath;
options.OutputContextPath = config.FileLayout.OutputDbContextPath;
options.UseSchemaFolders = config.FileLayout.UseSchemaFoldersPreview;
options.UseSchemaNamespaces = config.FileLayout.UseSchemaNamespacesPreview;
options.UseDbContextSplitting = config.FileLayout.SplitDbContextPreview;
return options;
}
public static bool TryGetCliConfig(string fullPath, string connectionString, DatabaseType databaseType, List<TableModel> objects, CodeGenerationMode codeGenerationMode, out CliConfig config, out List<string> warnings)
{
if (File.Exists(fullPath))
{
config = JsonSerializer.Deserialize<CliConfig>(File.ReadAllText(fullPath, Encoding.UTF8));
}
else
{
config = new CliConfig
{
Names =
{
DbContextName = GetDbContextNameSuggestion(connectionString, databaseType),
RootNamespace = GetRootNamespaceSuggestion(fullPath),
},
};
if (codeGenerationMode == CodeGenerationMode.EFCore8)
{
config.TypeMappings = new TypeMappings
{
UseDateOnlyTimeOnly = true,
};
}
}
if (config.CodeGeneration.RefreshObjectLists)
{
config.Tables = Add(objects, config.Tables);
config.Views = Add(objects, config.Views);
config.StoredProcedures = Add(objects, config.StoredProcedures);
config.Functions = Add(objects, config.Functions);
}
warnings = ValidateExcludedColumns(config, objects);
#pragma warning disable CA1869 // Cache and reuse 'JsonSerializerOptions' instances
var options = new JsonSerializerOptions { WriteIndented = true };
#pragma warning restore CA1869 // Cache and reuse 'JsonSerializerOptions' instances
File.WriteAllText(fullPath, JsonSerializer.Serialize(config, options), Encoding.UTF8);
return true;
}
public static List<SerializationTableModel> BuildObjectList(CliConfig config)
{
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
var objects = new List<SerializationTableModel>();
ToSerializationModel(config.Tables, objects.AddRange);
ToSerializationModel(config.Views, objects.AddRange);
ToSerializationModel(config.StoredProcedures, objects.AddRange);
ToSerializationModel(config.Functions, objects.AddRange);
return objects;
}
/// <summary>
/// Ensures that any excluded columns for tables are not required. Removes the invalid columns from the list.
/// </summary>
/// <param name="config">Configuration to Validate.</param>
/// <param name="objects">Table Models to check against.</param>
private static List<string> ValidateExcludedColumns(CliConfig config, List<TableModel> objects)
{
List<string> warnings = new();
var objectsToCheck = config.Tables.Where(x => x.ExcludedColumns?.Count > 0)
.Select(table => table as IEntity)
.Union(config.Views.Where(x => x.ExcludedColumns?.Count > 0));
foreach (var table in objectsToCheck)
{
var dbTable = objects.Single(x => x.DisplayName == table.Name);
var columnsThatCannotBeExcluded = dbTable.Columns.Where(x => x.IsForeignKey || x.IsPrimaryKey).Select(x => x.Name);
var badExclusions = columnsThatCannotBeExcluded.Intersect(table.ExcludedColumns);
foreach (var column in badExclusions)
{
var originalColumnString = table.ExcludedColumns.Single(x => string.Equals(x, column, StringComparison.Ordinal));
warnings.Add($"{table.Name}.{originalColumnString} cannot be excluded because it is either a Primary Key or Foreign Key of another Mapped Column. This entry has been removed from the config file.");
table.ExcludedColumns.Remove(originalColumnString);
}
}
return warnings;
}
private static void ToSerializationModel<T>(IEnumerable<T> entities, Action<IEnumerable<SerializationTableModel>> addRange)
where T : IEntity, new()
{
if (entities is null)
{
return;
}
var objectType = DefineObjectType<T>();
var excludeAll = entities.Any(t => t.ExclusionWildcard == "*");
var filters = GetFilters(entities);
var serializationTableModels = entities.Where<T>(entity => ExclusionFilter(entity, excludeAll, filters)
&& !string.IsNullOrEmpty(entity.Name))
.Select(entity => new SerializationTableModel(entity.Name, objectType, entity.ExcludedColumns));
addRange(serializationTableModels);
}
private static List<ExclusionFilter> GetFilters<T>(IEnumerable<T> entities)
where T : IEntity, new()
{
var filters = new List<ExclusionFilter>();
var candidates = entities.Where(t => !string.IsNullOrEmpty(t.ExclusionWildcard)
&& t.ExclusionWildcard != "*"
&& t.ExclusionWildcard.Contains('*')).ToList();
foreach (var candiate in candidates)
{
if (candiate.ExclusionWildcard.StartsWith("*", StringComparison.OrdinalIgnoreCase)
&& candiate.ExclusionWildcard.EndsWith("*", StringComparison.OrdinalIgnoreCase)
&& candiate.ExclusionWildcard.Length > 2)
{
filters.Add(new ExclusionFilter
{
Filter = candiate.ExclusionWildcard.Substring(0, candiate.ExclusionWildcard.Length - 1).Substring(1),
FilterType = ExclusionFilterType.Contains,
});
break;
}
if (candiate.ExclusionWildcard.StartsWith("*", StringComparison.OrdinalIgnoreCase))
{
filters.Add(new ExclusionFilter
{
Filter = candiate.ExclusionWildcard.Substring(1),
FilterType = ExclusionFilterType.EndsWith,
});
break;
}
if (candiate.ExclusionWildcard.EndsWith("*", StringComparison.OrdinalIgnoreCase))
{
filters.Add(new ExclusionFilter
{
Filter = candiate.ExclusionWildcard.Substring(0, candiate.ExclusionWildcard.Length - 1),
FilterType = ExclusionFilterType.StartsWith,
});
break;
}
}
return filters;
}
private static bool ExclusionFilter<T>(T entity, bool excludeAll, List<ExclusionFilter> filters)
where T : IEntity, new()
{
if (excludeAll)
{
return entity.Exclude.HasValue && !entity.Exclude.Value;
}
foreach (var filter in filters)
{
if (entity.Exclude.HasValue && !entity.Exclude.Value)
{
return true;
}
if (filter.FilterType == ExclusionFilterType.StartsWith
&& !string.IsNullOrEmpty(entity.Name)
&& string.IsNullOrEmpty(entity.ExclusionWildcard)
&& entity.Name.StartsWith(filter.Filter, StringComparison.Ordinal))
{
return false;
}
if (filter.FilterType == ExclusionFilterType.EndsWith
&& !string.IsNullOrEmpty(entity.Name)
&& string.IsNullOrEmpty(entity.ExclusionWildcard)
&& entity.Name.EndsWith(filter.Filter, StringComparison.Ordinal))
{
return false;
}
if (filter.FilterType == ExclusionFilterType.Contains
&& !string.IsNullOrEmpty(entity.Name)
&& string.IsNullOrEmpty(entity.ExclusionWildcard)
&& entity.Name.Contains(filter.Filter))
{
return false;
}
}
return !entity.Exclude ?? true;
}
private static List<T> Add<T>(IEnumerable<TableModel> models, List<T> entities)
where T : IEntity, new()
{
var objectType = DefineObjectType<T>();
var newItems = models.Where(o => o.ObjectType == objectType).ToList();
if (newItems.Count == 0)
{
return new List<T>();
}
var result = entities ?? new List<T>();
var newIds = new HashSet<string>(newItems.Select(x => x.DisplayName));
result.RemoveAll(x => !newIds.Contains(x.Name) && string.IsNullOrEmpty(x.ExclusionWildcard));
foreach (var displayName in newItems.Select(t => t.DisplayName))
{
T existing = result.SingleOrDefault(t => t.Name == displayName);
if (existing == null)
{
result.Add(new T { Name = displayName });
}
}
return result;
}
private static ObjectType DefineObjectType<T>()
where T : IEntity, new()
{
var instance = new T();
ObjectType objectType = instance switch
{
View _ => ObjectType.View,
Function _ => ObjectType.ScalarFunction,
StoredProcedure _ => ObjectType.Procedure,
_ => ObjectType.Table,
};
return objectType;
}
private static string GetRootNamespaceSuggestion(string fullPath)
{
var legacyConfig = GetLegacyOptions(fullPath);
if (legacyConfig != null && !string.IsNullOrWhiteSpace(legacyConfig.ProjectRootNamespace))
{
return legacyConfig.ProjectRootNamespace;
}
var dir = Path.GetDirectoryName(fullPath);
if (!string.IsNullOrEmpty(dir))
{
var csprojFiles = Directory.GetFiles(dir, "*.csproj");
if (csprojFiles.Length == 1)
{
return Path.GetFileNameWithoutExtension(csprojFiles[0]);
}
var info = new DirectoryInfo(dir);
return info.Name;
}
return "RootNamespace";
}
private static string GetDbContextNameSuggestion(string connectionString, DatabaseType databaseType)
{
return DbContextNamer.GetDatabaseName(connectionString, databaseType) + "Context";
}
private static List<Schema> GetNamingOptions(string configPath)
{
var path = Path.Combine(Path.GetDirectoryName(configPath), "efpt.renaming.json");
if (!File.Exists(path))
{
return new List<Schema>();
}
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(path, Encoding.UTF8)));
var ser = new DataContractJsonSerializer(typeof(List<Schema>));
return ser.ReadObject(ms) as List<Schema>;
}
private static ReverseEngineerCommandOptions GetLegacyOptions(string configPath)
{
var path = Path.Combine(Path.GetDirectoryName(configPath), "efpt.config.json");
if (!File.Exists(path))
{
return null;
}
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(File.ReadAllText(path, Encoding.UTF8)));
var ser = new DataContractJsonSerializer(typeof(ReverseEngineerCommandOptions));
return ser.ReadObject(ms) as ReverseEngineerCommandOptions;
}
}
}