|
6 | 6 |
|
7 | 7 | namespace DbReflector.Core
|
8 | 8 | {
|
9 |
| - class Orchestrator |
| 9 | + using System; |
| 10 | + using System.Collections.Generic; |
| 11 | + using CodeGenerationRoslynTest.Generators; |
| 12 | + using Microsoft.Extensions.Hosting; |
| 13 | + using Microsoft.Extensions.DependencyInjection; |
| 14 | + using DbReflector.Databases; |
| 15 | + |
| 16 | + namespace DbReflector.CLI |
10 | 17 | {
|
| 18 | + public class Orchestrator |
| 19 | + { |
| 20 | + static CommandLineConfiguration Configuration = new CommandLineConfiguration(); |
| 21 | + |
| 22 | + private readonly IPostgresMetadataMapper _postgresMetadataMapper; |
| 23 | + private readonly ISqlServerMetadataMapper _sqlServerMetadataMapper; |
| 24 | + private readonly IProjectLoader _projectLoader; |
| 25 | + private readonly IGenerator _entityGenerator; |
| 26 | + private readonly IGenerator _mapperGenerator; |
| 27 | + |
| 28 | + public Program(ISqlServerMetadataMapper sqlServerMapper, IPostgresMetadataMapper postgresMapper, IEnumerable<IGenerator> generators, IProjectLoader projectLoader) |
| 29 | + { |
| 30 | + _sqlServerMetadataMapper = sqlServerMapper; |
| 31 | + _postgresMetadataMapper = postgresMapper; |
| 32 | + _projectLoader = projectLoader; |
| 33 | + |
| 34 | + foreach (var generator in generators) |
| 35 | + { |
| 36 | + if (generator.GetType() == typeof(EntityGenerator)) |
| 37 | + { |
| 38 | + _entityGenerator = generator; |
| 39 | + } |
| 40 | + else if (generator.GetType() == typeof(RepoDbMapperGenerator)) |
| 41 | + { |
| 42 | + _mapperGenerator = generator; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static void InitConfig() |
| 48 | + { |
| 49 | + //Postgres Config |
| 50 | + Configuration = new CommandLineConfiguration |
| 51 | + { |
| 52 | + //CSharpProjectFilePath = "Your csproj file root here.", |
| 53 | + CSharpProjectFilePath = "C:/Users/cjime/Desktop/Professional Projects/LaCarte/LaCarteAPI/NewSolution/RestaurantAdmin/LaCarte.RestaurantAdmin.DataAccess/LaCarte.RestaurantAdmin.DataAccess.csproj", |
| 54 | + GenerateRepoDbMapper = true, |
| 55 | + DatabaseName = "restaurant-admin", |
| 56 | + ConnectionString = "User ID=postgres;Password=123456;Server=localhost;Port=5432;Database=restaurant-admin;", |
| 57 | + DatabaseEngine = SupportedDatabases.Postgres, |
| 58 | + ForceRecreate = true, |
| 59 | + TablesToIgnore = new List<string>() { "VersionInfo" } //Default migrations table for Fluent Migrator. |
| 60 | + }; |
| 61 | + |
| 62 | + //SQL Server Config |
| 63 | + /*Configuration = new CommandLineConfiguration |
| 64 | + { |
| 65 | + //CSharpProjectFilePath = "Your csproj file root here.", |
| 66 | + CSharpProjectFilePath = "C:/Users/cjime/Desktop/Professional Projects/SqlServerSample/SqlServerSample.csproj", |
| 67 | + GenerateRepoDbMapper = true, |
| 68 | + DatabaseName = "Northwind", |
| 69 | + ConnectionString = "Server=localhost;Database=Northwind;Trusted_Connection=True;", |
| 70 | + //ConnectionString = "User ID=postgres;Password=123456;Server=localhost;Port=5432;Database=trainme-dev;", |
| 71 | + //SupportedDatabase = SupportedDatabases.Postgres, |
| 72 | + DatabaseEngine = SupportedDatabases.SqlServer, |
| 73 | + ForceRecreate = true, |
| 74 | + TablesToIgnore = new List<string>() { "migrations" } //Default migrations table for Fluent Migrator. |
| 75 | + };*/ |
| 76 | + } |
| 77 | + |
| 78 | + static void Main(string[] args) |
| 79 | + { |
| 80 | + var host = CreateHostBuilder(args).Build(); |
| 81 | + InitConfig(); |
| 82 | + host.Services.GetRequiredService<Program>().Run(); |
| 83 | + } |
| 84 | + |
| 85 | + void Run() |
| 86 | + { |
| 87 | + try |
| 88 | + { |
| 89 | + var databaseMetadata = new Database(); |
| 90 | + var projectMetadata = _projectLoader.GetCSharpProjectMetadata(Configuration.CSharpProjectFilePath); |
| 91 | + |
| 92 | + switch (Configuration.DatabaseEngine) |
| 93 | + { |
| 94 | + case SupportedDatabases.Postgres: |
| 95 | + databaseMetadata = _postgresMetadataMapper.CreateGeneratorModel(Configuration.ConnectionString, Configuration.DatabaseName, Configuration.TablesToIgnore); |
| 96 | + break; |
| 97 | + case SupportedDatabases.SqlServer: |
| 98 | + databaseMetadata = _sqlServerMetadataMapper.CreateGeneratorModel(Configuration.ConnectionString, Configuration.DatabaseName, Configuration.TablesToIgnore); |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + //Entities must always be generated. |
| 103 | + _entityGenerator.Generate(Configuration, projectMetadata, databaseMetadata); |
| 104 | + |
| 105 | + if (Configuration.GenerateRepoDbMapper) |
| 106 | + _mapperGenerator.Generate(Configuration, projectMetadata, databaseMetadata); |
| 107 | + } |
| 108 | + catch (CodeGenerationException codeGenException) |
| 109 | + { |
| 110 | + Console.Out.WriteLine($"Code generation failed. Exception Message: {codeGenException.Message}"); |
| 111 | + } |
| 112 | + catch (ProjectLoadException projectException) |
| 113 | + { |
| 114 | + Console.Out.WriteLine($"Project loading failed. Exception Message: {projectException.Message}"); |
| 115 | + } |
| 116 | + catch (DatabaseScanningException databaseException) |
| 117 | + { |
| 118 | + Console.Out.WriteLine($"Database metadata failed to load. Exception Message: {databaseException.Message}"); |
| 119 | + } |
| 120 | + catch (Exception e) |
| 121 | + { |
| 122 | + Console.Out.WriteLine($"An unknown error has occurred. Exception Message: {e.Message}"); |
| 123 | + } |
| 124 | + |
| 125 | + Console.Out.WriteLine("Finished process."); |
| 126 | + } |
| 127 | + |
| 128 | + private static IHostBuilder CreateHostBuilder(string[] args) |
| 129 | + { |
| 130 | + return Host.CreateDefaultBuilder(args).ConfigureServices(services => |
| 131 | + { |
| 132 | + services.AddTransient<Program>(); |
| 133 | + services.AddTransient<IDbScanner<PostgresTable, PostgresColumn>>(provider => new PostgresDatabaseScanner(Configuration.ConnectionString)); |
| 134 | + services.AddTransient<IDbScanner<SqlServerTable, SqlServerColumn>>(provider => new SqlServerDatabaseScanner(Configuration.ConnectionString)); |
| 135 | + services.AddTransient<IGenerator, EntityGenerator>(); |
| 136 | + services.AddTransient<IGenerator, RepoDbMapperGenerator>(); |
| 137 | + services.AddTransient<IPostgresMetadataMapper, PostgresMetadataMapper>(); |
| 138 | + services.AddTransient<ISqlServerMetadataMapper, SqlServerMetadataMapper>(); |
| 139 | + services.AddTransient<IProjectLoader, ProjectLoader>(); |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
11 | 143 | }
|
| 144 | + |
12 | 145 | }
|
0 commit comments