Skip to content

Commit 899ddec

Browse files
authored
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
1 parent 98468ab commit 899ddec

37 files changed

+219
-484
lines changed

docs/file-post-processing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Also refer to the relevant documentation for [CLI](./usage.md), [Maven Plugin](h
1919
The following environment variables are supported by their respective generators:
2020
<!-- query with: grep -Rn '_POST_PROCESS_FILE"' modules | grep -Eo '[^"]+_POST_PROCESS_FILE' | sort -u -->
2121

22+
* `AVRO_POST_PROCESS_FILE`
2223
* `CPP_POST_PROCESS_FILE`
2324
* `CSHARP_POST_PROCESS_FILE`
2425
* `C_POST_PROCESS_FILE`

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
import org.slf4j.LoggerFactory;
7272

7373
import java.io.File;
74+
import java.io.BufferedReader;
75+
import java.io.InputStreamReader;
76+
import java.io.IOException;
77+
import java.nio.charset.StandardCharsets;
7478
import java.util.*;
7579
import java.util.Map.Entry;
7680
import java.util.concurrent.ConcurrentSkipListSet;
@@ -8088,6 +8092,43 @@ public void postProcessFile(File file, String fileType) {
80888092
LOGGER.debug("Post processing file {} ({})", file, fileType);
80898093
}
80908094

8095+
/**
8096+
* Executes an external command for file post processing.
8097+
*
8098+
* @param commandArr an array of commands and arguments. They will be concatenated with space and tokenized again.
8099+
* @return Whether the execution passed (true) or failed (false)
8100+
*/
8101+
protected boolean executePostProcessor(String[] commandArr) {
8102+
final String command = String.join(" ", commandArr);
8103+
try {
8104+
// we don't use the array variant here, because the command passed in by the user is often not only a single binary
8105+
// but a combination of binary + parameters, e.g. `/etc/bin prettier -w`, which would then not be found, as the
8106+
// first array item would be expected to be the binary only. The exec method is tokenizing the command for us.
8107+
Process p = Runtime.getRuntime().exec(command);
8108+
p.waitFor();
8109+
int exitValue = p.exitValue();
8110+
if (exitValue != 0) {
8111+
try (InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8);
8112+
BufferedReader br = new BufferedReader(inputStreamReader)) {
8113+
StringBuilder sb = new StringBuilder();
8114+
String line;
8115+
while ((line = br.readLine()) != null) {
8116+
sb.append(line);
8117+
}
8118+
LOGGER.error("Error running the command ({}). Exit value: {}, Error output: {}", command, exitValue, sb);
8119+
}
8120+
} else {
8121+
LOGGER.info("Successfully executed: {}", command);
8122+
return true;
8123+
}
8124+
} catch (InterruptedException | IOException e) {
8125+
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
8126+
// Restore interrupted state
8127+
Thread.currentThread().interrupt();
8128+
}
8129+
return false;
8130+
}
8131+
80918132
/**
80928133
* Boolean value indicating the state of the option for post-processing file using environment variables.
80938134
*

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractAdaCodegen.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import org.slf4j.LoggerFactory;
4343

4444
import java.io.File;
45-
import java.io.IOException;
4645
import java.util.*;
4746

4847
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
@@ -1065,20 +1064,8 @@ public void postProcessFile(File file, String fileType) {
10651064
if (StringUtils.isEmpty(commandPrefix)) {
10661065
commandPrefix = "gnatpp";
10671066
}
1068-
1069-
try {
1070-
Process p = Runtime.getRuntime().exec(new String[]{commandPrefix, "--no-compact", "--quiet", file.toString()});
1071-
int exitValue = p.waitFor();
1072-
if (exitValue != 0) {
1073-
LOGGER.error("Error running the command ({} {}). Exit code: {}", commandPrefix, file, exitValue);
1074-
} else {
1075-
LOGGER.debug("Successfully executed: {} {}", commandPrefix, file);
1076-
}
1077-
} catch (InterruptedException | IOException e) {
1078-
LOGGER.error("Error running the command ({} {}). Exception: {}", commandPrefix, file, e.getMessage());
1079-
// Restore interrupted state
1080-
Thread.currentThread().interrupt();
1081-
}
1067+
String[] commandArr = new String[]{commandPrefix, "--no-compact", "--quiet", file.toString()};
1068+
this.executePostProcessor(commandArr);
10821069
}
10831070
}
10841071

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCSharpCodegen.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ public void processOpts() {
242242
if (StringUtils.isEmpty(System.getenv("CSHARP_POST_PROCESS_FILE"))) {
243243
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");
244244
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
245+
} else if (!this.isEnablePostProcessFile()) {
246+
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).");
245247
}
246248

247249
// License info
@@ -1879,6 +1881,7 @@ public void setParameterExampleValue(CodegenParameter p) {
18791881

18801882
@Override
18811883
public void postProcessFile(File file, String fileType) {
1884+
super.postProcessFile(file, fileType);
18821885
if (file == null) {
18831886
return;
18841887
}
@@ -1890,20 +1893,7 @@ public void postProcessFile(File file, String fileType) {
18901893

18911894
// only process files with .cs extension
18921895
if ("cs".equals(FilenameUtils.getExtension(file.toString()))) {
1893-
String command = csharpPostProcessFile + " " + file;
1894-
try {
1895-
Process p = Runtime.getRuntime().exec(command);
1896-
int exitValue = p.waitFor();
1897-
if (exitValue != 0) {
1898-
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
1899-
} else {
1900-
LOGGER.info("Successfully executed: {}", command);
1901-
}
1902-
} catch (InterruptedException | IOException e) {
1903-
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
1904-
// Restore interrupted state
1905-
Thread.currentThread().interrupt();
1906-
}
1896+
this.executePostProcessor(new String[] {csharpPostProcessFile, file.toString()});
19071897
}
19081898
}
19091899

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractCppCodegen.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.slf4j.LoggerFactory;
3838

3939
import java.io.File;
40-
import java.io.IOException;
4140
import java.net.URL;
4241
import java.util.ArrayList;
4342
import java.util.Arrays;
@@ -315,6 +314,8 @@ public void processOpts() {
315314
if (StringUtils.isEmpty(System.getenv("CPP_POST_PROCESS_FILE"))) {
316315
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)");
317316
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
317+
} else if (!this.isEnablePostProcessFile()) {
318+
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).");
318319
}
319320

320321
if (additionalProperties.containsKey(RESERVED_WORD_PREFIX_OPTION)) {
@@ -337,6 +338,7 @@ protected ImmutableMap.Builder<String, Lambda> addMustacheLambdas() {
337338

338339
@Override
339340
public void postProcessFile(File file, String fileType) {
341+
super.postProcessFile(file, fileType);
340342
if (file == null) {
341343
return;
342344
}
@@ -346,21 +348,7 @@ public void postProcessFile(File file, String fileType) {
346348
}
347349
// only process files with cpp extension
348350
if ("cpp".equals(FilenameUtils.getExtension(file.toString())) || "h".equals(FilenameUtils.getExtension(file.toString()))) {
349-
String command = cppPostProcessFile + " " + file;
350-
try {
351-
Process p = Runtime.getRuntime().exec(command);
352-
p.waitFor();
353-
int exitValue = p.exitValue();
354-
if (exitValue != 0) {
355-
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
356-
} else {
357-
LOGGER.info("Successfully executed: {}", command);
358-
}
359-
} catch (InterruptedException | IOException e) {
360-
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
361-
// Restore interrupted state
362-
Thread.currentThread().interrupt();
363-
}
351+
this.executePostProcessor(new String[] {cppPostProcessFile, file.toString()});
364352
}
365353
}
366354

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractDartCodegen.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import java.io.BufferedReader;
2323
import java.io.File;
24-
import java.io.IOException;
2524
import java.io.InputStreamReader;
2625
import java.nio.charset.StandardCharsets;
2726
import java.util.*;
@@ -229,6 +228,8 @@ public void processOpts() {
229228
if (StringUtils.isEmpty(System.getenv("DART_POST_PROCESS_FILE"))) {
230229
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)");
231230
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
231+
} else if (!this.isEnablePostProcessFile()) {
232+
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).");
232233
}
233234

234235
if (additionalProperties.containsKey(PUB_NAME)) {
@@ -805,20 +806,7 @@ public void postProcessFile(File file, String fileType) {
805806
// process all files with dart extension
806807
if ("dart".equals(FilenameUtils.getExtension(file.toString()))) {
807808
// currently supported is "dartfmt -w" and "dart format"
808-
String command = dartPostProcessFile + " " + file;
809-
try {
810-
Process p = Runtime.getRuntime().exec(command);
811-
int exitValue = p.waitFor();
812-
if (exitValue != 0) {
813-
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
814-
} else {
815-
LOGGER.info("Successfully executed: {}", command);
816-
}
817-
} catch (InterruptedException | IOException e) {
818-
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
819-
// Restore interrupted state
820-
Thread.currentThread().interrupt();
821-
}
809+
this.executePostProcessor(new String[] {dartPostProcessFile, file.toString()});
822810
}
823811
}
824812

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractFSharpCodegen.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.slf4j.LoggerFactory;
3737

3838
import java.io.File;
39-
import java.io.IOException;
4039
import java.util.*;
4140

4241
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
@@ -1039,6 +1038,7 @@ public void setParameterExampleValue(CodegenParameter codegenParameter) {
10391038

10401039
@Override
10411040
public void postProcessFile(File file, String fileType) {
1041+
super.postProcessFile(file, fileType);
10421042
if (file == null) {
10431043
return;
10441044
}
@@ -1050,20 +1050,7 @@ public void postProcessFile(File file, String fileType) {
10501050

10511051
// only process files with .fs extension
10521052
if ("fs".equals(FilenameUtils.getExtension(file.toString()))) {
1053-
String command = fsharpPostProcessFile + " " + file;
1054-
try {
1055-
Process p = Runtime.getRuntime().exec(command);
1056-
int exitValue = p.waitFor();
1057-
if (exitValue != 0) {
1058-
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
1059-
} else {
1060-
LOGGER.info("Successfully executed: {}", command);
1061-
}
1062-
} catch (InterruptedException | IOException e) {
1063-
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
1064-
// Restore interrupted state
1065-
Thread.currentThread().interrupt();
1066-
}
1053+
this.executePostProcessor(new String[] {fsharpPostProcessFile, file.toString()});
10671054
}
10681055
}
10691056

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
import java.io.File;
31-
import java.io.IOException;
3231
import java.util.*;
3332

3433
import static org.openapitools.codegen.utils.CamelizeOption.LOWERCASE_FIRST_LETTER;
@@ -173,6 +172,8 @@ public void processOpts() {
173172
if (StringUtils.isEmpty(System.getenv("GO_POST_PROCESS_FILE"))) {
174173
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)");
175174
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
175+
} else if (!this.isEnablePostProcessFile()) {
176+
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).");
176177
}
177178
}
178179

@@ -982,6 +983,7 @@ public String toDefaultValue(Schema schema) {
982983

983984
@Override
984985
public void postProcessFile(File file, String fileType) {
986+
super.postProcessFile(file, fileType);
985987
if (file == null) {
986988
return;
987989
}
@@ -1007,20 +1009,7 @@ public void postProcessFile(File file, String fileType) {
10071009
if ("go".equals(FilenameUtils.getExtension(file.toString()))) {
10081010
// e.g. "gofmt -w yourcode.go"
10091011
// e.g. "go fmt path/to/your/package"
1010-
String command = goPostProcessFile + " " + file;
1011-
try {
1012-
Process p = Runtime.getRuntime().exec(command);
1013-
int exitValue = p.waitFor();
1014-
if (exitValue != 0) {
1015-
LOGGER.error("Error running the command ({}). Exit code: {}", command, exitValue);
1016-
} else {
1017-
LOGGER.info("Successfully executed: {}", command);
1018-
}
1019-
} catch (InterruptedException | IOException e) {
1020-
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
1021-
// Restore interrupted state
1022-
Thread.currentThread().interrupt();
1023-
}
1012+
this.executePostProcessor(new String[] {goPostProcessFile, file.toString()});
10241013
}
10251014
}
10261015

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import org.slf4j.LoggerFactory;
5252

5353
import java.io.File;
54-
import java.io.IOException;
5554
import java.time.LocalDate;
5655
import java.time.ZoneId;
5756
import java.util.*;
@@ -409,6 +408,8 @@ public void processOpts() {
409408
if (StringUtils.isEmpty(System.getenv("JAVA_POST_PROCESS_FILE"))) {
410409
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)");
411410
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
411+
} else if (!this.isEnablePostProcessFile()) {
412+
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).");
412413
}
413414

414415
convertPropertyToBooleanAndWriteBack(BeanValidationFeatures.USE_BEANVALIDATION, this::setUseBeanValidation);
@@ -2226,6 +2227,7 @@ public String getterAndSetterCapitalize(String name) {
22262227

22272228
@Override
22282229
public void postProcessFile(File file, String fileType) {
2230+
super.postProcessFile(file, fileType);
22292231
if (file == null) {
22302232
return;
22312233
}
@@ -2237,21 +2239,7 @@ public void postProcessFile(File file, String fileType) {
22372239

22382240
// only process files with java extension
22392241
if ("java".equals(FilenameUtils.getExtension(file.toString()))) {
2240-
String command = javaPostProcessFile + " " + file;
2241-
try {
2242-
Process p = Runtime.getRuntime().exec(command);
2243-
p.waitFor();
2244-
int exitValue = p.exitValue();
2245-
if (exitValue != 0) {
2246-
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
2247-
} else {
2248-
LOGGER.info("Successfully executed: {}", command);
2249-
}
2250-
} catch (InterruptedException | IOException e) {
2251-
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
2252-
// Restore interrupted state
2253-
Thread.currentThread().interrupt();
2254-
}
2242+
this.executePostProcessor(new String[] {javaPostProcessFile, file.toString()});
22552243
}
22562244
}
22572245

0 commit comments

Comments
 (0)