-
Notifications
You must be signed in to change notification settings - Fork 6k
/
CodegenConfigurator.java
474 lines (377 loc) · 14.5 KB
/
CodegenConfigurator.java
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package io.swagger.codegen.config;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.ClientOptInput;
import io.swagger.codegen.ClientOpts;
import io.swagger.codegen.CodegenConfig;
import io.swagger.codegen.CodegenConfigLoader;
import io.swagger.codegen.CodegenConstants;
import io.swagger.codegen.auth.AuthParser;
import io.swagger.models.Swagger;
import io.swagger.models.auth.AuthorizationValue;
import io.swagger.parser.SwaggerParser;
import io.swagger.util.Json;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
/**
* A class that contains all codegen configuration properties a user would want to manipulate.
* An instance could be created by deserializing a JSON file or being populated from CLI or Maven plugin parameters.
* It also has a convenience method for creating a ClientOptInput class which is THE object DefaultGenerator.java needs
* to generate code.
*/
public class CodegenConfigurator {
public static final Logger LOGGER = LoggerFactory.getLogger(CodegenConfigurator.class);
private String lang;
private String inputSpec;
private String outputDir;
private boolean verbose;
private boolean skipOverwrite;
private String templateDir;
private String auth;
private String apiPackage;
private String modelPackage;
private String invokerPackage;
private String modelNamePrefix;
private String modelNameSuffix;
private String groupId;
private String artifactId;
private String artifactVersion;
private String library;
private Map<String, String> systemProperties = new HashMap<String, String>();
private Map<String, String> instantiationTypes = new HashMap<String, String>();
private Map<String, String> typeMappings = new HashMap<String, String>();
private Map<String, String> additionalProperties = new HashMap<String, String>();
private Map<String, String> importMappings = new HashMap<String, String>();
private Set<String> languageSpecificPrimitives = new HashSet<String>();
private String gitUserId="GIT_USER_ID";
private String gitRepoId="GIT_REPO_ID";
private String releaseNote="Minor update";
private String httpUserAgent;
private final Map<String, String> dynamicProperties = new HashMap<String, String>(); //the map that holds the JsonAnySetter/JsonAnyGetter values
public CodegenConfigurator() {
this.setOutputDir(".");
}
public CodegenConfigurator setLang(String lang) {
this.lang = lang;
return this;
}
public CodegenConfigurator setInputSpec(String inputSpec) {
this.inputSpec = inputSpec;
return this;
}
public String getInputSpec() {
return inputSpec;
}
public String getOutputDir() {
return outputDir;
}
public CodegenConfigurator setOutputDir(String outputDir) {
this.outputDir = toAbsolutePathStr(outputDir);
return this;
}
public String getModelPackage() {
return modelPackage;
}
public CodegenConfigurator setModelPackage(String modelPackage) {
this.modelPackage = modelPackage;
return this;
}
public String getModelNamePrefix() {
return modelNamePrefix;
}
public CodegenConfigurator setModelNamePrefix(String prefix) {
this.modelNamePrefix = prefix;
return this;
}
public String getModelNameSuffix() {
return modelNameSuffix;
}
public CodegenConfigurator setModelNameSuffix(String suffix) {
this.modelNameSuffix = suffix;
return this;
}
public boolean isVerbose() {
return verbose;
}
public CodegenConfigurator setVerbose(boolean verbose) {
this.verbose = verbose;
return this;
}
public boolean isSkipOverwrite() {
return skipOverwrite;
}
public CodegenConfigurator setSkipOverwrite(boolean skipOverwrite) {
this.skipOverwrite = skipOverwrite;
return this;
}
public String getLang() {
return lang;
}
public String getTemplateDir() {
return templateDir;
}
public CodegenConfigurator setTemplateDir(String templateDir) {
File f = new File(templateDir);
// check to see if the folder exists
if (!(f != null && f.exists() && f.isDirectory())) {
throw new IllegalArgumentException("Template directory " + templateDir + " does not exist.");
}
this.templateDir = f.getAbsolutePath();
return this;
}
public String getAuth() {
return auth;
}
public CodegenConfigurator setAuth(String auth) {
this.auth = auth;
return this;
}
public String getApiPackage() {
return apiPackage;
}
public CodegenConfigurator setApiPackage(String apiPackage) {
this.apiPackage = apiPackage;
return this;
}
public String getInvokerPackage() {
return invokerPackage;
}
public CodegenConfigurator setInvokerPackage(String invokerPackage) {
this.invokerPackage = invokerPackage;
return this;
}
public String getGroupId() {
return groupId;
}
public CodegenConfigurator setGroupId(String groupId) {
this.groupId = groupId;
return this;
}
public String getArtifactId() {
return artifactId;
}
public CodegenConfigurator setArtifactId(String artifactId) {
this.artifactId = artifactId;
return this;
}
public String getArtifactVersion() {
return artifactVersion;
}
public CodegenConfigurator setArtifactVersion(String artifactVersion) {
this.artifactVersion = artifactVersion;
return this;
}
public Map<String, String> getSystemProperties() {
return systemProperties;
}
public CodegenConfigurator setSystemProperties(Map<String, String> systemProperties) {
this.systemProperties = systemProperties;
return this;
}
public CodegenConfigurator addSystemProperty(String key, String value) {
this.systemProperties.put(key, value);
return this;
}
public Map<String, String> getInstantiationTypes() {
return instantiationTypes;
}
public CodegenConfigurator setInstantiationTypes(Map<String, String> instantiationTypes) {
this.instantiationTypes = instantiationTypes;
return this;
}
public CodegenConfigurator addInstantiationType(String key, String value) {
this.instantiationTypes.put(key, value);
return this;
}
public Map<String, String> getTypeMappings() {
return typeMappings;
}
public CodegenConfigurator setTypeMappings(Map<String, String> typeMappings) {
this.typeMappings = typeMappings;
return this;
}
public CodegenConfigurator addTypeMapping(String key, String value) {
this.typeMappings.put(key, value);
return this;
}
public Map<String, String> getAdditionalProperties() {
return additionalProperties;
}
public CodegenConfigurator setAdditionalProperties(Map<String, String> additionalProperties) {
this.additionalProperties = additionalProperties;
return this;
}
public CodegenConfigurator addAdditionalProperty(String key, String value) {
this.additionalProperties.put(key, value);
return this;
}
public Map<String, String> getImportMappings() {
return importMappings;
}
public CodegenConfigurator setImportMappings(Map<String, String> importMappings) {
this.importMappings = importMappings;
return this;
}
public CodegenConfigurator addImportMapping(String key, String value) {
this.importMappings.put(key, value);
return this;
}
public Set<String> getLanguageSpecificPrimitives() {
return languageSpecificPrimitives;
}
public CodegenConfigurator setLanguageSpecificPrimitives(Set<String> languageSpecificPrimitives) {
this.languageSpecificPrimitives = languageSpecificPrimitives;
return this;
}
public CodegenConfigurator addLanguageSpecificPrimitive(String value) {
this.languageSpecificPrimitives.add(value);
return this;
}
public String getLibrary() {
return library;
}
public CodegenConfigurator setLibrary(String library) {
this.library = library;
return this;
}
public String getGitUserId() {
return gitUserId;
}
public CodegenConfigurator setGitUserId(String gitUserId) {
this.gitUserId = gitUserId;
return this;
}
public String getGitRepoId() {
return gitRepoId;
}
public CodegenConfigurator setGitRepoId(String gitRepoId) {
this.gitRepoId = gitRepoId;
return this;
}
public String getReleaseNote() {
return releaseNote;
}
public CodegenConfigurator setReleaseNote(String releaseNote) {
this.releaseNote = releaseNote;
return this;
}
public String getHttpUserAgent() {
return httpUserAgent;
}
public CodegenConfigurator setHttpUserAgent(String httpUserAgent) {
this.httpUserAgent= httpUserAgent;
return this;
}
public ClientOptInput toClientOptInput() {
Validate.notEmpty(lang, "language must be specified");
Validate.notEmpty(inputSpec, "input spec must be specified");
setVerboseFlags();
setSystemProperties();
CodegenConfig config = CodegenConfigLoader.forName(lang);
config.setOutputDir(outputDir);
config.setSkipOverwrite(skipOverwrite);
config.instantiationTypes().putAll(instantiationTypes);
config.typeMapping().putAll(typeMappings);
config.importMapping().putAll(importMappings);
config.languageSpecificPrimitives().addAll(languageSpecificPrimitives);
checkAndSetAdditionalProperty(apiPackage, CodegenConstants.API_PACKAGE);
checkAndSetAdditionalProperty(modelPackage, CodegenConstants.MODEL_PACKAGE);
checkAndSetAdditionalProperty(invokerPackage, CodegenConstants.INVOKER_PACKAGE);
checkAndSetAdditionalProperty(groupId, CodegenConstants.GROUP_ID);
checkAndSetAdditionalProperty(artifactId, CodegenConstants.ARTIFACT_ID);
checkAndSetAdditionalProperty(artifactVersion, CodegenConstants.ARTIFACT_VERSION);
checkAndSetAdditionalProperty(templateDir, toAbsolutePathStr(templateDir), CodegenConstants.TEMPLATE_DIR);
checkAndSetAdditionalProperty(modelNamePrefix, CodegenConstants.MODEL_NAME_PREFIX);
checkAndSetAdditionalProperty(modelNameSuffix, CodegenConstants.MODEL_NAME_SUFFIX);
checkAndSetAdditionalProperty(gitUserId, CodegenConstants.GIT_USER_ID);
checkAndSetAdditionalProperty(gitRepoId, CodegenConstants.GIT_REPO_ID);
checkAndSetAdditionalProperty(releaseNote, CodegenConstants.RELEASE_NOTE);
checkAndSetAdditionalProperty(httpUserAgent, CodegenConstants.HTTP_USER_AGENT);
handleDynamicProperties(config);
if (isNotEmpty(library)) {
config.setLibrary(library);
}
config.additionalProperties().putAll(additionalProperties);
ClientOptInput input = new ClientOptInput()
.config(config);
final List<AuthorizationValue> authorizationValues = AuthParser.parse(auth);
Swagger swagger = new SwaggerParser().read(inputSpec, authorizationValues, true);
input.opts(new ClientOpts())
.swagger(swagger);
return input;
}
@JsonAnySetter
public CodegenConfigurator addDynamicProperty(String name, Object value) {
dynamicProperties.put(name, value.toString());
return this;
}
@JsonAnyGetter
public Map<String, String> getDynamicProperties() {
return dynamicProperties;
}
private void handleDynamicProperties(CodegenConfig codegenConfig) {
for (CliOption langCliOption : codegenConfig.cliOptions()) {
String opt = langCliOption.getOpt();
if (dynamicProperties.containsKey(opt)) {
codegenConfig.additionalProperties().put(opt, dynamicProperties.get(opt));
}
else if(systemProperties.containsKey(opt)) {
codegenConfig.additionalProperties().put(opt, systemProperties.get(opt).toString());
}
}
}
private void setVerboseFlags() {
if (!verbose) {
return;
}
LOGGER.info("\nVERBOSE MODE: ON. Additional debug options are injected" +
"\n - [debugSwagger] prints the swagger specification as interpreted by the codegen" +
"\n - [debugModels] prints models passed to the template engine" +
"\n - [debugOperations] prints operations passed to the template engine" +
"\n - [debugSupportingFiles] prints additional data passed to the template engine");
System.setProperty("debugSwagger", "");
System.setProperty("debugModels", "");
System.setProperty("debugOperations", "");
System.setProperty("debugSupportingFiles", "");
}
private void setSystemProperties() {
for (Map.Entry<String, String> entry : systemProperties.entrySet()) {
System.setProperty(entry.getKey(), entry.getValue());
}
}
private static String toAbsolutePathStr(String path) {
if (isNotEmpty(path)) {
return Paths.get(path).toAbsolutePath().toString();
}
return path;
}
private void checkAndSetAdditionalProperty(String property, String propertyKey) {
checkAndSetAdditionalProperty(property, property, propertyKey);
}
private void checkAndSetAdditionalProperty(String property, String valueToSet, String propertyKey) {
if (isNotEmpty(property)) {
additionalProperties.put(propertyKey, valueToSet);
}
}
public static CodegenConfigurator fromFile(String configFile) {
if (isNotEmpty(configFile)) {
try {
return Json.mapper().readValue(new File(configFile), CodegenConfigurator.class);
} catch (IOException e) {
LOGGER.error("Unable to deserialize config file: " + configFile, e);
}
}
return null;
}
}