-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComponentGenerator.cs
217 lines (201 loc) · 9.39 KB
/
ComponentGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Input;
using Xamarin.Forms;
namespace LivingThing.XamarinRazor
{
public class ComponentGenerator
{
static string CodeFormat = @"
using Microsoft.AspNetCore.Components;
using LivingThing.Core.Frameworks.XamarinRazor.Forms;
namespace {0}
{{
public partial class {1} : {2}<{3}, {1}>
{{
public {1}()
{{
}}
public {1}({3} _element):base(_element)
{{
}}
{4}
}}
}}
";
public ComponentGenerator(string classNamespace, Type elementType, ComponentGenerator parent = null)
{
Namespace = classNamespace;
Parent = parent;
ElementType = elementType;
}
public string Namespace { get; }
public Type ElementType { get; }
ComponentGenerator Parent { get; }
protected int Depth
{
get
{
int d = 0;
ComponentGenerator par = Parent;
while (par != null)
{
d++;
par = par.Parent;
}
return d;
}
}
protected string Tabs
{
get
{
string tabs = "";
int d = Depth;
while (d-- > 0)
{
tabs += "\t";
}
return tabs;
}
}
protected static string GetTypeName(Type type)
{
if (type.IsGenericType)
{
return $"{type.FullName.Split(new char[] { '`' })[0]}<{string.Join(", ", type.GetGenericArguments().Select(arg => GetTypeName(arg)))}>";
}
return type.FullName.Replace("+", ".");
}
static string[] cSharpReservedNames = new string[] { "class" };
protected static string GetProperyName(string name)
{
if (cSharpReservedNames.Contains(name))
{
return "@" + name;
}
return name;
}
protected virtual string CodeFormatString => CodeFormat;
protected virtual string GenerateItem(string propertyType, PropertyInfo property)
{
return $"\t\t[Parameter] public {propertyType} this[{string.Join(", ", property.GetIndexParameters().Select(p => $"{GetTypeName(p.ParameterType)} {p.Name}"))}] {{ set => P[{string.Join(", ", property.GetIndexParameters().Select(p => p.Name))}] = value; get => P[{string.Join(", ", property.GetIndexParameters().Select(p => p.Name))}]; }}";
}
protected virtual string GenerateProperty(string propertyType, PropertyInfo property)
{
return $"\t\t[Parameter] public {propertyType} {GetProperyName(property.Name)} {{ set => P.{GetProperyName(property.Name)} = value; get => P.{GetProperyName(property.Name)}; }}";
}
protected virtual string GenerateEventHandler(MethodInfo method)
{
var parameter = method.GetParameters()[0];
var genericArgs = parameter.ParameterType.GetGenericArguments();
var genericArg = genericArgs[genericArgs.Length - 1];
string fieldType = GetTypeName(genericArg);
//string fieldType = GetTypeName(parameter.ParameterType);
return $"\t\t[Parameter] public System.EventHandler<{fieldType}> {method.Name.Replace("add_", "")} {{ set => P.{method.Name.Replace("add_", "")} += value; }}";
}
protected virtual string GenerateEventCallbackFromEventHandler(MethodInfo method)
{
var parameter = method.GetParameters()[0];
var genericArgs = parameter.ParameterType.GetGenericArguments();
var genericArg = genericArgs[genericArgs.Length - 1];
string fieldType = GetTypeName(genericArg);
string methodName = method.Name.Replace("add_", "");
return $"\t\tEventCallback<{fieldType}> _on{methodName};\r\n" +
$"\t\t[Parameter] public EventCallback<{fieldType}> On{methodName} {{ set {{ if (!_on{methodName}.HasDelegate) {{ P.{methodName} += (s, e) => _on{methodName}.InvokeAsync(e); }} _on{methodName} = value; }} }}";
// return $"\t\t[Parameter] public EventCallback<{fieldType}> On{method.Name.Replace("add_", "")} {{ set => P.{method.Name.Replace("add_", "")} += (s, e) => value.InvokeAsync(e); }}";
}
protected virtual string GenerateEventCallback(PropertyInfo property)
{
return $"\t\t[Parameter] public EventCallback On{property.Name} {{ set {{ {property.Name} = new Xamarin.Forms.Command(async () => {{ await value.InvokeAsync(this); }}); }} }}";
}
protected virtual string GenerateBindableProperty(FieldInfo field)
{
var name = field.Name.Replace("Property", "");
return $"\t\t[Parameter] public Xamarin.Forms.Binding Bind{name} {{ set {{ P.SetBinding({ElementType.FullName}.{field.Name}, value); }} }}";
}
public string Generate(string outputPath)
{
List<Type> innerGenerateComponents = new List<Type>();
var properties = string.Join("\r\n" + Tabs, ElementType.GetMembers(BindingFlags.Instance | BindingFlags.Public)
.Where(member =>
{
if (member is PropertyInfo p)
{
return p.CanWrite && p.CanRead && (p.GetSetMethod()?.IsPublic ?? false) && p.GetCustomAttribute<ObsoleteAttribute>() == null;
}
if (member is MethodInfo method)
{
var parameters = method.GetParameters();
return parameters.Length == 1 && (parameters[0].ParameterType is EventHandler || (parameters[0].ParameterType.IsGenericType && parameters[0].ParameterType.GetGenericTypeDefinition() == typeof(EventHandler<>)));
}
return false;
})
.SelectMany<MemberInfo, string>(member =>
{
List<string> returns = new List<string>();
if (member is PropertyInfo property)
{
string propertyType = GetTypeName(property.PropertyType);
if (!typeof(Element).IsAssignableFrom(property.PropertyType) && property.PropertyType.IsClass && !property.PropertyType.IsPrimitive && property.PropertyType.GetConstructor(new Type[0]) != null && property.PropertyType != typeof(object))
{
//if (!innerGenerateComponents.Contains(property.PropertyType))
//{
// innerGenerateComponents.Add(property.PropertyType);
// var generator = new InternalComponentGenerator(property.PropertyType, this);
// returns.Add(generator.Generate());
//}
if (!innerGenerateComponents.Contains(property.PropertyType))
{
innerGenerateComponents.Add(property.PropertyType);
var generator = new ParameterComponentGenerator(Namespace, property.PropertyType, null);
generator.Generate(outputPath);
}
//propertyType = propertyType.Split(new char[] { '.' }).Last()+"Parameter";
}
if (property.Name == "Item")
{
//returns.Add(GenerateItem(propertyType, property));
}
else
{
returns.Add(GenerateProperty(propertyType, property));
FieldInfo bindable;
if ((bindable = ElementType.GetField(property.Name + "Property", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy)) != null)
{
returns.Add(GenerateBindableProperty(bindable));
}
}
if (property.PropertyType == typeof(ICommand))
{
returns.Add(GenerateEventCallback(property));
}
}
if (member is MethodInfo method)
{
if (method.Name.StartsWith("add_"))
{
returns.Add(GenerateEventHandler(method));
returns.Add(GenerateEventCallbackFromEventHandler(method));
}
}
return returns;
}));
string baseClass = "ComponentToXamarinBridge";
if (typeof(Layout).IsAssignableFrom(ElementType))
{
baseClass = "ComponentToXamarinLayoutBridge";
}
var code = string.Format(CodeFormatString, Namespace, ElementType.Name, baseClass, ElementType.FullName, properties);
if (outputPath != null)
{
File.WriteAllText(outputPath + "/" + ElementType.Name + ".cs", code);
}
return code;
}
}
}