Skip to content

Commit cc6efe0

Browse files
authored
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
1 parent 3cacbcb commit cc6efe0

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20+
import org.apache.commons.io.FilenameUtils;
21+
import org.apache.commons.lang3.StringUtils;
2022
import io.swagger.v3.oas.models.media.ArraySchema;
2123
import io.swagger.v3.oas.models.media.Schema;
2224
import org.openapitools.codegen.CodegenConfig;
@@ -25,6 +27,7 @@
2527
import org.slf4j.Logger;
2628
import org.slf4j.LoggerFactory;
2729

30+
import java.io.File;
2831
import java.util.Arrays;
2932
import java.util.Locale;
3033

@@ -77,6 +80,15 @@ abstract class AbstractRubyCodegen extends DefaultCodegen implements CodegenConf
7780
typeMapping.put("UUID", "String");
7881
}
7982

83+
@Override
84+
public void processOpts() {
85+
super.processOpts();
86+
87+
if (StringUtils.isEmpty(System.getenv("RUBY_POST_PROCESS_FILE"))) {
88+
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)");
89+
}
90+
}
91+
8092
@Override
8193
public String escapeReservedWord(String name) {
8294
if (this.reservedWordsMappings().containsKey(name)) {
@@ -162,4 +174,30 @@ public String escapeQuotationMark(String input) {
162174
public String escapeUnsafeCharacters(String input) {
163175
return input.replace("=end", "=_end").replace("=begin", "=_begin");
164176
}
177+
178+
@Override
179+
public void postProcessFile(File file, String fileType) {
180+
if (file == null) {
181+
return;
182+
}
183+
String rubyPostProcessFile = System.getenv("RUBY_POST_PROCESS_FILE");
184+
if (StringUtils.isEmpty(rubyPostProcessFile)) {
185+
return; // skip if RUBY_POST_PROCESS_FILE env variable is not defined
186+
}
187+
// only process files with rb extension
188+
if ("rb".equals(FilenameUtils.getExtension(file.toString()))) {
189+
String command = rubyPostProcessFile + " " + file.toString();
190+
try {
191+
Process p = Runtime.getRuntime().exec(command);
192+
int exitValue = p.waitFor();
193+
if (exitValue != 0) {
194+
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
195+
} else {
196+
LOGGER.info("Successfully executed: " + command);
197+
}
198+
} catch (Exception e) {
199+
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
200+
}
201+
}
202+
}
165203
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.Locale;
3030
import java.util.Map;
3131

32-
3332
public class RubyClientCodegen extends AbstractRubyCodegen {
3433
private static final Logger LOGGER = LoggerFactory.getLogger(RubyClientCodegen.class);
3534
public static final String GEM_NAME = "gemName";

0 commit comments

Comments
 (0)