From a47e522fae1a70d927d0701cb3915c08897b6c58 Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Sat, 30 May 2020 00:19:03 -0400 Subject: [PATCH] [core] Refactor templating management (#6357) * [core] Refactor templating management This refactors template management to get logic out of DefaultGenerator and to provide a cleaner API to template search and read/compile. Deprecates MockDefaultGenerator, which is not a mock and causes in-memory retention of file contents. Maintainers should prefer executing a "dryRun" with new DefaultGenerator(true) or do true mock/spies if evaluating template intermediaries is truly necessary. Tests may read written files with lower overhead than the in-process retention of those bytes. This attempts to maintain some compatibility with existing templating adapter interfaces. Any breaking change here would be unintentional but minimal effort to retarget the new interface. * Tests for dry run file outputs * Update API usage in Meta, test TemplateManager * Wait on lastModified, lookup by filename in SpringCodegenTest * Test DefaultGenerator + ignore file * Move config.processOpenAPI in DefaultGenerator * Fix wrong use of libraries templateDirector (java) The samples scripts for Java incorrectly referenced the libraries directories directly rather than the upper-level Java directory. This was incorrect usage of template directories, because the generator expects to be given the "language" directory and perform a lookup for missing templates in the order: * user defined libraries directory * user defined language root * embedded libraries directory * embedded language root * _common directory This is incorrect in our samples scripts because a user or maintainer has the expectation that any template change to files at the Java/ root should also be honored on generation if the script specifies a custom template directory. * Fix handlebars extension usage, clean up Meta tasks HandlebarseEngineAdapter previously didn't handle files without extensions in the same was as the MustacheEngineAdapter. This now allows for files without extension (or dotfiles) to lookup in the same location. Meta tasks are cleaned up to use template manager only, rather than attempting to create an "empty" generator to use the previous templating specific methods. * Update kotlin-multiplatform gradle wrapper * Rename GraphQL .gitignore template The .gitignore file is unable to load via classpath resource from the graphql node server resource directory (for unknown reasons). Before this change, the missing template would fail silently. A .gitignore file may exist in other directories and load as expected. Added a default .gitignore to _common as a fallback so as not to break any custom generators which may also be failing silently. * Log entire stacktrace in go sdk built by gradle in AppVeyor * Rename PHP .gitignore to gitignore Java resources may not load .gitignore, this follows suit with other generators and uses "gitignore" (some use "gitignore.mustache"). * [php] Rename .gitignore templates to gitignore * Use same classpath lookup in common locator * [rust] Properly escape empty triple-braces * [samples] Regenerate --- appveyor.yml | 2 +- bin/ci/java-feign.json | 2 +- bin/ci/java-feign10x.json | 2 +- bin/ci/java-google-api-client.json | 2 +- bin/ci/java-native.json | 2 +- bin/ci/java-okhttp-gson-parcelableModel.json | 2 +- bin/ci/java-okhttp-gson.json | 2 +- bin/ci/java-petstore-feign-10x.json | 2 +- bin/ci/java-rest-assured-jackson.json | 2 +- bin/ci/java-rest-assured.json | 2 +- bin/ci/java-retrofit2.json | 2 +- bin/ci/java-retrofit2rx.json | 2 +- bin/ci/java-retrofit2rx2.json | 2 +- bin/ci/java-vertx.json | 2 +- bin/java-petstore-feign-10x.sh | 2 +- bin/java-petstore-google-api-client.sh | 2 +- bin/java-petstore-native-async.sh | 2 +- bin/java-petstore-native.sh | 2 +- bin/java-petstore-okhttp-gson-parcelable.sh | 2 +- bin/java-petstore-okhttp-gson.sh | 2 +- bin/java-petstore-rest-assured-jackson.sh | 2 +- bin/java-petstore-rest-assured.sh | 2 +- bin/java-petstore-retrofit2.sh | 2 +- bin/java-petstore-retrofit2rx.sh | 2 +- bin/java-petstore-retrofit2rx2.sh | 2 +- bin/java-petstore-vertx.sh | 2 +- bin/windows/java-petstore-feign-10x.bat | 2 +- .../java-petstore-google-api-client.bat | 2 +- bin/windows/java-petstore-native.bat | 2 +- .../java-petstore-okhttp-gson-parcelable.bat | 2 +- bin/windows/java-petstore-okhttp-gson.bat | 2 +- .../java-petstore-rest-assured-jackson.bat | 2 +- bin/windows/java-petstore-rest-assured.bat | 2 +- bin/windows/java-petstore-retrofit2.bat | 2 +- bin/windows/java-petstore-retrofit2rx.bat | 2 +- bin/windows/java-petstore-retrofit2rx2.bat | 2 +- bin/windows/java-petstore-vertx.bat | 2 +- .../org/openapitools/codegen/cmd/Meta.java | 69 +- .../codegen/api/TemplatePathLocator.java | 14 + .../codegen/api/TemplateProcessor.java | 57 ++ .../codegen/api/TemplatingEngineAdapter.java | 26 +- .../codegen/api/TemplatingExecutor.java | 26 + .../codegen/api/TemplatingGenerator.java | 23 +- .../generator/gradle/plugin/tasks/MetaTask.kt | 31 +- .../src/test/kotlin/GenerateTaskDslTest.kt | 5 +- .../src/test/kotlin/MetaTaskDslTest.kt | 2 +- .../codegen/AbstractGenerator.java | 213 ----- .../openapitools/codegen/CodegenConfig.java | 2 - .../openapitools/codegen/DefaultCodegen.java | 15 - .../codegen/DefaultGenerator.java | 516 ++++------ .../codegen/DryRunTemplateManager.java | 107 +++ .../codegen/GlobalSupportingFile.java | 29 - .../openapitools/codegen/TemplateManager.java | 235 +++++ .../codegen/languages/AbstractPhpCodegen.java | 2 +- .../GraphQLNodeJSExpressServerCodegen.java | 2 +- .../languages/PhpLaravelServerCodegen.java | 20 +- .../languages/PhpSilexServerCodegen.java | 2 +- .../CommonTemplateContentLocator.java | 50 + .../GeneratorTemplateContentLocator.java | 91 ++ .../templating/HandlebarsEngineAdapter.java | 21 +- .../templating/MustacheEngineAdapter.java | 27 +- .../templating/TemplateManagerOptions.java | 45 + .../src/main/resources/_common/.gitignore | 74 ++ .../src/main/resources/_common/LICENSE | 201 ---- .../{.gitignore => gitignore} | 0 .../bootstrap/cache/{.gitignore => gitignore} | 0 .../database/{.gitignore => gitignore} | 0 .../php-laravel/{.gitignore => gitignore} | 0 .../storage/app/{.gitignore => gitignore} | 0 .../app/public/{.gitignore => gitignore} | 0 .../framework/cache/{.gitignore => gitignore} | 0 .../framework/{.gitignore => gitignore} | 0 .../sessions/{.gitignore => gitignore} | 0 .../testing/{.gitignore => gitignore} | 0 .../framework/views/{.gitignore => gitignore} | 0 .../storage/logs/{.gitignore => gitignore} | 0 .../php-lumen/{.gitignore => gitignore} | 0 .../php-silex/{.gitignore => gitignore} | 0 .../php-slim-server/{.gitignore => gitignore} | 0 .../{.gitignore => gitignore} | 0 .../php-symfony/{.gitignore => gitignore} | 0 .../php-ze-ph/{.gitignore => gitignore} | 0 .../resources/php/{.gitignore => gitignore} | 0 .../src/main/resources/rust/request.rs | 2 +- .../codegen/DefaultGeneratorTest.java | 404 ++++++-- .../codegen/MockDefaultGenerator.java | 87 +- .../codegen/TemplateManagerTest.java | 189 ++++ .../org/openapitools/codegen/TestUtils.java | 82 +- .../asciidoc/AsciidocGeneratorTest.java | 7 +- .../HaskellServantCodegenTest.java | 16 +- .../codegen/java/JavaClientCodegenTest.java | 274 +++--- .../JavaJAXRSCXFExtServerCodegenTest.java | 47 +- .../jaxrs/JavaJAXRSSpecServerCodegenTest.java | 70 +- .../codegen/java/jaxrs/JavaJaxrsBaseTest.java | 19 +- .../java/spring/SpringCodegenTest.java | 880 +++++++++--------- .../kotlin/KotlinModelCodegenTest.java | 14 +- .../scalaakka/ScalaAkkaClientCodegenTest.java | 97 +- .../codegen/yaml/YamlGeneratorTest.java | 36 +- .../templating/templates/jmeter/api.hbs | 183 ++++ .../templates/jmeter/testdata-localhost.hbs | 2 + .../templating/templates/numbers.handlebars | 1 + .../templating/templates/simple.mustache | 1 + .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../jersey1/gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../native/gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../vertx/gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../gradle/wrapper/gradle-wrapper.jar | Bin 58702 -> 99814 bytes .../.openapi-generator-ignore | 26 +- .../.openapi-generator/FILES | 4 - .../gradle/wrapper/gradle-wrapper.jar | Bin 53639 -> 58702 bytes .../gradle/wrapper/gradle-wrapper.properties | 3 +- .../petstore/kotlin-multiplatform/gradlew | 137 +-- .../petstore/kotlin-multiplatform/gradlew.bat | 190 ++-- .../php-laravel/.openapi-generator/FILES | 129 +++ .../php-laravel/.openapi-generator/VERSION | 2 +- .../app/Http/Controllers/FakeController.php | 46 +- .../php-laravel/lib/app/Models/BigCat.php | 24 + .../lib/app/Models/BigCatAllOf.php | 15 + .../php-laravel/lib/app/Models/FormatTest.php | 3 + .../lib/app/Models/TypeHolderExample.php | 3 + .../lib/resources/views/welcome.blade.php | 8 +- .../petstore/php-laravel/lib/routes/api.php | 11 +- .../petstore/php-slim4/lib/Model/Pet.php | 1 + .../petstore/php-slim4/lib/SlimRouter.php | 2 + 139 files changed, 3053 insertions(+), 1941 deletions(-) create mode 100644 modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatePathLocator.java create mode 100644 modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplateProcessor.java create mode 100644 modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingExecutor.java delete mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/DryRunTemplateManager.java delete mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/GlobalSupportingFile.java create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/TemplateManager.java create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/CommonTemplateContentLocator.java create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/GeneratorTemplateContentLocator.java create mode 100644 modules/openapi-generator/src/main/java/org/openapitools/codegen/templating/TemplateManagerOptions.java create mode 100644 modules/openapi-generator/src/main/resources/_common/.gitignore delete mode 100644 modules/openapi-generator/src/main/resources/_common/LICENSE rename modules/openapi-generator/src/main/resources/graphql-nodejs-express-server/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/bootstrap/cache/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/database/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/app/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/app/public/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/framework/cache/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/framework/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/framework/sessions/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/framework/testing/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/framework/views/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-laravel/storage/logs/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-lumen/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-silex/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-slim-server/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-slim4-server/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-symfony/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php-ze-ph/{.gitignore => gitignore} (100%) rename modules/openapi-generator/src/main/resources/php/{.gitignore => gitignore} (100%) create mode 100644 modules/openapi-generator/src/test/java/org/openapitools/codegen/TemplateManagerTest.java create mode 100644 modules/openapi-generator/src/test/resources/templating/templates/jmeter/api.hbs create mode 100644 modules/openapi-generator/src/test/resources/templating/templates/jmeter/testdata-localhost.hbs create mode 100644 modules/openapi-generator/src/test/resources/templating/templates/numbers.handlebars create mode 100644 modules/openapi-generator/src/test/resources/templating/templates/simple.mustache mode change 100644 => 100755 samples/client/petstore/kotlin-multiplatform/gradlew create mode 100644 samples/server/petstore/php-laravel/.openapi-generator/FILES create mode 100644 samples/server/petstore/php-laravel/lib/app/Models/BigCat.php create mode 100644 samples/server/petstore/php-laravel/lib/app/Models/BigCatAllOf.php diff --git a/appveyor.yml b/appveyor.yml index c0feaeaa505..d3c02aa19f1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -50,7 +50,7 @@ build_script: # install openapi-generator locally - mvn --no-snapshot-updates --quiet clean install -Dorg.slf4j.simpleLogger.defaultLogLevel=error # run the locally installed openapi-generator-gradle-plugin - - gradle -b modules\openapi-generator-gradle-plugin\samples\local-spec\build.gradle buildGoSdk --info + - gradle -b modules\openapi-generator-gradle-plugin\samples\local-spec\build.gradle buildGoSdk --stacktrace test_script: # restore test-related files - copy /b/v/y CI\samples.ci\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj samples\client\petstore\csharp\OpenAPIClient\src\Org.OpenAPITools.Test\Org.OpenAPITools.Test.csproj diff --git a/bin/ci/java-feign.json b/bin/ci/java-feign.json index 3e2d314e4d5..8d2c23fa7ea 100644 --- a/bin/ci/java-feign.json +++ b/bin/ci/java-feign.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/feign", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/feign", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true, "booleanGetterPrefix": "is", diff --git a/bin/ci/java-feign10x.json b/bin/ci/java-feign10x.json index 77262b06219..50574a95423 100644 --- a/bin/ci/java-feign10x.json +++ b/bin/ci/java-feign10x.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/feign10x", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/feign", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true, "booleanGetterPrefix": "is" diff --git a/bin/ci/java-google-api-client.json b/bin/ci/java-google-api-client.json index 1a447f387fe..e30057f3c91 100644 --- a/bin/ci/java-google-api-client.json +++ b/bin/ci/java-google-api-client.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/google-api-client", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/google-api-client", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true } diff --git a/bin/ci/java-native.json b/bin/ci/java-native.json index 99506422cfa..7d8c792e5bc 100644 --- a/bin/ci/java-native.json +++ b/bin/ci/java-native.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/native", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/native", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true } diff --git a/bin/ci/java-okhttp-gson-parcelableModel.json b/bin/ci/java-okhttp-gson-parcelableModel.json index 31b5cd136e9..332fa46a24f 100644 --- a/bin/ci/java-okhttp-gson-parcelableModel.json +++ b/bin/ci/java-okhttp-gson-parcelableModel.json @@ -4,7 +4,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/okhttp-gson-parcelableModel", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true, "parcelableModel": true diff --git a/bin/ci/java-okhttp-gson.json b/bin/ci/java-okhttp-gson.json index 78ca97e598d..cdba0e71287 100644 --- a/bin/ci/java-okhttp-gson.json +++ b/bin/ci/java-okhttp-gson.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/okhttp-gson", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true } diff --git a/bin/ci/java-petstore-feign-10x.json b/bin/ci/java-petstore-feign-10x.json index 186e64fc9e3..50994004159 100644 --- a/bin/ci/java-petstore-feign-10x.json +++ b/bin/ci/java-petstore-feign-10x.json @@ -1,7 +1,7 @@ { "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/feign", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "outputDir": "samples/client/petstore/java/feign10x", "additionalProperties": { "hideGenerationTimestamp": true, diff --git a/bin/ci/java-rest-assured-jackson.json b/bin/ci/java-rest-assured-jackson.json index 1ff56d80ea8..c652a9bf7a2 100644 --- a/bin/ci/java-rest-assured-jackson.json +++ b/bin/ci/java-rest-assured-jackson.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/rest-assured-jackson", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/rest-assured", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true, "booleanGetterPrefix": "is", diff --git a/bin/ci/java-rest-assured.json b/bin/ci/java-rest-assured.json index 19119b115c7..c1328500ad3 100644 --- a/bin/ci/java-rest-assured.json +++ b/bin/ci/java-rest-assured.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/rest-assured", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/rest-assured", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true, "booleanGetterPrefix": "is" diff --git a/bin/ci/java-retrofit2.json b/bin/ci/java-retrofit2.json index 2b3d251241a..8f2c2a8fba1 100644 --- a/bin/ci/java-retrofit2.json +++ b/bin/ci/java-retrofit2.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/retrofit2", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/retrofit2", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true } diff --git a/bin/ci/java-retrofit2rx.json b/bin/ci/java-retrofit2rx.json index 3479b1d08b0..35ca3ab5831 100644 --- a/bin/ci/java-retrofit2rx.json +++ b/bin/ci/java-retrofit2rx.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/retrofit2rx", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/retrofit2", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "useRxJava": true, "hideGenerationTimestamp": true diff --git a/bin/ci/java-retrofit2rx2.json b/bin/ci/java-retrofit2rx2.json index c3a6d841683..ce0d7d89d4c 100644 --- a/bin/ci/java-retrofit2rx2.json +++ b/bin/ci/java-retrofit2rx2.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/retrofit2rx2", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/retrofit2", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "useRxJava2": true, "hideGenerationTimestamp": true diff --git a/bin/ci/java-vertx.json b/bin/ci/java-vertx.json index 0e6bf3edd75..2eb162e1c37 100644 --- a/bin/ci/java-vertx.json +++ b/bin/ci/java-vertx.json @@ -3,7 +3,7 @@ "generatorName": "java", "inputSpec": "modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml", "outputDir": "samples/client/petstore/java/vertx", - "templateDir": "modules/openapi-generator/src/main/resources/Java/libraries/vertx", + "templateDir": "modules/openapi-generator/src/main/resources/Java", "additionalProperties": { "hideGenerationTimestamp": true } diff --git a/bin/java-petstore-feign-10x.sh b/bin/java-petstore-feign-10x.sh index 15b1b40e214..3a98ad061bb 100755 --- a/bin/java-petstore-feign-10x.sh +++ b/bin/java-petstore-feign-10x.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/feign -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-feign-10x.json -o samples/client/petstore/java/feign10x --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-feign-10x.json -o samples/client/petstore/java/feign10x --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is $@" echo "Removing files and folders under samples/client/petstore/java/feign10x/src/main" rm -rf samples/client/petstore/java/feign10x/src/main diff --git a/bin/java-petstore-google-api-client.sh b/bin/java-petstore-google-api-client.sh index 20e3577f197..866e8d40106 100755 --- a/bin/java-petstore-google-api-client.sh +++ b/bin/java-petstore-google-api-client.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/google-api-client -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-google-api-client.json -o samples/client/petstore/java/google-api-client --additional-properties hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-google-api-client.json -o samples/client/petstore/java/google-api-client --additional-properties hideGenerationTimestamp=true $@" echo "Removing files and folders under samples/client/petstore/java/google-api-client/src/main" rm -rf samples/client/petstore/java/google-api-client/src/main diff --git a/bin/java-petstore-native-async.sh b/bin/java-petstore-native-async.sh index d163250beb6..1782614d9ee 100755 --- a/bin/java-petstore-native-async.sh +++ b/bin/java-petstore-native-async.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/native -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-native.json -o samples/client/petstore/java/native-async --additional-properties hideGenerationTimestamp=true,asyncNative=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-native.json -o samples/client/petstore/java/native-async --additional-properties hideGenerationTimestamp=true,asyncNative=true $@" echo "Removing files and folders under samples/client/petstore/java/httpclient/src/main" rm -rf samples/client/petstore/java/native-async/src/main diff --git a/bin/java-petstore-native.sh b/bin/java-petstore-native.sh index d155429a3a6..063275017e9 100755 --- a/bin/java-petstore-native.sh +++ b/bin/java-petstore-native.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/native -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-native.json -o samples/client/petstore/java/native --additional-properties hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-native.json -o samples/client/petstore/java/native --additional-properties hideGenerationTimestamp=true $@" echo "Removing files and folders under samples/client/petstore/java/httpclient/src/main" rm -rf samples/client/petstore/java/native/src/main diff --git a/bin/java-petstore-okhttp-gson-parcelable.sh b/bin/java-petstore-okhttp-gson-parcelable.sh index ae4fbfd8d2b..92087756ddb 100755 --- a/bin/java-petstore-okhttp-gson-parcelable.sh +++ b/bin/java-petstore-okhttp-gson-parcelable.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate --artifact-id petstore-okhttp-gson-parcelableModel -t modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson-parcelableModel --additional-properties hideGenerationTimestamp=true,parcelableModel=true $@" +ags="generate --artifact-id petstore-okhttp-gson-parcelableModel -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson-parcelableModel --additional-properties hideGenerationTimestamp=true,parcelableModel=true $@" rm -rf samples/client/petstore/java/okhttp-gson-parcelableModel/src/main find samples/client/petstore/java/okhttp-gson-parcelableModel -maxdepth 1 -type f ! -name "README.md" -exec rm {} + diff --git a/bin/java-petstore-okhttp-gson.sh b/bin/java-petstore-okhttp-gson.sh index 8e7e7ac84e4..8e2e527d2a3 100755 --- a/bin/java-petstore-okhttp-gson.sh +++ b/bin/java-petstore-okhttp-gson.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson --additional-properties hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson --additional-properties hideGenerationTimestamp=true $@" rm -rf samples/client/petstore/java/okhttp-gson/src/main find samples/client/petstore/java/okhttp-gson -maxdepth 1 -type f ! -name "README.md" -exec rm {} + diff --git a/bin/java-petstore-rest-assured-jackson.sh b/bin/java-petstore-rest-assured-jackson.sh index eeffe6aacee..b5d1888032b 100755 --- a/bin/java-petstore-rest-assured-jackson.sh +++ b/bin/java-petstore-rest-assured-jackson.sh @@ -28,7 +28,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -args="generate -t modules/openapi-generator/src/main/resources/Java/libraries/rest-assured -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-rest-assured-jackson.json -o ${target_dir} --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is --additional-properties java8=true --additional-properties dateLibrary=java8 --additional-properties serializationLibrary=jackson $@" +args="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-rest-assured-jackson.json -o ${target_dir} --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is --additional-properties java8=true --additional-properties dateLibrary=java8 --additional-properties serializationLibrary=jackson $@" echo "Removing ${target_dir}" rm -rf "${target_dir}" diff --git a/bin/java-petstore-rest-assured.sh b/bin/java-petstore-rest-assured.sh index 93de0703ed7..81f1893ed89 100755 --- a/bin/java-petstore-rest-assured.sh +++ b/bin/java-petstore-rest-assured.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/rest-assured -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-rest-assured.json -o samples/client/petstore/java/rest-assured --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-rest-assured.json -o samples/client/petstore/java/rest-assured --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is $@" echo "Removing files and folders under samples/client/petstore/java/rest-assured/src/main" rm -rf samples/client/petstore/java/rest-assured/src/main diff --git a/bin/java-petstore-retrofit2.sh b/bin/java-petstore-retrofit2.sh index 2dce65f2724..09bd66f4540 100755 --- a/bin/java-petstore-retrofit2.sh +++ b/bin/java-petstore-retrofit2.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/retrofit2 -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2.json -o samples/client/petstore/java/retrofit2 --additional-properties hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2.json -o samples/client/petstore/java/retrofit2 --additional-properties hideGenerationTimestamp=true $@" echo "Removing files and folders under samples/client/petstore/java/retrofit2/src/main" rm -rf samples/client/petstore/java/retrofit2/src/main diff --git a/bin/java-petstore-retrofit2rx.sh b/bin/java-petstore-retrofit2rx.sh index 74729919db1..c1ddc268e5a 100755 --- a/bin/java-petstore-retrofit2rx.sh +++ b/bin/java-petstore-retrofit2rx.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/retrofit2 -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2rx.json -o samples/client/petstore/java/retrofit2rx --additional-properties useRxJava=true,hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2rx.json -o samples/client/petstore/java/retrofit2rx --additional-properties useRxJava=true,hideGenerationTimestamp=true $@" echo "Removing files and folders under samples/client/petstore/java/retrofit2rx/src/main" rm -rf samples/client/petstore/java/retrofit2rx/src/main diff --git a/bin/java-petstore-retrofit2rx2.sh b/bin/java-petstore-retrofit2rx2.sh index 05d6acb7bd0..9e7d0fe4046 100755 --- a/bin/java-petstore-retrofit2rx2.sh +++ b/bin/java-petstore-retrofit2rx2.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/retrofit2 -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2rx2.json -o samples/client/petstore/java/retrofit2rx2 --additional-properties useRxJava2=true,hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2rx2.json -o samples/client/petstore/java/retrofit2rx2 --additional-properties useRxJava2=true,hideGenerationTimestamp=true $@" echo "Removing files and folders under samples/client/petstore/java/retrofit2rx2/src/main" rm -rf samples/client/petstore/java/retrofit2rx2/src/main diff --git a/bin/java-petstore-vertx.sh b/bin/java-petstore-vertx.sh index 8eeb623b278..a2536a3dbf2 100755 --- a/bin/java-petstore-vertx.sh +++ b/bin/java-petstore-vertx.sh @@ -27,7 +27,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="generate -t modules/openapi-generator/src/main/resources/Java/libraries/vertx -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-vertx.json -o samples/client/petstore/java/vertx --additional-properties hideGenerationTimestamp=true $@" +ags="generate -t modules/openapi-generator/src/main/resources/Java -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-vertx.json -o samples/client/petstore/java/vertx --additional-properties hideGenerationTimestamp=true $@" echo "Removing files and folders under samples/client/petstore/java/vertx/src/main" rm -rf samples/client/petstore/java/vertx/src/main diff --git a/bin/windows/java-petstore-feign-10x.bat b/bin/windows/java-petstore-feign-10x.bat index d9163fdac99..68fda6d0236 100644 --- a/bin/windows/java-petstore-feign-10x.bat +++ b/bin/windows/java-petstore-feign-10x.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\feign -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-feign-10x.json -o samples\client\petstore\java\feign10x --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-feign-10x.json -o samples\client\petstore\java\feign10x --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-google-api-client.bat b/bin/windows/java-petstore-google-api-client.bat index c9d2d761b66..ae005af5c94 100644 --- a/bin/windows/java-petstore-google-api-client.bat +++ b/bin/windows/java-petstore-google-api-client.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\google-api-client -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-google-api-client.json -o samples\client\petstore\java\google-api-client --additional-properties hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-google-api-client.json -o samples\client\petstore\java\google-api-client --additional-properties hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-native.bat b/bin/windows/java-petstore-native.bat index e7301e90679..8980399c815 100644 --- a/bin/windows/java-petstore-native.bat +++ b/bin/windows/java-petstore-native.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\native -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-native.json -o samples\client\petstore\java\native --additional-properties hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-native.json -o samples\client\petstore\java\native --additional-properties hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-okhttp-gson-parcelable.bat b/bin/windows/java-petstore-okhttp-gson-parcelable.bat index 402a3cda7f9..fb88907214e 100644 --- a/bin/windows/java-petstore-okhttp-gson-parcelable.bat +++ b/bin/windows/java-petstore-okhttp-gson-parcelable.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate --artifact-id petstore-okhttp-gson-parcelableModel -t modules\openapi-generator\src\main\resources\Java\libraries\okhttp-gson -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-okhttp-gson.json -o samples\client\petstore\java\okhttp-gson-parcelableModel --additional-properties hideGenerationTimestamp=true,parcelableModel=true +set ags=generate --artifact-id petstore-okhttp-gson-parcelableModel -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-okhttp-gson.json -o samples\client\petstore\java\okhttp-gson-parcelableModel --additional-properties hideGenerationTimestamp=true,parcelableModel=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-okhttp-gson.bat b/bin/windows/java-petstore-okhttp-gson.bat index 80581ca14d4..0965f6a6ae5 100755 --- a/bin/windows/java-petstore-okhttp-gson.bat +++ b/bin/windows/java-petstore-okhttp-gson.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\okhttp-gson -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-okhttp-gson.json -o samples\client\petstore\java\okhttp-gson --additional-properties hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-okhttp-gson.json -o samples\client\petstore\java\okhttp-gson --additional-properties hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-rest-assured-jackson.bat b/bin/windows/java-petstore-rest-assured-jackson.bat index 8104df8b771..070156e2f4e 100644 --- a/bin/windows/java-petstore-rest-assured-jackson.bat +++ b/bin/windows/java-petstore-rest-assured-jackson.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\rest-assured -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-rest-assured-jackson.json -o samples\client\petstore\java\rest-assured-jackson --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is,java8=true,dateLibrary=java8,serializationLibrary=jackson,useBeanValidation=true,performBeanValidation=true, +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-rest-assured-jackson.json -o samples\client\petstore\java\rest-assured-jackson --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is,java8=true,dateLibrary=java8,serializationLibrary=jackson,useBeanValidation=true,performBeanValidation=true, java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-rest-assured.bat b/bin/windows/java-petstore-rest-assured.bat index 758af247586..d83f8c42898 100644 --- a/bin/windows/java-petstore-rest-assured.bat +++ b/bin/windows/java-petstore-rest-assured.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\rest-assured -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-rest-assured.json -o samples\client\petstore\java\rest-assured --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-rest-assured.json -o samples\client\petstore\java\rest-assured --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-retrofit2.bat b/bin/windows/java-petstore-retrofit2.bat index e0d57dfdaa2..c21b2941a5f 100644 --- a/bin/windows/java-petstore-retrofit2.bat +++ b/bin/windows/java-petstore-retrofit2.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\retrofit2 -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-retrofit2.json -o samples\client\petstore\java\retrofit2 --additional-properties hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-retrofit2.json -o samples\client\petstore\java\retrofit2 --additional-properties hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-retrofit2rx.bat b/bin/windows/java-petstore-retrofit2rx.bat index e3801c9b7cd..c62e07cf07f 100644 --- a/bin/windows/java-petstore-retrofit2rx.bat +++ b/bin/windows/java-petstore-retrofit2rx.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\retrofit2 -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-retrofit2rx.json -o samples\client\petstore\java\retrofit2rx --additional-properties useRxJava=true,hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-retrofit2rx.json -o samples\client\petstore\java\retrofit2rx --additional-properties useRxJava=true,hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-retrofit2rx2.bat b/bin/windows/java-petstore-retrofit2rx2.bat index 29899b36960..a9521ef27c3 100644 --- a/bin/windows/java-petstore-retrofit2rx2.bat +++ b/bin/windows/java-petstore-retrofit2rx2.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\retrofit2 -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-retrofit2rx2.json -o samples\client\petstore\java\retrofit2rx2 --additional-properties useRxJava2=true,hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-retrofit2rx2.json -o samples\client\petstore\java\retrofit2rx2 --additional-properties useRxJava2=true,hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% diff --git a/bin/windows/java-petstore-vertx.bat b/bin/windows/java-petstore-vertx.bat index 151d1ac1fb3..133b8eb7173 100644 --- a/bin/windows/java-petstore-vertx.bat +++ b/bin/windows/java-petstore-vertx.bat @@ -5,6 +5,6 @@ If Not Exist %executable% ( ) REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\vertx -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-vertx.json -o samples\client\petstore\java\vertx --additional-properties hideGenerationTimestamp=true +set ags=generate -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-vertx.json -o samples\client\petstore\java\vertx --additional-properties hideGenerationTimestamp=true java %JAVA_OPTS% -jar %executable% %ags% 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 9fa400098fb..0c4a8f7cf18 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 @@ -30,6 +30,11 @@ import org.apache.commons.io.FileUtils; import org.openapitools.codegen.CodegenConfig; import org.openapitools.codegen.DefaultGenerator; import org.openapitools.codegen.SupportingFile; +import org.openapitools.codegen.TemplateManager; +import org.openapitools.codegen.api.TemplatePathLocator; +import org.openapitools.codegen.templating.MustacheEngineAdapter; +import org.openapitools.codegen.templating.TemplateManagerOptions; +import org.openapitools.codegen.templating.CommonTemplateContentLocator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -136,52 +141,38 @@ public class Meta extends OpenApiGeneratorCommand { */ private static Converter processFiles(final File targetDir, final Map data) { - return new Converter() { - private DefaultGenerator generator = new DefaultGenerator(); + return support -> { + try { + File destinationFolder = + new File(new File(targetDir.getAbsolutePath()), support.folder); + File outputFile = new File(destinationFolder, support.destinationFilename); - @Override - public File convert(SupportingFile support) { - try { - File destinationFolder = - new File(new File(targetDir.getAbsolutePath()), support.folder); - File outputFile = new File(destinationFolder, support.destinationFilename); + TemplateManager templateProcessor = new TemplateManager( + new TemplateManagerOptions(false, false), + new MustacheEngineAdapter(), + new TemplatePathLocator[]{ new CommonTemplateContentLocator("codegen") } + ); - String template = - generator.readTemplate(new File(TEMPLATE_DIR_CLASSPATH, - support.templateFile).getPath()); - String formatted = template; + String template = templateProcessor.readTemplate(new File(TEMPLATE_DIR_CLASSPATH, support.templateFile).getPath()); - if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) { - LOGGER.info("writing file to {}", outputFile.getAbsolutePath()); - formatted = - Mustache.compiler().withLoader(loader(generator)).defaultValue("") - .compile(template).execute(data); - } else { - LOGGER.info("copying file to {}", outputFile.getAbsolutePath()); - } + String formatted = template; - FileUtils.writeStringToFile(outputFile, formatted, StandardCharsets.UTF_8); - return outputFile; + Mustache.TemplateLoader loader = name -> templateProcessor.getTemplateReader(name.concat(MUSTACHE_EXTENSION)); - } catch (IOException e) { - throw new RuntimeException("Can't generate project", e); + if (support.templateFile.endsWith(MUSTACHE_EXTENSION)) { + LOGGER.info("writing file to {}", outputFile.getAbsolutePath()); + formatted = + Mustache.compiler().withLoader(loader).defaultValue("") + .compile(template).execute(data); + } else { + LOGGER.info("copying file to {}", outputFile.getAbsolutePath()); } - } - }; - } - /** - * Creates mustache loader for template using classpath loader - * - * @param generator - class with reader getter - * @return loader for template - */ - private static Mustache.TemplateLoader loader(final DefaultGenerator generator) { - return new Mustache.TemplateLoader() { - @Override - public Reader getTemplate(String name) { - return generator.getTemplateReader(TEMPLATE_DIR_CLASSPATH + File.separator - + name.concat(MUSTACHE_EXTENSION)); + FileUtils.writeStringToFile(outputFile, formatted, StandardCharsets.UTF_8); + return outputFile; + + } catch (IOException e) { + throw new RuntimeException("Can't generate project", e); } }; } diff --git a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatePathLocator.java b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatePathLocator.java new file mode 100644 index 00000000000..9bcd4e2cc8b --- /dev/null +++ b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatePathLocator.java @@ -0,0 +1,14 @@ +package org.openapitools.codegen.api; + +/** + * Provides means for searching for "actual" template location based on relative template file. + */ +public interface TemplatePathLocator { + /** + * Get the full path to a relative template file. + * + * @param relativeTemplateFile Template file + * @return String Full template file path + */ + String getFullTemplatePath(String relativeTemplateFile); +} diff --git a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplateProcessor.java b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplateProcessor.java new file mode 100644 index 00000000000..6f105167605 --- /dev/null +++ b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplateProcessor.java @@ -0,0 +1,57 @@ +package org.openapitools.codegen.api; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.util.Map; + +/** + * Interface for abstractions around writing templated data to a file. + */ +public interface TemplateProcessor { + /** + * Writes data to a compiled template + * + * @param data Input data + * @param template Input template location + * @param target The targeted file output location + * + * @return The actual file + * @throws IOException If file cannot be written. + */ + File write(Map data, String template, File target) throws IOException; + + /** + * Write bytes to a file + * + * @param filename The name of file to write + * @param contents The contents bytes. Typically this is a UTF-8 formatted string. + * @return File representing the written file. + * @throws IOException If file cannot be written. + */ + File writeToFile(String filename, byte[] contents) throws IOException; + + /** + * Allow a caller to mark a path as ignored with accompanying reason + * + * @param path The ignored path + * @param context The reason for ignoring this path + */ + void ignore(Path path, String context); + + /** + * Allow a caller to mark a path as skipped with accompanying reason + * + * @param path The skipped path + * @param context The reason for skipping this path + */ + void skip(Path path, String context); + + /** + * Allow a caller to mark a path having errored during processing with accompanying reason + * + * @param path The path which has caused an error + * @param context The reason for the error + */ + default void error(Path path, String context) { }; +} diff --git a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingEngineAdapter.java b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingEngineAdapter.java index 7fceac5246a..c7240cfe6c2 100644 --- a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingEngineAdapter.java +++ b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingEngineAdapter.java @@ -36,18 +36,6 @@ public interface TemplatingEngineAdapter { */ String getIdentifier(); - /** - * Compiles a template into a string - * - * @param generator From where we can fetch the templates content (e.g. an instance of DefaultGenerator) - * @param bundle The map of values to pass to the template - * @param templateFile The name of the template (e.g. model.mustache ) - * @return the processed template result - * @throws IOException an error ocurred in the template processing - */ - String compileTemplate(TemplatingGenerator generator, Map bundle, - String templateFile) throws IOException; - /** * During generation, if a supporting file has a file extension that is * inside that array, then it is considered a templated supporting file @@ -57,6 +45,18 @@ public interface TemplatingEngineAdapter { */ String[] getFileExtensions(); + /** + * Compiles a template into a string + * + * @param executor From where we can fetch the templates content (e.g. an instance of DefaultGenerator) + * @param bundle The map of values to pass to the template + * @param templateFile The name of the template (e.g. model.mustache ) + * @return the processed template result + * @throws IOException an error ocurred in the template processing + */ + String compileTemplate(TemplatingExecutor executor, Map bundle, + String templateFile) throws IOException; + /** * Determines whether the template file with supported extensions exists. This may be on the filesystem, * external filesystem, or classpath (implementation is up to TemplatingGenerator). @@ -65,7 +65,7 @@ public interface TemplatingEngineAdapter { * @param templateFile The original target filename * @return True if the template is available in the template search path, false if it can not be found */ - default boolean templateExists(TemplatingGenerator generator, String templateFile) { + default boolean templateExists(TemplatingExecutor generator, String templateFile) { return Arrays.stream(getFileExtensions()).anyMatch(ext -> { int idx = templateFile.lastIndexOf("."); String baseName; diff --git a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingExecutor.java b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingExecutor.java new file mode 100644 index 00000000000..d0fbb86b5db --- /dev/null +++ b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingExecutor.java @@ -0,0 +1,26 @@ +package org.openapitools.codegen.api; + +import java.nio.file.Path; + +/** + * interface to the full template content + * implementers might take into account the -t cli option, + * look in the resources for a generator specific template, etc + */ +public interface TemplatingExecutor { + /** + * returns the template content by name + * + * @param name the template name (e.g. model.mustache) + * @return the contents of that template + */ + String getFullTemplateContents(String name); + + /** + * Returns the path of a template, allowing access to the template where consuming literal contents aren't desirable or possible. + * + * @param name the template name (e.g. model.mustache) + * @return The {@link Path} to the template + */ + Path getFullTemplatePath(String name); +} \ No newline at end of file diff --git a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingGenerator.java b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingGenerator.java index 268cda6cdfb..30927307e96 100644 --- a/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingGenerator.java +++ b/modules/openapi-generator-core/src/main/java/org/openapitools/codegen/api/TemplatingGenerator.java @@ -16,28 +16,15 @@ package org.openapitools.codegen.api; -import java.nio.file.Path; - +// TODO: 6.0 Remove /** * interface to the full template content * implementers might take into account the -t cli option, * look in the resources for a language specific template, etc + * + * @deprecated as of 5.0, replaced by {@link TemplatingExecutor}. */ -public interface TemplatingGenerator { +@Deprecated() +public interface TemplatingGenerator extends TemplatingExecutor { - /** - * returns the template content by name - * - * @param name the template name (e.g. model.mustache) - * @return the contents of that template - */ - String getFullTemplateContents(String name); - - /** - * Returns the path of a template, allowing access to the template where consuming literal contents aren't desirable or possible. - * - * @param name the template name (e.g. model.mustache) - * @return The {@link Path} to the template - */ - Path getFullTemplatePath(String name); } diff --git a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/MetaTask.kt b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/MetaTask.kt index 6a4a180665a..9bebdd4f457 100644 --- a/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/MetaTask.kt +++ b/modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/MetaTask.kt @@ -24,10 +24,12 @@ import org.gradle.api.tasks.TaskAction import org.gradle.internal.logging.text.StyledTextOutput import org.gradle.internal.logging.text.StyledTextOutputFactory import org.gradle.kotlin.dsl.property -import org.openapitools.codegen.CodegenConfig -import org.openapitools.codegen.CodegenConstants -import org.openapitools.codegen.DefaultGenerator -import org.openapitools.codegen.SupportingFile +import org.openapitools.codegen.* +import org.openapitools.codegen.api.TemplatePathLocator +import org.openapitools.codegen.templating.CommonTemplateContentLocator +import org.openapitools.codegen.templating.GeneratorTemplateContentLocator +import org.openapitools.codegen.templating.MustacheEngineAdapter +import org.openapitools.codegen.templating.TemplateManagerOptions import java.io.File import java.io.IOException import java.nio.charset.Charset @@ -83,19 +85,28 @@ open class MetaTask : DefaultTask() { "fullyQualifiedGeneratorClass" to "${packageName.get()}.$klass", "openapiGeneratorVersion" to currentVersion) - val generator = DefaultGenerator() supportingFiles.map { try { val destinationFolder = File(File(dir.absolutePath), it.folder) destinationFolder.mkdirs() val outputFile = File(destinationFolder, it.destinationFilename) - val template = generator.readTemplate(File("codegen", it.templateFile).path) + val templateProcessor = TemplateManager( + TemplateManagerOptions(false, false), + MustacheEngineAdapter(), + arrayOf(CommonTemplateContentLocator("codegen")) + ) + + val template = templateProcessor.getFullTemplateContents(it.templateFile) var formatted = template + val loader = Mustache.TemplateLoader { name -> + templateProcessor.getTemplateReader("$name.mustache") + } + if (it.templateFile.endsWith(".mustache")) { formatted = Mustache.compiler() - .withLoader(loader(generator)) + .withLoader(loader) .defaultValue("") .compile(template).execute(data) } @@ -115,12 +126,6 @@ open class MetaTask : DefaultTask() { out.formatln("Created generator %s", klass) } - private fun loader(generator: DefaultGenerator): Mustache.TemplateLoader { - return Mustache.TemplateLoader { name -> - generator.getTemplateReader("codegen${File.separator}$name.mustache") - } - } - private fun String.titleCasedTextOnly(): String = this.split(Regex("[^a-zA-Z0-9]")).joinToString(separator = "", transform = String::capitalize) diff --git a/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt b/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt index 6aecc119252..5586a1d3fbf 100644 --- a/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt +++ b/modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt @@ -152,13 +152,14 @@ class GenerateTaskDslTest : TestBase() { // Act val result = GradleRunner.create() .withProjectDir(temp) - .withArguments("openApiGenerate") + .withArguments("openApiGenerate", "--stacktrace") .withPluginClasspath() .buildAndFail() // Assert // rather than write out full handlebars generator templates, we'll just test that the configurator has set handlebars as the engine. - assertTrue(result.output.contains("kotlin-client/model.handlebars (No such file or directory)"), "Build should have attempted to use handlebars.") + assertTrue(result.output.contains("HandlebarsException"), "Stack should expose an exception for missing templates.") + assertTrue(result.output.contains("handlebars"), "Build should have attempted to use handlebars.") assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome, "Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}") } diff --git a/modules/openapi-generator-gradle-plugin/src/test/kotlin/MetaTaskDslTest.kt b/modules/openapi-generator-gradle-plugin/src/test/kotlin/MetaTaskDslTest.kt index fe857d1b94a..62e600a92eb 100644 --- a/modules/openapi-generator-gradle-plugin/src/test/kotlin/MetaTaskDslTest.kt +++ b/modules/openapi-generator-gradle-plugin/src/test/kotlin/MetaTaskDslTest.kt @@ -29,7 +29,7 @@ class MetaTaskDslTest : TestBase() { // Act val result = GradleRunner.create() .withProjectDir(temp) - .withArguments("openApiMeta") + .withArguments("openApiMeta", "--stacktrace") .withPluginClasspath() .build() diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java deleted file mode 100644 index 3a2fecbc541..00000000000 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/AbstractGenerator.java +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech) - * Copyright 2018 SmartBear Software - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openapitools.codegen; - -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.StandardCopyOption; -import java.util.Arrays; -import org.apache.commons.lang3.StringUtils; -import org.openapitools.codegen.api.TemplatingGenerator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.*; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.Map; -import java.util.Scanner; -import java.util.regex.Pattern; - -public abstract class AbstractGenerator implements TemplatingGenerator { - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractGenerator.class); - protected boolean dryRun = false; - protected Map dryRunStatusMap = new HashMap<>(); - - /** - * Is the minimal-file-update option enabled? - * - * @return Option value - */ - public abstract boolean getEnableMinimalUpdate(); - - /** - * Write String to a file, formatting as UTF-8 - * - * @param filename The name of file to write - * @param contents The contents string. - * @return File representing the written file. - * @throws IOException If file cannot be written. - */ - public File writeToFile(String filename, String contents) throws IOException { - return writeToFile(filename, contents.getBytes(StandardCharsets.UTF_8)); - } - - /** - * Write bytes to a file - * - * @param filename The name of file to write - * @param contents The contents bytes. Typically, this is a UTF-8 formatted string. - * @return File representing the written file. - * @throws IOException If file cannot be written. - */ - @SuppressWarnings("static-method") - public File writeToFile(String filename, byte[] contents) throws IOException { - if (getEnableMinimalUpdate()) { - String tempFilename = filename + ".tmp"; - // Use Paths.get here to normalize path (for Windows file separator, space escaping on Linux/Mac, etc) - File outputFile = Paths.get(filename).toFile(); - File tempFile = null; - try { - tempFile = writeToFileRaw(tempFilename, contents); - if (!filesEqual(tempFile, outputFile)) { - LOGGER.info("writing file " + filename); - Files.move(tempFile.toPath(), outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - tempFile = null; - } else { - LOGGER.info("skipping unchanged file " + filename); - } - } finally { - if (tempFile != null && tempFile.exists()) { - try { - tempFile.delete(); - } catch (Exception ex) { - LOGGER.error("Error removing temporary file " + tempFile, ex); - } - } - } - return outputFile; - } else { - LOGGER.info("writing file " + filename); - return writeToFileRaw(filename, contents); - } - } - - private boolean filesEqual(File file1, File file2) throws IOException { - return file1.exists() && file2.exists() && Arrays.equals(Files.readAllBytes(file1.toPath()), Files.readAllBytes(file2.toPath())); - } - - private File writeToFileRaw(String filename, byte[] contents) throws IOException { - // Use Paths.get here to normalize path (for Windows file separator, space escaping on Linux/Mac, etc) - File output = Paths.get(filename).toFile(); - if (output.getParent() != null && !new File(output.getParent()).exists()) { - File parent = Paths.get(output.getParent()).toFile(); - parent.mkdirs(); - } - Files.write(output.toPath(), contents); - return output; - } - - public String readTemplate(String name) { - try { - Reader reader = getTemplateReader(name); - if (reader == null) { - throw new RuntimeException("no file found"); - } - Scanner s = new Scanner(reader).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } catch (Exception e) { - LOGGER.error(e.getMessage()); - } - throw new RuntimeException("can't load template " + name); - } - - @SuppressWarnings("squid:S2095") - // ignored rule as used in the CLI and it's required to return a reader - public Reader getTemplateReader(String name) { - InputStream is = null; - try { - is = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(name)); - if (is == null) { - is = new FileInputStream(new File(name)); // May throw but never return a null value - } - return new InputStreamReader(is, "UTF-8"); - } catch (FileNotFoundException | UnsupportedEncodingException e) { - LOGGER.error(e.getMessage()); - throw new RuntimeException("can't load template " + name); - } - } - - private String buildLibraryFilePath(String dir, String library, String file) { - return dir + File.separator + "libraries" + File.separator + library + File.separator + file; - } - - /** - * Get the template file path with template dir prepended, and use the - * library template if exists. - * - * @param config Codegen config - * @param templateFile Template file - * @return String Full template file path - */ - public String getFullTemplateFile(CodegenConfig config, String templateFile) { - //1st the code will check if there's a