Skip to content

Commit 9b8c417

Browse files
committed
Add overload for GetBoolMSBuildProperty with default value
Introduces a new overload of GetBoolMSBuildProperty in AnalyzerConfigOptionsProviderExtensions to allow specifying a default value if the property is missing or empty. Updates PolyfillsGenerator to use this overload for UseEmbeddedAttributeForGeneratedTypes, defaulting to true.
1 parent 1080617 commit 9b8c417

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/PolySharp.SourceGenerators/Extensions/AnalyzerConfigOptionsProviderExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ public static bool GetBoolMSBuildProperty(this AnalyzerConfigOptionsProvider opt
4646
string.Equals(propertyValue, bool.TrueString, StringComparison.OrdinalIgnoreCase);
4747
}
4848

49+
/// <summary>
50+
/// Gets the value of a <see cref="bool"/> MSBuild property.
51+
/// </summary>
52+
/// <param name="options">The input <see cref="AnalyzerConfigOptionsProvider"/> instance.</param>
53+
/// <param name="propertyName">The MSBuild property name.</param>
54+
/// <param name="defaultValue">The default value for the property, if missing.</param>
55+
/// <returns>The value of the specified MSBuild property, or <paramref name="defaultValue"/>.</returns>
56+
public static bool GetBoolMSBuildProperty(this AnalyzerConfigOptionsProvider options, string propertyName, bool defaultValue = false)
57+
{
58+
if (!options.GlobalOptions.TryGetValue($"build_property.{propertyName}", out string? propertyValue))
59+
{
60+
return defaultValue;
61+
}
62+
63+
if (string.IsNullOrEmpty(propertyValue))
64+
{
65+
return defaultValue;
66+
}
67+
68+
return string.Equals(propertyValue, bool.TrueString, StringComparison.OrdinalIgnoreCase);
69+
}
70+
4971
/// <summary>
5072
/// Gets the value of an MSBuild property representing a semicolon-separated list of <see cref="string"/>-s.
5173
/// </summary>

src/PolySharp.SourceGenerators/PolyfillsGenerator.Polyfills.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static GenerationOptions GetGenerationOptions(AnalyzerConfigOptionsProvi
8686
bool usePublicAccessibilityForGeneratedTypes = options.GetBoolMSBuildProperty(PolySharpMSBuildProperties.UsePublicAccessibilityForGeneratedTypes);
8787

8888
// Check whether to emit the '[Embedded]' attribute and apply it to generated types
89-
bool useEmbeddedAttributeForGeneratedTypes = options.GetBoolMSBuildProperty(PolySharpMSBuildProperties.UseEmbeddedAttributeForGeneratedTypes);
89+
bool useEmbeddedAttributeForGeneratedTypes = options.GetBoolMSBuildProperty(PolySharpMSBuildProperties.UseEmbeddedAttributeForGeneratedTypes, defaultValue: true);
9090

9191
// Do the same as above for all other available boolean properties
9292
bool includeRuntimeSupportedAttributes = options.GetBoolMSBuildProperty(PolySharpMSBuildProperties.IncludeRuntimeSupportedAttributes);

0 commit comments

Comments
 (0)