From a749ac73adeda0d4183d2fdf164fd5263e752954 Mon Sep 17 00:00:00 2001 From: rrmanzano Date: Wed, 27 Jul 2022 18:20:57 -0500 Subject: [PATCH] Added "ExcludeFromCodeCoverage" attribute to generated code --- .../BindableProperty/AutoBindableConstants.cs | 4 +++- .../AutoBindablePropertyGenerator.cs | 7 ++++--- .../Implementation/PropertyChanged.cs | 2 +- .../Helpers/AttributeBuilder.cs | 20 +++++++++++++++++++ .../Maui.BindableProperty.Generator.csproj | 2 +- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/Maui.BindableProperty.Generator/Helpers/AttributeBuilder.cs diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindableConstants.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindableConstants.cs index 9786464..4675bc1 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindableConstants.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindableConstants.cs @@ -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"; diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs index 01e4016..8e8f2e9 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/AutoBindablePropertyGenerator.cs @@ -55,11 +55,11 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List fie var namespaceName = classSymbol.ContainingNamespace.ToDisplayString(); var w = new CodeWriter(CodeWriterSettings.CSharpDefault); + w._("// "); 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 @@ -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("); @@ -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});"); diff --git a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs index 63034c1..c60da07 100644 --- a/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs +++ b/src/Maui.BindableProperty.Generator/Core/BindableProperty/Implementation/PropertyChanged.cs @@ -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); diff --git a/src/Maui.BindableProperty.Generator/Helpers/AttributeBuilder.cs b/src/Maui.BindableProperty.Generator/Helpers/AttributeBuilder.cs new file mode 100644 index 0000000..f6f5057 --- /dev/null +++ b/src/Maui.BindableProperty.Generator/Helpers/AttributeBuilder.cs @@ -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); + } +} \ No newline at end of file diff --git a/src/Maui.BindableProperty.Generator/Maui.BindableProperty.Generator.csproj b/src/Maui.BindableProperty.Generator/Maui.BindableProperty.Generator.csproj index 9c712ec..7a4c39e 100644 --- a/src/Maui.BindableProperty.Generator/Maui.BindableProperty.Generator.csproj +++ b/src/Maui.BindableProperty.Generator/Maui.BindableProperty.Generator.csproj @@ -10,7 +10,7 @@ Source generator that automatically transforms fields into BindableProperties that can be used in MAUI MAUI;BindableProperty;Source Generator README.md - 0.8.2 + 0.8.3 https://github.com/rrmanzano/maui-bindableproperty-generator https://github.com/rrmanzano/maui-bindableproperty-generator M.BindableProperty.Generator