Skip to content

Commit

Permalink
Moved some strings to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
rrmanzano committed Apr 25, 2022
1 parent 2ec4565 commit 7f9b96a
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@
{
public class AutoBindableConstants
{
public const string FullNameMauiControls = "Microsoft.Maui.Controls";

public const string ProjectName = "Maui.BindableProperty.Generator";

public const string AutoBindableAttributeName = "AutoBindableAttribute";
public const string AttrName = "AutoBindableAttribute";

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

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

public const string AttrPropertyName = "PropertyName";

public const string AttrOnChanged = "OnChanged";

public const string AttributeClassDisplayString = @$"{ProjectName}.Core.{AutoBindableAttributeName}";
public const string AttrDefaultValue = "DefaultValue";

public const string AttributeGeneratedCodeString = @$"[global::System.CodeDom.Compiler.GeneratedCode(""{ProjectName}"", null)]";
public const string AttrDefaultBindingMode = "DefaultBindingMode";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public AutoBindableAttribute(){}

public void Execute(GeneratorExecutionContext context)
{
context.EachField<AutoBindableSyntaxReceiver>(AutoBindableConstants.AttributeClassDisplayString, (attributeSymbol, group) => {
context.EachField<AutoBindableSyntaxReceiver>(AutoBindableConstants.AttrClassDisplayString, (attributeSymbol, group) => {
var classSource = this.ProcessClass(group.Key, group.ToList(), attributeSymbol, context);
context.AddSource($"{group.Key.Name}.generated.cs", SourceText.From(classSource, Encoding.UTF8));
});
Expand All @@ -53,7 +53,7 @@ private string ProcessClass(INamedTypeSymbol classSymbol, List<IFieldSymbol> fie
var w = new CodeWriter(CodeWriterSettings.CSharpDefault);
using (w.B(@$"namespace {namespaceName}"))
{
w._(AutoBindableConstants.AttributeGeneratedCodeString);
w._(AutoBindableConstants.AttrGeneratedCodeString);
using (w.B(@$"public partial class {classSymbol.Name}"))
{
// Create properties for each field
Expand Down Expand Up @@ -84,9 +84,9 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy

var bindablePropertyName = $@"{propertyName}Property";
var customParameters = this.ProcessBindableParameters();
w._(AutoBindableConstants.AttributeGeneratedCodeString);
w._($@"public static readonly Microsoft.Maui.Controls.BindableProperty {bindablePropertyName} = Microsoft.Maui.Controls.BindableProperty.Create(nameof({propertyName}), typeof({fieldType}), typeof({classSymbol.Name}){customParameters});");
w._(AutoBindableConstants.AttributeGeneratedCodeString);
w._(AutoBindableConstants.AttrGeneratedCodeString);
w._($@"public static readonly {AutoBindableConstants.FullNameMauiControls}.BindableProperty {bindablePropertyName} = {AutoBindableConstants.FullNameMauiControls}.BindableProperty.Create(nameof({propertyName}), typeof({fieldType}), typeof({classSymbol.Name}){customParameters});");
w._(AutoBindableConstants.AttrGeneratedCodeString);
using (w.B(@$"public {fieldType} {propertyName}"))
{
w._($@"get => ({fieldType})GetValue({bindablePropertyName});");
Expand All @@ -109,7 +109,7 @@ private void ProcessBindableProperty(CodeWriter w, IFieldSymbol fieldSymbol, ISy

private void InitializeAttrProperties(IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol)
{
this.NameProperty = fieldSymbol.GetTypedConstant(attributeSymbol, "PropertyName");
this.NameProperty = fieldSymbol.GetTypedConstant(attributeSymbol, AutoBindableConstants.AttrPropertyName);
this.CustomImplementations.ForEach(i => i.Initialize(this.NameProperty, fieldSymbol, attributeSymbol, classSymbol));
}

Expand Down Expand Up @@ -159,7 +159,7 @@ private string ChooseName(string fieldName, TypedConstant overridenNameOpt)
public void Initialize(GeneratorInitializationContext context)
{
// Register the attribute source
context.RegisterForPostInitialization((i) => i.AddSource("AutoBindableAttribute", attributeText));
context.RegisterForPostInitialization((i) => i.AddSource(AutoBindableConstants.AttrName, attributeText));

// Register a syntax receiver that will be created for each generation pass
context.RegisterForSyntaxNotifications(() => new AutoBindableSyntaxReceiver());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
// Get the symbol being declared by the field, and keep it if its annotated
if (context.SemanticModel.GetDeclaredSymbol(variable) is IFieldSymbol fieldSymbol &&
fieldSymbol.GetAttributes().Any(ad => ad?.AttributeClass?.ToDisplayString() == AutoBindableConstants.AttributeClassDisplayString))
fieldSymbol.GetAttributes().Any(ad => ad?.AttributeClass?.ToDisplayString() == AutoBindableConstants.AttrClassDisplayString))
{
Fields.Add(fieldSymbol);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Maui.BindableProperty.Generator.Core.BindableProperty.Implementation
{
public class DefaultBindingMode : IImplementation
{
private const string FullNameSpaceBindingMode = "Microsoft.Maui.Controls.BindingMode";
private const string FullNameSpaceBindingMode = $"{AutoBindableConstants.FullNameMauiControls}.BindingMode";
private TypedConstant DefaultBindingModeValueProperty { get; set; }
private IFieldSymbol FieldSymbol { get; set; }
private ISymbol AttributeSymbol { get; set; }
private INamedTypeSymbol ClassSymbol { get; set; }

public void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol)
{
this.DefaultBindingModeValueProperty = fieldSymbol.GetTypedConstant(attributeSymbol, "DefaultBindingMode");
this.DefaultBindingModeValueProperty = fieldSymbol.GetTypedConstant(attributeSymbol, AutoBindableConstants.AttrDefaultBindingMode);
this.FieldSymbol = fieldSymbol;
this.AttributeSymbol = attributeSymbol;
this.ClassSymbol = classSymbol;
Expand All @@ -28,7 +28,7 @@ public bool SetterImplemented()
public string ProcessBindableParameters()
{
var fieldType = this.FieldSymbol.Type;
var defaultValue = this.DefaultBindingModeValueProperty.Validate(value =>
var defaultValue = this.DefaultBindingModeValueProperty.GetValue<string>(value =>
{
if (!value.Contains(FullNameSpaceBindingMode))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class DefaultValue : IImplementation

public void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol)
{
this.DefaultValueProperty = fieldSymbol.GetTypedConstant(attributeSymbol, "DefaultValue");
this.DefaultValueProperty = fieldSymbol.GetTypedConstant(attributeSymbol, AutoBindableConstants.AttrDefaultValue);
this.FieldSymbol = fieldSymbol;
this.AttributeSymbol = attributeSymbol;
this.ClassSymbol = classSymbol;
Expand All @@ -27,7 +27,7 @@ public bool SetterImplemented()
public string ProcessBindableParameters()
{
var fieldType = this.FieldSymbol.Type;
var defaultValue = this.DefaultValueProperty.Validate(value =>
var defaultValue = this.DefaultValueProperty.GetValue<string>(value =>
{
if (value != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class PropertyChanged : IImplementation
public void Initialize(TypedConstant nameProperty, IFieldSymbol fieldSymbol, ISymbol attributeSymbol, INamedTypeSymbol classSymbol)
{
this.NameProperty = nameProperty;
this.OnChangedProperty = fieldSymbol.GetTypedConstant(attributeSymbol, "OnChanged");
this.OnChangedProperty = fieldSymbol.GetTypedConstant(attributeSymbol, AutoBindableConstants.AttrOnChanged);
this.FieldSymbol = fieldSymbol;
this.AttributeSymbol = attributeSymbol;
this.ClassSymbol = classSymbol;
Expand All @@ -28,7 +28,7 @@ public bool SetterImplemented()

public string ProcessBindableParameters()
{
return this.OnChangedProperty.Validate(methodName => {
return this.OnChangedProperty.GetValue<string>(methodName => {
return $@"propertyChanged: __{methodName}";
});
}
Expand All @@ -40,13 +40,13 @@ public void ProcessBodyStter(CodeWriter w)

public void ProcessImplementationLogic(CodeWriter w)
{
this.OnChangedProperty.Validate(methodName => {
this.OnChangedProperty.GetValue<string>(methodName => {
var methodDefinition = @$"private static void __{methodName}(Microsoft.Maui.Controls.BindableObject bindable, object oldValue, object newValue)";
if (w.ToString().Contains(methodDefinition))
return default;
w._(AutoBindableConstants.AttributeGeneratedCodeString);
w._(AutoBindableConstants.AttrGeneratedCodeString);
using (w.B(methodDefinition))
{
var methods = this.GetMethodsToCall(methodName);
Expand Down
22 changes: 19 additions & 3 deletions src/Maui.BindableProperty.Generator/Helpers/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,28 @@ public static bool IsStringType(this ITypeSymbol type)
return type.SpecialType == SpecialType.System_String;
}

public static string Validate(this TypedConstant TypedConstant, Func<string, string> onSuccess)
public static T GetValue<T>(
this IFieldSymbol fieldSymbol,
ISymbol attributeSymbol,
string key,
Func<T, T> onSuccess = null)
{
var typeConstant = fieldSymbol.GetTypedConstant(attributeSymbol, key);
return typeConstant.GetValue(onSuccess);
}

public static T GetValue<T>(
this TypedConstant TypedConstant,
Func<T, T> onSuccess = null)
{
if (!TypedConstant.IsNull)
{
var value = TypedConstant.Value?.ToString();
return onSuccess.Invoke(value);

var value = TypedConstant.Value;
if (value.GetType() == typeof(T))
{
return onSuccess.Invoke((T)value);
}
}

return default;
Expand Down
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.5.0</VersionPrefix>
<VersionPrefix>0.6.0</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 7f9b96a

Please sign in to comment.