| | | 1 | | using System.Reflection; |
| | | 2 | | using System.Runtime.Versioning; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Utility class to expose information about the runtime environment |
| | | 6 | | /// that Kestrun was built for, and to gate features by TFM and runtime. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class KestrunAnnotationsRuntimeInfo |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Determines whether the current distribution is a release distribution. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <returns> True if the current distribution is a release distribution; otherwise, false.</returns> |
| | | 14 | | #if DEBUG |
| | | 15 | | public static bool IsReleaseDistribution => false; |
| | | 16 | | #else |
| | 130 | 17 | | public static bool IsReleaseDistribution => true; |
| | | 18 | | #endif |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Determines whether the current build is a debug build. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <returns> True if the current build is a debug build; otherwise, false.</returns> |
| | 0 | 24 | | public static bool IsDebugBuild => !IsReleaseDistribution; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Returns the target framework version this assembly was built against |
| | | 28 | | /// as a System.Version (e.g., 8.0, 9.0). |
| | | 29 | | /// </summary> |
| | | 30 | | public static Version GetBuiltTargetFrameworkVersion() |
| | | 31 | | { |
| | 0 | 32 | | var asm = typeof(KestrunAnnotationsRuntimeInfo).Assembly; |
| | 0 | 33 | | var tfm = asm.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName; |
| | 0 | 34 | | if (string.IsNullOrEmpty(tfm)) |
| | | 35 | | { |
| | 0 | 36 | | return new Version(0, 0); |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | var key = "Version="; |
| | 0 | 40 | | var idx = tfm.IndexOf(key, StringComparison.OrdinalIgnoreCase); |
| | 0 | 41 | | if (idx >= 0) |
| | | 42 | | { |
| | 0 | 43 | | var ver = tfm[(idx + key.Length)..].TrimStart('v'); |
| | 0 | 44 | | if (Version.TryParse(ver, out var parsed)) |
| | | 45 | | { |
| | 0 | 46 | | return parsed; |
| | | 47 | | } |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | return new Version(0, 0); |
| | | 51 | | } |
| | | 52 | | } |