| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder.Design; |
| | | 2 | | |
| | | 3 | | /// What every command works on: the context, its seed configuration, the app's services and the console. One per <c>Run |
| | | 4 | | internal sealed class DesignSession |
| | | 5 | | { |
| | | 6 | | private readonly IServiceProvider _services; |
| | | 7 | | |
| | | 8 | | |
| | 411 | 9 | | public DesignSession( DbContext dbContext, SeedConfiguration configuration, IServiceProvider services, IAnsiConsole |
| | | 10 | | { |
| | | 11 | | DbContext = dbContext; |
| | | 12 | | Configuration = configuration; |
| | 411 | 13 | | _services = services; |
| | | 14 | | Console = console; |
| | 411 | 15 | | } |
| | | 16 | | |
| | | 17 | | |
| | | 18 | | public DbContext DbContext { get; } |
| | | 19 | | |
| | | 20 | | public SeedConfiguration Configuration { get; } |
| | | 21 | | |
| | | 22 | | public IAnsiConsole Console { get; } |
| | | 23 | | |
| | | 24 | | /// The context's database provider (as registered with <c>UseSqlServer()</c> …). |
| | 186 | 25 | | public ISeedDatabaseProvider Provider => Configuration.CreateProvider( _services ); |
| | | 26 | | |
| | | 27 | | |
| | 411 | 28 | | public SeedDesigner Designer( DesignSettings settings ) => new( DbContext, Configuration, settings.Directory ?? Envi |
| | | 29 | | |
| | | 30 | | /// The app host's environment name (IHostEnvironment.EnvironmentName), <see langword="null"/> without a host. Found |
| | | 31 | | /// doesn't depend on Hosting, and an app with a host has its abstractions loaded. |
| | | 32 | | public string? HostEnvironmentName() |
| | | 33 | | { |
| | 24 | 34 | | var contract = AppDomain.CurrentDomain.GetAssemblies() |
| | 24 | 35 | | .Select( a => a.GetType( "Microsoft.Extensions.Hosting.IHostEnvironment", throwOnError: |
| | 24 | 36 | | .FirstOrDefault( t => t is not null ); |
| | | 37 | | |
| | 24 | 38 | | return contract is not null && _services.GetService( contract ) is { } environment ? contract.GetProperty( "Envi |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// Prints messages, warnings (yellow) and errors (red); returns the exit code. |
| | | 42 | | public int Report( DesignResult result ) |
| | | 43 | | { |
| | 2160 | 44 | | foreach ( var message in result.Messages ) { Console.WriteLine( message ); } |
| | | 45 | | |
| | 495 | 46 | | foreach ( var warning in result.Warnings ) { Console.MarkupLine( $"[yellow]warning:[/] {Markup.Escape( warning ) |
| | | 47 | | |
| | 576 | 48 | | foreach ( var error in result.Errors ) { Console.MarkupLine( $"[red]error:[/] {Markup.Escape( error )}" ); } |
| | | 49 | | |
| | | 50 | | |
| | 243 | 51 | | return result.ExitCode; |
| | | 52 | | } |
| | | 53 | | } |