Add file post processing to Ruby generators (#1062)

* add post processing for Ruby file

* move ruby file post process to abstract ruby class

* remove line break
This commit is contained in:
William Cheng 2018-09-20 19:31:31 +08:00 committed by GitHub
parent 3cacbcb965
commit cc6efe01c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -17,6 +17,8 @@
package org.openapitools.codegen.languages;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import org.openapitools.codegen.CodegenConfig;
@ -25,6 +27,7 @@ import org.openapitools.codegen.utils.ModelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Arrays;
import java.util.Locale;
@ -77,6 +80,15 @@ abstract class AbstractRubyCodegen extends DefaultCodegen implements CodegenConf
typeMapping.put("UUID", "String");
}
@Override
public void processOpts() {
super.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)");
}
}
@Override
public String escapeReservedWord(String name) {
if (this.reservedWordsMappings().containsKey(name)) {
@ -162,4 +174,30 @@ abstract class AbstractRubyCodegen extends DefaultCodegen implements CodegenConf
public String escapeUnsafeCharacters(String input) {
return input.replace("=end", "=_end").replace("=begin", "=_begin");
}
@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}
String rubyPostProcessFile = System.getenv("RUBY_POST_PROCESS_FILE");
if (StringUtils.isEmpty(rubyPostProcessFile)) {
return; // skip if RUBY_POST_PROCESS_FILE env variable is not defined
}
// only process files with rb extension
if ("rb".equals(FilenameUtils.getExtension(file.toString()))) {
String command = rubyPostProcessFile + " " + 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 (Exception e) {
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
}
}
}
}

View File

@ -29,7 +29,6 @@ import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
public class RubyClientCodegen extends AbstractRubyCodegen {
private static final Logger LOGGER = LoggerFactory.getLogger(RubyClientCodegen.class);
public static final String GEM_NAME = "gemName";