Skip to content

Commit

Permalink
fix: Refactor Program.cs Main structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jejikeh committed Feb 21, 2024
1 parent ae385ce commit c1218ec
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 93 deletions.
111 changes: 111 additions & 0 deletions UEScript.CLI/Configurations/ProgramConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.CommandLine;
using System.CommandLine.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using UEScript.CLI.Commands;
using UEScript.CLI.Services;
using UEScript.CLI.Services.Impl;
using UEScript.Logging;

namespace UEScript.CLI.Configurations;

public static class ProgramConfiguration
{
public static CliConfiguration Configure(this CliConfiguration cliConfiguration, string[] args)
{
cliConfiguration.UseHost(
_ => Host.CreateDefaultBuilder().UseContentRoot(AppDomain.CurrentDomain.BaseDirectory),
host => host.ConfigureHostBuilder(cliConfiguration, args));

return cliConfiguration;
}

public static CliConfiguration BuildCommandLine()
{
return new CliConfiguration(ConfigureRootCommand.AddRootCommand());
}

public static IHostBuilder ConfigureHostBuilder(this IHostBuilder host, CliConfiguration cliConfiguration, string[] args)
{
ConfigureAppConfiguration(host);

host.ConfigureLogging(builder =>
{
builder.ClearProviders();
builder.AddUEScriptLogger(ConfigureLogger);
var parseResult = cliConfiguration.Parse(args);
var debugLog = parseResult.GetValue<bool>("--debug-log");
if (debugLog)
{
builder.AddFilter("Microsoft.Hosting", LogLevel.Information);
}
});

ConfigureServices(host);

return host;
}

private static void ConfigureServices(IHostBuilder host)
{
host.ConfigureServices(services =>
{
services.AddSingleton<IUnrealEngineAssociationRepository, UnrealEngineEngineAssociationRepository>();
services.AddSingleton<IUnrealBuildToolService, UnrealBuildToolService>();
services.AddSingleton<IUnrealEngineEditorService, UnrealEngineEditorService>();
});
}

private static void ConfigureLogger(UEScriptLoggerConfiguration configure)
{
// @Cleanup: Check how to do this stuff automatically
var configuration = GetConfiguration();
var logLevelColors = configuration.GetSection("Logging:UEScript:LogLevelColors").Get<Dictionary<LogLevel, ConsoleColor>>();

if (logLevelColors is not null)
{
return;
}

// Fallback to default values
// @Cleanup: Move it just to UEScriptLoggerConfiguration default value
logLevelColors = new Dictionary<LogLevel, ConsoleColor>()
{
{ LogLevel.Trace, ConsoleColor.Gray },
{ LogLevel.Debug, ConsoleColor.Gray },
{ LogLevel.Information, ConsoleColor.DarkGreen },
{ LogLevel.Warning, ConsoleColor.Cyan },
{ LogLevel.Error, ConsoleColor.Red },
{ LogLevel.Critical, ConsoleColor.Red }
};

configure.LogLevelColors = logLevelColors;
}

private static void ConfigureAppConfiguration(IHostBuilder host)
{
host.ConfigureAppConfiguration(config =>
{
config
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "appsettings.json")
.AddJsonFile(
AppDomain.CurrentDomain.BaseDirectory +
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables();
});
}

private static IConfiguration GetConfiguration()
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "appsettings.json")
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables()
.Build();

return config;
}
}
98 changes: 5 additions & 93 deletions UEScript.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
using System.CommandLine;
using System.CommandLine.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using UEScript.CLI.Commands;
using UEScript.CLI.Common;
using UEScript.CLI.Services;
using UEScript.CLI.Services.Impl;
using System.Reflection;
using UEScript.Logging;
using System.Reflection;
using UEScript.CLI.Configurations;

[assembly: AssemblyVersion("3.1.*")]

Expand All @@ -20,87 +9,10 @@ public static class Program
{
public static Task Main(string[] args)
{
var cliConfiguration = BuildCommandLine();
cliConfiguration.UseHost(_ => Host.CreateDefaultBuilder().UseContentRoot(AppDomain.CurrentDomain.BaseDirectory),
host =>
{
host.ConfigureAppConfiguration(config =>
{
config
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "appsettings.json")
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables();
});
host.ConfigureLogging(builder =>
{
builder.ClearProviders();
builder.AddUEScriptLogger(configure =>
{
// @Cleanup: Check how to do this stuff automatically
var configuration = GetConfiguration();
var logLevelColors = configuration.GetSection("Logging:UEScript:LogLevelColors").Get<Dictionary<LogLevel, ConsoleColor>>();
if (logLevelColors is null)
{
// Fallback to default values
// @Cleanup: Move it just to UEScriptLoggerConfiguration default value
logLevelColors = new Dictionary<LogLevel, ConsoleColor>()
{
{
LogLevel.Trace, ConsoleColor.Gray
},
{
LogLevel.Debug, ConsoleColor.Gray
},
{
LogLevel.Information, ConsoleColor.DarkGreen
},
{
LogLevel.Warning, ConsoleColor.Cyan
},
{
LogLevel.Error, ConsoleColor.Red
},
{
LogLevel.Critical, ConsoleColor.Red
}
};
configure.LogLevelColors = logLevelColors;
}
});
var parseResult = cliConfiguration.Parse(args);
var debugLog = parseResult.GetValue<bool>("--debug-log");
if (debugLog)
{
builder.AddFilter("Microsoft.Hosting", LogLevel.Information);
}
});
host.ConfigureServices(services =>
{
services.AddSingleton<IUnrealEngineAssociationRepository, UnrealEngineEngineAssociationRepository>();
services.AddSingleton<IUnrealBuildToolService, UnrealBuildToolService>();
services.AddSingleton<IUnrealEngineEditorService, UnrealEngineEditorService>();
});
});
var cliConfiguration = ProgramConfiguration
.BuildCommandLine()
.Configure(args);

return cliConfiguration.InvokeAsync(args);
}

private static CliConfiguration BuildCommandLine()
{
return new CliConfiguration(ConfigureRootCommand.AddRootCommand());
}

private static IConfiguration GetConfiguration()
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "appsettings.json")
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + $"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
.AddEnvironmentVariables()
.Build();

return config;
}
}

0 comments on commit c1218ec

Please sign in to comment.