Skip to content

Commit

Permalink
Fix IsDirty generator when using a custom namespace
Browse files Browse the repository at this point in the history
In this case, the generator shouldn't emit anything at all, since the legacy generation will be in place, which already emits the right IsDirty boolean property.
  • Loading branch information
kzu committed Mar 22, 2023
1 parent 8303091 commit 48c7db4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Analyzer/CodeAnalysis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>GitInfo.CodeAnalysis</AssemblyName>
<TargetFramework>netstandard2.0</TargetFramework>
<PackFolder>analyzers/dotnet</PackFolder>
<PackFolder>analyzers/dotnet/roslyn4.0</PackFolder>
</PropertyGroup>

<ItemGroup>
Expand Down
17 changes: 11 additions & 6 deletions src/Analyzer/GitInfoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ class GitInfoGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterSourceOutput(
context.AdditionalTextsProvider
.Combine(context.AnalyzerConfigOptionsProvider)
.Combine(context.ParseOptionsProvider)
.Collect(),
(c, _) =>
var ns = context.AnalyzerConfigOptionsProvider
.Select((c, _) => c.GlobalOptions.TryGetValue("build_property.ThisAssemblyNamespace", out var ns)
&& !string.IsNullOrEmpty(ns) ? ns : null);

context.RegisterSourceOutput(ns,
(c, ns) =>
{
// Legacy codegen used for this scenario, emit nothing.
if (!string.IsNullOrEmpty(ns))
return;

c.AddSource("ThisAssembly.Git.IsDirty.g",
$$"""
//------------------------------------------------------------------------------
Expand All @@ -24,6 +28,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
partial class ThisAssembly
{
partial class Git
Expand Down
2 changes: 2 additions & 0 deletions src/GitInfo/buildMultiTargeting/GitInfo.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<!-- This makes sure we don't enforce SponsorLink transitively, but only when
consumers are directly referencing our package. -->
<SponsorablePackageId Include="GitInfo" />

<CompilerVisibleProperty Include="ThisAssemblyNamespace" />
</ItemGroup>

<Import Project="..\build\GitInfo.targets"/>
Expand Down
2 changes: 2 additions & 0 deletions src/GitInfo/buildTransitive/GitInfo.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<!-- This makes sure we don't enforce SponsorLink transitively, but only when
consumers are directly referencing our package. -->
<SponsorablePackageId Include="GitInfo" />

<CompilerVisibleProperty Include="ThisAssemblyNamespace" />
</ItemGroup>

<Import Project="..\build\GitInfo.targets"/>
Expand Down

0 comments on commit 48c7db4

Please sign in to comment.