Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file post-processing to PHP generators #1402

Merged
merged 2 commits into from
Nov 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}
}