Skip to content

Commit 8e1e05e

Browse files
authored
[CLI] Improvements for meta and list command (#799)
1 parent 09a62d7 commit 8e1e05e

File tree

12 files changed

+99
-48
lines changed

12 files changed

+99
-48
lines changed

bin/meta-codegen.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ then
2626
fi
2727

2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="meta -n myClientCodegen -p com.my.company.codegen -o samples/meta-codegen/lib $@"
29+
ags="meta -n myClientCodegen -t DOCUMENTATION -p com.my.company.codegen -o samples/meta-codegen/lib $@"
3030

3131
java $JAVA_OPTS -jar $executable $ags
3232

33-
mvn verify -f samples/meta-codegen/lib/pom.xml
33+
mvn clean package -f samples/meta-codegen/pom.xml
3434

3535
ags2="generate -g myClientCodegen -i modules/openapi-generator/src/test/resources/2_0/petstore.json -o samples/meta-codegen/usage $@"
3636

bin/utils/ensure-up-to-date

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ sleep 5
3232
./bin/rust-server-petstore.sh > /dev/null 2>&1
3333
./bin/openapi3/haskell-http-client-petstore.sh > /dev/null 2>&1
3434
./bin/csharp-petstore.sh > /dev/null 2>&1
35+
./bin/meta-codegen.sh > /dev/null 2>&1
3536

3637
# Check:
3738
if [ -n "$(git status --porcelain)" ]; then

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ListGenerators.java

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.openapitools.codegen.cmd;
22

3+
import com.google.common.base.Objects;
4+
35
import io.airlift.airline.Command;
46
import io.airlift.airline.Option;
7+
58
import org.openapitools.codegen.CodegenConfig;
69
import org.openapitools.codegen.CodegenConfigLoader;
710
import org.openapitools.codegen.CodegenType;
@@ -10,6 +13,7 @@
1013
import java.util.Comparator;
1114
import java.util.List;
1215
import java.util.Locale;
16+
import java.util.stream.Collectors;
1317

1418
// NOTE: List can later have subcommands such as list languages, list types, list frameworks, etc.
1519
@Command(name = "list", description = "Lists the available generators")
@@ -41,19 +45,29 @@ public void run() {
4145
sb.append(System.lineSeparator());
4246

4347
for (CodegenType type : types) {
44-
sb.append(type.name()).append(" generators:");
45-
sb.append(System.lineSeparator());
46-
47-
generators.stream()
48-
.filter(g -> g.getTag().equals(type))
49-
.sorted(Comparator.comparing(CodegenConfig::getName))
50-
.forEach(generator -> sb.append(" - ").append(generator.getName()).append(System.lineSeparator()));
51-
52-
sb.append(System.lineSeparator());
53-
sb.append(System.lineSeparator());
48+
appendForType(sb, type, type.name(), generators);
5449
}
50+
appendForType(sb, null, "UNSPECIFIED", generators);
5551
}
5652

57-
System.out.printf(Locale.ROOT,"%s%n", sb.toString());
53+
System.out.printf(Locale.ROOT, "%s%n", sb.toString());
54+
}
55+
56+
private void appendForType(StringBuilder sb, CodegenType type, String typeName, List<CodegenConfig> generators) {
57+
List<CodegenConfig> list = generators.stream()
58+
.filter(g -> Objects.equal(type, g.getTag()))
59+
.sorted(Comparator.comparing(CodegenConfig::getName))
60+
.collect(Collectors.toList());
61+
62+
if(list.size() > 0) {
63+
sb.append(typeName).append(" generators:");
64+
sb.append(System.lineSeparator());
65+
66+
list.stream()
67+
.forEach(generator -> sb.append(" - ").append(generator.getName()).append(System.lineSeparator()));
68+
69+
sb.append(System.lineSeparator());
70+
sb.append(System.lineSeparator());
71+
}
5872
}
5973
}

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public class Meta implements Runnable {
6767
description = "the package to put the main class into (defaults to org.openapitools.codegen)")
6868
private String targetPackage = "org.openapitools.codegen";
6969

70+
@Option(name = {"-t", "--type"}, title = "type",
71+
description = "the type of generator that is created",
72+
allowedValues = {"CLIENT", "SERVER", "DOCUMENTATION", "CONFIG", "OTHER"})
73+
private String type = "OTHER";
74+
7075
@Override
7176
public void run() {
7277
final File targetDir = new File(outputFolder);
@@ -87,8 +92,11 @@ public void run() {
8792
String currentVersion = Version.readVersionFromResources();
8893

8994
Map<String, Object> data =
90-
new ImmutableMap.Builder<String, Object>().put("generatorPackage", targetPackage)
91-
.put("generatorClass", mainClass).put("name", name)
95+
new ImmutableMap.Builder<String, Object>()
96+
.put("generatorPackage", targetPackage)
97+
.put("generatorClass", mainClass)
98+
.put("name", name)
99+
.put("generatorType", type)
92100
.put("fullyQualifiedGeneratorClass", targetPackage + "." + mainClass)
93101
.put("openapiGeneratorVersion", currentVersion).build();
94102

modules/openapi-generator/src/main/resources/codegen/README.mustache

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# OpenAPI Generator for the {{name}} library
22

33
## Overview
4-
This is a boiler-plate project to generate your own client library with Swagger. Its goal is
5-
to get you started with the basic plumbing so you can put in your own logic. It won't work without
6-
your changes applied.
4+
This is a boiler-plate project to generate your own project derived from an OpenAPI specification.
5+
Its goal is to get you started with the basic plumbing so you can put in your own logic.
6+
It won't work without your changes applied.
77

8-
## What's Swagger?
9-
The goal of Swagger™ is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interfaces have done for lower-level programming, Swagger removes the guesswork in calling the service.
8+
## What's OpenAPI
9+
The goal of OpenAPI is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.
10+
When properly described with OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.
11+
Similar to what interfaces have done for lower-level programming, OpenAPI removes the guesswork in calling the service.
1012

11-
12-
Check out [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) for additional information about the Swagger project, including additional libraries with support for other languages and more.
13+
Check out [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) for additional information about the OpenAPI project, including additional libraries with support for other languages and more.
1314

1415
## How do I use this?
1516
At this point, you've likely generated a client setup. It will include something along these lines:
@@ -43,10 +44,17 @@ Once modified, you can run this:
4344
mvn package
4445
```
4546

46-
In your generator project. A single jar file will be produced in `target`. You can now use that with codegen:
47+
In your generator project. A single jar file will be produced in `target`. You can now use that with [OpenAPI Generator](https://openapi-generator.tech):
48+
49+
For mac/linux:
50+
```
51+
java -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g {{name}} -i /path/to/openapi.yaml -o ./test
52+
```
53+
(Do not forget to replace the values `/path/to/openapi-generator-cli.jar`, `/path/to/your.jar` and `/path/to/openapi.yaml` in the previous command)
4754

55+
For Windows users, you will need to use `;` instead of `:` in the classpath, e.g.
4856
```
49-
java -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.Codegen -g {{name}} -i /path/to/openapi.yaml -o ./test
57+
java -cp /path/to/openapi-generator-cli.jar;/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g {{name}} -i /path/to/openapi.yaml -o ./test
5058
```
5159

5260
Now your templates are available to the client generator and you can write output values
@@ -67,8 +75,8 @@ the object you have available during client generation:
6775
# -DdebugOperations prints operations passed to the template engine
6876
# -DdebugSupportingFiles prints additional data passed to the template engine
6977

70-
java -DdebugOperations -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.Codegen -g {{name}} -i /path/to/swagger.yaml -o ./test
78+
java -DdebugOperations -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g {{name}} -i /path/to/openapi.yaml -o ./test
7179
```
7280

73-
Will, for example, output the debug info for operations. You can use this info
74-
in the `api.mustache` file.
81+
Will, for example, output the debug info for operations.
82+
You can use this info in the `api.mustache` file.

modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class {{generatorClass}} extends DefaultCodegen implements CodegenConfig
1919
* @see org.openapitools.codegen.CodegenType
2020
*/
2121
public CodegenType getTag() {
22-
return CodegenType.CLIENT;
22+
return CodegenType.{{generatorType}};
2323
}
2424

2525
/**

modules/openapi-generator/src/main/resources/codegen/pom.mustache

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
<artifactId>maven-compiler-plugin</artifactId>
101101
<version>3.6.1</version>
102102
<configuration>
103-
<source>1.7</source>
104-
<target>1.7</target>
103+
<source>1.8</source>
104+
<target>1.8</target>
105105
</configuration>
106106
</plugin>
107107
</plugins>

samples/meta-codegen/lib/README.md

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# OpenAPI Generator for the myClientCodegen library
22

33
## Overview
4-
This is a boiler-plate project to generate your own client library with Swagger. Its goal is
5-
to get you started with the basic plumbing so you can put in your own logic. It won't work without
6-
your changes applied.
4+
This is a boiler-plate project to generate your own project derived from an OpenAPI specification.
5+
Its goal is to get you started with the basic plumbing so you can put in your own logic.
6+
It won't work without your changes applied.
77

8-
## What's Swagger?
9-
The goal of Swagger™ is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection. When properly defined via Swagger, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interfaces have done for lower-level programming, Swagger removes the guesswork in calling the service.
8+
## What's OpenAPI
9+
The goal of OpenAPI is to define a standard, language-agnostic interface to REST APIs which allows both humans and computers to discover and understand the capabilities of the service without access to source code, documentation, or through network traffic inspection.
10+
When properly described with OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic.
11+
Similar to what interfaces have done for lower-level programming, OpenAPI removes the guesswork in calling the service.
1012

11-
12-
Check out [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) for additional information about the Swagger project, including additional libraries with support for other languages and more.
13+
Check out [OpenAPI-Spec](https://github.com/OAI/OpenAPI-Specification) for additional information about the OpenAPI project, including additional libraries with support for other languages and more.
1314

1415
## How do I use this?
1516
At this point, you've likely generated a client setup. It will include something along these lines:
@@ -43,10 +44,17 @@ Once modified, you can run this:
4344
mvn package
4445
```
4546

46-
In your generator project. A single jar file will be produced in `target`. You can now use that with codegen:
47+
In your generator project. A single jar file will be produced in `target`. You can now use that with [OpenAPI Generator](https://openapi-generator.tech):
48+
49+
For mac/linux:
50+
```
51+
java -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g myClientCodegen -i /path/to/openapi.yaml -o ./test
52+
```
53+
(Do not forget to replace the values `/path/to/openapi-generator-cli.jar`, `/path/to/your.jar` and `/path/to/openapi.yaml` in the previous command)
4754

55+
For Windows users, you will need to use `;` instead of `:` in the classpath, e.g.
4856
```
49-
java -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.Codegen -g myClientCodegen -i /path/to/openapi.yaml -o ./test
57+
java -cp /path/to/openapi-generator-cli.jar;/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g myClientCodegen -i /path/to/openapi.yaml -o ./test
5058
```
5159

5260
Now your templates are available to the client generator and you can write output values
@@ -62,13 +70,13 @@ the object you have available during client generation:
6270

6371
```
6472
# The following additional debug options are available for all codegen targets:
65-
# -DdebugSwagger prints the OpenAPI Specification as interpreted by the codegen
73+
# -DdebugOpenAPI prints the OpenAPI Specification as interpreted by the codegen
6674
# -DdebugModels prints models passed to the template engine
6775
# -DdebugOperations prints operations passed to the template engine
6876
# -DdebugSupportingFiles prints additional data passed to the template engine
6977
70-
java -DdebugOperations -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.Codegen -g myClientCodegen -i /path/to/swagger.yaml -o ./test
78+
java -DdebugOperations -cp /path/to/openapi-generator-cli.jar:/path/to/your.jar org.openapitools.codegen.OpenAPIGenerator generate -g myClientCodegen -i /path/to/openapi.yaml -o ./test
7179
```
7280

73-
Will, for example, output the debug info for operations. You can use this info
74-
in the `api.mustache` file.
81+
Will, for example, output the debug info for operations.
82+
You can use this info in the `api.mustache` file.

samples/meta-codegen/lib/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@
100100
<artifactId>maven-compiler-plugin</artifactId>
101101
<version>3.6.1</version>
102102
<configuration>
103-
<source>1.7</source>
104-
<target>1.7</target>
103+
<source>1.8</source>
104+
<target>1.8</target>
105105
</configuration>
106106
</plugin>
107107
</plugins>
@@ -116,7 +116,7 @@
116116
</dependencies>
117117
<properties>
118118
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
119-
<openapi-generator-version>3.0.0-SNAPSHOT</openapi-generator-version>
119+
<openapi-generator-version>3.2.3-SNAPSHOT</openapi-generator-version>
120120
<maven-plugin-version>1.0.0</maven-plugin-version>
121121
<junit-version>4.8.1</junit-version>
122122
</properties>

samples/meta-codegen/lib/src/main/java/com/my/company/codegen/MyclientcodegenGenerator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MyclientcodegenGenerator extends DefaultCodegen implements CodegenC
1919
* @see org.openapitools.codegen.CodegenType
2020
*/
2121
public CodegenType getTag() {
22-
return CodegenType.CLIENT;
22+
return CodegenType.DOCUMENTATION;
2323
}
2424

2525
/**

samples/meta-codegen/pom.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.openapitools</groupId>
5+
<artifactId>meta-codegen</artifactId>
6+
<version>1.0.0</version>
7+
<packaging>pom</packaging>
8+
<modules>
9+
<module>lib/</module>
10+
<module>../../modules/openapi-generator</module>
11+
</modules>
12+
</project>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.0.0-SNAPSHOT
1+
3.2.3-SNAPSHOT

0 commit comments

Comments
 (0)