Skip to content

Commit

Permalink
Add an easy way to check for design-time builds
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Sep 3, 2024
1 parent a0cbdfb commit 013b7a7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion samples/dotnet/Analyzer/StatusReportingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var (manifests, options) = source;
var status = Diagnostics.GetOrSetStatus(manifests, options);
spc.AddSource("StatusReporting.cs", $"// Status: {status}");
spc.AddSource("StatusReporting.cs",
$"""
// Status: {status}
// DesignTimeBuild: {options.IsDesignTimeBuild()}
""");
spc.ReportDiagnostic(Diagnostic.Create("SL200", "Compiler", "Don't disable me!",
DiagnosticSeverity.Warning,
DiagnosticSeverity.Warning, true, 1));
});
}
}
18 changes: 18 additions & 0 deletions samples/dotnet/SponsorLink/AnalyzerOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.CodeAnalysis.Diagnostics;

static class AnalyzerOptionsExtensions
{
/// <summary>
/// Gets whether the current build is a design-time build.
/// </summary>
public static bool IsDesignTimeBuild(this AnalyzerConfigOptionsProvider options) =>
options.GlobalOptions.TryGetValue("build_property.DesignTimeBuild", out var value) &&
bool.TryParse(value, out var isDesignTime) && isDesignTime;

/// <summary>
/// Gets whether the current build is a design-time build.
/// </summary>
public static bool IsDesignTimeBuild(this AnalyzerConfigOptions options) =>
options.TryGetValue("build_property.DesignTimeBuild", out var value) &&
bool.TryParse(value, out var isDesignTime) && isDesignTime;
}

0 comments on commit 013b7a7

Please sign in to comment.