@@ -39,6 +39,14 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
39
39
40
40
public static final String USE_SWASHBUCKLE = "useSwashbuckle" ;
41
41
public static final String ASPNET_CORE_VERSION = "aspnetCoreVersion" ;
42
+ public static final String CLASS_MODIFIER = "classModifier" ;
43
+ public static final String OPERATION_MODIFIER = "operationModifier" ;
44
+ public static final String GENERATE_BODY = "generateBody" ;
45
+ public static final String BUILD_TARGET = "buildTarget" ;
46
+
47
+ public static final String PROJECT_SDK = "projectSdk" ;
48
+ public static final String SDK_WEB = "Microsoft.NET.Sdk.Web" ;
49
+ public static final String SDK_LIB = "Microsoft.NET.Sdk" ;
42
50
43
51
private String packageGuid = "{" + randomUUID ().toString ().toUpperCase (Locale .ROOT ) + "}" ;
44
52
@@ -49,6 +57,12 @@ public class AspNetCoreServerCodegen extends AbstractCSharpCodegen {
49
57
protected int serverPort = 8080 ;
50
58
protected String serverHost = "0.0.0.0" ;
51
59
protected String aspnetCoreVersion = "2.1" ; // default to 2.1
60
+ // TODO Make next two enums toensure fixed list.
61
+ private String classModifier = "" ;
62
+ private String operationModifier = "virtual" ;
63
+ private boolean generateBody = true ;
64
+ private String buildTarget = "program" ;
65
+ private String projectSdk = SDK_WEB ;
52
66
53
67
public AspNetCoreServerCodegen () {
54
68
super ();
@@ -69,6 +83,26 @@ public AspNetCoreServerCodegen() {
69
83
cliOptions .clear ();
70
84
71
85
// CLI options
86
+ addOption (CodegenConstants .LICENSE_URL ,
87
+ CodegenConstants .LICENSE_URL_DESC ,
88
+ licenseUrl );
89
+
90
+ addOption (CodegenConstants .LICENSE_NAME ,
91
+ CodegenConstants .LICENSE_NAME_DESC ,
92
+ licenseName );
93
+
94
+ addOption (CodegenConstants .PACKAGE_COPYRIGHT ,
95
+ CodegenConstants .PACKAGE_COPYRIGHT_DESC ,
96
+ packageCopyright );
97
+
98
+ addOption (CodegenConstants .PACKAGE_AUTHORS ,
99
+ CodegenConstants .PACKAGE_AUTHORS_DESC ,
100
+ packageAuthors );
101
+
102
+ addOption (CodegenConstants .PACKAGE_TITLE ,
103
+ CodegenConstants .PACKAGE_TITLE_DESC ,
104
+ packageTitle );
105
+
72
106
addOption (CodegenConstants .PACKAGE_NAME ,
73
107
"C# package name (convention: Title.Case)." ,
74
108
packageName );
@@ -110,6 +144,22 @@ public AspNetCoreServerCodegen() {
110
144
"Uses the Swashbuckle.AspNetCore NuGet package for documentation." ,
111
145
useSwashbuckle );
112
146
147
+ addOption (CLASS_MODIFIER ,
148
+ "Class modifiers such as abstract or partial" ,
149
+ classModifier );
150
+
151
+ addOption (OPERATION_MODIFIER ,
152
+ "Operation modifiers such as virtual or abstract." ,
153
+ operationModifier );
154
+
155
+ addSwitch (GENERATE_BODY ,
156
+ "Generates method body." ,
157
+ generateBody );
158
+
159
+ addOption (BUILD_TARGET ,
160
+ "Target the build for a program or library." ,
161
+ buildTarget );
162
+
113
163
}
114
164
115
165
@ Override
@@ -138,6 +188,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
138
188
@ Override
139
189
public void processOpts () {
140
190
super .processOpts ();
191
+ boolean isLibrary = false ;
141
192
142
193
if (additionalProperties .containsKey (CodegenConstants .OPTIONAL_PROJECT_GUID )) {
143
194
setPackageGuid ((String ) additionalProperties .get (CodegenConstants .OPTIONAL_PROJECT_GUID ));
@@ -155,6 +206,50 @@ public void processOpts() {
155
206
setAspnetCoreVersion ((String ) additionalProperties .get (ASPNET_CORE_VERSION ));
156
207
}
157
208
209
+ // CHeck for class modifier if not present set the default value.
210
+ if (additionalProperties .containsKey (CLASS_MODIFIER )) {
211
+ classModifier = additionalProperties .get (CLASS_MODIFIER ).toString ();
212
+ } else {
213
+ additionalProperties .put (CLASS_MODIFIER , classModifier );
214
+ }
215
+
216
+ // TODO Validate modifier values
217
+ // If class modifierier is abstract then the methods need to be abstrat too.
218
+ if ("abstract" .equals (classModifier )) {
219
+ operationModifier = classModifier ;
220
+ additionalProperties .put (OPERATION_MODIFIER , operationModifier );
221
+ }
222
+
223
+ if (additionalProperties .containsKey (OPERATION_MODIFIER )) {
224
+ operationModifier = additionalProperties .get (OPERATION_MODIFIER ).toString ();
225
+ } else {
226
+ additionalProperties .put (OPERATION_MODIFIER , operationModifier );
227
+ }
228
+
229
+ // TODO Validate modifier values
230
+ // If operation modifier is abstract then dont generate any body
231
+ if ("abstract" .equals (operationModifier )) {
232
+ generateBody = false ;
233
+ additionalProperties .put (GENERATE_BODY , generateBody );
234
+ }
235
+ if (additionalProperties .containsKey (GENERATE_BODY )) {
236
+ generateBody = convertPropertyToBooleanAndWriteBack (GENERATE_BODY );
237
+ } else {
238
+ additionalProperties .put (GENERATE_BODY , generateBody );
239
+ }
240
+
241
+ // CHeck for class modifier if not present set the default value.
242
+ if (additionalProperties .containsKey (BUILD_TARGET )) {
243
+ buildTarget = additionalProperties .get (BUILD_TARGET ).toString ();
244
+ } else {
245
+ additionalProperties .put (BUILD_TARGET , buildTarget );
246
+ }
247
+ if ("library" .equals (buildTarget )) {
248
+ isLibrary = true ;
249
+ projectSdk = SDK_LIB ;
250
+ }
251
+ additionalProperties .put (PROJECT_SDK , projectSdk );
252
+
158
253
additionalProperties .put ("dockerTag" , packageName .toLowerCase (Locale .ROOT ));
159
254
160
255
apiPackage = packageName + ".Controllers" ;
@@ -173,35 +268,60 @@ public void processOpts() {
173
268
throw new IllegalArgumentException ("aspnetCoreVersion must be '2.1', '2.0' but found " + aspnetCoreVersion );
174
269
}
175
270
271
+
176
272
supportingFiles .add (new SupportingFile ("build.sh.mustache" , "" , "build.sh" ));
177
273
supportingFiles .add (new SupportingFile ("build.bat.mustache" , "" , "build.bat" ));
178
274
supportingFiles .add (new SupportingFile ("README.mustache" , "" , "README.md" ));
179
275
supportingFiles .add (new SupportingFile ("Solution.mustache" , "" , packageName + ".sln" ));
180
- supportingFiles .add (new SupportingFile ("Dockerfile.mustache" , packageFolder , "Dockerfile" ));
181
276
supportingFiles .add (new SupportingFile ("gitignore" , packageFolder , ".gitignore" ));
182
- supportingFiles .add (new SupportingFile ("appsettings.json" , packageFolder , "appsettings.json" ));
183
-
184
- supportingFiles .add (new SupportingFile ("Startup.mustache" , packageFolder , "Startup.cs" ));
185
- supportingFiles .add (new SupportingFile ("Program.mustache" , packageFolder , "Program.cs" ));
186
277
supportingFiles .add (new SupportingFile ("validateModel.mustache" , packageFolder + File .separator + "Attributes" , "ValidateModelStateAttribute.cs" ));
187
278
supportingFiles .add (new SupportingFile ("Project.csproj.mustache" , packageFolder , packageName + ".csproj" ));
279
+ if (!isLibrary ) {
280
+ supportingFiles .add (new SupportingFile ("Dockerfile.mustache" , packageFolder , "Dockerfile" ));
281
+ supportingFiles .add (new SupportingFile ("appsettings.json" , packageFolder , "appsettings.json" ));
282
+
283
+ supportingFiles .add (new SupportingFile ("Startup.mustache" , packageFolder , "Startup.cs" ));
284
+ supportingFiles .add (new SupportingFile ("Program.mustache" , packageFolder , "Program.cs" ));
285
+ supportingFiles .add (new SupportingFile ("Properties" + File .separator + "launchSettings.json" ,
286
+ packageFolder + File .separator + "Properties" , "launchSettings.json" ));
287
+ } else {
288
+ supportingFiles .add (new SupportingFile ("Project.nuspec.mustache" , packageFolder , packageName + ".nuspec" ));
289
+ // wwwroot files.
290
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "README.md" , packageFolder + File .separator + "wwwroot" , "README.md" ));
291
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "index.html" , packageFolder + File .separator + "wwwroot" , "index.html" ));
292
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "web.config" , packageFolder + File .separator + "wwwroot" , "web.config" ));
293
+
294
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "openapi-original.mustache" ,
295
+ packageFolder + File .separator + "wwwroot" , "openapi-original.json" ));
296
+ }
297
+ supportingFiles .add (new SupportingFile ("validateModel.mustache" , packageFolder + File .separator + "Attributes" , "ValidateModelStateAttribute.cs" ));
298
+ supportingFiles .add (new SupportingFile ("Project.csproj.mustache" , packageFolder , packageName + ".csproj" ));
299
+ if (!isLibrary ) {
300
+ supportingFiles .add (new SupportingFile ("Dockerfile.mustache" , packageFolder , "Dockerfile" ));
301
+ supportingFiles .add (new SupportingFile ("appsettings.json" , packageFolder , "appsettings.json" ));
302
+
303
+ supportingFiles .add (new SupportingFile ("Startup.mustache" , packageFolder , "Startup.cs" ));
304
+ supportingFiles .add (new SupportingFile ("Program.mustache" , packageFolder , "Program.cs" ));
305
+ supportingFiles .add (new SupportingFile ("Properties" + File .separator + "launchSettings.json" ,
306
+ packageFolder + File .separator + "Properties" , "launchSettings.json" ));
307
+ } else {
308
+ supportingFiles .add (new SupportingFile ("Project.nuspec.mustache" , packageFolder , packageName + ".nuspec" ));
309
+ // wwwroot files.
310
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "README.md" , packageFolder + File .separator + "wwwroot" , "README.md" ));
311
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "index.html" , packageFolder + File .separator + "wwwroot" , "index.html" ));
312
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "web.config" , packageFolder + File .separator + "wwwroot" , "web.config" ));
313
+
314
+ supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "openapi-original.mustache" ,
315
+ packageFolder + File .separator + "wwwroot" , "openapi-original.json" ));
316
+ }
188
317
189
- supportingFiles .add (new SupportingFile ("Properties" + File .separator + "launchSettings.json" ,
190
- packageFolder + File .separator + "Properties" , "launchSettings.json" ));
191
318
192
319
if (useSwashbuckle ) {
193
320
supportingFiles .add (new SupportingFile ("Filters" + File .separator + "BasePathFilter.mustache" ,
194
321
packageFolder + File .separator + "Filters" , "BasePathFilter.cs" ));
195
322
supportingFiles .add (new SupportingFile ("Filters" + File .separator + "GeneratePathParamsValidationFilter.mustache" ,
196
323
packageFolder + File .separator + "Filters" , "GeneratePathParamsValidationFilter.cs" ));
197
324
}
198
-
199
- supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "README.md" , packageFolder + File .separator + "wwwroot" , "README.md" ));
200
- supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "index.html" , packageFolder + File .separator + "wwwroot" , "index.html" ));
201
- supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "web.config" , packageFolder + File .separator + "wwwroot" , "web.config" ));
202
-
203
- supportingFiles .add (new SupportingFile ("wwwroot" + File .separator + "openapi-original.mustache" ,
204
- packageFolder + File .separator + "wwwroot" , "openapi-original.json" ));
205
325
}
206
326
207
327
public void setPackageGuid (String packageGuid ) {
0 commit comments