From 8e1e05e86cfcbac29eae0e9589498d7caa2cc77a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Bresson?= Date: Wed, 29 Aug 2018 06:56:37 +0200 Subject: [PATCH] [CLI] Improvements for meta and list command (#799) --- bin/meta-codegen.sh | 4 +-- bin/utils/ensure-up-to-date | 1 + .../codegen/cmd/ListGenerators.java | 36 +++++++++++++------ .../org/openapitools/codegen/cmd/Meta.java | 12 +++++-- .../main/resources/codegen/README.mustache | 32 ++++++++++------- .../resources/codegen/generatorClass.mustache | 2 +- .../src/main/resources/codegen/pom.mustache | 4 +-- samples/meta-codegen/lib/README.md | 34 +++++++++++------- samples/meta-codegen/lib/pom.xml | 6 ++-- .../codegen/MyclientcodegenGenerator.java | 2 +- samples/meta-codegen/pom.xml | 12 +++++++ .../usage/.openapi-generator/VERSION | 2 +- 12 files changed, 99 insertions(+), 48 deletions(-) create mode 100644 samples/meta-codegen/pom.xml diff --git a/bin/meta-codegen.sh b/bin/meta-codegen.sh index 00b67984464..2a0f55cf8a0 100755 --- a/bin/meta-codegen.sh +++ b/bin/meta-codegen.sh @@ -26,11 +26,11 @@ then fi 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 -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 $@" diff --git a/bin/utils/ensure-up-to-date b/bin/utils/ensure-up-to-date index 6e2fd3a1ec2..e29fed7643c 100755 --- a/bin/utils/ensure-up-to-date +++ b/bin/utils/ensure-up-to-date @@ -32,6 +32,7 @@ sleep 5 ./bin/rust-server-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/meta-codegen.sh > /dev/null 2>&1 # Check: if [ -n "$(git status --porcelain)" ]; then diff --git a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ListGenerators.java b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ListGenerators.java index edd85ff79af..83a3ada42c9 100644 --- a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ListGenerators.java +++ b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/ListGenerators.java @@ -1,7 +1,10 @@ package org.openapitools.codegen.cmd; +import com.google.common.base.Objects; + import io.airlift.airline.Command; import io.airlift.airline.Option; + import org.openapitools.codegen.CodegenConfig; import org.openapitools.codegen.CodegenConfigLoader; import org.openapitools.codegen.CodegenType; @@ -10,6 +13,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Locale; +import java.util.stream.Collectors; // NOTE: List can later have subcommands such as list languages, list types, list frameworks, etc. @Command(name = "list", description = "Lists the available generators") @@ -41,19 +45,29 @@ public class ListGenerators implements Runnable { sb.append(System.lineSeparator()); for (CodegenType type : types) { - sb.append(type.name()).append(" 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, type, type.name(), generators); } + 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 generators) { + List 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()); + } } } diff --git a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java index 1865550ca91..c373aaa8c70 100644 --- a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java +++ b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Meta.java @@ -67,6 +67,11 @@ public class Meta implements Runnable { description = "the package to put the main class into (defaults to 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 public void run() { final File targetDir = new File(outputFolder); @@ -87,8 +92,11 @@ public class Meta implements Runnable { String currentVersion = Version.readVersionFromResources(); Map data = - new ImmutableMap.Builder().put("generatorPackage", targetPackage) - .put("generatorClass", mainClass).put("name", name) + new ImmutableMap.Builder() + .put("generatorPackage", targetPackage) + .put("generatorClass", mainClass) + .put("name", name) + .put("generatorType", type) .put("fullyQualifiedGeneratorClass", targetPackage + "." + mainClass) .put("openapiGeneratorVersion", currentVersion).build(); diff --git a/modules/openapi-generator/src/main/resources/codegen/README.mustache b/modules/openapi-generator/src/main/resources/codegen/README.mustache index d8c6ab3ca32..7b274871839 100644 --- a/modules/openapi-generator/src/main/resources/codegen/README.mustache +++ b/modules/openapi-generator/src/main/resources/codegen/README.mustache @@ -1,15 +1,16 @@ # OpenAPI Generator for the {{name}} library ## Overview -This is a boiler-plate project to generate your own client library with Swagger. Its goal is -to get you started with the basic plumbing so you can put in your own logic. It won't work without -your changes applied. +This is a boiler-plate project to generate your own project derived from an OpenAPI specification. +Its goal is to get you started with the basic plumbing so you can put in your own logic. +It won't work without your changes applied. -## What's Swagger? -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. +## What's OpenAPI +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 Swagger 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 OpenAPI project, including additional libraries with support for other languages and more. ## How do I use this? 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 ``` -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 @@ -67,8 +75,8 @@ the object you have available during client generation: # -DdebugOperations prints operations 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 -in the `api.mustache` file. \ No newline at end of file +Will, for example, output the debug info for operations. +You can use this info in the `api.mustache` file. \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache b/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache index acdeeba6f49..cefd218cb50 100644 --- a/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache +++ b/modules/openapi-generator/src/main/resources/codegen/generatorClass.mustache @@ -19,7 +19,7 @@ public class {{generatorClass}} extends DefaultCodegen implements CodegenConfig * @see org.openapitools.codegen.CodegenType */ public CodegenType getTag() { - return CodegenType.CLIENT; + return CodegenType.{{generatorType}}; } /** diff --git a/modules/openapi-generator/src/main/resources/codegen/pom.mustache b/modules/openapi-generator/src/main/resources/codegen/pom.mustache index 2548456f813..cb3712e87ad 100644 --- a/modules/openapi-generator/src/main/resources/codegen/pom.mustache +++ b/modules/openapi-generator/src/main/resources/codegen/pom.mustache @@ -100,8 +100,8 @@ maven-compiler-plugin 3.6.1 - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/samples/meta-codegen/lib/README.md b/samples/meta-codegen/lib/README.md index e970d9f068f..94907133436 100644 --- a/samples/meta-codegen/lib/README.md +++ b/samples/meta-codegen/lib/README.md @@ -1,15 +1,16 @@ # OpenAPI Generator for the myClientCodegen library ## Overview -This is a boiler-plate project to generate your own client library with Swagger. Its goal is -to get you started with the basic plumbing so you can put in your own logic. It won't work without -your changes applied. +This is a boiler-plate project to generate your own project derived from an OpenAPI specification. +Its goal is to get you started with the basic plumbing so you can put in your own logic. +It won't work without your changes applied. -## What's Swagger? -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. +## What's OpenAPI +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 Swagger 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 OpenAPI project, including additional libraries with support for other languages and more. ## How do I use this? 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 ``` -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 @@ -62,13 +70,13 @@ the object you have available during client generation: ``` # 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 # -DdebugOperations prints operations 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 -in the `api.mustache` file. \ No newline at end of file +Will, for example, output the debug info for operations. +You can use this info in the `api.mustache` file. \ No newline at end of file diff --git a/samples/meta-codegen/lib/pom.xml b/samples/meta-codegen/lib/pom.xml index dd8dcf14fea..91bb6efc757 100644 --- a/samples/meta-codegen/lib/pom.xml +++ b/samples/meta-codegen/lib/pom.xml @@ -100,8 +100,8 @@ maven-compiler-plugin 3.6.1 - 1.7 - 1.7 + 1.8 + 1.8 @@ -116,7 +116,7 @@ UTF-8 - 3.0.0-SNAPSHOT + 3.2.3-SNAPSHOT 1.0.0 4.8.1 diff --git a/samples/meta-codegen/lib/src/main/java/com/my/company/codegen/MyclientcodegenGenerator.java b/samples/meta-codegen/lib/src/main/java/com/my/company/codegen/MyclientcodegenGenerator.java index 877b5778383..fd43a342519 100644 --- a/samples/meta-codegen/lib/src/main/java/com/my/company/codegen/MyclientcodegenGenerator.java +++ b/samples/meta-codegen/lib/src/main/java/com/my/company/codegen/MyclientcodegenGenerator.java @@ -19,7 +19,7 @@ public class MyclientcodegenGenerator extends DefaultCodegen implements CodegenC * @see org.openapitools.codegen.CodegenType */ public CodegenType getTag() { - return CodegenType.CLIENT; + return CodegenType.DOCUMENTATION; } /** diff --git a/samples/meta-codegen/pom.xml b/samples/meta-codegen/pom.xml new file mode 100644 index 00000000000..fc3ad25673e --- /dev/null +++ b/samples/meta-codegen/pom.xml @@ -0,0 +1,12 @@ + + 4.0.0 + org.openapitools + meta-codegen + 1.0.0 + pom + + lib/ + ../../modules/openapi-generator + + diff --git a/samples/meta-codegen/usage/.openapi-generator/VERSION b/samples/meta-codegen/usage/.openapi-generator/VERSION index 096bf47efe3..c791c986fbb 100644 --- a/samples/meta-codegen/usage/.openapi-generator/VERSION +++ b/samples/meta-codegen/usage/.openapi-generator/VERSION @@ -1 +1 @@ -3.0.0-SNAPSHOT \ No newline at end of file +3.2.3-SNAPSHOT \ No newline at end of file