Skip to content

Commit

Permalink
Address CR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed May 14, 2024
1 parent db1420d commit 80d8f1f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<AdditionalText> textFiles = context.AdditionalTextsProvider.Where(static file => file.Path.EndsWith(".versionstring"));
IncrementalValueProvider<(Compilation Left, ImmutableArray<AdditionalText> Right)> compilationAndFiles =
context.CompilationProvider.Combine(textFiles.Collect());

context.RegisterSourceOutput(compilationAndFiles, (spc, content) =>
context.RegisterPostInitializationOutput(ctx =>
{
string? informationalVersion = typeof(DotnetProductVersionInfoGenerator).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.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.");
spc.AddSource("Environment.DotnetProductVersionInfo.g.cs", $@"
// <auto-generated/>
// strip semver prerelease label followed by - sign for Environment.Version
Version versionObject = Version.Parse(productVersion.Split('-')?[0]);
namespace System;
ctx.AddSource("DotnetProductVersionInfo.g.cs", $@"// <auto-generated/>
namespace System
{{
public static partial class Environment
{{
/// <summary>
/// Gets a version consisting of the major, minor, build, and revision numbers of the common language runtime.
/// </summary>
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
{{
/// <summary>
/// Gets the name of the .NET installation on which an app is running.
/// </summary>
public static string FrameworkDescription => "".NET {productVersion}"";
}}
}}");
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2783,7 +2783,4 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(Version).versionstring" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ public static partial class RuntimeInformation
{
private static string? s_runtimeIdentifier;

/// <summary>
/// Gets the name of the .NET installation on which an app is running.
/// </summary>
public static string FrameworkDescription { get; } = ".NET " + Environment.ProductVersionWithLabel;

/// <summary>
/// Returns an opaque string that identifies the platform on which an app is running.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-unix;$(NetCoreAppCurrent)-browser</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<DefineConstants Condition="'$(StabilizePackageVersion)' == 'true'">$(DefineConstants);STABILIZE_PACKAGE_VERSION</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="CheckArchitectureTests.cs" />
Expand Down

0 comments on commit 80d8f1f

Please sign in to comment.