| | | 1 | | namespace Kestrun.Runtime; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Helpers for determining the current environment name. |
| | | 5 | | /// </summary> |
| | | 6 | | public static class EnvironmentHelper |
| | | 7 | | { |
| | | 8 | | private static Func<string?>? _overrideProvider; |
| | | 9 | | private static IHostEnvironment? _cachedEnv; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Set the host environment (usually from DI). |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="env">The host environment.</param> |
| | 0 | 15 | | public static void SetHostEnvironment(IHostEnvironment env) => _cachedEnv = env; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Set an explicit override for the environment name. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="name">The environment name to override with.</param> |
| | | 21 | | public static void SetOverrideName(string? name) |
| | 39 | 22 | | => _overrideProvider = string.IsNullOrWhiteSpace(name) ? null : () => name; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Set an explicit override provider for the environment name. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="provider">The provider function to retrieve the environment name.</param> |
| | 0 | 28 | | public static void SetOverride(Func<string?> provider) => _overrideProvider = provider; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Clear any explicit override for the environment name. |
| | | 32 | | /// </summary> |
| | 1 | 33 | | public static void ClearOverride() => _overrideProvider = null; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Determine the current environment name. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <returns> The current environment name.</returns> |
| | | 39 | | private static string ResolveEnvironment() |
| | | 40 | | { |
| | | 41 | | // 1️⃣ Explicit override |
| | 323 | 42 | | var fromOverride = _overrideProvider?.Invoke(); |
| | 323 | 43 | | if (!string.IsNullOrWhiteSpace(fromOverride)) |
| | | 44 | | { |
| | 38 | 45 | | return fromOverride!; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // 2️⃣ Cached host environment (from SetHostEnvironment) |
| | 285 | 49 | | if (!string.IsNullOrWhiteSpace(_cachedEnv?.EnvironmentName)) |
| | | 50 | | { |
| | 0 | 51 | | return _cachedEnv!.EnvironmentName; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // 3️⃣ Standard environment variables (like Kestrel) |
| | 285 | 55 | | return Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") |
| | 285 | 56 | | ?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") |
| | 285 | 57 | | ?? "Production"; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// The current environment name. |
| | | 62 | | /// </summary> |
| | 323 | 63 | | public static string Name => ResolveEnvironment(); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Is the current environment "Development"? |
| | | 67 | | /// </summary> |
| | | 68 | | /// <returns>True if the current environment is "Development"; otherwise, false.</returns> |
| | | 69 | | public static bool IsDevelopment() |
| | 0 | 70 | | => string.Equals(Name, Environments.Development, StringComparison.OrdinalIgnoreCase); |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Is the current environment "Staging"? |
| | | 74 | | /// </summary> |
| | | 75 | | /// <returns>True if the current environment is "Staging"; otherwise, false.</returns> |
| | | 76 | | public static bool IsStaging() |
| | 0 | 77 | | => string.Equals(Name, Environments.Staging, StringComparison.OrdinalIgnoreCase); |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Is the current environment "Production"? |
| | | 81 | | /// </summary> |
| | | 82 | | /// <returns>True if the current environment is "Production"; otherwise, false.</returns> |
| | | 83 | | public static bool IsProduction() |
| | 0 | 84 | | => string.Equals(Name, Environments.Production, StringComparison.OrdinalIgnoreCase); |
| | | 85 | | } |