Skip to content

Commit

Permalink
Ensure IsDirty is a constant
Browse files Browse the repository at this point in the history
Fixes the regression reported in #287.
Fixes #285.
  • Loading branch information
kzu committed Jun 5, 2023
1 parent b0eba91 commit 6e2383f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/Analyzer/GitInfoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@ class GitInfoGenerator : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var ns = context.AnalyzerConfigOptionsProvider
.Select((c, _) => c.GlobalOptions.TryGetValue("build_property.ThisAssemblyNamespace", out var ns)
&& !string.IsNullOrEmpty(ns) ? ns : null);
.Select((c, _) => new
{
Namespace = c.GlobalOptions.TryGetValue("build_property.ThisAssemblyNamespace", out var ns)
&& !string.IsNullOrEmpty(ns) ? ns : null,
IsDirty = c.GlobalOptions.TryGetValue("build_property.GitIsDirty", out var dirty)
&& dirty == "1" ? true : false
});

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

c.AddSource("ThisAssembly.Git.IsDirty.g",
Expand All @@ -36,7 +41,7 @@ partial class Git
/// <summary>
/// Gets whether the current repository is dirty.
/// </summary>
public static bool IsDirty => bool.TryParse(IsDirtyString, out var dirty) && dirty;
public const bool IsDirty = {{(state.IsDirty ? "true" : "false")}};
}
}
""");
Expand Down
8 changes: 6 additions & 2 deletions src/GitInfo/build/GitInfo.ThisAssembly.targets
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@
</PropertyGroup>

<Target Name="GitThisAssembly" DependsOnTargets="$(GitThisAssemblyDependsOn)"
BeforeTargets="PrepareConstants" Condition="'$(GitThisAssembly)' == 'true'">
BeforeTargets="PrepareConstants;GenerateMSBuildEditorConfigFileShouldRun" Condition="'$(GitThisAssembly)' == 'true'">

<ItemGroup>
<Constant Include="IsDirtyString" Value="$(IsDirtyString)" Root="Git" />
<CompilerVisibleProperty Include="GitIsDirty" />

<Constant Include="IsDirtyString" Value="true" Root="Git" Condition="$(GitIsDirty) == '1'" />
<Constant Include="IsDirtyString" Value="false" Root="Git" Condition="$(GitIsDirty) == '0'" />

<Constant Include="RepositoryUrl" Value="$(GitRepositoryUrl)" Root="Git" />

<Constant Include="Branch" Value="$(GitBranch)" Root="Git" />
Expand Down

0 comments on commit 6e2383f

Please sign in to comment.