Skip to content

Commit

Permalink
Introduce JkOpenApi for dealing with project without using KBean mech…
Browse files Browse the repository at this point in the history
…anism.
  • Loading branch information
djeang committed Nov 22, 2023
1 parent 01ab4fd commit e00cb8e
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .idea/openapi-plugin.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 21 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,33 @@ source code generator, as demonstrated below.
```java
import dev.jeka.core.tool.JkInjectClasspath;
@JkInjectClasspath("dev.jeka:openapi-plugin:0.10.38.0")
@JkInjectClasspath("dev.jeka:openapi-plugin:0.10.38.1")
public class SampleBuild_Programmatic extends JkBean {
private static final String SPEC_URL = "https://petstore.swagger.io/v2/swagger.json";
ProjectJkBean projectBean = getBean(ProjectJkBean.class).lately(this::configure);
OpenapiJkBean openApi = getBean(OpenapiJkBean.class).setCliVersion("7.0.1");
private void configure(JkProject project) {
openApi.addSourceGenerator(project, "spring", SPEC_URL).customize(cmdBuilder -> cmdBuilder
.addApiAndModelPackage("com.mycompany")
.add(GenerateCmdBuilder.MODEL_NAME_PREFIX, "Rest")
.add("--language-specific-primitives=Pet")
.addImportMapping("Pet", "com.yourpackage.models.Pet")
.addImportMapping("DateTime", "java.time.LocalDateTime")
.addTypeMapping("DateTime", "java.time.LocalDateTime")
);
private static final String OPENAPI_CLI_VERSION = "7.0.1";
private JkProject project() {
JkProject project = JkProject.of();
//...
JkOpenApi.ofVersion(OPENAPI_CLI_VERSION).addSourceGenerator(project, "spring", SPEC_URL)
.customize(cmdBuilder -> cmdBuilder
.addApiAndModelPackage("com.mycompany")
.add(JkOpenapiCmdBuilder.MODEL_NAME_PREFIX, "Rest")
.addAdditionalProperties("useSpringBoot3", "true")
.add("--language-specific-primitives=Pet")
.addImportMapping("Pet", "com.yourpackage.models.Pet")
.addImportMapping("DateTime", "java.time.LocalDateTime")
.addTypeMapping("DateTime", "java.time.LocalDateTime")
);
return project;
}
public void genCode() {
project().compilation.generateSources();
}
...
}
```

Expand Down
71 changes: 71 additions & 0 deletions jeka/def/dev/jeka/plugins/openapi/JkOpenApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package dev.jeka.plugins.openapi;

import dev.jeka.core.api.project.JkProject;

public class JkOpenApi {

private final String version;

private JkOpenApi(String version) {
this.version = version;
}

public static JkOpenApi ofVersion(String version) {
return new JkOpenApi(version);
}

public static JkOpenApi ofDefaultVersion() {
return ofVersion(JkOpenApiGeneratorCli.DEFAULT_CLI_VERSION);
}

/**
* Appends a source generator to the specified project, generating code using the specified generatorName and
* the specified location for specification. <p>
* This returns a {@link JkOpenApiSourceGenerator} that can be customized.
*
*
* @param generatorName The code generator to use. See <a href="https://openapi-generator.tech/docs/generators">list here</a>.
* @param specLocation The url or local path of the specification location.
*/
public JkOpenApiSourceGenerator addSourceGenerator(JkProject project, String generatorName, String specLocation) {
JkOpenApiSourceGenerator generator = JkOpenApiSourceGenerator.of(generatorName, specLocation)
.setCliVersion(version);
project.compilation.addSourceGenerator(generator);
return generator;
}

/**
* @see #addSourceGenerator(JkProject, String, String)
* @param packageName the package name where source code should be generated
*/
public JkOpenApiSourceGenerator addSourceGenerator(JkProject project, String generatorName, String specLocation, String packageName) {
JkOpenApiSourceGenerator generator = JkOpenApiSourceGenerator.of(generatorName, specLocation)
.setCliVersion(version).customize(builder -> builder
.addApiAndModelPackage(packageName)
.add(JkOpenapiCmdBuilder.MODEL_NAME_PREFIX, "Rest")
);
project.compilation.addSourceGenerator(generator);
return generator;
}

/**
* Appends a Springboot server code generation to the specified project.
* The generated model code will bbe prefixed with 'Rest'.
* @see #addSourceGenerator(JkProject, String, String)
*/
public JkOpenApiSourceGenerator addSpringbootServerGenerator(JkProject project, String specLocation, String packageName) {
return addSourceGenerator(project, "spring", specLocation, packageName).customize(builder ->
builder.addAdditionalProperties("useSpringBoot3", "true")
);
}

/**
* Appends a Java client code generation to the specified project.
* The generated model code will bbe prefixed with 'Rest'.
* @see #addSourceGenerator(JkProject, String, String)
*/
public JkOpenApiSourceGenerator addJavaGenerator(JkProject project, String specLocation, String packageName) {
return addSourceGenerator(project, "client", specLocation, packageName);
}

}
25 changes: 0 additions & 25 deletions jeka/def/dev/jeka/plugins/openapi/OpenapiJkBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,6 @@ public OpenapiJkBean() {
});
}

public JkOpenApiSourceGenerator addSourceGenerator(JkProject project, String generatorName, String specLocation) {
JkOpenApiSourceGenerator generator = JkOpenApiSourceGenerator.of(generatorName, specLocation)
.setCliVersion(this.cliVersion);
project.compilation.addSourceGenerator(generator);
return generator;
}

public JkOpenApiSourceGenerator addSpringServerSourceGenerator(JkProject project, String specLocation) {
return addSourceGenerator(project, "spring", specLocation);
}

public JkOpenApiSourceGenerator addJavaClientSourceGenerator(JkProject project, String specLocation) {
return addSourceGenerator(project, "java", specLocation);
}

public OpenapiJkBean setCliVersion(String cliVersion) {
this.cliVersion = cliVersion;
return this;
}

public OpenapiJkBean setAutoGenerate(boolean autoGenerate) {
this.autoGenerate = autoGenerate;
return this;
}

private int exec(String cmdLine) {
JkRepoProperties repoProperties = JkRepoProperties.of(this.getRuntime().getProperties());
JkOpenApiGeneratorCli cmd = JkOpenApiGeneratorCli.of(repoProperties.getDownloadRepos(), cliVersion);
Expand Down
34 changes: 19 additions & 15 deletions jeka/def/sample/SampleBuild_KBeanProgrammatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.jeka.core.api.system.JkLog;
import dev.jeka.core.tool.JkBean;
import dev.jeka.core.tool.builtins.project.ProjectJkBean;
import dev.jeka.plugins.openapi.JkOpenApi;
import dev.jeka.plugins.openapi.JkOpenapiCmdBuilder;
import dev.jeka.plugins.openapi.OpenapiJkBean;

Expand All @@ -14,24 +15,27 @@ public class SampleBuild_KBeanProgrammatic extends JkBean {

private static final String SPEC_URL = "https://petstore.swagger.io/v2/swagger.json";

ProjectJkBean projectBean = getBean(ProjectJkBean.class).lately(this::configure);

OpenapiJkBean openApi = getBean(OpenapiJkBean.class).setCliVersion("7.0.1");

private void configure(JkProject project) {
openApi.addSourceGenerator(project, "spring", SPEC_URL).customize(cmdBuilder -> cmdBuilder
.addApiAndModelPackage("com.mycompany")
.add(JkOpenapiCmdBuilder.MODEL_NAME_PREFIX, "Rest")
.add("--language-specific-primitives=Pet")
.addImportMapping("Pet", "com.yourpackage.models.Pet")
.addImportMapping("DateTime", "java.time.LocalDateTime")
.addTypeMapping("DateTime", "java.time.LocalDateTime")
private static final String OPENAPI_CLI_VERSION = "7.0.1";


private JkProject project() {
JkProject project = JkProject.of();
//...
JkOpenApi.ofVersion(OPENAPI_CLI_VERSION).addSourceGenerator(project, "spring", SPEC_URL)
.customize(cmdBuilder -> cmdBuilder
.addApiAndModelPackage("com.mycompany")
.add(JkOpenapiCmdBuilder.MODEL_NAME_PREFIX, "Rest")
.addAdditionalProperties("useSpringBoot3", "true")
.add("--language-specific-primitives=Pet")
.addImportMapping("Pet", "com.yourpackage.models.Pet")
.addImportMapping("DateTime", "java.time.LocalDateTime")
.addTypeMapping("DateTime", "java.time.LocalDateTime")
);
return project;
}

public void gen() {
cleanOutput();
projectBean.getProject().compilation.generateSources();
public void genCode() {
project().compilation.generateSources();
}

}
2 changes: 1 addition & 1 deletion sample-prog/.idea/sample-prog.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sample-props/.idea/sample-props.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e00cb8e

Please sign in to comment.