Iyon_Groznyy

Untitled

Mar 26th, 2022
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.40 KB | None | 0 0
  1. using BuildApps.Core.Mobile.Configurations.Models;
  2. using Microsoft.AppCenter.Crashes;
  3. using Newtonsoft.Json;
  4. using Newtonsoft.Json.Linq;
  5. using System;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Text;
  10.  
  11. namespace BuildApps.Core.Mobile.Configurations.Providers
  12. {
  13.     public class EnvironmentConfigurationProvider : IEnvironmentConfigurationProvider
  14.     {
  15.         private const string AppConfigFileName = "AppConfig.json";
  16.  
  17.         private readonly Assembly contextAssembly;
  18.  
  19.         public EnvironmentConfigurationProvider(object contextObject)
  20.         {
  21.             this.contextAssembly = contextObject.GetType().Assembly;
  22.  
  23.             Initialize();
  24.         }
  25.  
  26.         public EnvironmentConfiguration Environment { get; set; }
  27.  
  28.         private void Initialize()
  29.         {
  30.             var jsonContent = LoadConfig();
  31.             if (jsonContent is null)
  32.             {
  33.                 return;
  34.             }
  35.  
  36.             LogConfigWarningIfNeed(jsonContent);
  37.  
  38.             try
  39.             {
  40.                 if (string.IsNullOrWhiteSpace(jsonContent))
  41.                 {
  42.                     return;
  43.                 }
  44.  
  45.                 var appConfig = JsonConvert.DeserializeObject<ApplicationConfiguration>(jsonContent);
  46.                 Environment = appConfig.Configurations.FirstOrDefault(config => config.EnvironmentName == appConfig.Environment);
  47.             }
  48.             catch (Exception exception)
  49.             {
  50.                 Crashes.TrackError(exception);
  51.             }
  52.         }
  53.  
  54.         private string LoadConfig()
  55.         {
  56.             var appConfigPath = contextAssembly
  57.                                 .GetManifestResourceNames()
  58.                                 .Where(resource => resource.Contains(AppConfigFileName))
  59.                                 .FirstOrDefault();
  60.             if (appConfigPath is null)
  61.             {
  62.                 return null;
  63.             }
  64.  
  65.             var stream = contextAssembly.GetManifestResourceStream(appConfigPath);
  66.             using (var reader = new StreamReader(stream))
  67.             {
  68.                 return reader.ReadToEnd();
  69.             };
  70.         }
  71.  
  72.         private void LogConfigWarningIfNeed(string configJson)
  73.         {
  74.             try
  75.             {
  76.                 var currentConfig = LoadCurrentConfigFromToken(configJson);
  77.                 if (currentConfig is null)
  78.                 {
  79.                     return;
  80.                 }
  81.  
  82.                 var stringBuilder = new StringBuilder();
  83.                 stringBuilder.AppendLine("Found not resolved customer properties in AppConfig.json:");
  84.  
  85.                 var notResolvedPropertiesCount = 0;
  86.                 var properties = typeof(Environment).GetProperties(BindingFlags.Public | BindingFlags.Instance);
  87.                 foreach (var property in properties)
  88.                 {
  89.                     var currentConfigProperty = currentConfig[property.Name];
  90.                     if (currentConfigProperty is null)
  91.                     {
  92.                         stringBuilder.AppendLine(property.Name);
  93.                         ++notResolvedPropertiesCount;
  94.                     }
  95.                 }
  96.  
  97.                 if (notResolvedPropertiesCount == 0)
  98.                 {
  99.                     return;
  100.                 }
  101.  
  102.                 System.Diagnostics.Debug.WriteLine(stringBuilder.ToString());
  103.             }
  104.             catch (Exception ex)
  105.             {
  106.                 Crashes.TrackError(ex);
  107.                 System.Diagnostics.Debug.WriteLine(ex);
  108.             }
  109.         }
  110.  
  111.         private JToken LoadCurrentConfigFromToken(string configJson)
  112.         {
  113.             try
  114.             {
  115.                 var jsonObject = JObject.Parse(configJson);
  116.                 var environmentName = jsonObject[nameof(ApplicationConfiguration.Environment)];
  117.                 var configs = jsonObject[nameof(ApplicationConfiguration.Configurations)];
  118.  
  119.                 var currentConfig = configs
  120.                     .Where(config => config[nameof(EnvironmentConfiguration.EnvironmentName)].Value<string>() == environmentName.Value<string>())
  121.                     .ToList()
  122.                     .FirstOrDefault();
  123.                 return currentConfig;
  124.             }
  125.             catch (Exception ex)
  126.             {
  127.                 Crashes.TrackError(ex);
  128.                 System.Diagnostics.Debug.WriteLine(ex);
  129.                 return null;
  130.             }
  131.         }
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment