[CLI] Improvements for meta and list command (#799)

This commit is contained in:
Jérémie Bresson 2018-08-29 06:56:37 +02:00 committed by GitHub
parent 09a62d7fef
commit 8e1e05e86c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 99 additions and 48 deletions

View File

@ -26,11 +26,11 @@ then
fi fi
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="meta -n myClientCodegen -p com.my.company.codegen -o samples/meta-codegen/lib $@" ags="meta -n myClientCodegen -t DOCUMENTATION -p com.my.company.codegen -o samples/meta-codegen/lib $@"
java $JAVA_OPTS -jar $executable $ags java $JAVA_OPTS -jar $executable $ags
mvn verify -f samples/meta-codegen/lib/pom.xml mvn clean package -f samples/meta-codegen/pom.xml
ags2="generate -g myClientCodegen -i modules/openapi-generator/src/test/resources/2_0/petstore.json -o samples/meta-codegen/usage $@" ags2="generate -g myClientCodegen -i modules/openapi-generator/src/test/resources/2_0/petstore.json -o samples/meta-codegen/usage $@"

View File

@ -32,6 +32,7 @@ sleep 5
./bin/rust-server-petstore.sh > /dev/null 2>&1 ./bin/rust-server-petstore.sh > /dev/null 2>&1
./bin/openapi3/haskell-http-client-petstore.sh > /dev/null 2>&1 ./bin/openapi3/haskell-http-client-petstore.sh > /dev/null 2>&1
./bin/csharp-petstore.sh > /dev/null 2>&1 ./bin/csharp-petstore.sh > /dev/null 2>&1
./bin/meta-codegen.sh > /dev/null 2>&1
# Check: # Check:
if [ -n "$(git status --porcelain)" ]; then if [ -n "$(git status --porcelain)" ]; then

View File

@ -1,7 +1,10 @@
package org.openapitools.codegen.cmd; package org.openapitools.codegen.cmd;
import com.google.common.base.Objects;
import io.airlift.airline.Command; import io.airlift.airline.Command;
import io.airlift.airline.Option; import io.airlift.airline.Option;
import org.openapitools.codegen.CodegenConfig; import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConfigLoader; import org.openapitools.codegen.CodegenConfigLoader;
import org.openapitools.codegen.CodegenType; import org.openapitools.codegen.CodegenType;
@ -10,6 +13,7 @@ import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Collectors;
// NOTE: List can later have subcommands such as list languages, list types, list frameworks, etc. // NOTE: List can later have subcommands such as list languages, list types, list frameworks, etc.
@Command(name = "list", description = "Lists the available generators") @Command(name = "list", description = "Lists the available generators")
@ -41,19 +45,29 @@ public class ListGenerators implements Runnable {
sb.append(System.lineSeparator()); sb.append(System.lineSeparator());
for (CodegenType type : types) { for (CodegenType type : types) {
sb.append(type.name()).append(" generators:"); appendForType(sb, type, type.name(), generators);
sb.append(System.lineSeparator());
generators.stream()
.filter(g -> g.getTag().equals(type))
.sorted(Comparator.comparing(CodegenConfig::getName))
.forEach(generator -> sb.append(" - ").append(generator.getName()).append(System.lineSeparator()));
sb.append(System.lineSeparator());
sb.append(System.lineSeparator());
} }
appendForType(sb, null, "UNSPECIFIED", generators);
} }
System.out.printf(Locale.ROOT,"%s%n", sb.toString()); System.out.printf(Locale.ROOT, "%s%n", sb.toString());
}
private void appendForType(StringBuilder sb, CodegenType type, String typeName, List<CodegenConfig> generators) {
List<CodegenConfig> list = generators.stream()
.filter(g -> Objects.equal(type, g.getTag()))
.sorted(Comparator.comparing(CodegenConfig::getName))
.collect(Collectors.toList());
if(list.size() > 0) {
sb.append(typeName).append(" generators:");
sb.append(System.lineSeparator());
list.stream()
.forEach(generator -> sb.append(" - ").append(generator.getName()).append(System.lineSeparator()));
sb.append(System.lineSeparator());
sb.append(System.lineSeparator());
}
} }
} }

View File

@ -67,6 +67,11 @@ public class Meta implements Runnable {
description = "the package to put the main class into (defaults to org.openapitools.codegen)") description = "the package to put the main class into (defaults to org.openapitools.codegen)")
private String targetPackage = "org.openapitools.codegen"; private String targetPackage = "org.openapitools.codegen";
@Option(name = {"-t", "--type"}, title = "type",
description = "the type of generator that is created",
allowedValues = {"CLIENT", "SERVER", "DOCUMENTATION", "CONFIG", "OTHER"})
private String type = "OTHER";
@Override @Override
public void run() { public void run() {
final File targetDir = new File(outputFolder); final File targetDir = new File(outputFolder);
@ -87,8 +92,11 @@ public class Meta implements Runnable {
String currentVersion = Version.readVersionFromResources(); String currentVersion = Version.readVersionFromResources();
Map<String, Object> data = Map<String, Object> data =
new ImmutableMap.Builder<String, Object>().put("generatorPackage", targetPackage) new ImmutableMap.Builder<String, Object>()
.put("generatorClass", mainClass).put("name", name) .put("generatorPackage", targetPackage)
.put("generatorClass", mainClass)
.put("name", name)
.put("generatorType", type)
.put("fullyQualifiedGeneratorClass", targetPackage + "." + mainClass) .put("fullyQualifiedGeneratorClass", targetPackage + "." + mainClass)
.put("openapiGeneratorVersion", currentVersion).build(); .put("openapiGeneratorVersion", currentVersion).build();

View File

@ -1,15 +1,16 @@
# OpenAPI Generator for the {{name}} library # OpenAPI Generator for the {{name}} library
## Overview ## Overview
This is a boiler-plate project to generate your own client library with Swagger. Its goal is This is a boiler-plate project to generate your own project derived from an OpenAPI specification.
to get you started with the basic plumbing so you can put in your own logic. It won't work without Its goal is to get you started with the basic plumbing so you can put in your own logic.
your changes applied. It won't work without your changes applied.
## What's Swagger? ## What's OpenAPI
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. 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.
When properly described with OpenAPI, 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, OpenAPI removes the guesswork in calling the service.
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.
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.
## How do I use this? ## How do I use this?
At this point, you've likely generated a client setup. It will include something along these lines: 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:
mvn package mvn package
``` ```
In your generator project. A single jar file will be produced in `target`. You can now use that with codegen: 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):
For mac/linux:
``` ```
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 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
```
(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)
For Windows users, you will need to use `;` instead of `:` in the classpath, e.g.
```
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
``` ```
Now your templates are available to the client generator and you can write output values 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:
# -DdebugOperations prints operations passed to the template engine # -DdebugOperations prints operations passed to the template engine
# -DdebugSupportingFiles prints additional data passed to the template engine # -DdebugSupportingFiles prints additional data passed to the template engine
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 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
``` ```
Will, for example, output the debug info for operations. You can use this info Will, for example, output the debug info for operations.
in the `api.mustache` file. You can use this info in the `api.mustache` file.

View File

@ -19,7 +19,7 @@ public class {{generatorClass}} extends DefaultCodegen implements CodegenConfig
* @see org.openapitools.codegen.CodegenType * @see org.openapitools.codegen.CodegenType
*/ */
public CodegenType getTag() { public CodegenType getTag() {
return CodegenType.CLIENT; return CodegenType.{{generatorType}};
} }
/** /**

View File

@ -100,8 +100,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version> <version>3.6.1</version>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -1,15 +1,16 @@
# OpenAPI Generator for the myClientCodegen library # OpenAPI Generator for the myClientCodegen library
## Overview ## Overview
This is a boiler-plate project to generate your own client library with Swagger. Its goal is This is a boiler-plate project to generate your own project derived from an OpenAPI specification.
to get you started with the basic plumbing so you can put in your own logic. It won't work without Its goal is to get you started with the basic plumbing so you can put in your own logic.
your changes applied. It won't work without your changes applied.
## What's Swagger? ## What's OpenAPI
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. 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.
When properly described with OpenAPI, 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, OpenAPI removes the guesswork in calling the service.
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.
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.
## How do I use this? ## How do I use this?
At this point, you've likely generated a client setup. It will include something along these lines: 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:
mvn package mvn package
``` ```
In your generator project. A single jar file will be produced in `target`. You can now use that with codegen: 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):
For mac/linux:
``` ```
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 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
```
(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)
For Windows users, you will need to use `;` instead of `:` in the classpath, e.g.
```
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
``` ```
Now your templates are available to the client generator and you can write output values 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:
``` ```
# The following additional debug options are available for all codegen targets: # The following additional debug options are available for all codegen targets:
# -DdebugSwagger prints the OpenAPI Specification as interpreted by the codegen # -DdebugOpenAPI prints the OpenAPI Specification as interpreted by the codegen
# -DdebugModels prints models passed to the template engine # -DdebugModels prints models passed to the template engine
# -DdebugOperations prints operations passed to the template engine # -DdebugOperations prints operations passed to the template engine
# -DdebugSupportingFiles prints additional data passed to the template engine # -DdebugSupportingFiles prints additional data passed to the template engine
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 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
``` ```
Will, for example, output the debug info for operations. You can use this info Will, for example, output the debug info for operations.
in the `api.mustache` file. You can use this info in the `api.mustache` file.

View File

@ -100,8 +100,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version> <version>3.6.1</version>
<configuration> <configuration>
<source>1.7</source> <source>1.8</source>
<target>1.7</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -116,7 +116,7 @@
</dependencies> </dependencies>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<openapi-generator-version>3.0.0-SNAPSHOT</openapi-generator-version> <openapi-generator-version>3.2.3-SNAPSHOT</openapi-generator-version>
<maven-plugin-version>1.0.0</maven-plugin-version> <maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
</properties> </properties>

View File

@ -19,7 +19,7 @@ public class MyclientcodegenGenerator extends DefaultCodegen implements CodegenC
* @see org.openapitools.codegen.CodegenType * @see org.openapitools.codegen.CodegenType
*/ */
public CodegenType getTag() { public CodegenType getTag() {
return CodegenType.CLIENT; return CodegenType.DOCUMENTATION;
} }
/** /**

View File

@ -0,0 +1,12 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openapitools</groupId>
<artifactId>meta-codegen</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>lib/</module>
<module>../../modules/openapi-generator</module>
</modules>
</project>

View File

@ -1 +1 @@
3.0.0-SNAPSHOT 3.2.3-SNAPSHOT