changed signature for usage in generator

This commit is contained in:
Tony Tam
2015-02-07 14:59:35 -08:00
parent cbc9fdbe34
commit ffe5fc7fd2
24 changed files with 1719 additions and 16 deletions

View File

@@ -12,26 +12,33 @@ import java.io.File;
import java.util.*;
public class Codegen extends DefaultGenerator {
static Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
static String configString;
static {
List<CodegenConfig> extensions = getExtensions();
StringBuilder sb = new StringBuilder();
for(CodegenConfig config : extensions) {
if(sb.toString().length() != 0)
sb.append(", ");
sb.append(config.getName());
configs.put(config.getName(), config);
configString = sb.toString();
}
}
static String debugInfoOptions = "\nThe following additional debug options are available for all codegen targets:" +
"\n -DdebugSwagger prints the swagger specification as interpreted by the codegen" +
"\n -DdebugModels prints models passed to the template engine" +
"\n -DdebugOperations prints operations passed to the template engine" +
"\n -DdebugSupportingFiles prints additional data passed to the template engine";
public static void main(String[] args) {
List<CodegenConfig> extensions = getExtensions();
Map<String, CodegenConfig> configs = new HashMap<String, CodegenConfig>();
StringBuilder sb = new StringBuilder();
for(CodegenConfig config : extensions) {
if(sb.toString().length() != 0)
sb.append(", ");
sb.append(config.getName());
configs.put(config.getName(), config);
}
Options options = new Options();
options.addOption("h", "help", false, "shows this message");
options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + sb.toString() + "]");
options.addOption("l", "lang", true, "client language to generate.\nAvailable languages include:\n\t[" + configString + "]");
options.addOption("o", "output", true, "where to write the generated files");
options.addOption("i", "input-spec", true, "location of the swagger spec, as URL or file");
options.addOption("t", "template-dir", true, "folder containing the template files");
@@ -53,12 +60,12 @@ public class Codegen extends DefaultGenerator {
return;
}
if (cmd.hasOption("l"))
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l"), configs));
clientOptInput.setConfig(getConfig(cmd.getOptionValue("l")));
if (cmd.hasOption("o"))
clientOptInput.getConfig().setOutputDir(cmd.getOptionValue("o"));
if (cmd.hasOption("h")) {
if(cmd.hasOption("l")) {
config = getConfig(String.valueOf(cmd.getOptionValue("l")), configs);
config = getConfig(String.valueOf(cmd.getOptionValue("l")));
if(config != null) {
options.addOption("h", "help", true, config.getHelp());
usage(options);
@@ -103,7 +110,7 @@ public class Codegen extends DefaultGenerator {
formatter.printHelp( "Codegen", options );
}
static CodegenConfig getConfig(String name, Map<String, CodegenConfig> configs) {
public static CodegenConfig getConfig(String name) {
if(configs.containsKey(name)) {
return configs.get(name);
}

View File

@@ -26,13 +26,14 @@ public class DefaultGenerator implements Generator {
return this;
}
public void generate() {
public List<File> generate() {
if(swagger == null || config == null) {
throw new RuntimeException("missing swagger input or config!");
}
if(System.getProperty("debugSwagger") != null) {
Json.prettyPrint(swagger);
}
List<File> files = new ArrayList<File>();
try {
config.processOpts();
if(swagger.getInfo() != null) {
@@ -94,6 +95,7 @@ public class DefaultGenerator implements Generator {
.defaultValue("")
.compile(template);
writeToFile(filename, tmpl.execute(models));
files.add(new File(filename));
}
}
}
@@ -131,6 +133,7 @@ public class DefaultGenerator implements Generator {
.compile(template);
writeToFile(filename, tmpl.execute(operation));
files.add(new File(filename));
}
}
if(System.getProperty("debugOperations") != null) {
@@ -187,11 +190,13 @@ public class DefaultGenerator implements Generator {
.compile(template);
writeToFile(outputFilename, tmpl.execute(bundle));
files.add(new File(outputFilename));
}
else {
String template = readTemplate(config.templateDir() + File.separator + support.templateFile);
FileUtils.writeStringToFile(new File(outputFilename), template);
System.out.println("copying file to " + outputFilename);
files.add(new File(outputFilename));
}
}
@@ -200,6 +205,7 @@ public class DefaultGenerator implements Generator {
catch (Exception e) {
e.printStackTrace();
}
return files;
}
public Map<String, List<CodegenOperation>> processPaths(Map<String, Path> paths) {

View File

@@ -2,7 +2,10 @@ package com.wordnik.swagger.codegen;
import com.wordnik.swagger.models.Swagger;
import java.io.File;
import java.util.List;
public interface Generator {
Generator opts(ClientOptInput opts);
void generate();
List<File> generate();
}