Skip to content

Commit

Permalink
add environment var for config files
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Dec 20, 2023
1 parent d91bd4f commit c9e2d62
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public static partial class ConfigurationBuilderExtensions
/// <param name="builder"></param>
/// <param name="optional"></param>
/// <returns></returns>
public static IConfigurationBuilder AddJsonAndroidAsset(this IConfigurationBuilder builder, bool optional = true, bool includePlatformSpecific = true)
public static IConfigurationBuilder AddJsonAndroidAsset(this IConfigurationBuilder builder, string? environment = null, bool optional = true, bool includePlatformSpecific = true)
{
if (includePlatformSpecific)
builder.AddJsonAssetInternal("appsettings.android.json", true);
builder.AddJsonAssetInternal("appsettings.android.json", environment, true);

return builder.AddJsonAssetInternal("appsettings.json", optional);
return builder.AddJsonAssetInternal("appsettings.json", environment, optional);
}


Expand All @@ -33,6 +33,17 @@ public static IConfigurationBuilder AddAndroidPreferences(this IConfigurationBui
=> builder.Add(new SharedPreferencesConfigurationSource());


static IConfigurationBuilder AddJsonAssetInternal(this IConfigurationBuilder builder, string fileName, string? environment, bool optional)
{
if (!String.IsNullOrWhiteSpace(environment))
{
var newFileName = GetEnvFileName(fileName, environment);
builder.AddJsonAssetInternal(newFileName, true);
}
return builder.AddJsonAssetInternal(fileName, optional);
}


static IConfigurationBuilder AddJsonAssetInternal(this IConfigurationBuilder builder, string fileName, bool optional)
{
var assets = Application.Context.Assets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Foundation;
using Shiny.Extensions.Configuration;
using System;
using System.IO;

namespace Microsoft.Extensions.Configuration;
Expand All @@ -14,18 +15,18 @@ public static partial class ConfigurationBuilderExtensions
/// <param name="fileName"></param>
/// <param name="optional"></param>
/// <returns></returns>
public static IConfigurationBuilder AddJsonIosBundle(this IConfigurationBuilder builder, bool optional = true, bool reloadOnChange = true, bool includePlatformSpecific = true)
public static IConfigurationBuilder AddJsonIosBundle(this IConfigurationBuilder builder, string? environment = null, bool optional = true, bool reloadOnChange = true, bool includePlatformSpecific = true)
{
if (includePlatformSpecific)
{
builder.AddJsonFileInternal("appsettings.apple.json", true, reloadOnChange);
builder.AddJsonFileInternal("appsettings.apple.json", environment, true, reloadOnChange);
#if IOS
builder.AddJsonFileInternal("appsettings.ios.json", true, reloadOnChange);
builder.AddJsonFileInternal("appsettings.ios.json", environment, true, reloadOnChange);
#elif MACCATALYST
builder.AddJsonFileInternal("appsettings.maccatalyst.json", true, reloadOnChange);
builder.AddJsonFileInternal("appsettings.maccatalyst.json", environment, true, reloadOnChange);
#endif
}
return builder.AddJsonFileInternal("appsettings.json", optional, reloadOnChange);
return builder.AddJsonFileInternal("appsettings.json", environment, optional, reloadOnChange);
}


Expand All @@ -38,6 +39,17 @@ public static IConfigurationBuilder AddIosUserDefaults(this IConfigurationBuilde
=> builder.Add(new NSUserDefaultsConfigurationSource());


static IConfigurationBuilder AddJsonFileInternal(this IConfigurationBuilder builder, string fileName, string? environment, bool optional, bool reloadOnChange)
{
if (!String.IsNullOrWhiteSpace(environment))
{
var newFileName = GetEnvFileName(fileName, environment);
builder.AddJsonFileInternal(newFileName, true, reloadOnChange);
}
return builder.AddJsonFileInternal(fileName, true, reloadOnChange);
}


static IConfigurationBuilder AddJsonFileInternal(this IConfigurationBuilder builder, string fileName, bool optional, bool reloadOnChange)
{
#if MACCATALYST
Expand All @@ -46,5 +58,5 @@ static IConfigurationBuilder AddJsonFileInternal(this IConfigurationBuilder buil
var path = Path.Combine(NSBundle.MainBundle.BundlePath, fileName);
#endif
return builder.AddJsonFile(path, optional, reloadOnChange);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#if APPLE || ANDROID
using System;
using System.IO;

namespace Microsoft.Extensions.Configuration;


public static partial class ConfigurationBuilderExtensions
{
public static IConfigurationBuilder AddJsonPlatformBundle(this IConfigurationBuilder builder, bool optional = true, bool addPlatformSpecific = true)
public static IConfigurationBuilder AddJsonPlatformBundle(this IConfigurationBuilder builder, string? environment = null, bool optional = true, bool addPlatformSpecific = true)
{
#if APPLE
builder.AddJsonIosBundle(optional, addPlatformSpecific);
builder.AddJsonIosBundle(environment, optional, addPlatformSpecific);
#elif ANDROID
builder.AddJsonAndroidAsset(optional, addPlatformSpecific);
builder.AddJsonAndroidAsset(environment, optional, addPlatformSpecific);
#endif
return builder;
}
Expand All @@ -26,6 +29,14 @@ public static IConfigurationBuilder AddPlatformPreferences(this IConfigurationBu
}


internal static string GetEnvFileName(string fileName, string environment)
{
var ext = Path.GetExtension(fileName);
var name = Path.GetFileNameWithoutExtension(fileName);
var newFileName = $"{name}.{environment}{ext}";
return newFileName;
}

//public static T BindTwoWay<T>(this IConfiguration configuration, T obj) where T : INotifyPropertyChanged
//{
// // TODO: sub-binding if deep binding set?
Expand Down

0 comments on commit c9e2d62

Please sign in to comment.