diff --git a/docs/file-post-processing.md b/docs/file-post-processing.md index a23845adae2e..d53656195cf0 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 5206c18682da..3d5840ffdd7e 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.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 void postProcessFile(File file, String fileType) { 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 d67987aca813..39efaf8f9242 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.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 @@ public void postProcessFile(File file, String fileType) { 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 3117e9fe1ae0..5c022bc82e62 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 void processOpts() { 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 void setParameterExampleValue(CodegenParameter p) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1890,20 +1893,7 @@ public void postProcessFile(File file, String fileType) { // 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 40335e87c37c..3d7dea3dab86 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.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 @@ public void processOpts() { 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 @@ protected ImmutableMap.Builder addMustacheLambdas() { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -346,21 +348,7 @@ public void postProcessFile(File file, String fileType) { } // 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 fba1dcecf253..eaba6303835d 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 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 void 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)."); + } 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 void postProcessFile(File file, String fileType) { // 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 e203225c162a..a0e688c8fbcf 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.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 void setParameterExampleValue(CodegenParameter codegenParameter) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1050,20 +1050,7 @@ public void postProcessFile(File file, String fileType) { // 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 9f65605bc693..a0624b2544cd 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.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 void processOpts() { 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 String toDefaultValue(Schema schema) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1007,20 +1009,7 @@ public void postProcessFile(File file, String fileType) { 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 6ae58f6bff4c..dcfbad983e0b 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.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 void processOpts() { 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 String getterAndSetterCapitalize(String name) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -2237,21 +2239,7 @@ public void postProcessFile(File file, String fileType) { // 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 65c3b738c77d..456b972244a8 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.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 void processOpts() { 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 @@ private boolean startsWithTwoUppercaseLetters(String name) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -975,21 +977,7 @@ public void postProcessFile(File file, String fileType) { // 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 16930ee127ef..040b6cddd248 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.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 void processOpts() { 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 @@ protected String extractSimpleName(String phpClassName) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -865,21 +867,7 @@ public void postProcessFile(File file, String fileType) { } // 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 b833739796c4..c3d95ec1e93e 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -361,20 +363,7 @@ public void postProcessFile(File file, String fileType) { // 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 fc2d73b3bb2a..58808b6b01a1 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -306,20 +308,7 @@ public void postProcessFile(File file, String fileType) { // 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 7ef29f104a72..b6377164d633 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.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 @@ public void processOpts() { 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 @@ public String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -262,28 +261,7 @@ public void postProcessFile(File file, String fileType) { } // 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 228728eb42d8..2542da515088 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 void processOpts() { 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 String escapeQuotationMark(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -558,20 +561,7 @@ public void postProcessFile(File file, String fileType) { // 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 c4caca0c6227..753cd1b4110b 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 void postProcessFile(File file, String fileType) { } // 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 2d01cfd9f212..98cd3a82cbf6 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 AvroSchemaCodegen() { @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 ModelsMap postProcessModels(ModelsMap objs) { 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 a1154fb8a92a..0ac95f48a9ea 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.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 void processOpts() { 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 CodegenProperty fromProperty(String name, Schema p, boolean required) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -922,20 +924,7 @@ public void postProcessFile(File file, String fileType) { // 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 cd7c18145c6a..684df587ffb5 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -899,30 +898,7 @@ public void postProcessFile(File file, String fileType) { } // 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 95a49a9ee44e..6cd2c7906026 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 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 String getHelp() { 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 a78e9a703cae..3023afab5865 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.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 void processOpts() { 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 String escapeText(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1481,20 +1483,7 @@ public void postProcessFile(File file, String fileType) { // 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 a7785024a06f..608e75d03c02 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.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Pattern; @@ -232,6 +231,8 @@ public void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -706,20 +708,7 @@ public void postProcessFile(File file, String fileType) { // 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 f3587ad772f4..11422819c26e 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.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.regex.Pattern; @@ -189,6 +188,8 @@ public void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -573,20 +575,7 @@ public void postProcessFile(File file, String fileType) { // 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 b7c3d4227043..d0cf4c9e7dd1 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1168,20 +1170,7 @@ public void postProcessFile(File file, String fileType) { // 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 4860b8f2515c..a03647b91060 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1174,20 +1176,7 @@ public void postProcessFile(File file, String fileType) { // 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 c6aa293cdeea..5413a9838695 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.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 void processOpts() { 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 String escapeQuotationMark(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -433,20 +435,7 @@ public void postProcessFile(File file, String fileType) { // 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 32329d4a8595..de6cb04ee967 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.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @@ -806,20 +805,7 @@ public void postProcessFile(File file, String fileType) { } // 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 67dee27a017d..f1043e409861 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -637,20 +639,7 @@ public void postProcessFile(File file, String fileType) { 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 6550d352bb5d..b6e466dd5b52 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.LoggerFactory; import java.io.File; -import java.io.IOException; import java.util.*; import static java.util.UUID.randomUUID; @@ -618,6 +617,8 @@ public void processOpts() { 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 @@ private String toMethodName(String operationId) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1466,20 +1468,7 @@ public void postProcessFile(File file, String fileType) { // 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 da30b6e1ed78..ee91757e1012 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 void processOpts() { // 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 bf1823b9016d..3c5413e1fee8 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 void processOpts() { 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 6cef19c836f8..d73b35b66b07 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.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 void processOpts() { " '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 ModelsMap postProcessModels(ModelsMap modelsMap) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -858,18 +860,7 @@ public void postProcessFile(File file, String fileType) { // 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 399266d4bd28..5065df651e95 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.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 void processOpts() { " '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 @@ private void processParam(CodegenParameter param, CodegenOperation op) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1581,19 +1582,7 @@ public void postProcessFile(File file, String fileType) { // 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 13595c2a5dff..00e727cf2dba 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1169,20 +1171,7 @@ public void postProcessFile(File file, String fileType) { } // 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 091dec2201b2..07eee1f2c05b 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.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 void processOpts() { "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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -1238,20 +1240,7 @@ public void postProcessFile(File file, String fileType) { } // 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 1a927937c888..95bd6c3b0bb7 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.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 void processOpts() { 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 String escapeUnsafeCharacters(String input) { @Override public void postProcessFile(File file, String fileType) { + super.postProcessFile(file, fileType); if (file == null) { return; } @@ -703,20 +705,7 @@ public void postProcessFile(File file, String fileType) { } // 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 d5da26e5be8f..79a90cad2ee0 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.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 void testOneOfMergeProperties() { 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");