Skip to content

Commit

Permalink
More fixes and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
adospace committed Jan 2, 2024
1 parent 335d8ad commit ffeef86
Show file tree
Hide file tree
Showing 16 changed files with 1,201 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
env:
Solution_Name: ./src/MauiReactor.Build.sln
TemplatePack_Name: ./src/MauiReactor.TemplatePack/MauiReactor.TemplatePack.csproj
Version: 2.0.13-beta
Version: 2.0.14-beta

steps:
- name: Checkout
Expand Down
9 changes: 9 additions & 0 deletions samples/UnitTests/TestScaffoldGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ partial class MyComponent
[Prop]
Action<int?>? _myProp10;
[Prop("MyTestMethod")]
int _myProp11;
}
""");

Expand Down Expand Up @@ -106,6 +109,12 @@ public MyComponent MyProp10(Action<int?>? propValue)
_myProp10 = propValue;
return this;
}
public MyComponent MyTestMethod(int propValue)
{
_myProp11 = propValue;
return this;
}
public MyComponent MyProp2(int? propValue)
{
Expand Down
73 changes: 72 additions & 1 deletion src/MauiReactor.Scaffold/ComponentTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,80 @@ public partial class Component

#line default
#line hidden
this.Write(";\r\n }\r\n\r\n ");
this.Write(";\r\n }\r\n \r\n ");

#line 48 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
if (typeof(Element).IsAssignableFrom(type)) {

#line default
#line hidden
this.Write(" public static ");

#line 49 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

#line default
#line hidden
this.Write(" ");

#line 49 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(type.Name));

#line default
#line hidden
this.Write("(Action<");

#line 49 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(type.FullName));

#line default
#line hidden
this.Write("?> componentRefAction, params VisualNode?[]? children)\r\n {\r\n var @");

#line 51 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(type.Name.ToLower()));

#line default
#line hidden
this.Write(" = GetNodeFromPool<");

#line 51 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(typeName));

#line default
#line hidden
this.Write(">();\r\n @");

#line 52 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(type.Name.ToLower()));

#line default
#line hidden
this.Write(".ComponentRefAction = componentRefAction;\r\n if (children != null)\r\n " +
" {\r\n @");

#line 55 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(type.Name.ToLower()));

#line default
#line hidden
this.Write(".AddChildren(children);\r\n }\r\n return @");

#line 57 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(type.Name.ToLower()));

#line default
#line hidden
this.Write(";\r\n }\r\n ");

#line 59 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
}

#line default
#line hidden
this.Write(" ");

#line 60 "C:\Source\github\reactorui-maui\src\MauiReactor.Scaffold\ComponentTypeGenerator.tt"
}

#line default
Expand Down
14 changes: 13 additions & 1 deletion src/MauiReactor.Scaffold/ComponentTypeGenerator.tt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ public partial class Component
@<#= type.Name.ToLower() #>.ComponentRefAction = componentRefAction;
return @<#= type.Name.ToLower() #>;
}


<# if (typeof(Element).IsAssignableFrom(type)) { #>
public static <#= typeName #> <#= type.Name #>(Action<<#= type.FullName #>?> componentRefAction, params VisualNode?[]? children)
{
var @<#= type.Name.ToLower() #> = GetNodeFromPool<<#= typeName #>>();
@<#= type.Name.ToLower() #>.ComponentRefAction = componentRefAction;
if (children != null)
{
@<#= type.Name.ToLower() #>.AddChildren(children);
}
return @<#= type.Name.ToLower() #>;
}
<# } #>
<# } #>
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public virtual string TransformText()
this.Write(" ");

#line 30 "C:\Source\github\reactorui-maui\src\MauiReactor.ScaffoldGenerator\ComponentPartialClassGenerator.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(GetPropMethodName(fieldItem.FieldName)));
this.Write(this.ToStringHelper.ToStringWithCulture(fieldItem.GetPropMethodName()));

#line default
#line hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@ public string TransformAndPrettify()
return $"// <auto-generated />{Environment.NewLine}{ret}";
}

private string GetPropMethodName(string fieldName)
{
fieldName = fieldName.TrimStart('_');
fieldName = char.ToUpper(fieldName[0]) + fieldName.Substring(1);
return fieldName;
}

private IEnumerable<GeneratorFieldItem> GetInjectFields()
{
return _classItem.FieldItems.Where(_ => _.Value.Type == FieldAttributeType.Inject).OrderBy(_ => _.Key).Select(_=>_.Value);
}

private IEnumerable<GeneratorFieldItem> GetPropFields()
{
return _classItem.FieldItems.Where(_ => _.Value.Type == FieldAttributeType.Prop).OrderBy(_ => _.Key).Select(_ => _.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace <#= _classItem.Namespace #>
<# } #>

<# foreach (var fieldItem in GetPropFields()) { #>
public <#= _classItem.ClassName #> <#= GetPropMethodName(fieldItem.FieldName) #>(<#= fieldItem.FieldTypeFullyQualifiedName #> propValue)
public <#= _classItem.ClassName #> <#= fieldItem.GetPropMethodName() #>(<#= fieldItem.FieldTypeFullyQualifiedName #> propValue)
{
<#= fieldItem.FieldName #> = propValue;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForPostInitialization((i) => i.AddSource("ComponentAttributes.g.cs", @"using System;
#nullable enable
namespace MauiReactor
{
[AttributeUsage(AttributeTargets.Field)]
Expand All @@ -33,7 +34,7 @@ public InjectAttribute()
[AttributeUsage(AttributeTargets.Field)]
internal class PropAttribute : Attribute
{
public PropAttribute()
public PropAttribute(string? methodName = null)
{
}
}
Expand Down Expand Up @@ -118,15 +119,33 @@ void generateClassItem(FieldDeclarationSyntax fieldDeclaration, FieldAttributeTy
generatingClassItems[fullyQualifiedTypeName] = generatingClassItem = new GeneratorClassItem(namespaceName, className);
}

foreach (var variableFieldName in fieldDeclaration.Declaration.Variables.Select(_=>_.Identifier.ValueText))
foreach (var variableFieldSyntax in fieldDeclaration.Declaration.Variables)
{
var variableFieldName = variableFieldSyntax.Identifier.ValueText;

if (generatingClassItem.FieldItems.ContainsKey(variableFieldName))
{
return;
}

string? methodName = null;
if (attributeType == FieldAttributeType.Prop)
{
if (semanticModel.GetDeclaredSymbol(variableFieldSyntax)
is IFieldSymbol variableDeclaratorFieldSymbol)
{
var propAttributeData = variableDeclaratorFieldSymbol.GetAttributes()
.FirstOrDefault(_ => _.AttributeClass?.Name == "PropAttribute" || _.AttributeClass?.Name == "Prop");

if (propAttributeData?.ConstructorArguments.Length > 0)
{
methodName = propAttributeData.ConstructorArguments[0].Value?.ToString();
}
}
}

generatingClassItem.FieldItems[variableFieldName]
= new GeneratorFieldItem(variableFieldName, fieldTypeFullyQualifiedName, attributeType);
= new GeneratorFieldItem(variableFieldName, fieldTypeFullyQualifiedName, attributeType, methodName);
}
}

Expand Down Expand Up @@ -201,17 +220,33 @@ public GeneratorClassItem(string @namespace, string className)

public class GeneratorFieldItem
{
public GeneratorFieldItem(string fieldName, string fieldTypeFullyQualifiedName, FieldAttributeType type)
private readonly string? _propMethodName;

public GeneratorFieldItem(string fieldName, string fieldTypeFullyQualifiedName, FieldAttributeType type, string? propMethodName)
{
FieldName = fieldName;
FieldTypeFullyQualifiedName = fieldTypeFullyQualifiedName;
Type = type;
_propMethodName = propMethodName;
}

public string FieldName { get; }

public string FieldTypeFullyQualifiedName { get; }

public FieldAttributeType Type { get; }

public string GetPropMethodName()
{
if (_propMethodName != null)
{
return _propMethodName;
}

var fieldName = FieldName.TrimStart('_');
fieldName = char.ToUpper(fieldName[0]) + fieldName.Substring(1);
return fieldName;
}
}

public enum FieldAttributeType
Expand Down
6 changes: 3 additions & 3 deletions src/MauiReactor/Component.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ object IComponentWithProps.Props

internal override void MergeWith(VisualNode newNode)
{
if (!_derivedProps && newNode is IComponentWithProps newComponentWithProps)
if (!_derivedProps && newNode != this && newNode is IComponentWithProps newComponentWithProps)
{
if (newNode.GetType() == GetType())
{
Expand Down Expand Up @@ -311,7 +311,7 @@ protected Component(S? state = null, P? props = null)
: base(props)
{
_state = state;
_derivedState = state != null;
_derivedState = state != null;
}

internal override void Reset()
Expand Down Expand Up @@ -458,7 +458,7 @@ protected virtual void SetState(Action<S> action, bool invalidateComponent = tru

internal override void MergeWith(VisualNode newNode)
{
if (!_derivedState && newNode is IComponentWithState newComponentWithState)
if (!_derivedState && newNode != this && newNode is IComponentWithState newComponentWithState)
{
_newComponent = newComponentWithState;
if (newNode.GetType() == this.GetType())
Expand Down
Loading

0 comments on commit ffeef86

Please sign in to comment.