using System; using System.Diagnostics; using System.Runtime.InteropServices; namespace Adaptation.Shared { public class IsEnvironment { public enum Name { LinuxDevelopment, LinuxProduction, LinuxStaging, OSXDevelopment, OSXProduction, OSXStaging, WindowsDevelopment, WindowsProduction, WindowsStaging } public bool DebuggerWasAttachedDuringConstructor { get; private set; } public bool Development { get; private set; } public bool Linux { get; private set; } public bool OSX { get; private set; } public bool Production { get; private set; } public bool Staging { get; private set; } public bool Windows { get; private set; } public string Profile { get; private set; } public string AppSettingsFileName { get; private set; } public string ASPNetCoreEnvironment { get; private set; } public IsEnvironment(string testCategory) { if (testCategory.EndsWith(".json")) { Production = testCategory == "appsettings.json"; Staging = testCategory.Contains(nameof(Staging)); OSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); Development = testCategory.Contains(nameof(Development)); Linux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); DebuggerWasAttachedDuringConstructor = Debugger.IsAttached; Windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); ASPNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); } else { DebuggerWasAttachedDuringConstructor = Debugger.IsAttached; OSX = !string.IsNullOrEmpty(testCategory) && testCategory.StartsWith(nameof(OSX)); ASPNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); Linux = !string.IsNullOrEmpty(testCategory) && testCategory.StartsWith(nameof(Linux)); Staging = !string.IsNullOrEmpty(testCategory) && testCategory.EndsWith(nameof(Staging)); Windows = !string.IsNullOrEmpty(testCategory) && testCategory.StartsWith(nameof(Windows)); Production = !string.IsNullOrEmpty(testCategory) && testCategory.EndsWith(nameof(Production)); Development = !string.IsNullOrEmpty(testCategory) && testCategory.EndsWith(nameof(Development)); } SetProfile(); SetAppSettingsFileName(processesCount: null); } public IsEnvironment(bool isDevelopment, bool isStaging, bool isProduction) { Staging = isStaging; Production = isProduction; Development = isDevelopment; OSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); Linux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); DebuggerWasAttachedDuringConstructor = Debugger.IsAttached; Windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); ASPNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); SetProfile(); SetAppSettingsFileName(processesCount: null); } public IsEnvironment(int? processesCount, bool nullASPNetCoreEnvironmentIsDevelopment, bool nullASPNetCoreEnvironmentIsProduction) { OSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); Linux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); DebuggerWasAttachedDuringConstructor = Debugger.IsAttached; Windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); ASPNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); if (nullASPNetCoreEnvironmentIsDevelopment && nullASPNetCoreEnvironmentIsProduction) throw new Exception(); else if (string.IsNullOrEmpty(ASPNetCoreEnvironment) && nullASPNetCoreEnvironmentIsProduction) Production = true; else if (string.IsNullOrEmpty(ASPNetCoreEnvironment) && nullASPNetCoreEnvironmentIsDevelopment) Development = true; else if (string.IsNullOrEmpty(ASPNetCoreEnvironment) && !nullASPNetCoreEnvironmentIsDevelopment && !nullASPNetCoreEnvironmentIsProduction) throw new Exception(); else { Staging = ASPNetCoreEnvironment.EndsWith(nameof(Staging)); Production = ASPNetCoreEnvironment.EndsWith(nameof(Production)); Development = ASPNetCoreEnvironment.EndsWith(nameof(Development)); } SetProfile(); SetAppSettingsFileName(processesCount); } private void SetProfile() { if (Windows && Production) Profile = nameof(Production); else if (Windows && Staging) Profile = nameof(Staging); else if (Windows && Development) Profile = nameof(Development); else if (Linux && Production) Profile = nameof(Name.LinuxProduction); else if (Linux && Staging) Profile = nameof(Name.LinuxStaging); else if (Linux && Development) Profile = nameof(Name.LinuxDevelopment); else if (OSX && Production) Profile = nameof(Name.OSXProduction); else if (OSX && Staging) Profile = nameof(Name.OSXStaging); else if (OSX && Development) Profile = nameof(Name.OSXDevelopment); else throw new Exception(); } private void SetAppSettingsFileName(int? processesCount) { if (Production) { if (processesCount is null) AppSettingsFileName = "appsettings.json"; else AppSettingsFileName = $"appsettings.{processesCount}.json"; } else { string environment; if (Staging) environment = nameof(Staging); else if (Development) environment = nameof(Development); else throw new Exception(); if (processesCount is null) AppSettingsFileName = $"appsettings.{environment}.json"; else AppSettingsFileName = $"appsettings.{environment}.{processesCount}.json"; } } } }