Skip to content

Commit

Permalink
Merge pull request #1844 from qdraw/feature/202411_code_smells_26
Browse files Browse the repository at this point in the history
code smells
  • Loading branch information
qdraw authored Nov 26, 2024
2 parents dbbcb99 + a543699 commit 91ba630
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 263 deletions.
173 changes: 86 additions & 87 deletions starsky/starsky.foundation.platform/Helpers/SetupAppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,113 +12,112 @@

[assembly: InternalsVisibleTo("starskytest")]

namespace starsky.foundation.platform.Helpers
namespace starsky.foundation.platform.Helpers;

public static class SetupAppSettings
{
public static class SetupAppSettings
public static async Task<ServiceCollection> FirstStepToAddSingleton(
ServiceCollection services)
{
public static async Task<ServiceCollection> FirstStepToAddSingleton(
ServiceCollection services)
{
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var configurationRoot = await AppSettingsToBuilder();
services.ConfigurePoCo<AppSettings>(configurationRoot.GetSection("App"));
return services;
}

/// <summary>
/// Default appSettings.json to builder
/// </summary>
/// <returns>ConfigBuilder</returns>
public static async Task<IConfigurationRoot> AppSettingsToBuilder(string[]? args = null)
{
var appSettings = new AppSettings();
var builder = new ConfigurationBuilder();

var settings = await MergeJsonFiles(appSettings.BaseDirectoryProject);
services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());
var configurationRoot = await AppSettingsToBuilder();
services.ConfigurePoCo<AppSettings>(configurationRoot.GetSection("App"));
return services;
}

// Make sure is wrapped in a AppContainer app
var appContainer = new AppContainerAppSettings { App = settings };
var utf8Bytes = JsonSerializer.SerializeToUtf8Bytes(appContainer);
/// <summary>
/// Default appSettings.json to builder
/// </summary>
/// <returns>ConfigBuilder</returns>
public static async Task<IConfigurationRoot> AppSettingsToBuilder(string[]? args = null)
{
var appSettings = new AppSettings();
var builder = new ConfigurationBuilder();

builder
.AddJsonStream(new MemoryStream(utf8Bytes))
// overwrite envs
// current dir gives problems on linux arm
.AddEnvironmentVariables();
var settings = await MergeJsonFiles(appSettings.BaseDirectoryProject);

if ( args != null )
{
builder.AddCommandLine(args);
}
// Make sure is wrapped in a AppContainer app
var appContainer = new AppContainerAppSettings { App = settings };
var utf8Bytes = JsonSerializer.SerializeToUtf8Bytes(appContainer);

return builder.Build();
}
builder
.AddJsonStream(new MemoryStream(utf8Bytes))
// overwrite envs
// current dir gives problems on linux arm
.AddEnvironmentVariables();

internal static string AppSettingsMachineNameWithDot()
if ( args != null )
{
// to remove spaces and other signs, check help to get your name
return $"appsettings.{Environment.MachineName.ToLowerInvariant()}."; // dot here
builder.AddCommandLine(args);
}

private static IEnumerable<string> Order(string baseDirectoryProject)
{
var appSettingsMachine = AppSettingsMachineNameWithDot();
var appSettingsPath =
Environment.GetEnvironmentVariable("app__appsettingspath") ?? string.Empty;
return new List<string>
{
Path.Combine(baseDirectoryProject, "appsettings.json"),
Path.Combine(baseDirectoryProject, "appsettings.default.json"),
Path.Combine(baseDirectoryProject, "appsettings.patch.json"),
Path.Combine(baseDirectoryProject, appSettingsMachine + "json"),
Path.Combine(baseDirectoryProject, appSettingsMachine + "patch.json"),
appSettingsPath
}.Where(p => !string.IsNullOrEmpty(p));
}
return builder.Build();
}

internal static string AppSettingsMachineNameWithDot()
{
// to remove spaces and other signs, check help to get your name
return $"appsettings.{Environment.MachineName.ToLowerInvariant()}."; // dot here
}

internal static async Task<AppSettings> MergeJsonFiles(string baseDirectoryProject)
private static IEnumerable<string> Order(string baseDirectoryProject)
{
var appSettingsMachine = AppSettingsMachineNameWithDot();
var appSettingsPath =
Environment.GetEnvironmentVariable("app__appsettingspath") ?? string.Empty;
return new List<string>
{
var paths = Order(baseDirectoryProject);
var appSettingsList = new List<AppSettings>();
Path.Combine(baseDirectoryProject, "appsettings.json"),
Path.Combine(baseDirectoryProject, "appsettings.default.json"),
Path.Combine(baseDirectoryProject, "appsettings.patch.json"),
Path.Combine(baseDirectoryProject, appSettingsMachine + "json"),
Path.Combine(baseDirectoryProject, appSettingsMachine + "patch.json"),
appSettingsPath
}.Where(p => !string.IsNullOrEmpty(p));
}

foreach ( var path in paths.Where(File.Exists) )
{
var appSettings = await ReadAppSettings.Read(path);
appSettingsList.Add(appSettings!.App);
}
internal static async Task<AppSettings> MergeJsonFiles(string baseDirectoryProject)
{
var paths = Order(baseDirectoryProject);
var appSettingsList = new List<AppSettings>();

if ( appSettingsList.Count == 0 )
{
return new AppSettings();
}
foreach ( var path in paths.Where(File.Exists) )
{
var appSettings = await ReadAppSettings.Read(path);
appSettingsList.Add(appSettings!.App);
}

var appSetting = appSettingsList.FirstOrDefault()!;
if ( appSettingsList.Count == 0 )
{
return new AppSettings();
}

for ( var i = 1; i < appSettingsList.Count; i++ )
{
var currentAppSetting = appSettingsList[i];
AppSettingsCompareHelper.Compare(appSetting, currentAppSetting);
}
var appSetting = appSettingsList[0];

return appSetting;
for ( var i = 1; i < appSettingsList.Count; i++ )
{
var currentAppSetting = appSettingsList[i];
AppSettingsCompareHelper.Compare(appSetting, currentAppSetting);
}

/// <summary>
/// Configure the PoCo the dependency injection
/// </summary>
/// <param name="services">services</param>
/// <param name="configuration"></param>
/// <returns></returns>
public static AppSettings ConfigurePoCoAppSettings(IServiceCollection services,
IConfigurationRoot configuration)
{
// configs
services.ConfigurePoCo<AppSettings>(configuration.GetSection("App"));
return appSetting;
}

// Need to rebuild for AppSettings
var serviceProvider = services.BuildServiceProvider();
/// <summary>
/// Configure the PoCo the dependency injection
/// </summary>
/// <param name="services">services</param>
/// <param name="configuration"></param>
/// <returns></returns>
public static AppSettings ConfigurePoCoAppSettings(IServiceCollection services,
IConfigurationRoot configuration)
{
// configs
services.ConfigurePoCo<AppSettings>(configuration.GetSection("App"));

return serviceProvider.GetRequiredService<AppSettings>();
}
// Need to rebuild for AppSettings
var serviceProvider = services.BuildServiceProvider();

return serviceProvider.GetRequiredService<AppSettings>();
}
}
Loading

0 comments on commit 91ba630

Please sign in to comment.