Skip to content

Commit

Permalink
Added "ExcludeFromCodeCoverage" attribute to generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Jul 27, 2022
1 parent 0eac337 commit a749ac7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public class AutoBindableConstants

public const string AttrClassDisplayString = @$"{ProjectName}.Core.{AttrName}";

public const string AttrGeneratedCodeString = @$"[global::System.CodeDom.Compiler.GeneratedCode(""{ProjectName}"", null)]";
public const string AttrGeneratedCodeString = @$"[global::System.CodeDom.Compiler.GeneratedCode(""{{GeneratorFullName}}"", ""{{Version}}"")]";

public const string AttrExcludeFromCodeCoverageString = "[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]";

public const string AttrPropertyName = "PropertyName";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fie

var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
var w = new CodeWriter(CodeWriterSettings.CSharpDefault);
w._("// <auto-generated/>");
w._("#pragma warning disable");
w._("#nullable enable");
using (w.B(@$"namespace {namespaceName}"))
{
w._(AutoBindableConstants.AttrGeneratedCodeString);
using (w.B(@$"public partial class {classSymbol.Name}"))
{
// Create properties for each field
Expand Down Expand Up @@ -94,7 +94,8 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy
var hidesUnderlying = applyHidesUnderlying ? " new" : string.Empty;
var declaringType = fieldType.WithNullableAnnotation(NullableAnnotation.None);
var parameters = $"nameof({propertyName}),typeof({declaringType}),typeof({classSymbol.Name}){customParameters}".Split(',');
w._(AutoBindableConstants.AttrGeneratedCodeString);

w._(AttributeBuilder.GetAttrGeneratedCodeString());
w._($@"public static{hidesUnderlying} readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} =");
w._($"{w.GetIndentString(6)}{AutoBindableConstants.FullNameMauiControls}.BindableProperty.Create(");

Expand All @@ -106,7 +107,7 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy
}

w._();
w._(AutoBindableConstants.AttrGeneratedCodeString);
AttributeBuilder.WriteAllAttrGeneratedCodeStrings(w);
using (w.B(@$"public{hidesUnderlying} {fieldType} {propertyName}"))
{
w._($@"get => ({fieldType})GetValue({bindablePropertyName});");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void ProcessImplementationLogic(CodeWriter w)
if (w.ToString().Contains(methodDefinition))
return default;
w._(AutoBindableConstants.AttrGeneratedCodeString);
AttributeBuilder.WriteAllAttrGeneratedCodeStrings(w);
using (w.B(methodDefinition))
{
var methods = this.GetMethodsToCall(methodName);
Expand Down
20 changes: 20 additions & 0 deletions src/Maui.BindableProperty.Generator/Helpers/AttributeBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Maui.BindableProperty.Generator.Core.BindableProperty;

namespace Maui.BindableProperty.Generator.Helpers;

public static class AttributeBuilder
{
public static string GetAttrGeneratedCodeString()
{
var version = typeof(AutoBindablePropertyGenerator).Assembly.GetName().Version.ToString();
var genertorFullName = typeof(AutoBindablePropertyGenerator).FullName;
var generatedCodeString = AutoBindableConstants.AttrGeneratedCodeString.Replace("{GeneratorFullName}", genertorFullName).Replace("{Version}", version);
return generatedCodeString;
}

public static void WriteAllAttrGeneratedCodeStrings(CodeWriter w)
{
w._(GetAttrGeneratedCodeString());
w._(AutoBindableConstants.AttrExcludeFromCodeCoverageString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Description>Source generator that automatically transforms fields into BindableProperties that can be used in MAUI</Description>
<PackageTags>MAUI;BindableProperty;Source Generator</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<VersionPrefix>0.8.2</VersionPrefix>
<VersionPrefix>0.8.3</VersionPrefix>
<PackageProjectUrl>https://github.com/rrmanzano/maui-bindableproperty-generator</PackageProjectUrl>
<RepositoryUrl>https://github.com/rrmanzano/maui-bindableproperty-generator</RepositoryUrl>
<PackageId>M.BindableProperty.Generator</PackageId>
Expand Down

0 comments on commit a749ac7

Please sign in to comment.