-
-
Notifications
You must be signed in to change notification settings - Fork 220
Build Variables Source Generator #4101
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
Open
aritchie
wants to merge
21
commits into
main
Choose a base branch
from
2927-aot_buildvars_sourcegen
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
6826c9f
Initial commit
aritchie a3d3a16
Update CHANGELOG.md
aritchie 48860aa
Updates path refs
aritchie 94800bb
Format code
getsentry-bot fe82638
Merge branch 'main' into pr/4101
jamescrosswell d79cf1f
Merge branch 'main' into 2927-aot_buildvars_sourcegen
aritchie 7c9f1e2
Package analyzer with sentry nuget
aritchie 5320e8a
add tests - having tooling issues lighting this up - trying in CI
aritchie c034685
Format code
getsentry-bot 09fba9e
Updates
aritchie f59f230
Merge branch '2927-aot_buildvars_sourcegen' of https://github.com/get…
aritchie a9ca3d2
Format code
getsentry-bot f5ea7a2
code review fix
aritchie 194041f
Update Sentry.SourceGenerators.csproj
aritchie 4ebc989
Update for unit tests
aritchie bc99a75
Fixes and name alignments
aritchie 4547af6
Fix bad bot changes
aritchie ca53bc4
Add disable sentry changes per code review as well as unit tests for it
aritchie 6a03323
Merge branch 'main' into 2927-aot_buildvars_sourcegen
aritchie 800c7bf
Try to revert bad bot
aritchie 214abda
Format code
getsentry-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/Sentry.SourceGenerators/BuildPropertySourceGenerator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Sentry.SourceGenerators; | ||
|
||
|
||
/// <summary> | ||
/// Generates the necessary msbuild variables | ||
/// </summary> | ||
[Generator(LanguageNames.CSharp)] | ||
public class BuildPropertySourceGenerator : ISourceGenerator | ||
{ | ||
/// <summary> | ||
/// Initialize the source gen | ||
/// </summary> | ||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Execute the source gen | ||
/// </summary> | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
var opts = context.AnalyzerConfigOptions.GlobalOptions; | ||
var properties = opts.Keys.Where(x => x.StartsWith("build_property.")).ToList(); | ||
if (properties.Count == 0) | ||
return; | ||
|
||
if (opts.TryGetValue("build_property.SentryDisableSourceGenerator", out var disable) && disable.Equals("true", StringComparison.InvariantCultureIgnoreCase)) | ||
return; | ||
|
||
// we only want to generate code where host setup takes place | ||
if (!opts.TryGetValue("build_property.outputtype", out var outputType)) | ||
return; | ||
|
||
if (!outputType.Equals("exe", StringComparison.InvariantCultureIgnoreCase)) | ||
return; | ||
|
||
var sb = new StringBuilder(); | ||
sb | ||
.Append( | ||
""" | ||
// <auto-generated> | ||
// Code generated by Sentry Source Generators | ||
// Changes may cause incorrect behavior and will be lost if the code is | ||
// regenerated. | ||
// </auto-generated> | ||
|
||
#if NET8_0_OR_GREATER | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Sentry; | ||
|
||
[global::System.Runtime.CompilerServices.CompilerGenerated] | ||
public static class BuildVariableInitializer | ||
{ | ||
[global::System.Runtime.CompilerServices.ModuleInitializer] | ||
public static void Initialize() | ||
{ | ||
global::Sentry.CompilerServices.BuildProperties.Initialize(new global::System.Collections.Generic.Dictionary<string, string> { | ||
|
||
""" | ||
); | ||
|
||
foreach (var property in properties) | ||
{ | ||
if (opts.TryGetValue(property, out var value)) | ||
{ | ||
var pn = EscapeString(property.Replace("build_property.", "")); | ||
var ev = EscapeString(value); | ||
sb | ||
.Append("\t\t\t{") | ||
.Append($"\"{pn}\", \"{ev}\"") | ||
.AppendLine("},"); | ||
} | ||
} | ||
|
||
sb | ||
.AppendLine("\t\t});") // close dictionary | ||
.AppendLine("\t}") | ||
.AppendLine("}") | ||
.AppendLine("#endif"); | ||
|
||
context.AddSource("__BuildProperties.g.cs", sb.ToString()); | ||
} | ||
|
||
|
||
private static string EscapeString(string value) => value.Replace("\\", "\\\\"); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Sentry.SourceGenerators/Sentry.SourceGenerators.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>false</ImplicitUsings> | ||
|
||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
<IsRoslynComponent>true</IsRoslynComponent> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" PrivateAssets="all"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Remove="System.Text.Json" /> | ||
<Using Remove="System.Text.Json.Serialization" /> | ||
</ItemGroup> | ||
</Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Sentry.Internal.Extensions; | ||
|
||
namespace Sentry.CompilerServices; | ||
|
||
/// <summary> | ||
/// This class is not meant for external usage | ||
/// </summary> | ||
public static class BuildProperties | ||
{ | ||
/// <summary> | ||
/// The Build Variables generated from you csproj file and initialized by the Sentry Source Generated Module Initializer | ||
/// </summary> | ||
public static IReadOnlyDictionary<string, string>? Values { get; private set; } | ||
|
||
/// <summary> | ||
/// This is called by a Sentry Source-Generator module initializers to help us determine things like | ||
/// Is your app AOT | ||
/// Has your application been trimmed | ||
/// What build configuration is being used | ||
/// </summary> | ||
/// <param name="properties"></param> | ||
public static void Initialize(Dictionary<string, string> properties) | ||
{ | ||
Values ??= properties.AsReadOnly(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project> | ||
<ItemGroup> | ||
<CompilerVisibleProperty Include="PublishAot" /> | ||
<CompilerVisibleProperty Include="UseDotNetNativeToolchain" /> | ||
<CompilerVisibleProperty Include="PublishTrimmed" /> | ||
<CompilerVisibleProperty Include="SentryDisableSourceGenerator" /> | ||
<CompilerVisibleProperty Include="Configuration" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
question: Do both
Sentry.SourceGenerators.targets
work?I thought that only
build/{PACKAGE-ID}.props
andbuild/{PACKAGE-ID}.targets
are picked up automatically from NuGet packages.I believe that - since we have all
CompilerVisibleProperty
inSentry.targets
too, we can removeSentry.SourceGenerators.targets
.Or am I wrong here? I'm a bit uncertain.
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.
This has long been an unknown. Sentry included its targets in both. I've notoriously included my targets in both in the past due to past msbuild issues.
The targets for the dll of the source generator have to be there or compilervisibleproperty is not seen. It doesn't source gen in the test projects with the current targets, I had to add this. Considering this file doesn't hurt anything and seems to fix any corner cases I've ever had, I think we should just move forward with this.