Repository navigation
Expand file tree
/
Copy pathBuilderGenerator.cs
More file actions
124 lines (101 loc) · 4.56 KB
/
Copy pathBuilderGenerator.cs
File metadata and controls
124 lines (101 loc) · 4.56 KB
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
using M31.FluentApi.Generator.CodeBuilding;
using M31.FluentApi.Generator.CodeGeneration.CodeBoardElements;
namespace M31.FluentApi.Generator.CodeGeneration.CodeBoardActors.BuilderMethodsGeneration;
internal class BuilderGenerator : ICodeBoardActor
{
public void Modify(CodeBoard codeBoard)
{
BuilderStepMethods builderStepMethods =
BuilderStepMethodsCreator.CreateBuilderMethods(codeBoard.Forks, codeBoard.CancellationToken);
foreach (BuilderStepMethod staticMethod in builderStepMethods.StaticMethods)
{
if (codeBoard.CancellationToken.IsCancellationRequested)
{
break;
}
Method method = CreateMethod(staticMethod, codeBoard);
codeBoard.BuilderClass.AddMethod(method);
}
List<Interface> interfaces = new List<Interface>(builderStepMethods.Interfaces.Count);
interfaces.Add(CreateInitialStepInterface(builderStepMethods, codeBoard));
interfaces.Add(CreateFromAnyStepInterface(builderStepMethods, codeBoard));
foreach (BuilderInterface builderInterface in builderStepMethods.Interfaces)
{
if (codeBoard.CancellationToken.IsCancellationRequested)
{
break;
}
Interface @interface =
new Interface(codeBoard.Info.DefaultAccessModifier, builderInterface.InterfaceName);
foreach (InterfaceBuilderMethod interfaceMethod in builderInterface.Methods)
{
Method method = CreateMethod(interfaceMethod, codeBoard);
codeBoard.BuilderClass.AddMethod(CreateMethodWithInheritedDocs(method));
CommentedMethodSignature methodSignature = new CommentedMethodSignature(
method.MethodSignature.ToSignatureForInterface(),
method.MethodComments);
@interface.AddMethodSignature(methodSignature);
}
@interface.AddBaseInterfaces(builderInterface.BaseInterfaces);
interfaces.Add(@interface);
}
AddInterfacesToBuilderClass(
interfaces,
codeBoard.BuilderClass,
codeBoard.Info.BuilderClassNameWithTypeParameters);
AddInterfaceDefinitionsToBuilderClass(interfaces, codeBoard.BuilderClass);
}
private Method CreateMethodWithInheritedDocs(Method method)
{
return new Method(
new MethodComments(method.MethodComments.Any
? new List<string>() { "/// <inheritdoc/>" }
: new List<string>()),
method.MethodSignature,
method.MethodBody);
}
private Method CreateMethod(BuilderStepMethod builderStepMethod, CodeBoard codeBoard)
{
ReservedVariableNames reservedVariableNames = codeBoard.ReservedVariableNames.NewLocalScope();
reservedVariableNames.ReserveLocalVariableNames(builderStepMethod.Parameters.Select(p => p.Name));
Method method = builderStepMethod.BuildMethodCode(
codeBoard.Info,
reservedVariableNames);
return method;
}
private Interface CreateInitialStepInterface(BuilderStepMethods builderStepMethods, CodeBoard codeBoard)
{
string? firstInterfaceName = builderStepMethods.Interfaces.FirstOrDefault()?.InterfaceName;
Interface initialStepInterface =
new Interface(codeBoard.Info.DefaultAccessModifier, codeBoard.Info.InitialStepInterfaceName);
if (firstInterfaceName != null)
{
initialStepInterface.AddBaseInterface(firstInterfaceName);
}
return initialStepInterface;
}
private Interface CreateFromAnyStepInterface(BuilderStepMethods builderStepMethods, CodeBoard codeBoard)
{
Interface fromExistingInterface =
new Interface(codeBoard.Info.DefaultAccessModifier, codeBoard.Info.FromExistingInterfaceName);
foreach (BuilderInterface @interface in builderStepMethods.Interfaces)
{
fromExistingInterface.AddBaseInterface(@interface.InterfaceName);
}
return fromExistingInterface;
}
private void AddInterfacesToBuilderClass(List<Interface> interfaces, Class builderClass, string prefix)
{
foreach (Interface @interface in interfaces)
{
builderClass.AddInterface($"{prefix}.{@interface.Name}");
}
}
private void AddInterfaceDefinitionsToBuilderClass(List<Interface> interfaces, Class builderClass)
{
foreach (Interface @interface in interfaces)
{
builderClass.AddDefinition(@interface);
}
}
}