Skip to content

Commit 7bc1c43

Browse files
author
Carlos José jiménez bermúdez
committed
Reorganize code for usage of the right variables.
1 parent a029db6 commit 7bc1c43

File tree

6 files changed

+65
-54
lines changed

6 files changed

+65
-54
lines changed

Diff for: DbReflector.CLI/Program.cs

+26-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
using DbReflector.Core.DI;
1212
using DbReflector.Core;
1313
using Microsoft.Extensions.DependencyInjection;
14+
using DbReflector.Databases.Exceptions;
15+
using DbReflector.Core.Exceptions;
16+
using DbReflector.CodeGeneration.Exceptions;
1417

1518
namespace DbReflector.CLI
1619
{
@@ -81,7 +84,28 @@ void Reflect(string dbName, string projectPath, string connectionString, string
8184
DatabaseEngine = databaseEngine
8285
};
8386

84-
_orchestrator.Reflect(commandModel);
87+
try
88+
{
89+
_orchestrator.Reflect(commandModel);
90+
}
91+
catch (CodeGenerationException codeGenException)
92+
{
93+
Console.Out.WriteLine($"Code generation failed. Exception Message: {codeGenException.Message}");
94+
}
95+
catch (ProjectLoadException projectException)
96+
{
97+
Console.Out.WriteLine($"Project loading failed. Exception Message: {projectException.Message}");
98+
}
99+
catch (DatabaseScanningException databaseException)
100+
{
101+
Console.Out.WriteLine($"Database metadata failed to load. Exception Message: {databaseException.Message}");
102+
}
103+
catch (Exception e)
104+
{
105+
Console.Out.WriteLine($"An unknown error has occurred. Exception Message: {e.Message}");
106+
}
107+
108+
Console.Out.WriteLine("Finished process.");
85109
}
86110

87111
void Scan(string dbName, string connectionString, string outputFolder, string dbEngine, bool forceOverride, IConsole console)
@@ -94,7 +118,7 @@ void Scan(string dbName, string connectionString, string outputFolder, string db
94118
ConnectionString = connectionString,
95119
OutputFolder = outputFolder,
96120
ForceOverride = forceOverride,
97-
DbEngine = databaseEngine
121+
DatabaseEngine = databaseEngine
98122
};
99123

100124
_orchestrator.Scan(commandModel);

Diff for: DbReflector.CodeGeneration/Interfaces/IGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ namespace DbReflector.CodeGeneration.Interfaces
1010
{
1111
public interface IGenerator
1212
{
13-
public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database);
13+
public void Generate(string entitiesFolder, bool force, VSProjectMetadata projectMetadata, Database database);
1414
}
1515
}

Diff for: DbReflector.CodeGeneration/RepoDB/EntityGenerator.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ namespace DbReflector.CodeGeneration.RepoDB
1212
{
1313
public class EntityGenerator : IGenerator
1414
{
15-
public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database)
15+
public void Generate(string entitiesFolder, bool force, VSProjectMetadata projectMetadata, Database database)
1616
{
17-
var entitiesDirectory = new DirectoryInfo($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}");
17+
var entitiesDirectory = new DirectoryInfo($"{projectMetadata.BasePath}/{entitiesFolder}");
1818
var shouldGenerate = false;
1919

2020
if(entitiesDirectory.Exists)
2121
{
2222
//Entities have already been created or files exist in the target folder.
2323
if(DirectoryHasFiles(entitiesDirectory.FullName))
2424
{
25-
if(!cliConfig.ForceRecreate)
25+
if(!force)
2626
{
2727
throw new CodeGenerationException("To be able to run the generation routine when you have previously generated code, you must set ForceRecreate to true or your target directories must be empty.");
2828
}
@@ -41,16 +41,16 @@ public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata proje
4141
}
4242
else
4343
{
44-
Directory.CreateDirectory($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}");
44+
Directory.CreateDirectory($"{projectMetadata.BasePath}/{entitiesFolder}");
4545
shouldGenerate = true;
4646
}
4747

4848
if(shouldGenerate)
4949
{
5050
foreach (var tableMeta in database.Tables)
5151
{
52-
var entityCode = GenerateEntity(cliConfig.EntitiesFolder, tableMeta, projectMetadata);
53-
WriteEntityFileToDisk($"{projectMetadata.BasePath}/{cliConfig.EntitiesFolder}/{tableMeta.FormattedTableName}.cs", entityCode);
52+
var entityCode = GenerateEntity(entitiesFolder, tableMeta, projectMetadata);
53+
WriteEntityFileToDisk($"{projectMetadata.BasePath}/{entitiesFolder}/{tableMeta.FormattedTableName}.cs", entityCode);
5454
}
5555
}
5656
}

Diff for: DbReflector.CodeGeneration/RepoDB/RepoDbMapperGenerator.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace CodeGenerationRoslynTest.Generators
1313
{
1414
public class RepoDbMapperGenerator : IGenerator
1515
{
16-
public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata projectMetadata, Database database)
16+
public void Generate(string entitiesFolder, bool force, VSProjectMetadata projectMetadata, Database database)
1717
{
1818
var mapperStringBuilder = new StringBuilder();
1919

@@ -77,7 +77,7 @@ public void Generate(CommandLineConfiguration cliConfig, VSProjectMetadata proje
7777

7878
if(File.Exists(filePath))
7979
{
80-
if(cliConfig.ForceRecreate)
80+
if(force)
8181
{
8282
WriteCodeFileToDisk(filePath, mapperStringBuilder.ToString());
8383
}
@@ -173,4 +173,4 @@ private string UsingStatements(string defaultNamespace)
173173
}
174174
}*/
175175
}
176-
}
176+
}

Diff for: DbReflector.Core/Orchestrator.cs

+27-36
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
using DbReflector.CodeGeneration.Exceptions;
1414
using DbReflector.Core.Exceptions;
1515
using DbReflector.Databases.Exceptions;
16-
using DbReflector.Databases.Models;
17-
using DbReflector.Core.DI;
1816
using DbReflector.Common.CommandModels;
1917

2018
namespace DbReflector.Core
@@ -46,55 +44,48 @@ public Orchestrator(ISqlServerMetadataMapper sqlServerMapper, IPostgresMetadataM
4644
}
4745
}
4846

49-
public void Execute(CommandLineConfiguration config)
47+
public void Reflect(ReflectCommandModel command)
48+
{
49+
var databaseMetadata = new Database();
50+
var projectMetadata = _projectLoader.GetCSharpProjectMetadata(command.CSharpProjectFilePath);
51+
52+
switch (command.DatabaseEngine)
53+
{
54+
case SupportedDatabases.Postgres:
55+
databaseMetadata = _postgresMetadataMapper.CreateGeneratorModel(command.ConnectionString, command.DatabaseName, command.TablesToIgnore);
56+
break;
57+
case SupportedDatabases.SqlServer:
58+
databaseMetadata = _sqlServerMetadataMapper.CreateGeneratorModel(command.ConnectionString, command.DatabaseName, command.TablesToIgnore);
59+
break;
60+
}
61+
62+
//Entities must always be generated.
63+
_entityGenerator.Generate(command.EntitiesFolder, command.ForceRecreate, projectMetadata, databaseMetadata);
64+
65+
_mapperGenerator.Generate(command.EntitiesFolder, command.ForceRecreate, projectMetadata, databaseMetadata);
66+
67+
}
68+
69+
public void Scan(ScanCommandModel command)
5070
{
5171
try
5272
{
5373
var databaseMetadata = new Database();
54-
var projectMetadata = _projectLoader.GetCSharpProjectMetadata(config.CSharpProjectFilePath);
5574

56-
switch (config.DatabaseEngine)
75+
switch (command.DatabaseEngine)
5776
{
5877
case SupportedDatabases.Postgres:
59-
databaseMetadata = _postgresMetadataMapper.CreateGeneratorModel(config.ConnectionString, config.DatabaseName, config.TablesToIgnore);
78+
databaseMetadata = _postgresMetadataMapper.CreateGeneratorModel(command.ConnectionString, command.DatabaseName, command.TablesToIgnore);
6079
break;
6180
case SupportedDatabases.SqlServer:
62-
databaseMetadata = _sqlServerMetadataMapper.CreateGeneratorModel(config.ConnectionString, config.DatabaseName, config.TablesToIgnore);
81+
databaseMetadata = _sqlServerMetadataMapper.CreateGeneratorModel(command.ConnectionString, command.DatabaseName, command.TablesToIgnore);
6382
break;
6483
}
65-
66-
//Entities must always be generated.
67-
_entityGenerator.Generate(config, projectMetadata, databaseMetadata);
68-
69-
if (config.GenerateRepoDbMapper)
70-
_mapperGenerator.Generate(config, projectMetadata, databaseMetadata);
71-
}
72-
catch (CodeGenerationException codeGenException)
73-
{
74-
Console.Out.WriteLine($"Code generation failed. Exception Message: {codeGenException.Message}");
75-
}
76-
catch (ProjectLoadException projectException)
77-
{
78-
Console.Out.WriteLine($"Project loading failed. Exception Message: {projectException.Message}");
79-
}
80-
catch (DatabaseScanningException databaseException)
81-
{
82-
Console.Out.WriteLine($"Database metadata failed to load. Exception Message: {databaseException.Message}");
8384
}
8485
catch (Exception e)
8586
{
86-
Console.Out.WriteLine($"An unknown error has occurred. Exception Message: {e.Message}");
87-
}
88-
89-
Console.Out.WriteLine("Finished process.");
90-
}
91-
92-
public void Reflect(ReflectCommandModel reflectCommand)
93-
{
94-
}
9587

96-
public void Scan(ScanCommandModel scanCommand)
97-
{
88+
}
9889
}
9990
}
10091
}

Diff for: DbReflector.Utils/CommandModels/ScanCommandModel.cs

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ namespace DbReflector.Common.CommandModels
99
public class ScanCommandModel
1010
{
1111
public string DatabaseName { get; set; }
12-
1312
public string ConnectionString { get; set; }
14-
1513
public string OutputFolder { get; set; }
16-
17-
public SupportedDatabases DbEngine { get; set; }
18-
14+
public SupportedDatabases DatabaseEngine { get; set; }
1915
public bool ForceOverride { get; set; }
20-
16+
public List<string> TablesToIgnore { get; set; } = new List<string>();
2117
}
2218
}

0 commit comments

Comments
 (0)