Skip to content

Commit

Permalink
add configFiles generation
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 4, 2024
1 parent 11553dd commit 3b9e045
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map;

import io.swagger.codegen.v3.CodegenArgument;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
Expand Down Expand Up @@ -434,7 +435,11 @@ protected void execute_() throws MojoExecutionException {
}

if (null != generateSupportingFiles && generateSupportingFiles) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
if (StringUtils.isBlank(supportingFilesToGenerate)) {
System.setProperty(CodegenConstants.SUPPORTING_FILES, "true");
} else {
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
}
} else {
System.clearProperty(CodegenConstants.SUPPORTING_FILES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public interface CodegenConfig {

List<SupportingFile> supportingFiles();

List<SupportingFile> configFiles();

String getInputSpec();

void setInputSpec(String inputSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,65 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl

}

private void generateConfigFiles(List<File> files, Map<String, Object> bundle) {
for (SupportingFile support : config.configFiles()) {
try {
String outputFolder = config.outputFolder();
if (StringUtils.isNotEmpty(support.folder)) {
outputFolder += File.separator + support.folder;
}
File of = new File(outputFolder);
if (!of.isDirectory()) {
of.mkdirs();
}
String outputFilename = outputFolder + File.separator + support.destinationFilename.replace('/', File.separatorChar);
if (!config.shouldOverwrite(outputFilename)) {
LOGGER.info("Skipped overwriting " + outputFilename);
continue;
}
String templateFile;
if( support instanceof GlobalSupportingFile) {
templateFile = config.getCommonTemplateDir() + File.separator + support.templateFile;
} else {
templateFile = getFullTemplateFile(config, support.templateFile);
}

if(ignoreProcessor.allowsFile(new File(outputFilename))) {
if (templateFile.endsWith("mustache")) {
String rendered = templateEngine.getRendered(templateFile, bundle);
writeToFile(outputFilename, rendered);
files.add(new File(outputFilename));
} else {
InputStream in = null;

try {
in = new FileInputStream(templateFile);
} catch (Exception e) {
// continue
}
if (in == null) {
in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile));
}
File outputFile = new File(outputFilename);
OutputStream out = new FileOutputStream(outputFile, false);
if (in != null) {
LOGGER.info("writing file " + outputFile);
IOUtils.copy(in, out);
out.close();
} else {
LOGGER.warn("can't open " + templateFile + " for input");
}
files.add(outputFile);
}
} else {
LOGGER.info("Skipped generation of " + outputFilename + " due to rule in .swagger-codegen-ignore");
}
} catch (Exception e) {
throw new RuntimeException("Could not generate config file '" + support + "'", e);
}
}
}

private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {

Map<String, Object> bundle = new HashMap<>();
Expand Down Expand Up @@ -795,8 +854,9 @@ public List<File> generate() {
List<Object> allOperations = new ArrayList<>();
generateApis(files, allOperations, allModels);

// supporting files
// supporting and config files
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
generateConfigFiles(files, bundle);
generateSupportingFiles(files, bundle);
config.processOpenAPI(openAPI);
return files;
Expand Down Expand Up @@ -840,7 +900,7 @@ public Map<String, Object> generateBundle() {

// supporting files
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
Json.prettyPrint(bundle);
generateConfigFiles(files, bundle);
generateSupportingFiles(files, bundle);
config.processOpenAPI(openAPI);
return bundle;
Expand Down

0 comments on commit 3b9e045

Please sign in to comment.