From 899ddecdbdb9b157ebbf3efbcfdf95ad7cf9740e Mon Sep 17 00:00:00 2001 From: Joscha Feth Date: Sat, 5 Oct 2024 10:25:17 +0100 Subject: [PATCH] feat(avro)/refactor(core): unify `*_POST_PROCESS_FILE` behaviour and reuse code (#19761) * feat(avro)/refactor: unify `*_POST_PROCESS_FILE` bahviour and code * refactor: use existing function * test: add simple test for post processor execution * test: restrict concurrency to 1 * docs: add explanation to method --- docs/file-post-processing.md | 1 + .../openapitools/codegen/DefaultCodegen.java | 41 +++++++++++++++++++ .../codegen/languages/AbstractAdaCodegen.java | 17 +------- .../languages/AbstractCSharpCodegen.java | 18 ++------ .../codegen/languages/AbstractCppCodegen.java | 20 ++------- .../languages/AbstractDartCodegen.java | 18 ++------ .../languages/AbstractFSharpCodegen.java | 17 +------- .../codegen/languages/AbstractGoCodegen.java | 19 ++------- .../languages/AbstractJavaCodegen.java | 20 ++------- .../languages/AbstractKotlinCodegen.java | 20 ++------- .../codegen/languages/AbstractPhpCodegen.java | 20 ++------- .../languages/AbstractPythonCodegen.java | 19 ++------- .../AbstractPythonPydanticV1Codegen.java | 19 ++------- .../languages/AbstractRubyCodegen.java | 30 ++------------ .../languages/AbstractScalaCodegen.java | 18 ++------ .../AbstractTypeScriptClientCodegen.java | 15 +------ .../codegen/languages/AvroSchemaCodegen.java | 26 ++++++++++++ .../languages/CLibcurlClientCodegen.java | 19 ++------- .../languages/CrystalClientCodegen.java | 32 ++------------- .../languages/DartDioClientCodegen.java | 6 --- .../languages/HaskellHttpClientCodegen.java | 19 ++------- .../languages/HaskellServantCodegen.java | 19 ++------- .../languages/HaskellYesodServerCodegen.java | 19 ++------- .../JavascriptApolloClientCodegen.java | 19 ++------- .../languages/JavascriptClientCodegen.java | 19 ++------- .../languages/NodeJSExpressServerCodegen.java | 19 ++------- .../codegen/languages/OCamlClientCodegen.java | 16 +------- .../codegen/languages/PerlClientCodegen.java | 19 ++------- .../languages/PowerShellClientCodegen.java | 19 ++------- .../languages/PythonClientCodegen.java | 5 --- .../PythonPydanticV1ClientCodegen.java | 2 + .../languages/RustAxumServerCodegen.java | 17 ++------ .../codegen/languages/RustServerCodegen.java | 19 ++------- .../languages/Swift5ClientCodegen.java | 19 ++------- .../languages/Swift6ClientCodegen.java | 19 ++------- .../languages/SwiftCombineClientCodegen.java | 19 ++------- .../codegen/DefaultCodegenTest.java | 40 ++++++++++++++++++ 37 files changed, 219 insertions(+), 484 deletions(-) diff --git a/docs/file-post-processing.md b/docs/file-post-processing.md index a23845adae2..d53656195cf 100644 --- a/docs/file-post-processing.md +++ b/docs/file-post-processing.md @@ -19,6 +19,7 @@ Also refer to the relevant documentation for [CLI](./usage.md), [Maven Plugin](h The following environment variables are supported by their respective generators: +* `AVRO_POST_PROCESS_FILE` * `CPP_POST_PROCESS_FILE` * `CSHARP_POST_PROCESS_FILE` * `C_POST_PROCESS_FILE` diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 5206c18682d..3d5840ffdd7 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -71,6 +71,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.Map.Entry; import java.util.concurrent.ConcurrentSkipListSet; @@ -8088,6 +8092,43 @@ public class DefaultCodegen implements CodegenConfig { LOGGER.debug("Post processing file {} ({})", file, fileType); } + /** + * Executes an external command for file post processing. + * + * @param commandArr an array of commands and arguments. They will be concatenated with space and tokenized again. + * @return Whether the execution passed (true) or failed (false) + */ + protected boolean executePostProcessor(String[] commandArr) { + final String command = String.join(" ", commandArr); + try { + // we don't use the array variant here, because the command passed in by the user is often not only a single binary + // but a combination of binary + parameters, e.g. `/etc/bin prettier -w`, which would then not be found, as the + // first array item would be expected to be the binary only. The exec method is tokenizing the command for us. + Process p = Runtime.getRuntime().exec(command); + p.waitFor(); + int exitValue = p.exitValue(); + if (exitValue != 0) { + try (InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8); + BufferedReader br = new BufferedReader(inputStreamReader)) { + StringBuilder sb = new StringBuilder(); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + LOGGER.error("Error running the command ({}). Exit value: {}, Error output: {}", command, exitValue, sb); + } + } else { + LOGGER.info("Successfully executed: {}", command); + return true; + } + } catch (InterruptedException | IOException e) { + LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); + // Restore interrupted state + Thread.currentThread().interrupt(); + } + return false; + } + /** * Boolean value indicating the state of the option for post-processing file using environment variables. * diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java index d67987aca81..39efaf8f924 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java @@ -42,7 +42,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; @@ -1065,20 +1064,8 @@ abstract public class AbstractAdaCodegen extends DefaultCodegen implements Codeg if (StringUtils.isEmpty(commandPrefix)) { commandPrefix = "gnatpp"; } - - try { - Process p = Runtime.getRuntime().exec(new String[]{commandPrefix, "--no-compact", "--quiet", file.toString()}); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({} {}). Exit code: {}", commandPrefix, file, exitValue); - } else { - LOGGER.debug("Successfully executed: {} {}", commandPrefix, file); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({} {}). Exception: {}", commandPrefix, file, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + String[] commandArr = new String[]{commandPrefix, "--no-compact", "--quiet", file.toString()}; + this.executePostProcessor(commandArr); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java index 3117e9fe1ae..5c022bc82e6 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java @@ -242,6 +242,8 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen { if (StringUtils.isEmpty(System.getenv("CSHARP_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable CSHARP_POST_PROCESS_FILE not defined so the C# code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export CSHARP_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'CSHARP_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } // License info @@ -1879,6 +1881,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1890,20 +1893,7 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen { // only process files with .cs extension if ("cs".equals(FilenameUtils.getExtension(file.toString()))) { - String command = csharpPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {csharpPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java index 40335e87c37..3d7dea3dab8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java @@ -37,7 +37,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -315,6 +314,8 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg if (StringUtils.isEmpty(System.getenv("CPP_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable CPP_POST_PROCESS_FILE not defined so the C++ code may not be properly formatted. To define it, try 'export CPP_POST_PROCESS_FILE=\"/usr/local/bin/clang-format -i\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'CPP_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(RESERVED_WORD_PREFIX_OPTION)) { @@ -337,6 +338,7 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -346,21 +348,7 @@ abstract public class AbstractCppCodegen extends DefaultCodegen implements Codeg } // only process files with cpp extension if ("cpp".equals(FilenameUtils.getExtension(file.toString())) || "h".equals(FilenameUtils.getExtension(file.toString()))) { - String command = cppPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {cppPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java index fba1dcecf25..eaba6303835 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java @@ -21,7 +21,6 @@ import org.slf4j.LoggerFactory; import java.io.BufferedReader; import java.io.File; -import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.*; @@ -229,6 +228,8 @@ public abstract class AbstractDartCodegen extends DefaultCodegen { if (StringUtils.isEmpty(System.getenv("DART_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable DART_POST_PROCESS_FILE not defined so the Dart code may not be properly formatted. To define it, try `export DART_POST_PROCESS_FILE=\"/usr/local/bin/dartfmt -w\"` (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'DART_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(PUB_NAME)) { @@ -805,20 +806,7 @@ public abstract class AbstractDartCodegen extends DefaultCodegen { // process all files with dart extension if ("dart".equals(FilenameUtils.getExtension(file.toString()))) { // currently supported is "dartfmt -w" and "dart format" - String command = dartPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {dartPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractFSharpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractFSharpCodegen.java index e203225c162..a0e688c8fbc 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractFSharpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractFSharpCodegen.java @@ -36,7 +36,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; @@ -1039,6 +1038,7 @@ public abstract class AbstractFSharpCodegen extends DefaultCodegen implements Co @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1050,20 +1050,7 @@ public abstract class AbstractFSharpCodegen extends DefaultCodegen implements Co // only process files with .fs extension if ("fs".equals(FilenameUtils.getExtension(file.toString()))) { - String command = fsharpPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {fsharpPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index 9f65605bc69..a0624b2544c 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -28,7 +28,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; @@ -173,6 +172,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege if (StringUtils.isEmpty(System.getenv("GO_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable GO_POST_PROCESS_FILE not defined so Go code may not be properly formatted. To define it, try `export GO_POST_PROCESS_FILE=\"/usr/local/bin/gofmt -w\"` (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'GO_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } } @@ -982,6 +983,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1007,20 +1009,7 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege if ("go".equals(FilenameUtils.getExtension(file.toString()))) { // e.g. "gofmt -w yourcode.go" // e.g. "go fmt path/to/your/package" - String command = goPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {goPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java index 6ae58f6bff4..dcfbad983e0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java @@ -51,7 +51,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.time.LocalDate; import java.time.ZoneId; import java.util.*; @@ -409,6 +408,8 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code if (StringUtils.isEmpty(System.getenv("JAVA_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE=\"/usr/local/bin/clang-format -i\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'JAVA_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } convertPropertyToBooleanAndWriteBack(BeanValidationFeatures.USE_BEANVALIDATION, this::setUseBeanValidation); @@ -2226,6 +2227,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -2237,21 +2239,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code // only process files with java extension if ("java".equals(FilenameUtils.getExtension(file.toString()))) { - String command = javaPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {javaPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java index 65c3b738c77..456b972244a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractKotlinCodegen.java @@ -35,7 +35,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.function.Function; import java.util.regex.Pattern; @@ -432,6 +431,8 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co if (StringUtils.isEmpty(System.getenv("KOTLIN_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable KOTLIN_POST_PROCESS_FILE not defined so the Kotlin code may not be properly formatted. To define it, try 'export KOTLIN_POST_PROCESS_FILE=\"/usr/local/bin/ktlint -F\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'KOTLIN_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(MODEL_MUTABLE)) { @@ -964,6 +965,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -975,21 +977,7 @@ public abstract class AbstractKotlinCodegen extends DefaultCodegen implements Co // only process files with kt extension if ("kt".equals(FilenameUtils.getExtension(file.toString()))) { - String command = kotlinPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {kotlinPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java index 16930ee127e..040b6cddd24 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPhpCodegen.java @@ -33,7 +33,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher; @@ -169,6 +168,8 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg if (StringUtils.isEmpty(System.getenv("PHP_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable PHP_POST_PROCESS_FILE not defined so the PHP code may not be properly formatted. To define it, try 'export PHP_POST_PROCESS_FILE=\"/usr/local/bin/prettier --write\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'PHP_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(PACKAGE_NAME)) { @@ -856,6 +857,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -865,21 +867,7 @@ public abstract class AbstractPhpCodegen extends DefaultCodegen implements Codeg } // only process files with php extension if ("php".equals(FilenameUtils.getExtension(file.toString()))) { - String command = phpPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {phpPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java index b833739796c..c3d95ec1e93 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonCodegen.java @@ -21,7 +21,6 @@ import static org.openapitools.codegen.utils.StringUtils.escape; import static org.openapitools.codegen.utils.StringUtils.underscore; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -178,6 +177,8 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'PYTHON_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } } @@ -351,6 +352,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -361,20 +363,7 @@ public abstract class AbstractPythonCodegen extends DefaultCodegen implements Co // only process files with py extension if ("py".equals(FilenameUtils.getExtension(file.toString()))) { - String command = pythonPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {pythonPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java index fc2d73b3bb2..58808b6b01a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractPythonPydanticV1Codegen.java @@ -34,7 +34,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -142,6 +141,8 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'PYTHON_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } } @@ -296,6 +297,7 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -306,20 +308,7 @@ public abstract class AbstractPythonPydanticV1Codegen extends DefaultCodegen imp // only process files with py extension if ("py".equals(FilenameUtils.getExtension(file.toString()))) { - String command = pythonPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {pythonPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java index 7ef29f104a7..b6377164d63 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractRubyCodegen.java @@ -27,11 +27,7 @@ import org.openapitools.codegen.utils.ModelUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.BufferedReader; import java.io.File; -import java.io.InputStreamReader; -import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.ZoneId; import java.util.Arrays; @@ -104,6 +100,8 @@ abstract public class AbstractRubyCodegen extends DefaultCodegen implements Code if (StringUtils.isEmpty(System.getenv("RUBY_POST_PROCESS_FILE"))) { LOGGER.info("Hint: Environment variable 'RUBY_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export RUBY_POST_PROCESS_FILE=\"/usr/local/bin/rubocop -a\"' (Linux/Mac)"); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'RUBY_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } } @@ -253,6 +251,7 @@ abstract public class AbstractRubyCodegen extends DefaultCodegen implements Code @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -262,28 +261,7 @@ abstract public class AbstractRubyCodegen extends DefaultCodegen implements Code } // only process files with rb extension if ("rb".equals(FilenameUtils.getExtension(file.toString()))) { - String command = rubyPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - try (InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(inputStreamReader)) { - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - LOGGER.error("Error running the command ({}). Exit value: {}, Error output: {}", command, exitValue, sb); - } - } else { - LOGGER.info("Successfully executed: `{}`", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {rubyPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractScalaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractScalaCodegen.java index 228728eb42d..2542da51508 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractScalaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractScalaCodegen.java @@ -193,6 +193,8 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen { if (StringUtils.isEmpty(System.getenv("SCALA_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable SCALA_POST_PROCESS_FILE not defined so the Scala code may not be properly formatted. To define it, try 'export SCALA_POST_PROCESS_FILE=/usr/local/bin/scalafmt' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'SCALA_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } this.appName = Optional.ofNullable(openAPI).map(o -> o.getInfo()).filter(i -> i != null).map(i -> i.getTitle()).filter(t -> t != null).orElse(this.appName); @@ -547,6 +549,7 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -558,20 +561,7 @@ public abstract class AbstractScalaCodegen extends DefaultCodegen { // only process files with scala extension if ("scala".equals(FilenameUtils.getExtension(file.toString()))) { - String command = scalaPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {scalaPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java index c4caca0c622..753cd1b4110 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractTypeScriptClientCodegen.java @@ -1092,20 +1092,7 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp } // only process files with ts extension if ("ts".equals(FilenameUtils.getExtension(file.toString()))) { - String command = tsPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {tsPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java index 2d01cfd9f21..98cd3a82cbf 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AvroSchemaCodegen.java @@ -16,6 +16,8 @@ package org.openapitools.codegen.languages; +import org.apache.commons.io.FilenameUtils; +import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.openapitools.codegen.meta.GeneratorMetadata; import org.openapitools.codegen.meta.Stability; @@ -132,6 +134,14 @@ public class AvroSchemaCodegen extends DefaultCodegen implements CodegenConfig { @Override public void processOpts() { super.processOpts(); + + if (StringUtils.isEmpty(System.getenv("AVRO_POST_PROCESS_FILE"))) { + LOGGER.info("Environment variable AVRO_POST_PROCESS_FILE not defined so the Avro schemas may not be properly formatted. To define it, try `export AVRO_POST_PROCESS_FILE=\"/usr/local/bin/prettier -w\"` (Linux/Mac)"); + LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'AVRO_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } + if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { packageName = (String) additionalProperties.get(CodegenConstants.PACKAGE_NAME); @@ -177,6 +187,22 @@ public class AvroSchemaCodegen extends DefaultCodegen implements CodegenConfig { return postProcessModelsEnum(objs); } + @Override + public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); + if (file == null) { + return; + } + String avroPostProcessFile = System.getenv("AVRO_POST_PROCESS_FILE"); + if (StringUtils.isEmpty(avroPostProcessFile)) { + return; // skip if AVRO_POST_PROCESS_FILE env variable is not defined + } + // only process files with avsc extension + if ("avsc".equals(FilenameUtils.getExtension(file.toString()))) { + this.executePostProcessor(new String[] {avroPostProcessFile, file.toString()}); + } + } + @Override protected void setNonArrayMapProperty(CodegenProperty property, String type) { super.setNonArrayMapProperty(property, type); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java index a1154fb8a92..0ac95f48a9e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CLibcurlClientCodegen.java @@ -29,7 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; @@ -320,6 +319,8 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf if (StringUtils.isEmpty(System.getenv("C_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable C_POST_PROCESS_FILE not defined so the C code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export C_POST_PROCESS_FILE=\"/usr/local/bin/uncrustify --no-backup\" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg"); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'C_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(USE_JSON_UNFORMATTED)) { @@ -898,6 +899,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -922,20 +924,7 @@ public class CLibcurlClientCodegen extends DefaultCodegen implements CodegenConf // only process files with .c or .h extension if ("c".equals(FilenameUtils.getExtension(file.toString())) || "h".equals(FilenameUtils.getExtension(file.toString()))) { - String command = cPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {cPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CrystalClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CrystalClientCodegen.java index cd7c18145c6..684df587ffb 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CrystalClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CrystalClientCodegen.java @@ -33,11 +33,7 @@ import org.openapitools.codegen.utils.ModelUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.BufferedReader; import java.io.File; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.ZoneId; import java.util.*; @@ -219,6 +215,8 @@ public class CrystalClientCodegen extends DefaultCodegen { if (StringUtils.isEmpty(System.getenv("CRYSTAL_POST_PROCESS_FILE"))) { LOGGER.info( "Hint: Environment variable 'CRYSTAL_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export CRYSTAL_POST_PROCESS_FILE=\"/usr/local/bin/crystal tool format\"' (Linux/Mac)"); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'CRYSTAL_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(SHARD_NAME)) { @@ -890,6 +888,7 @@ public class CrystalClientCodegen extends DefaultCodegen { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -899,30 +898,7 @@ public class CrystalClientCodegen extends DefaultCodegen { } // only process files with cr extension if ("cr".equals(FilenameUtils.getExtension(file.toString()))) { - String command = crystalPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - try (InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream(), - StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(inputStreamReader)) { - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - LOGGER.error("Error running the command ({}). Exit value: {}, Error output: {}", command, - exitValue, sb); - } - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {crystalPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java index 95a49a9ee44..6cd2c790602 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/DartDioClientCodegen.java @@ -24,7 +24,6 @@ import io.swagger.v3.oas.models.media.Schema; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.ObjectUtils; -import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.openapitools.codegen.CodegenDiscriminator.MappedModel; import org.openapitools.codegen.api.TemplatePathLocator; @@ -155,11 +154,6 @@ public class DartDioClientCodegen extends AbstractDartCodegen { public void processOpts() { super.processOpts(); - if (StringUtils.isEmpty(System.getenv("DART_POST_PROCESS_FILE"))) { - LOGGER.info("Environment variable DART_POST_PROCESS_FILE not defined so the Dart code may not be properly formatted. To define it, try `export DART_POST_PROCESS_FILE=\"/usr/local/bin/dartfmt -w\"` (Linux/Mac)"); - LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); - } - if (!additionalProperties.containsKey(CodegenConstants.SERIALIZATION_LIBRARY)) { additionalProperties.put(CodegenConstants.SERIALIZATION_LIBRARY, SERIALIZATION_LIBRARY_DEFAULT); LOGGER.debug("Serialization library not set, using default {}", SERIALIZATION_LIBRARY_DEFAULT); diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java index a78e9a703ca..3023afab586 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellHttpClientCodegen.java @@ -36,7 +36,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -436,6 +435,8 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC if (StringUtils.isEmpty(System.getenv("HASKELL_POST_PROCESS_FILE"))) { LOGGER.info("Hint: Environment variable HASKELL_POST_PROCESS_FILE not defined so the Haskell code may not be properly formatted. To define it, try 'export HASKELL_POST_PROCESS_FILE=\"$HOME/.local/bin/hfmt -w\"' (Linux/Mac)"); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'HASKELL_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(PROP_ALLOW_FROMJSON_NULLS)) { @@ -1471,6 +1472,7 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1481,20 +1483,7 @@ public class HaskellHttpClientCodegen extends DefaultCodegen implements CodegenC // only process files with hs extension if ("hs".equals(FilenameUtils.getExtension(file.toString()))) { - String command = haskellPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {haskellPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellServantCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellServantCodegen.java index a7785024a06..608e75d03c0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellServantCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellServantCodegen.java @@ -30,7 +30,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Pattern; @@ -232,6 +231,8 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf if (StringUtils.isEmpty(System.getenv("HASKELL_POST_PROCESS_FILE"))) { LOGGER.info("Hint: Environment variable HASKELL_POST_PROCESS_FILE not defined so the Haskell code may not be properly formatted. To define it, try 'export HASKELL_POST_PROCESS_FILE=\"$HOME/.local/bin/hfmt -w\"' (Linux/Mac)"); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'HASKELL_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } setBooleanProperty(PROP_SERVE_STATIC, PROP_SERVE_STATIC_DEFAULT); @@ -696,6 +697,7 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -706,20 +708,7 @@ public class HaskellServantCodegen extends DefaultCodegen implements CodegenConf // only process files with hs extension if ("hs".equals(FilenameUtils.getExtension(file.toString()))) { - String command = haskellPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {haskellPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellYesodServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellYesodServerCodegen.java index f3587ad772f..11422819c26 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellYesodServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/HaskellYesodServerCodegen.java @@ -34,7 +34,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Pattern; @@ -189,6 +188,8 @@ public class HaskellYesodServerCodegen extends DefaultCodegen implements Codegen if (StringUtils.isEmpty(System.getenv("HASKELL_POST_PROCESS_FILE"))) { LOGGER.info("Hint: Environment variable HASKELL_POST_PROCESS_FILE not defined so the Haskell code may not be properly formatted. To define it, try 'export HASKELL_POST_PROCESS_FILE=\"$HOME/.local/bin/hfmt -w\"' (Linux/Mac)"); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'HASKELL_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(PROJECT_NAME)) { @@ -563,6 +564,7 @@ public class HaskellYesodServerCodegen extends DefaultCodegen implements Codegen @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -573,20 +575,7 @@ public class HaskellYesodServerCodegen extends DefaultCodegen implements Codegen // only process files with hs extension if ("hs".equals(FilenameUtils.getExtension(file.toString()))) { - String command = haskellPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {haskellPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptApolloClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptApolloClientCodegen.java index b7c3d422704..d0cf4c9e7dd 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptApolloClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptApolloClientCodegen.java @@ -39,7 +39,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; @@ -240,6 +239,8 @@ public class JavascriptApolloClientCodegen extends DefaultCodegen implements Cod if (StringUtils.isEmpty(System.getenv("JS_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable JS_POST_PROCESS_FILE not defined so the JS code may not be properly formatted. To define it, try 'export JS_POST_PROCESS_FILE=\"/usr/local/bin/js-beautify -r -f\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'JS_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(PROJECT_NAME)) { @@ -1157,6 +1158,7 @@ public class JavascriptApolloClientCodegen extends DefaultCodegen implements Cod @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1168,20 +1170,7 @@ public class JavascriptApolloClientCodegen extends DefaultCodegen implements Cod // only process files with js extension if ("js".equals(FilenameUtils.getExtension(file.toString()))) { - String command = jsPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } - LOGGER.info("Successfully executed: {}", command); - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {jsPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java index 4860b8f2515..a03647b9106 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavascriptClientCodegen.java @@ -38,7 +38,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER; @@ -222,6 +221,8 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo if (StringUtils.isEmpty(System.getenv("JS_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable JS_POST_PROCESS_FILE not defined so the JS code may not be properly formatted. To define it, try 'export JS_POST_PROCESS_FILE=\"/usr/local/bin/js-beautify -r -f\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'JS_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(PROJECT_NAME)) { @@ -1163,6 +1164,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1174,20 +1176,7 @@ public class JavascriptClientCodegen extends DefaultCodegen implements CodegenCo // only process files with js extension if ("js".equals(FilenameUtils.getExtension(file.toString()))) { - String command = jsPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } - LOGGER.info("Successfully executed: {}", command); - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {jsPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java index c6aa293cdee..5413a983869 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/NodeJSExpressServerCodegen.java @@ -42,7 +42,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.net.URL; import java.util.*; import java.util.Map.Entry; @@ -303,6 +302,8 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege if (StringUtils.isEmpty(System.getenv("JS_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable JS_POST_PROCESS_FILE not defined so the JS code may not be properly formatted. To define it, try 'export JS_POST_PROCESS_FILE=\"/usr/local/bin/js-beautify -r -f\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'JS_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(EXPORTED_NAME)) { @@ -422,6 +423,7 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -433,20 +435,7 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege // only process files with js extension if ("js".equals(FilenameUtils.getExtension(file.toString()))) { - String command = jsPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - p.waitFor(); - int exitValue = p.exitValue(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } - LOGGER.info("Successfully executed: {}", command); - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {jsPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/OCamlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/OCamlClientCodegen.java index 32329d4a859..de6cb04ee96 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/OCamlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/OCamlClientCodegen.java @@ -35,7 +35,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @@ -806,20 +805,7 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig } // only process files with ml or mli extension if ("ml".equals(FilenameUtils.getExtension(file.toString())) || "mli".equals(FilenameUtils.getExtension(file.toString()))) { - String command = ocamlPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {ocamlPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PerlClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PerlClientCodegen.java index 67dee27a017..f1043e40986 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PerlClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PerlClientCodegen.java @@ -28,7 +28,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.Arrays; import java.util.EnumSet; import java.util.HashMap; @@ -173,6 +172,8 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { if (StringUtils.isEmpty(System.getenv("PERL_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable PERL_POST_PROCESS_FILE not defined so the Perl code may not be properly formatted. To define it, try 'export PERL_POST_PROCESS_FILE=/usr/local/bin/perltidy -b -bext=\"/\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'PERL_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey(MODULE_VERSION)) { @@ -624,6 +625,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -637,20 +639,7 @@ public class PerlClientCodegen extends DefaultCodegen implements CodegenConfig { if ("t".equals(FilenameUtils.getExtension(file.toString())) || "pm".equals(FilenameUtils.getExtension(file.toString())) || "pl".equals(FilenameUtils.getExtension(file.toString()))) { - String command = perlTidyPath + " -b -bext='/' " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {perlTidyPath, "-b", "-bext='/'", file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java index 6550d352bb5..b6e466dd5b5 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellClientCodegen.java @@ -35,7 +35,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static java.util.UUID.randomUUID; @@ -618,6 +617,8 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo if (StringUtils.isEmpty(System.getenv("POWERSHELL_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable POWERSHELL_POST_PROCESS_FILE not defined so the PowerShell code may not be properly formatted. To define it, try 'export POWERSHELL_POST_PROCESS_FILE=\"Edit-DTWBeautifyScript\"'"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'POWERSHELL_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (additionalProperties.containsKey("powershellGalleryUrl")) { @@ -1456,6 +1457,7 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1466,20 +1468,7 @@ public class PowerShellClientCodegen extends DefaultCodegen implements CodegenCo // only process files with ps extension if ("ps".equals(FilenameUtils.getExtension(file.toString()))) { - String command = powershellPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {powershellPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java index da30b6e1ed7..ee91757e101 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonClientCodegen.java @@ -178,11 +178,6 @@ public class PythonClientCodegen extends AbstractPythonCodegen implements Codege // map to Dot instead of Period specialCharReplacements.put(".", "Dot"); - if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) { - LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)"); - LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); - } - Boolean excludeTests = false; if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonPydanticV1ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonPydanticV1ClientCodegen.java index bf1823b9016..3c5413e1fee 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonPydanticV1ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PythonPydanticV1ClientCodegen.java @@ -193,6 +193,8 @@ public class PythonPydanticV1ClientCodegen extends AbstractPythonPydanticV1Codeg if (StringUtils.isEmpty(System.getenv("PYTHON_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable PYTHON_POST_PROCESS_FILE not defined so the Python code may not be properly formatted. To define it, try 'export PYTHON_POST_PROCESS_FILE=\"/usr/local/bin/yapf -i\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'PYTHON_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } Boolean excludeTests = false; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java index 6cef19c836f..d73b35b66b0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustAxumServerCodegen.java @@ -43,7 +43,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.math.BigInteger; import java.nio.file.Path; import java.util.*; @@ -275,6 +274,8 @@ public class RustAxumServerCodegen extends AbstractRustCodegen implements Codege " 'export RUST_POST_PROCESS_FILE=\"/usr/local/bin/rustfmt\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` " + " (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'RUST_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (!Boolean.TRUE.equals(ModelUtils.isGenerateAliasAsModel())) { @@ -840,6 +841,7 @@ public class RustAxumServerCodegen extends AbstractRustCodegen implements Codege @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -858,18 +860,7 @@ public class RustAxumServerCodegen extends AbstractRustCodegen implements Codege // only process files with .rs extension if ("rs".equals(FilenameUtils.getExtension(fileName))) { - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({} {}). Exit code: {}", cmd, file, exitValue); - } else { - LOGGER.info("Successfully executed: {} {}", cmd, file); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({} {}). Exception: {}", cmd, file, e.getMessage()); - Thread.currentThread().interrupt(); - } + this.executePostProcessor(command); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java index 399266d4bd2..5065df651e9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/RustServerCodegen.java @@ -49,11 +49,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.math.BigInteger; import java.net.URL; import java.util.*; -import java.util.Map.Entry; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -265,6 +263,8 @@ public class RustServerCodegen extends AbstractRustCodegen implements CodegenCon " 'export RUST_POST_PROCESS_FILE=\"/usr/local/bin/rustfmt\"' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` " + " (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'RUST_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } if (!Boolean.TRUE.equals(ModelUtils.isGenerateAliasAsModel())) { @@ -1570,6 +1570,7 @@ public class RustServerCodegen extends AbstractRustCodegen implements CodegenCon @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1581,19 +1582,7 @@ public class RustServerCodegen extends AbstractRustCodegen implements CodegenCon // only process files with .rs extension if ("rs".equals(FilenameUtils.getExtension(file.toString()))) { - try { - Process p = Runtime.getRuntime().exec(new String[]{commandPrefix, file.toString()}); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({} {}). Exit code: {}", commandPrefix, file, exitValue); - } else { - LOGGER.info("Successfully executed: {} {}", commandPrefix, file); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({} ()). Exception: {}", commandPrefix, file, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {commandPrefix, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java index 13595c2a5df..00e727cf2db 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java @@ -36,7 +36,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.time.OffsetDateTime; import java.time.Instant; @@ -411,6 +410,8 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig if (StringUtils.isEmpty(System.getenv("SWIFT_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable SWIFT_POST_PROCESS_FILE not defined so the Swift code may not be properly formatted. To define it, try 'export SWIFT_POST_PROCESS_FILE=/usr/local/bin/swiftformat' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'SWIFT_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } // Setup project name @@ -1160,6 +1161,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1169,20 +1171,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } // only process files with swift extension if ("swift".equals(FilenameUtils.getExtension(file.toString()))) { - String command = swiftPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {swiftPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java index 091dec2201b..07eee1f2c05 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java @@ -36,7 +36,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.time.OffsetDateTime; import java.time.Instant; @@ -450,6 +449,8 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig "Environment variable SWIFT_POST_PROCESS_FILE not defined so the Swift code may not be properly formatted. To define it, try 'export SWIFT_POST_PROCESS_FILE=/usr/local/bin/swiftformat' (Linux/Mac)"); LOGGER.info( "NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'SWIFT_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } // Setup project name @@ -1229,6 +1230,7 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1238,20 +1240,7 @@ public class Swift6ClientCodegen extends DefaultCodegen implements CodegenConfig } // only process files with swift extension if ("swift".equals(FilenameUtils.getExtension(file.toString()))) { - String command = swiftPostProcessFile + " " + file; - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {swiftPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java index 1a927937c88..95bd6c3b0bb 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SwiftCombineClientCodegen.java @@ -34,7 +34,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.function.Function; import java.util.regex.Matcher; @@ -225,6 +224,8 @@ public class SwiftCombineClientCodegen extends DefaultCodegen implements Codegen if (StringUtils.isEmpty(System.getenv("SWIFT_POST_PROCESS_FILE"))) { LOGGER.info("Environment variable SWIFT_POST_PROCESS_FILE not defined so the Swift code may not be properly formatted. To define it, try 'export SWIFT_POST_PROCESS_FILE=/usr/local/bin/swiftformat' (Linux/Mac)"); LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); + } else if (!this.isEnablePostProcessFile()) { + LOGGER.info("Warning: Environment variable 'SWIFT_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI)."); } // Setup project name @@ -694,6 +695,7 @@ public class SwiftCombineClientCodegen extends DefaultCodegen implements Codegen @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -703,20 +705,7 @@ public class SwiftCombineClientCodegen extends DefaultCodegen implements Codegen } // only process files with swift extension if ("swift".equals(FilenameUtils.getExtension(file.toString()))) { - String command = swiftPostProcessFile + " " + file.toString(); - try { - Process p = Runtime.getRuntime().exec(command); - int exitValue = p.waitFor(); - if (exitValue != 0) { - LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue); - } else { - LOGGER.info("Successfully executed: {}", command); - } - } catch (InterruptedException | IOException e) { - LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage()); - // Restore interrupted state - Thread.currentThread().interrupt(); - } + this.executePostProcessor(new String[] {swiftPostProcessFile, file.toString()}); } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index d5da26e5be8..79a90cad2ee 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -57,6 +57,11 @@ import java.io.IOException; import java.nio.file.Files; import java.util.*; import java.util.stream.Collectors; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import static org.assertj.core.api.Assertions.assertThat; import static org.testng.Assert.*; @@ -719,6 +724,41 @@ public class DefaultCodegenTest { Assertions.assertTrue(typeSeen); } + @Test + public void testExecutePostProcessor() throws InterruptedException, ExecutionException { + final DefaultCodegen codegen = new DefaultCodegen(); + + ExecutorService executor = Executors.newFixedThreadPool(1); + try { + Future call1 = executor.submit(new Callable() { + @Override + public Boolean call() throws Exception { + return codegen.executePostProcessor(new String[] { "binary_does_not_exist" }); + } + }); + + Future call2 = executor.submit(new Callable() { + @Override + public Boolean call() throws Exception { + return codegen.executePostProcessor(new String[] { "echo Hello" }); + } + }); + + Future call3 = executor.submit(new Callable() { + @Override + public Boolean call() throws Exception { + return codegen.executePostProcessor(new String[] { "echo", "Hello" }); + } + }); + + assertFalse(call1.get()); + assertTrue(call2.get()); + assertTrue(call3.get()); + } finally { + executor.shutdown(); + } + } + @Test public void testComposedSchemaOneOfWithProperties() { final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/oneOf.yaml");