Skip to content

Commit 3b9e045

Browse files
committed
add configFiles generation
1 parent 11553dd commit 3b9e045

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

modules/swagger-codegen-maven-plugin/src/main/java/io/swagger/codegen/v3/maven/plugin/CodeGenMojo.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Map;
3636

3737
import io.swagger.codegen.v3.CodegenArgument;
38+
import org.apache.commons.lang3.StringUtils;
3839
import org.apache.maven.plugin.AbstractMojo;
3940
import org.apache.maven.plugin.MojoExecutionException;
4041
import org.apache.maven.plugins.annotations.LifecyclePhase;
@@ -434,7 +435,11 @@ protected void execute_() throws MojoExecutionException {
434435
}
435436

436437
if (null != generateSupportingFiles && generateSupportingFiles) {
437-
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
438+
if (StringUtils.isBlank(supportingFilesToGenerate)) {
439+
System.setProperty(CodegenConstants.SUPPORTING_FILES, "true");
440+
} else {
441+
System.setProperty(CodegenConstants.SUPPORTING_FILES, supportingFilesToGenerate);
442+
}
438443
} else {
439444
System.clearProperty(CodegenConstants.SUPPORTING_FILES);
440445
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/CodegenConfig.java

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public interface CodegenConfig {
8383

8484
List<SupportingFile> supportingFiles();
8585

86+
List<SupportingFile> configFiles();
87+
8688
String getInputSpec();
8789

8890
void setInputSpec(String inputSpec);

modules/swagger-codegen/src/main/java/io/swagger/codegen/v3/DefaultGenerator.java

+62-2
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,65 @@ private void generateSupportingFiles(List<File> files, Map<String, Object> bundl
725725

726726
}
727727

728+
private void generateConfigFiles(List<File> files, Map<String, Object> bundle) {
729+
for (SupportingFile support : config.configFiles()) {
730+
try {
731+
String outputFolder = config.outputFolder();
732+
if (StringUtils.isNotEmpty(support.folder)) {
733+
outputFolder += File.separator + support.folder;
734+
}
735+
File of = new File(outputFolder);
736+
if (!of.isDirectory()) {
737+
of.mkdirs();
738+
}
739+
String outputFilename = outputFolder + File.separator + support.destinationFilename.replace('/', File.separatorChar);
740+
if (!config.shouldOverwrite(outputFilename)) {
741+
LOGGER.info("Skipped overwriting " + outputFilename);
742+
continue;
743+
}
744+
String templateFile;
745+
if( support instanceof GlobalSupportingFile) {
746+
templateFile = config.getCommonTemplateDir() + File.separator + support.templateFile;
747+
} else {
748+
templateFile = getFullTemplateFile(config, support.templateFile);
749+
}
750+
751+
if(ignoreProcessor.allowsFile(new File(outputFilename))) {
752+
if (templateFile.endsWith("mustache")) {
753+
String rendered = templateEngine.getRendered(templateFile, bundle);
754+
writeToFile(outputFilename, rendered);
755+
files.add(new File(outputFilename));
756+
} else {
757+
InputStream in = null;
758+
759+
try {
760+
in = new FileInputStream(templateFile);
761+
} catch (Exception e) {
762+
// continue
763+
}
764+
if (in == null) {
765+
in = this.getClass().getClassLoader().getResourceAsStream(getCPResourcePath(templateFile));
766+
}
767+
File outputFile = new File(outputFilename);
768+
OutputStream out = new FileOutputStream(outputFile, false);
769+
if (in != null) {
770+
LOGGER.info("writing file " + outputFile);
771+
IOUtils.copy(in, out);
772+
out.close();
773+
} else {
774+
LOGGER.warn("can't open " + templateFile + " for input");
775+
}
776+
files.add(outputFile);
777+
}
778+
} else {
779+
LOGGER.info("Skipped generation of " + outputFilename + " due to rule in .swagger-codegen-ignore");
780+
}
781+
} catch (Exception e) {
782+
throw new RuntimeException("Could not generate config file '" + support + "'", e);
783+
}
784+
}
785+
}
786+
728787
private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, List<Object> allModels) {
729788

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

798-
// supporting files
857+
// supporting and config files
799858
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
859+
generateConfigFiles(files, bundle);
800860
generateSupportingFiles(files, bundle);
801861
config.processOpenAPI(openAPI);
802862
return files;
@@ -840,7 +900,7 @@ public Map<String, Object> generateBundle() {
840900

841901
// supporting files
842902
Map<String, Object> bundle = buildSupportFileBundle(allOperations, allModels);
843-
Json.prettyPrint(bundle);
903+
generateConfigFiles(files, bundle);
844904
generateSupportingFiles(files, bundle);
845905
config.processOpenAPI(openAPI);
846906
return bundle;

0 commit comments

Comments
 (0)