From 791a6e70698a66fb69f8a61470fdc918e5d1b6a8 Mon Sep 17 00:00:00 2001 From: Adeel <3840695+am11@users.noreply.github.com> Date: Tue, 14 May 2024 10:03:14 +0300 Subject: [PATCH] Address CR feedback --- .../gen/DotnetProductVersionInfoGenerator.cs | 46 ++++++++++++------- .../System.Private.CoreLib.Shared.projitems | 3 -- .../src/System/Environment.cs | 3 -- .../InteropServices/RuntimeInformation.cs | 5 -- ...opServices.RuntimeInformation.Tests.csproj | 3 ++ 5 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/gen/DotnetProductVersionInfoGenerator.cs b/src/libraries/System.Private.CoreLib/gen/DotnetProductVersionInfoGenerator.cs index 2d9624765351a..ea716d19386b3 100644 --- a/src/libraries/System.Private.CoreLib/gen/DotnetProductVersionInfoGenerator.cs +++ b/src/libraries/System.Private.CoreLib/gen/DotnetProductVersionInfoGenerator.cs @@ -2,8 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Collections.Immutable; -using System.IO; +using System.Reflection; using Microsoft.CodeAnalysis; namespace Generators @@ -13,28 +12,41 @@ public partial class DotnetProductVersionInfoGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { - // we only care about filename which is of the form "$(Version).versionstring", no need to actually read the file. - IncrementalValuesProvider textFiles = context.AdditionalTextsProvider.Where(static file => file.Path.EndsWith(".versionstring")); - IncrementalValueProvider<(Compilation Left, ImmutableArray Right)> compilationAndFiles = - context.CompilationProvider.Combine(textFiles.Collect()); - - context.RegisterSourceOutput(compilationAndFiles, (spc, content) => + context.RegisterPostInitializationOutput(ctx => { + string? informationalVersion = typeof(DotnetProductVersionInfoGenerator).Assembly.GetCustomAttribute()?.InformationalVersion; + // strip semver metadata (git hash) followed by + sign - string? productVersion = Path.GetFileNameWithoutExtension(content.Right[0].Path.Split('+')?[0]); + string? productVersion = informationalVersion?.Split('+')?[0]; - if (productVersion is null) - throw new InvalidOperationException($"Unable to get product version at build time."); + if (string.IsNullOrEmpty(productVersion)) + throw new InvalidOperationException($"Unable to obtain product version at build-time."); - spc.AddSource("Environment.DotnetProductVersionInfo.g.cs", $@" -// + // strip semver prerelease label followed by - sign for Environment.Version + Version versionObject = Version.Parse(productVersion.Split('-')[0]); -namespace System; + ctx.AddSource("DotnetProductVersionInfo.g.cs", $@"// + +namespace System +{{ + public static partial class Environment + {{ + /// + /// Gets a version consisting of the major, minor, build, and revision numbers of the common language runtime. + /// + public static Version Version => new Version({versionObject.Major}, {versionObject.Minor}, {versionObject.Build}); + }} +}} -public static partial class Environment +namespace System.Runtime.InteropServices {{ - internal const string ProductVersionWithLabel = ""{productVersion}""; - private const string ProductVersionWithoutLabel = ""{productVersion.Split('-')?[0]}""; + public static partial class RuntimeInformation + {{ + /// + /// Gets the name of the .NET installation on which an app is running. + /// + public static string FrameworkDescription => "".NET {productVersion}""; + }} }}"); }); } diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index d44e826137682..ce22178ca8a0f 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -2783,7 +2783,4 @@ - - - diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.cs index aa2a4df985a11..ff58e43d54eb3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.cs @@ -3,7 +3,6 @@ using System.Collections; using System.Diagnostics; -using System.Reflection; using System.Runtime.CompilerServices; using System.Threading; @@ -201,8 +200,6 @@ public static OperatingSystem OSVersion } } - public static Version Version => Version.Parse(ProductVersionWithoutLabel); - public static string StackTrace { [MethodImpl(MethodImplOptions.NoInlining)] // Prevent inlining from affecting where the stacktrace starts diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/RuntimeInformation.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/RuntimeInformation.cs index a86c3ee8da9f2..e750d1011584c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/RuntimeInformation.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/RuntimeInformation.cs @@ -7,11 +7,6 @@ public static partial class RuntimeInformation { private static string? s_runtimeIdentifier; - /// - /// Gets the name of the .NET installation on which an app is running. - /// - public static string FrameworkDescription { get; } = ".NET " + Environment.ProductVersionWithLabel; - /// /// Returns an opaque string that identifies the platform on which an app is running. /// diff --git a/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj b/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj index 72355db6c397d..94daeb2079a35 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj +++ b/src/libraries/System.Runtime/tests/System.Runtime.InteropServices.RuntimeInformation.Tests/System.Runtime.InteropServices.RuntimeInformation.Tests.csproj @@ -4,6 +4,9 @@ true $(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser + + $(DefineConstants);STABILIZE_PACKAGE_VERSION +