-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Changes from all commits
c398549
0bbf65b
db1420d
c1ce415
bd4f66e
4165c9c
a031659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Generators | ||
{ | ||
[Generator] | ||
public partial class ProductVersionInfoGenerator : IIncrementalGenerator | ||
{ | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
context.RegisterPostInitializationOutput(ctx => | ||
{ | ||
string? informationalVersion = typeof(ProductVersionInfoGenerator).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; | ||
|
||
// strip semver metadata (git hash) followed by + sign | ||
string? productVersion = informationalVersion?.Split('+')?[0]; | ||
|
||
if (string.IsNullOrEmpty(productVersion)) | ||
throw new InvalidOperationException($"Unable to obtain product version at build-time."); | ||
|
||
// strip semver prerelease label followed by - sign for Environment.Version | ||
Version versionObject = Version.Parse(productVersion.Split('-')[0]); | ||
|
||
ctx.AddSource("ProductVersionInfo.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.ToString().Replace(".", ", ")}); | ||
}} | ||
}} | ||
|
||
namespace System.Runtime.InteropServices | ||
{{ | ||
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}""; | ||
}} | ||
}}"); | ||
}); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
</PropertyGroup> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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="ProductVersionInfoGenerator.cs" /> | ||
<Compile Include="EventSourceGenerator.cs" /> | ||
<Compile Include="EventSourceGenerator.Emitter.cs" /> | ||
<Compile Include="EventSourceGenerator.Parser.cs" /> | ||
|
@@ -20,4 +21,5 @@ | |
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" PrivateAssets="all" Version="$(MicrosoftCodeAnalysisVersion_LatestVS)" /> | ||
</ItemGroup> | ||
|
||
</Project> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how our build system handles this. Is this guaranteed to always match what typeof(object).Assembly would in the built corelib?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell, the build system stamps the exact same
AssemblyInformationalVersionAttribute
on all binaries.