Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reflection from FrameworkDescription #102164

Merged
merged 7 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Immutable;
using System.IO;
using Microsoft.CodeAnalysis;

namespace Generators
{
[Generator]
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) =>
{
// strip semver metadata (git hash) followed by + sign
string? productVersion = Path.GetFileNameWithoutExtension(content.Right[0].Path.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/>

namespace System;

public static partial class Environment
{{
internal const string ProductVersionWithLabel = ""{productVersion}"";
private const string ProductVersionWithoutLabel = ""{productVersion.Split('-')?[0]}"";
}}");
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note, <TargetFramework>netstandard2.0</TargetFramework> this can probably use $(NetCoreAppCurrent) as it is only used by corelib building for current version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be cases where the generator runs on .NET Framework if people open the project in Visual Studio.


<ItemGroup>
<Compile Include="DotnetProductVersionInfoGenerator.cs" />
<Compile Include="EventSourceGenerator.cs" />
<Compile Include="EventSourceGenerator.Emitter.cs" />
<Compile Include="EventSourceGenerator.Parser.cs" />
Expand All @@ -20,4 +21,5 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2783,4 +2783,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnaryPlusOperators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Numerics\IUnsignedNumber.cs" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="$(Version).versionstring" />
jkotas marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>
</Project>
18 changes: 1 addition & 17 deletions src/libraries/System.Private.CoreLib/src/System/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,7 @@ public static OperatingSystem OSVersion
}
}

public static Version Version
{
get
{
string? versionString = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;

ReadOnlySpan<char> versionSpan = versionString.AsSpan();

// Strip optional suffixes
int separatorIndex = versionSpan.IndexOfAny('-', '+', ' ');
if (separatorIndex >= 0)
versionSpan = versionSpan.Slice(0, separatorIndex);

// Return zeros rather then failing if the version string fails to parse
return Version.TryParse(versionSpan, out Version? version) ? version : new Version();
}
}
public static Version Version => Version.Parse(ProductVersionWithoutLabel);
am11 marked this conversation as resolved.
Show resolved Hide resolved

public static string StackTrace
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Reflection;

namespace System.Runtime.InteropServices
{
public static partial class RuntimeInformation
{
private const string FrameworkName = ".NET";
private static string? s_frameworkDescription;
private static string? s_runtimeIdentifier;

public static string FrameworkDescription
{
get
{
if (s_frameworkDescription == null)
{
ReadOnlySpan<char> versionString = typeof(object).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;

// Strip the git hash if there is one
int plusIndex = versionString.IndexOf('+');
if (plusIndex >= 0)
{
versionString = versionString.Slice(0, plusIndex);
}

s_frameworkDescription = !versionString.Trim().IsEmpty ? $"{FrameworkName} {versionString}" : FrameworkName;
}

return s_frameworkDescription;
}
}
/// <summary>
/// Gets the name of the .NET installation on which an app is running.
/// </summary>
public static string FrameworkDescription { get; } = ".NET " + Environment.ProductVersionWithLabel;
am11 marked this conversation as resolved.
Show resolved Hide resolved

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