-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
440 lines (384 loc) · 16.6 KB
/
Copy pathProgram.cs
File metadata and controls
440 lines (384 loc) · 16.6 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using CommandLine;
using MySqlConnector;
using Npgsql;
using SqExpress.CodeGen.Shared;
using SqExpress.CodeGenUtil.Ef;
using SqExpress.CodeGenUtil.Logger;
using SqExpress.DbMetadata.Internal.DbManagers;
using SqExpress.DbMetadata.Internal.DbManagers.MsSql;
using SqExpress.DbMetadata.Internal.DbManagers.MySql;
using SqExpress.DbMetadata.Internal.DbManagers.PgSql;
using SqExpress.DbMetadata.Internal.Model;
namespace SqExpress.CodeGenUtil;
public class Program
{
public static int Main(string[] args)
{
try
{
var parser = new Parser(with =>
{
with.CaseInsensitiveEnumValues = true;
with.CaseSensitive = false;
with.AutoHelp = true;
with.AutoVersion = true;
with.HelpWriter = Console.Error;
});
return parser.ParseArguments<GenTablesOptions, GenModelsOptions>(args)
.MapResult(
(GenTablesOptions opts) => Run(opts, RunGenTablesOptions, GetOperationName(opts)),
(GenModelsOptions opts) => Run(opts, RunGenModelsOptions, "model generation"),
errs => 1);
}
catch (Exception e)
{
Console.Error.WriteLine("Command line parser exception: ");
Console.Error.WriteLine(e);
return 1;
}
}
private static int Run<TOpts>(TOpts opts, Func<TOpts,Task> task, string operationName)
{
try
{
task(opts).GetAwaiter().GetResult();
return 0;
}
catch (SqExpressCodeGenException e)
{
Console.Error.WriteLine($"SqExpress {operationName} failed: {e.Message}");
return 1;
}
catch (Exception e)
{
Console.Error.WriteLine("Unhandled Exception: ");
Console.Error.WriteLine(e);
return 1;
}
}
private static string GetOperationName(GenTablesOptions options)
=> options.ConnectionType == ConnectionType.Ef
? "EF table generation"
: $"{options.ConnectionType} table generation";
public static async Task RunGenTablesOptions(GenTablesOptions options)
{
if (options.ConnectionType == ConnectionType.Ef && options.IncludeViews)
{
throw new SqExpressCodeGenException("--include-views is not supported in EF mode.");
}
ILogger logger = new DefaultLogger(Console.Out, options.Verbosity);
logger.LogMinimal("Table proxy classes generation is running...");
string directory = EnsureDirectory(options.OutputDir, logger, "Output", true);
if (string.IsNullOrWhiteSpace(options.Source))
{
throw new SqExpressCodeGenException(
options.ConnectionType == ConnectionType.Ef
? "EF project path cannot be empty"
: "Connection string cannot be empty");
}
var existingCode = (IReadOnlyDictionary<TableRef, Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax>)
new Dictionary<TableRef, Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax>();
if (!options.UseTableDeclarationAttributes)
{
logger.LogNormal("Checking existing code...");
existingCode = CodeGenLegacySqModelSupport.FindTableDescriptors(directory, DefaultFileSystem.Instance);
if(logger.IsNormalOrHigher) logger.LogNormal(existingCode.Count > 0
? $"Found {existingCode.Count} already existing table descriptor classes."
: "No table descriptor classes found.");
}
IReadOnlyList<TableModel> tables;
if (options.ConnectionType == ConnectionType.Ef)
{
logger.LogNormal("Reading EF model metadata...");
var efProjectPath = ValidateEfProjectPath(options.Source);
var metadata = await EfMetadataExtractorRunner.Extract(
efProjectPath,
options.DbContext,
string.IsNullOrWhiteSpace(options.Framework) ? null : options.Framework);
tables = EfMetadataTableReader.SelectTables(metadata, options.TableClassPrefix, options.SkipUnknownColumnTypes);
}
else
{
using var sqlManager = CreateDbManager(options);
logger.LogNormal("Connecting to database...");
var connectionTest = await sqlManager.TryOpenConnection();
if (!string.IsNullOrEmpty(connectionTest))
{
throw new SqExpressCodeGenException(connectionTest);
}
logger.LogNormal("Success!");
tables = await sqlManager.SelectTables(options.SkipUnknownColumnTypes, options.IncludeViews);
}
tables = TableFilter.Apply(tables, options.Include, options.Exclude);
if(logger.IsNormalOrHigher)
{
logger.LogNormal(tables.Count > 0
? $"Found {tables.Count} tables."
: "No tables found in the database.");
if (logger.IsDetailed)
{
foreach (var tableModel in tables)
{
Console.WriteLine($"{tableModel.DbName} ({tableModel.Name})");
foreach (var tableModelColumn in tableModel.Columns)
{
Console.WriteLine($"- {tableModelColumn.DbName.Name} {tableModelColumn.ColumnType.GetType().Name}{(tableModelColumn.Pk.HasValue ? " (PK)":null)}{(tableModelColumn.Fk != null ? $" (FK: {string.Join(';', tableModelColumn.Fk.Select(f=>f.ToString()))})" : null)}");
}
}
}
}
logger.LogNormal("Code generation...");
IReadOnlyDictionary<TableRef, TableModel> tableMap = tables.ToDictionary(t => t.DbName);
var layout = TableGenerationLayout.Create(
tables,
directory,
options.Namespace,
options.SplitTablesBySchema);
var tableNamespaces = layout.Entries.ToDictionary(static pair => pair.Key, static pair => pair.Value.Namespace);
foreach (var table in tables)
{
var layoutEntry = layout.Entries[table.DbName];
string filePath = layoutEntry.FilePath;
Directory.CreateDirectory(Path.GetDirectoryName(filePath)!);
if(logger.IsDetailed) logger.LogDetailed($"{table.DbName} to \"{filePath}\".");
string text;
bool existing;
if (options.UseTableDeclarationAttributes)
{
text = CodeGenTableDescriptorSupport.GenerateTableDeclaration(
table,
tableMap,
layoutEntry.Namespace,
options.SkipUnknownColumnTypes,
filePath,
DefaultFileSystem.Instance,
out existing,
tableNamespaces).ToFullString();
}
else
{
text = CodeGenTableDescriptorSupport.GenerateTableDescriptor(
table,
tableMap,
layoutEntry.Namespace,
existingCode,
options.SkipUnknownColumnTypes,
out existing,
tableNamespaces).ToFullString();
}
await WriteAllTextIfChangedAsync(filePath, text);
if (logger.IsDetailed) logger.LogDetailed(existing ? "Existing file updated." : "New file created.");
}
var allTablePath = Path.Combine(directory, "AllTables.cs");
if (logger.IsDetailed) logger.LogDetailed($"AllTables to \"{allTablePath}\".");
await WriteAllTextIfChangedAsync(
allTablePath,
CodeGenAllTablesSupport.Generate(
allTablePath,
tables,
options.Namespace,
options.TableClassPrefix,
DefaultFileSystem.Instance,
options.SplitTablesBySchema ? tableNamespaces : null,
options.SplitTablesBySchema
? layout.Entries.ToDictionary(static pair => pair.Key, static pair => pair.Value.SchemaSegment!)
: null).ToFullString());
if (options.CleanOutput)
{
var removedFiles = TableOutputCleaner.Clean(directory, layout.Entries);
foreach (var removedFile in removedFiles)
{
logger.LogNormal($"Removed obsolete table descriptor file \"{removedFile}\".");
}
}
logger.LogMinimal("Table proxy classes generation successfully completed!");
}
private static async Task RunGenModelsOptions(GenModelsOptions options)
{
ILogger logger = new DefaultLogger(Console.Out, options.Verbosity);
logger.LogMinimal("Model classes generation is running...");
string inDirectory = EnsureDirectory(options.InputDir, logger, "Input", false);
string outDirectory = EnsureDirectory(options.OutputDir, logger, "Output", true);
var analysis = CodeGenLegacySqModelSupport.AnalyzeLegacySqModels(inDirectory, DefaultFileSystem.Instance, options.NullRefTypes);
if (analysis.Count < 1)
{
logger.LogNormal("No model attributes detected in the input directory.");
}
else
{
logger.LogNormal($"Found {analysis.Count} models in the input directory.");
}
if (logger.IsDetailed)
{
foreach (var model in analysis)
{
logger.LogDetailed(model.Name);
foreach (var property in model.Properties)
{
logger.LogDetailed(
$" -{property.Type} {property.Name}");
foreach (var col in property.Column)
{
logger.LogDetailed(
$" ={(property.CastType != null ? $"({property.CastType})" : null)}{col.TableRef.TableTypeName}.{col.ColumnName}");
}
}
}
}
logger.LogNormal("Code generation...");
foreach (var meta in analysis)
{
string path = Path.Combine(outDirectory, $"{meta.Name}.cs");
if (logger.IsDetailed) logger.LogDetailed(path);
await WriteAllTextIfChangedAsync(
path,
CodeGenModelSupport.Generate(
meta,
options.Namespace,
path,
options.RwClasses,
options.NullRefTypes,
options.ModelType == ModelType.Record ? CodeGenModelType.Record : CodeGenModelType.ImmutableClass,
DefaultFileSystem.Instance,
out var existing).ToFullString());
if (logger.IsDetailed) logger.LogDetailed(existing ? "Existing file updated." : "New file created.");
}
if (options.CleanOutput)
{
var modelFiles = analysis.Select(meta => $"{meta.Name}.cs").ToHashSet(StringComparer.InvariantCultureIgnoreCase);
var toRemove = Directory.EnumerateFiles(outDirectory).Where(p=> !modelFiles.Contains(Path.GetFileName(p))).ToList();
foreach (var delPath in toRemove)
{
File.Delete(delPath);
if(logger.IsNormalOrHigher) logger.LogNormal($"File {Path.GetFileName(delPath)} has been removed since it does not contain any model class");
}
}
logger.LogMinimal("Model classes generation successfully completed!");
}
private static DbManager CreateDbManager(GenTablesOptions options)
{
DbConnection connection;
switch (options.ConnectionType)
{
case ConnectionType.MsSql:
try
{
connection = new SqlConnection(options.Source);
}
catch (ArgumentException e)
{
throw new SqExpressCodeGenException($"MsSQL connection string has incorrect format \"{options.Source}\"", e);
}
if (string.IsNullOrEmpty(connection.Database))
{
throw new SqExpressCodeGenException("MsSQL connection string has to contain \"database\" attribute");
}
return MsSqlDbStrategy.Create(new DbManagerOptions(options.TableClassPrefix), connection);
case ConnectionType.MySql:
try
{
connection = new MySqlConnection(options.Source);
}
catch (ArgumentException e)
{
throw new SqExpressCodeGenException($"MySQL connection string has incorrect format \"{options.Source}\"", e);
}
if (string.IsNullOrEmpty(connection.Database))
{
throw new SqExpressCodeGenException("MySQL connection string has to contain \"database\" attribute");
}
return MySqlDbStrategy.Create(new DbManagerOptions(options.TableClassPrefix), connection);
case ConnectionType.PgSql:
try
{
connection = new NpgsqlConnection(options.Source);
}
catch (ArgumentException e)
{
throw new SqExpressCodeGenException($"PgSQL connection string has incorrect format \"{options.Source}\"", e);
}
if (string.IsNullOrEmpty(connection.Database))
{
throw new SqExpressCodeGenException("PgSQL connection string has to contain \"database\" attribute");
}
return PgSqlDbStrategy.Create(new DbManagerOptions(options.TableClassPrefix), connection);
default:
throw new SqExpressCodeGenException("Unknown connection type: " + options.ConnectionType);
}
}
private static string ValidateEfProjectPath(string source)
{
string fullPath;
try
{
fullPath = Path.GetFullPath(source, Directory.GetCurrentDirectory());
}
catch (Exception e) when (e is ArgumentException || e is NotSupportedException || e is PathTooLongException)
{
throw new SqExpressCodeGenException($"EF project path \"{source}\" has invalid format.", e);
}
if (!string.Equals(Path.GetExtension(fullPath), ".csproj", StringComparison.OrdinalIgnoreCase))
{
throw new SqExpressCodeGenException(
$"EF table generation expects a .csproj path. Received \"{source}\". Pass the EF project file so SqExpress can run metadata extraction in the project's target framework.");
}
if (!File.Exists(fullPath))
{
throw new SqExpressCodeGenException($"Could not find EF project \"{source}\".");
}
return fullPath;
}
private static async Task WriteAllTextIfChangedAsync(string path, string text)
{
if (File.Exists(path))
{
var existing = await File.ReadAllTextAsync(path);
if (string.Equals(existing, text, StringComparison.Ordinal))
{
return;
}
}
await File.WriteAllTextAsync(path, text);
}
private static string EnsureDirectory(string directory, ILogger logger, string dirAlias, bool create)
{
if (string.IsNullOrEmpty(directory))
{
directory = Directory.GetCurrentDirectory();
logger.LogDetailed(
$"{dirAlias} directory was not specified, so the current directory \"{directory}\" is used as an output one.");
}
else if (!Path.IsPathFullyQualified(directory))
{
directory = Path.GetFullPath(directory, Directory.GetCurrentDirectory());
logger.LogDetailed($"{dirAlias} directory is converted to fully qualified \"{directory}\".");
}
if (!Directory.Exists(directory))
{
if (create)
{
try
{
Directory.CreateDirectory(directory);
logger.LogDetailed($"Directory \"{directory}\" was created.");
}
catch (Exception e)
{
throw new SqExpressCodeGenException($"Could not create directory: \"{directory}\".", e);
}
}
else
{
throw new SqExpressCodeGenException($"\"{directory}\" directory does not exist.");
}
}
return directory;
}
}