Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BuildApps.Core.Mobile.Configurations.Models;
- using Microsoft.AppCenter.Crashes;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- namespace BuildApps.Core.Mobile.Configurations.Providers
- {
- public class EnvironmentConfigurationProvider : IEnvironmentConfigurationProvider
- {
- private const string AppConfigFileName = "AppConfig.json";
- private readonly Assembly contextAssembly;
- public EnvironmentConfigurationProvider(object contextObject)
- {
- this.contextAssembly = contextObject.GetType().Assembly;
- Initialize();
- }
- public EnvironmentConfiguration Environment { get; set; }
- private void Initialize()
- {
- var jsonContent = LoadConfig();
- if (jsonContent is null)
- {
- return;
- }
- LogConfigWarningIfNeed(jsonContent);
- try
- {
- if (string.IsNullOrWhiteSpace(jsonContent))
- {
- return;
- }
- var appConfig = JsonConvert.DeserializeObject<ApplicationConfiguration>(jsonContent);
- Environment = appConfig.Configurations.FirstOrDefault(config => config.EnvironmentName == appConfig.Environment);
- }
- catch (Exception exception)
- {
- Crashes.TrackError(exception);
- }
- }
- private string LoadConfig()
- {
- var appConfigPath = contextAssembly
- .GetManifestResourceNames()
- .Where(resource => resource.Contains(AppConfigFileName))
- .FirstOrDefault();
- if (appConfigPath is null)
- {
- return null;
- }
- var stream = contextAssembly.GetManifestResourceStream(appConfigPath);
- using (var reader = new StreamReader(stream))
- {
- return reader.ReadToEnd();
- };
- }
- private void LogConfigWarningIfNeed(string configJson)
- {
- try
- {
- var currentConfig = LoadCurrentConfigFromToken(configJson);
- if (currentConfig is null)
- {
- return;
- }
- var stringBuilder = new StringBuilder();
- stringBuilder.AppendLine("Found not resolved customer properties in AppConfig.json:");
- var notResolvedPropertiesCount = 0;
- var properties = typeof(Environment).GetProperties(BindingFlags.Public | BindingFlags.Instance);
- foreach (var property in properties)
- {
- var currentConfigProperty = currentConfig[property.Name];
- if (currentConfigProperty is null)
- {
- stringBuilder.AppendLine(property.Name);
- ++notResolvedPropertiesCount;
- }
- }
- if (notResolvedPropertiesCount == 0)
- {
- return;
- }
- System.Diagnostics.Debug.WriteLine(stringBuilder.ToString());
- }
- catch (Exception ex)
- {
- Crashes.TrackError(ex);
- System.Diagnostics.Debug.WriteLine(ex);
- }
- }
- private JToken LoadCurrentConfigFromToken(string configJson)
- {
- try
- {
- var jsonObject = JObject.Parse(configJson);
- var environmentName = jsonObject[nameof(ApplicationConfiguration.Environment)];
- var configs = jsonObject[nameof(ApplicationConfiguration.Configurations)];
- var currentConfig = configs
- .Where(config => config[nameof(EnvironmentConfiguration.EnvironmentName)].Value<string>() == environmentName.Value<string>())
- .ToList()
- .FirstOrDefault();
- return currentConfig;
- }
- catch (Exception ex)
- {
- Crashes.TrackError(ex);
- System.Diagnostics.Debug.WriteLine(ex);
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment