generated from jeka-dev/wrapper-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenapiKBean.java
114 lines (91 loc) · 3.91 KB
/
OpenapiKBean.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
package dev.jeka.plugins.openapi;
import dev.jeka.core.api.depmanagement.JkDepSuggest;
import dev.jeka.core.api.depmanagement.JkRepoProperties;
import dev.jeka.core.api.project.JkProject;
import dev.jeka.core.api.project.JkProjectSourceGenerator;
import dev.jeka.core.api.system.JkLog;
import dev.jeka.core.api.system.JkProperties;
import dev.jeka.core.tool.JkDoc;
import dev.jeka.core.tool.KBean;
import dev.jeka.core.tool.builtins.project.ProjectKBean;
import java.nio.file.Path;
import java.util.LinkedList;
import java.util.List;
@JkDoc("Provides project configuration for generating code from openApi specifications.")
public class OpenapiKBean extends KBean {
@JkDoc("The command line arguments in conjunction with 'exec' method.")
public String cmdLine;
@JkDoc("Version of openapi-generator-cli to use.")
@JkDepSuggest(versionOnly = true, hint = "org.openapitools:openapi-generator:")
public String cliVersion = JkOpenApiGeneratorCli.DEFAULT_CLI_VERSION;
@JkDoc("If true, the specified cmdLine will be run to generate sources at compile time")
public boolean configureProject = true;
@JkDoc("Execute openApi cli with argument specified in 'cmdArgs'.")
public void exec() {
exec(this.cmdLine);
}
@JkDoc("Display generic help about openApi cli options")
public void cliHelp() {
exec("help");
}
@JkDoc("Display help about available for 'generate' options")
public void cliHelpGenerate() {
exec("help generate");
}
@JkDoc("Display the available generators")
public void cliHelpList() {
exec("list");
}
@JkDoc("Display config-help for spring server")
public void cliHelpSpringServer() {
exec("config-help -g spring");
}
@JkDoc("Display config-help for java client")
public void cliHelpJavaClient() {
exec("config-help -g java");
}
@Override
protected void init() {
ProjectKBean projectKBean = find(ProjectKBean.class).orElse(null);
if (projectKBean != null && configureProject) {
for (String command : commands(this.getRunbase().getProperties())) {
projectKBean.project.compilation.addSourceGenerator(new CmdLineGenerator(command));
}
}
}
private int exec(String cmdLine) {
JkRepoProperties repoProperties = JkRepoProperties.of(this.getRunbase().getProperties());
JkOpenApiGeneratorCli cmd = JkOpenApiGeneratorCli.of(repoProperties.getDownloadRepos(), cliVersion);
return cmd.execCmdLine(cmdLine);
}
// Source Generator from pure command line. Needed only for command expressed through properties.
private class CmdLineGenerator implements JkProjectSourceGenerator {
private final String command;
CmdLineGenerator(String command) {
this.command = command;
}
@Override
public String getDirName() {
return "openapi";
}
@Override
public void generate(JkProject project, Path generatedSourceDir) {
JkOpenApiGeneratorCli cmd = JkOpenApiGeneratorCli.of(project.dependencyResolver.getRepos(), cliVersion);
String effectiveCmdLine = command + " " + JkOpenapiCmdBuilder.OUTPUT_PATH + " " + generatedSourceDir;
effectiveCmdLine = effectiveCmdLine + " " + JkOpenapiCmdBuilder.ADDITIONAL_PROPERTIES
+ "=sourceFolder=/";
effectiveCmdLine = effectiveCmdLine + " --global-property modelTests=false,apiTests=false";
if (JkLog.isVerbose()) {
effectiveCmdLine = effectiveCmdLine + " --verbose";
}
cmd.execCmdLine(effectiveCmdLine);
}
@Override
public String toString() {
return "OpenapiKBeanGenerator";
}
}
private static List<String> commands(JkProperties properties) {
return new LinkedList<>(properties.getAllStartingWith("openapi.gen.", false).values());
}
}