using System.Reflection; namespace AIStudio.Tools.Metadata; /// /// The .NET runtime which runs the app. /// /// /// Asked while the app runs instead of taken from metadata.txt: a release brings its own runtime, /// and a development build runs on whichever runtime the developer installed. /// public static class DotnetRuntimeVersion { /// /// How many characters of the commit we show, as metadata.txt does. /// private const int COMMIT_LENGTH = 10; /// /// Gets the version of the runtime together with the commit it was built from, such as /// 10.0.14 (commit 0427b9abbe). /// public static string Current { get; } = Describe(Environment.Version, typeof(object).Assembly.GetCustomAttribute()?.InformationalVersion); /// /// Describes a version of the runtime together with the commit which the informational version /// of its core library names after a plus sign, such as 10.0.14+0427b9abbead3f…. /// /// The version of the runtime. /// The informational version of the core library, if any. /// The version with its commit, or the version alone when the commit is unknown. internal static string Describe(Version version, string? informationalVersion) { if (string.IsNullOrWhiteSpace(informationalVersion)) return version.ToString(); var plusIndex = informationalVersion.IndexOf('+'); var commit = plusIndex < 0 ? string.Empty : informationalVersion[(plusIndex + 1)..]; return commit.Length < COMMIT_LENGTH ? version.ToString() : $"{version} (commit {commit[..COMMIT_LENGTH]})"; } }