Skip to content

Commit 0001682

Browse files
authored
Add file post-processing support for Java generators (#1052)
* add clang format support for java * rename java env variable * fix export command
1 parent e782001 commit 0001682

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.swagger.v3.oas.models.media.StringSchema;
2828
import io.swagger.v3.parser.util.SchemaTypeUtil;
2929

30+
import org.apache.commons.io.FilenameUtils;
3031
import org.apache.commons.lang3.BooleanUtils;
3132
import org.apache.commons.lang3.StringUtils;
3233
import org.openapitools.codegen.CliOption;
@@ -212,6 +213,10 @@ public AbstractJavaCodegen() {
212213
public void processOpts() {
213214
super.processOpts();
214215

216+
if (StringUtils.isEmpty(System.getenv("JAVA_POST_PROCESS_FILE"))) {
217+
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)");
218+
}
219+
215220
if (additionalProperties.containsKey(SUPPORT_JAVA6)) {
216221
this.setSupportJava6(Boolean.valueOf(additionalProperties.get(SUPPORT_JAVA6).toString()));
217222
}
@@ -226,7 +231,6 @@ public void processOpts() {
226231
this.setBooleanGetterPrefix(additionalProperties.get(BOOLEAN_GETTER_PREFIX).toString());
227232
}
228233
additionalProperties.put(BOOLEAN_GETTER_PREFIX, booleanGetterPrefix);
229-
230234
if (additionalProperties.containsKey(USE_NULL_FOR_UNKNOWN_ENUM_VALUE)) {
231235
this.setUseNullForUnknownEnumValue(Boolean.valueOf(additionalProperties.get(USE_NULL_FOR_UNKNOWN_ENUM_VALUE).toString()));
232236
}
@@ -1382,6 +1386,35 @@ public String sanitizeTag(String tag) {
13821386
return tag;
13831387
}
13841388

1389+
@Override
1390+
public void postProcessFile(File file, String fileType) {
1391+
if (file == null) {
1392+
return;
1393+
}
1394+
1395+
String javaPostProcessFile = System.getenv("JAVA_POST_PROCESS_FILE");
1396+
if (StringUtils.isEmpty(javaPostProcessFile)) {
1397+
return; // skip if JAVA_POST_PROCESS_FILE env variable is not defined
1398+
}
1399+
1400+
// only process files with hs extension
1401+
if ("java".equals(FilenameUtils.getExtension(file.toString()))) {
1402+
String command = javaPostProcessFile + " " + file.toString();
1403+
try {
1404+
Process p = Runtime.getRuntime().exec(command);
1405+
p.waitFor();
1406+
int exitValue = p.exitValue();
1407+
if (exitValue != 0) {
1408+
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
1409+
} else {
1410+
LOGGER.info("Successfully executed: " + command);
1411+
}
1412+
} catch (Exception e) {
1413+
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
1414+
}
1415+
}
1416+
}
1417+
13851418
public void setParentGroupId(final String parentGroupId) {
13861419
this.parentGroupId = parentGroupId;
13871420
}

0 commit comments

Comments
 (0)