Skip to content

Commit

Permalink
Add file post-processing to PHP generators (OpenAPITools#1402)
Browse files Browse the repository at this point in the history
* add file post processing to php

* restore php petstore client
  • Loading branch information
wing328 authored Nov 9, 2018
1 parent 0b1f428 commit 78a8d26
Showing 1 changed file with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
Expand All @@ -40,12 +41,6 @@
import java.util.Map;
import java.util.regex.Matcher;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class AbstractPhpCodegen extends DefaultCodegen implements CodegenConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPhpCodegen.class);
Expand Down Expand Up @@ -161,6 +156,11 @@ public AbstractPhpCodegen() {
public void processOpts() {
super.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).");
}

if (additionalProperties.containsKey(PACKAGE_NAME)) {
this.setPackageName((String) additionalProperties.get(PACKAGE_NAME));
} else {
Expand Down Expand Up @@ -773,4 +773,31 @@ protected String extractSimpleName(String phpClassName) {
final int lastBackslashIndex = phpClassName.lastIndexOf('\\');
return phpClassName.substring(lastBackslashIndex + 1);
}

@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}
String phpPostProcessFile = System.getenv("PHP_POST_PROCESS_FILE");
if (StringUtils.isEmpty(phpPostProcessFile)) {
return; // skip if PHP_POST_PROCESS_FILE env variable is not defined
}
// only process files with php extension
if ("php".equals(FilenameUtils.getExtension(file.toString()))) {
String command = phpPostProcessFile + " " + file.toString();
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 (Exception e) {
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
}
}
}
}

0 comments on commit 78a8d26

Please sign in to comment.