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 configFiles generation #12487

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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 @@ -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
Loading