/
MvcControllerGeneratorProcessor.cs
253 lines (211 loc) · 11.1 KB
/
MvcControllerGeneratorProcessor.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2020 DHGMS Solutions and Contributors. All rights reserved.
// DHGMS Solutions and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.Models;
using Dhgms.Nucleotide.Generators.PropertyInfo;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Dhgms.Nucleotide.Generators.Features.Mvc
{
/// <summary>
/// Generates the MVC Controllers
/// </summary>
public sealed class MvcControllerGeneratorProcessor : BaseClassLevelCodeGeneratorProcessor<IEntityGenerationModel>
{
/// <inheritdoc />
protected override IEnumerable<PropertyDeclarationSyntax> GetPropertyDeclarations(IEntityGenerationModel entityGenerationModel)
{
return null;
}
protected override string[] GetClassPrefixes() => null;
protected override string GetClassSuffix()
{
return "Controller";
}
protected override IList<string> GetUsings()
{
return new List<string>
{
"System.Threading.Tasks",
"Microsoft.Extensions.Logging",
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc"
};
}
protected override bool GetWhetherClassShouldBePartialClass() => true;
protected override bool GetWhetherClassShouldBeSealedClass() => true;
/// <inheritdoc />
protected override string GetBaseClass(IEntityGenerationModel entityGenerationModel)
{
var entityName = entityGenerationModel.ClassName;
return $"global::Whipstaff.AspNetCore.QueryOnlyMvcController<Queries.IList{entityName}Query, RequestDtos.List{entityName}RequestDto, ResponseDtos.List{entityName}ResponseDto, Queries.IView{entityName}Query, ResponseDtos.View{entityName}ResponseDto, LoggerMessageActions.{entityName}LoggerMessageActions>";
}
protected override IEnumerable<string> GetImplementedInterfaces(IEntityGenerationModel entityGenerationModel)
{
return null;
/*
return new List<string>
{
$"Services.I{entityName}Service"
};
*/
}
/// <inheritdoc />
protected override IList<Tuple<Func<string, string>, string, Accessibility>> GetConstructorArguments()
{
var result = new List<Tuple<Func<string, string>, string, Accessibility>>
{
new Tuple<Func<string, string>, string, Accessibility>(_ => "Microsoft.AspNetCore.Authorization.IAuthorizationService", "authorizationService", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"Microsoft.Extensions.Logging.ILogger<{entityName}Controller>", "logger", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"MediatR.IMediator", "mediator", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"QueryFactories.I{entityName}QueryFactory", "queryFactory", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"LoggerMessageActions.{entityName}LoggerMessageActions", "logMessageActions", Accessibility.Private),
};
return result;
}
protected override IList<string> GetBaseConstructorArguments()
{
var result = new List<string>
{
"authorizationService",
"logger",
"mediator",
"queryFactory",
"logMessageActions",
};
return result;
}
protected override string[] GetClassLevelCommentSummary(string entityName)
{
return new[]
{
$"MVC Controller for {entityName}"
};
}
protected override string[] GetClassLevelCommentRemarks(string entityName)
{
return null;
}
protected override IList<Tuple<string, IList<string>>> GetClassAttributes(IEntityGenerationModel entityDeclaration)
{
return new List<Tuple<string, IList<string>>>
{
new Tuple<string, IList<string>>("Microsoft.AspNetCore.Mvc.AutoValidateAntiforgeryToken", null),
new Tuple<string, IList<string>>("Microsoft.AspNetCore.Authorization.Authorize", null),
};
}
/// <inheritdoc />
protected override MethodDeclarationSyntax[] GetMethodDeclarations(IEntityGenerationModel entityGenerationModel)
{
var entityName = entityGenerationModel.ClassName;
var result = new List<MethodDeclarationSyntax>
{
GetMvcViewActionResultDeclaration(entityName, "list"),
GetMvcViewActionResultDeclaration(entityName, "view"),
//GetCommandMethodDeclaration(entityName, "Add"),
//GetAddMethodDeclaration(entityName),
//GetDeleteMethodDeclaration(entityName),
//GetListMethodDeclaration(entityName),
//GetUpdateMethodDeclaration(entityName),
//GetViewMethodDeclaration(entityName),
GetPolicyMethodDeclaration(entityName, "List"),
GetPolicyMethodDeclaration(entityName, "View"),
GetQueryMethodDeclaration(entityName, "List", $"RequestDtos.List{entityName}RequestDto"),
GetQueryMethodDeclaration(entityName, "View", "long"),
//GetEventIdMethodDeclaration(entityName, "List"),
//GetEventIdMethodDeclaration(entityName, "View"),
};
return result.ToArray();
}
private MethodDeclarationSyntax GetCommandMethodDeclaration(string entityName, string action)
{
var methodName = $"Get{action}CommandAsync";
var requestDtoType = $"RequestDtos.{action}{entityName}RequestDto";
var camelAction = action.Substring(0, 1).ToUpper() + action.Substring(1);
var returnStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression($"_commandFactory.Get{camelAction}CommandAsync(query, claimsPrincipal, cancellationToken)"));
var body = new StatementSyntax[]
{
returnStatement
};
var returnType = SyntaxFactory.ParseTypeName($"Task<Queries.I{camelAction}{entityName}Query>");
var parameters = GetParams(new []
{
$"{requestDtoType} command",
"System.Security.Claims.ClaimsPrincipal claimsPrincipal",
"System.Threading.CancellationToken cancellationToken",
});
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.WithParameterList(parameters)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword))
.AddBodyStatements(body);
return declaration;
}
private MethodDeclarationSyntax GetQueryMethodDeclaration(string entityName, string action, string requestDtoType)
{
var methodName = $"Get{action}QueryAsync";
var camelAction = action.Substring(0, 1).ToUpper() + action.Substring(1);
var returnStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression($"QueryFactory.Get{camelAction}QueryAsync(query, claimsPrincipal, cancellationToken)"));
var body = new StatementSyntax[]
{
returnStatement
};
var returnType = SyntaxFactory.ParseTypeName($"Task<Queries.I{camelAction}{entityName}Query>");
var parameters = GetParams(new []
{
$"{requestDtoType} query",
"System.Security.Claims.ClaimsPrincipal claimsPrincipal",
"System.Threading.CancellationToken cancellationToken",
});
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.WithParameterList(parameters)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword))
.AddBodyStatements(body);
return declaration;
}
private MethodDeclarationSyntax GetPolicyMethodDeclaration(string entityName, string action)
{
var methodName = $"Get{action}PolicyAsync";
var returnStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression($"await Task.FromResult(\"Mvc.{entityName}.{action}\").ConfigureAwait(false)"));
var body = new StatementSyntax[]
{
returnStatement
};
var returnType = SyntaxFactory.ParseTypeName("Task<string>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
.AddBodyStatements(body);
return declaration;
}
private MethodDeclarationSyntax GetMvcViewActionResultDeclaration(string entityName, string action)
{
var camelAction = action.Substring(0, 1).ToUpper() + action.Substring(1);
var lowerAction = action.Substring(0, 1).ToLower() + action.Substring(1);
var methodName = $"Get{camelAction}ActionResultAsync";
var baseMethodInvocationSyntax =
RoslynGenerationHelpers.GetMethodOnClassInvocationSyntax("View", new [] {$"{lowerAction}Response"}, false);
var taskFromResult =
RoslynGenerationHelpers.GetStaticMethodInvocationSyntax("Task", "FromResult",
baseMethodInvocationSyntax, true);
var returnStatement = SyntaxFactory.ReturnStatement(taskFromResult);
var body = new StatementSyntax[]
{
returnStatement
};
var parameters = GetParams(new []{ $"ResponseDtos.{camelAction}{entityName}ResponseDto {lowerAction}Response"});
var returnType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task<IActionResult>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.WithParameterList(parameters)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword))
.AddBodyStatements(body);
return declaration;
}
protected override SeparatedSyntaxList<AttributeSyntax> GetAttributesForProperty(PropertyInfoBase propertyInfo)
{
return default(SeparatedSyntaxList<AttributeSyntax>);
}
}
}